0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-28 00:59:18 -05:00

refactor: migrate audit repository to kysely (#15269)

This commit is contained in:
Daniel Dietzler 2025-01-14 03:23:12 +01:00 committed by GitHub
parent b74f013b53
commit 28b08ed417
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 16 deletions

View file

@ -0,0 +1,21 @@
-- NOTE: This file is auto generated by ./sql-generator
-- AuditRepository.getAfter
select distinct
on ("audit"."entityId", "audit"."entityType") "audit"."entityId"
from
"audit"
where
"audit"."createdAt" > $1
and "audit"."action" = $2
and "audit"."entityType" = $3
and "audit"."ownerId" in ($4)
order by
"audit"."entityId" desc,
"audit"."entityType" desc,
"audit"."createdAt" desc
-- AuditRepository.removeBefore
delete from "audit"
where
"createdAt" < $1

View file

@ -1,31 +1,38 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { Kysely } from 'kysely';
import { AuditEntity } from 'src/entities/audit.entity'; 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 { AuditSearch, IAuditRepository } from 'src/interfaces/audit.interface';
import { In, LessThan, MoreThan, Repository } from 'typeorm';
@Injectable() @Injectable()
export class AuditRepository implements IAuditRepository { 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[]> { async getAfter(since: Date, options: AuditSearch): Promise<string[]> {
const records = await this.repository const records = await this.db
.createQueryBuilder('audit') .selectFrom('audit')
.where({ .where('audit.createdAt', '>', since)
createdAt: MoreThan(since), .$if(!!options.action, (qb) => qb.where('audit.action', '=', options.action!))
action: options.action, .$if(!!options.entityType, (qb) => qb.where('audit.entityType', '=', options.entityType!))
entityType: options.entityType, .where('audit.ownerId', 'in', options.userIds)
ownerId: In(options.userIds),
})
.distinctOn(['audit.entityId', 'audit.entityType']) .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') .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> { async removeBefore(before: Date): Promise<void> {
await this.repository.delete({ createdAt: LessThan(before) }); await this.db.deleteFrom('audit').where('createdAt', '<', before).execute();
} }
} }