src/session/session.service.ts
Methods |
constructor(sessionRepository: Repository<Session>, configService: ConfigService, schedulerRegistry: SchedulerRegistry)
|
||||||||||||
Defined in src/session/session.service.ts:10
|
||||||||||||
Parameters :
|
add | |||||||||
add(issuer_state: string, values: QueryDeepPartialEntity<Session>)
|
|||||||||
Defined in src/session/session.service.ts:50
|
|||||||||
Update an existing session.
Parameters :
Returns :
any
|
create | ||||||
create(session: DeepPartial<Session>)
|
||||||
Defined in src/session/session.service.ts:40
|
||||||
Create a new session.
Parameters :
Returns :
any
|
get | ||||||
get(state: string)
|
||||||
Defined in src/session/session.service.ts:67
|
||||||
Get a session by its state.
Parameters :
Returns :
any
|
getAll |
getAll()
|
Defined in src/session/session.service.ts:58
|
Get all sessions.
Returns :
Promise<Session[]>
|
getBy | ||||||
getBy(where: FindOptionsWhere<Session>)
|
||||||
Defined in src/session/session.service.ts:76
|
||||||
Get a session by a specific condition.
Parameters :
Returns :
any
|
onApplicationBootstrap |
onApplicationBootstrap()
|
Defined in src/session/session.service.ts:23
|
Register the tidy up cron job on application bootstrap.
This will run every hour by default, but can be configured via the
Returns :
any
|
tidyUpSessions |
tidyUpSessions()
|
Defined in src/session/session.service.ts:83
|
Tidy up sessions that are older than 1 day.
Returns :
any
|
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { Session } from './entities/session.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { DeepPartial, FindOptionsWhere, LessThan, Repository } from 'typeorm';
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
import { SchedulerRegistry } from '@nestjs/schedule';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class SessionService implements OnApplicationBootstrap {
constructor(
@InjectRepository(Session)
private sessionRepository: Repository<Session>,
private readonly configService: ConfigService,
private readonly schedulerRegistry: SchedulerRegistry,
) {}
/**
* Register the tidy up cron job on application bootstrap.
* This will run every hour by default, but can be configured via the `SESSION_TIDY_UP_INTERVAL` config variable.
* @returns
*/
onApplicationBootstrap() {
const callback = () => {
void this.tidyUpSessions();
};
const intervalTime =
this.configService.getOrThrow<number>('SESSION_TIDY_UP_INTERVAL') *
1000;
const interval = setInterval(callback, intervalTime);
this.schedulerRegistry.addInterval('tidyUpSessions', interval);
return this.tidyUpSessions();
}
/**
* Create a new session.
* @param session
* @returns
*/
create(session: DeepPartial<Session>) {
return this.sessionRepository.save(session);
}
/**
* Update an existing session.
* @param issuer_state
* @param values
* @returns
*/
add(issuer_state: string, values: QueryDeepPartialEntity<Session>) {
return this.sessionRepository.update({ id: issuer_state }, values);
}
/**
* Get all sessions.
* @returns
*/
getAll(): Promise<Session[]> {
return this.sessionRepository.find();
}
/**
* Get a session by its state.
* @param state
* @returns
*/
get(state: string) {
return this.sessionRepository.findOneByOrFail({ id: state });
}
/**
* Get a session by a specific condition.
* @param where
* @returns
*/
getBy(where: FindOptionsWhere<Session>) {
return this.sessionRepository.findOneByOrFail(where);
}
/**
* Tidy up sessions that are older than 1 day.
*/
tidyUpSessions() {
const ttl = this.configService.getOrThrow<number>('SESSION_TTL') * 1000;
return this.sessionRepository.delete({
createdAt: LessThan(new Date(Date.now() - ttl)),
});
}
}