import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { DummyValue, GenerateSql } from 'src/decorators'; import { SessionEntity } from 'src/entities/session.entity'; import { ISessionRepository, SessionSearchOptions } from 'src/interfaces/session.interface'; import { Instrumentation } from 'src/utils/instrumentation'; import { LessThanOrEqual, Repository } from 'typeorm'; @Instrumentation() @Injectable() export class SessionRepository implements ISessionRepository { constructor(@InjectRepository(SessionEntity) private repository: Repository) {} @GenerateSql({ params: [DummyValue.DATE] }) search(options: SessionSearchOptions): Promise { return this.repository.find({ where: { updatedAt: LessThanOrEqual(options.updatedBefore) } }); } @GenerateSql({ params: [DummyValue.STRING] }) getByToken(token: string): Promise { return this.repository.findOne({ where: { token }, relations: { user: { metadata: true, }, }, }); } getByUserId(userId: string): Promise { return this.repository.find({ where: { userId, }, relations: { user: true, }, order: { updatedAt: 'desc', createdAt: 'desc', }, }); } create>(dto: T): Promise { return this.repository.save(dto); } update>(dto: T): Promise { return this.repository.save(dto); } @GenerateSql({ params: [DummyValue.UUID] }) async delete(id: string): Promise { await this.repository.delete({ id }); } }