mirror of
https://github.com/immich-app/immich.git
synced 2025-01-21 00:52:43 -05:00
refactor: migrate audit repository to kysely (#15269)
This commit is contained in:
parent
b74f013b53
commit
28b08ed417
2 changed files with 44 additions and 16 deletions
21
server/src/queries/audit.repository.sql
Normal file
21
server/src/queries/audit.repository.sql
Normal 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
|
|
@ -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>) {}
|
||||
|
||||
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),
|
||||
@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.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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue