File

src/session/session.service.ts

Index

Methods

Constructor

constructor(sessionRepository: Repository<Session>, configService: ConfigService, schedulerRegistry: SchedulerRegistry)
Parameters :
Name Type Optional
sessionRepository Repository<Session> No
configService ConfigService No
schedulerRegistry SchedulerRegistry No

Methods

add
add(issuer_state: string, values: QueryDeepPartialEntity<Session>)

Update an existing session.

Parameters :
Name Type Optional
issuer_state string No
values QueryDeepPartialEntity<Session> No
Returns : any
create
create(session: DeepPartial<Session>)

Create a new session.

Parameters :
Name Type Optional
session DeepPartial<Session> No
Returns : any
get
get(state: string)

Get a session by its state.

Parameters :
Name Type Optional
state string No
Returns : any
getAll
getAll()

Get all sessions.

Returns : Promise<Session[]>
getBy
getBy(where: FindOptionsWhere<Session>)

Get a session by a specific condition.

Parameters :
Name Type Optional
where FindOptionsWhere<Session> No
Returns : any
onApplicationBootstrap
onApplicationBootstrap()

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 : any
tidyUpSessions
tidyUpSessions()

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)),
        });
    }
}

results matching ""

    No results matching ""