|
|
|
@ -3,36 +3,37 @@ import { Insertable, Kysely, Updateable } from 'kysely';
|
|
|
|
import { InjectKysely } from 'nestjs-kysely';
|
|
|
|
import { InjectKysely } from 'nestjs-kysely';
|
|
|
|
import { DB, Sessions } from 'src/db';
|
|
|
|
import { DB, Sessions } from 'src/db';
|
|
|
|
import { DummyValue, GenerateSql } from 'src/decorators';
|
|
|
|
import { DummyValue, GenerateSql } from 'src/decorators';
|
|
|
|
import { SessionEntity, withUser } from 'src/entities/session.entity';
|
|
|
|
import { withUser } from 'src/entities/session.entity';
|
|
|
|
import { ISessionRepository, SessionSearchOptions } from 'src/interfaces/session.interface';
|
|
|
|
|
|
|
|
import { asUuid } from 'src/utils/database';
|
|
|
|
import { asUuid } from 'src/utils/database';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type SessionSearchOptions = { updatedBefore: Date };
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
@Injectable()
|
|
|
|
export class SessionRepository implements ISessionRepository {
|
|
|
|
export class SessionRepository {
|
|
|
|
constructor(@InjectKysely() private db: Kysely<DB>) {}
|
|
|
|
constructor(@InjectKysely() private db: Kysely<DB>) {}
|
|
|
|
|
|
|
|
|
|
|
|
@GenerateSql({ params: [{ updatedBefore: DummyValue.DATE }] })
|
|
|
|
@GenerateSql({ params: [{ updatedBefore: DummyValue.DATE }] })
|
|
|
|
search(options: SessionSearchOptions): Promise<SessionEntity[]> {
|
|
|
|
search(options: SessionSearchOptions) {
|
|
|
|
return this.db
|
|
|
|
return this.db
|
|
|
|
.selectFrom('sessions')
|
|
|
|
.selectFrom('sessions')
|
|
|
|
.selectAll()
|
|
|
|
.selectAll()
|
|
|
|
.where('sessions.updatedAt', '<=', options.updatedBefore)
|
|
|
|
.where('sessions.updatedAt', '<=', options.updatedBefore)
|
|
|
|
.execute() as Promise<SessionEntity[]>;
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GenerateSql({ params: [DummyValue.STRING] })
|
|
|
|
@GenerateSql({ params: [DummyValue.STRING] })
|
|
|
|
getByToken(token: string): Promise<SessionEntity | undefined> {
|
|
|
|
getByToken(token: string) {
|
|
|
|
return this.db
|
|
|
|
return this.db
|
|
|
|
.selectFrom('sessions')
|
|
|
|
.selectFrom('sessions')
|
|
|
|
.innerJoinLateral(withUser, (join) => join.onTrue())
|
|
|
|
.innerJoinLateral(withUser, (join) => join.onTrue())
|
|
|
|
.selectAll('sessions')
|
|
|
|
.selectAll('sessions')
|
|
|
|
.select((eb) => eb.fn.toJson('user').as('user'))
|
|
|
|
.select((eb) => eb.fn.toJson('user').as('user'))
|
|
|
|
.where('sessions.token', '=', token)
|
|
|
|
.where('sessions.token', '=', token)
|
|
|
|
.executeTakeFirst() as Promise<SessionEntity | undefined>;
|
|
|
|
.executeTakeFirst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GenerateSql({ params: [DummyValue.UUID] })
|
|
|
|
@GenerateSql({ params: [DummyValue.UUID] })
|
|
|
|
getByUserId(userId: string): Promise<SessionEntity[]> {
|
|
|
|
getByUserId(userId: string) {
|
|
|
|
return this.db
|
|
|
|
return this.db
|
|
|
|
.selectFrom('sessions')
|
|
|
|
.selectFrom('sessions')
|
|
|
|
.innerJoinLateral(withUser, (join) => join.onTrue())
|
|
|
|
.innerJoinLateral(withUser, (join) => join.onTrue())
|
|
|
|
@ -41,30 +42,24 @@ export class SessionRepository implements ISessionRepository {
|
|
|
|
.where('sessions.userId', '=', userId)
|
|
|
|
.where('sessions.userId', '=', userId)
|
|
|
|
.orderBy('sessions.updatedAt', 'desc')
|
|
|
|
.orderBy('sessions.updatedAt', 'desc')
|
|
|
|
.orderBy('sessions.createdAt', 'desc')
|
|
|
|
.orderBy('sessions.createdAt', 'desc')
|
|
|
|
.execute() as unknown as Promise<SessionEntity[]>;
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async create(dto: Insertable<Sessions>): Promise<SessionEntity> {
|
|
|
|
create(dto: Insertable<Sessions>) {
|
|
|
|
const { id, token, userId, createdAt, updatedAt, deviceType, deviceOS } = await this.db
|
|
|
|
return this.db.insertInto('sessions').values(dto).returningAll().executeTakeFirstOrThrow();
|
|
|
|
.insertInto('sessions')
|
|
|
|
|
|
|
|
.values(dto)
|
|
|
|
|
|
|
|
.returningAll()
|
|
|
|
|
|
|
|
.executeTakeFirstOrThrow();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { id, token, userId, createdAt, updatedAt, deviceType, deviceOS } as SessionEntity;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
update(id: string, dto: Updateable<Sessions>): Promise<SessionEntity> {
|
|
|
|
update(id: string, dto: Updateable<Sessions>) {
|
|
|
|
return this.db
|
|
|
|
return this.db
|
|
|
|
.updateTable('sessions')
|
|
|
|
.updateTable('sessions')
|
|
|
|
.set(dto)
|
|
|
|
.set(dto)
|
|
|
|
.where('sessions.id', '=', asUuid(id))
|
|
|
|
.where('sessions.id', '=', asUuid(id))
|
|
|
|
.returningAll()
|
|
|
|
.returningAll()
|
|
|
|
.executeTakeFirstOrThrow() as Promise<SessionEntity>;
|
|
|
|
.executeTakeFirstOrThrow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GenerateSql({ params: [DummyValue.UUID] })
|
|
|
|
@GenerateSql({ params: [DummyValue.UUID] })
|
|
|
|
async delete(id: string): Promise<void> {
|
|
|
|
async delete(id: string) {
|
|
|
|
await this.db.deleteFrom('sessions').where('id', '=', asUuid(id)).execute();
|
|
|
|
await this.db.deleteFrom('sessions').where('id', '=', asUuid(id)).execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|