mirror of
https://github.com/immich-app/immich.git
synced 2026-03-01 18:19:10 +03:00
refactor: migrate audit repository to kysely (#15269)
This commit is contained in:
@@ -1,31 +1,38 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { AuditEntity } from 'src/entities/audit.entity';
|
||||
import { Kysely } from 'kysely';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { DB } from 'src/db';
|
||||
import { DummyValue, GenerateSql } from 'src/decorators';
|
||||
import { DatabaseAction, EntityType } from 'src/enum';
|
||||
import { AuditSearch, IAuditRepository } from 'src/interfaces/audit.interface';
|
||||
import { In, LessThan, MoreThan, Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class AuditRepository implements IAuditRepository {
|
||||
constructor(@InjectRepository(AuditEntity) private repository: Repository<AuditEntity>) {}
|
||||
constructor(@InjectKysely() private db: Kysely<DB>) {}
|
||||
|
||||
@GenerateSql({
|
||||
params: [
|
||||
DummyValue.DATE,
|
||||
{ action: DatabaseAction.CREATE, entityType: EntityType.ASSET, userIds: [DummyValue.UUID] },
|
||||
],
|
||||
})
|
||||
async getAfter(since: Date, options: AuditSearch): Promise<string[]> {
|
||||
const records = await this.repository
|
||||
.createQueryBuilder('audit')
|
||||
.where({
|
||||
createdAt: MoreThan(since),
|
||||
action: options.action,
|
||||
entityType: options.entityType,
|
||||
ownerId: In(options.userIds),
|
||||
})
|
||||
const records = await this.db
|
||||
.selectFrom('audit')
|
||||
.where('audit.createdAt', '>', since)
|
||||
.$if(!!options.action, (qb) => qb.where('audit.action', '=', options.action!))
|
||||
.$if(!!options.entityType, (qb) => qb.where('audit.entityType', '=', options.entityType!))
|
||||
.where('audit.ownerId', 'in', options.userIds)
|
||||
.distinctOn(['audit.entityId', 'audit.entityType'])
|
||||
.orderBy('audit.entityId, audit.entityType, audit.createdAt', 'DESC')
|
||||
.orderBy(['audit.entityId desc', 'audit.entityType desc', 'audit.createdAt desc'])
|
||||
.select('audit.entityId')
|
||||
.getMany();
|
||||
.execute();
|
||||
|
||||
return records.map((r) => r.entityId);
|
||||
return records.map(({ entityId }) => entityId);
|
||||
}
|
||||
|
||||
@GenerateSql({ params: [DummyValue.DATE] })
|
||||
async removeBefore(before: Date): Promise<void> {
|
||||
await this.repository.delete({ createdAt: LessThan(before) });
|
||||
await this.db.deleteFrom('audit').where('createdAt', '<', before).execute();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user