From 28b08ed4178b4d7d7c6fbbe994b89ab2de7f4ae3 Mon Sep 17 00:00:00 2001 From: Daniel Dietzler <36593685+danieldietzler@users.noreply.github.com> Date: Tue, 14 Jan 2025 03:23:12 +0100 Subject: [PATCH] refactor: migrate audit repository to kysely (#15269) --- server/src/queries/audit.repository.sql | 21 +++++++++++ server/src/repositories/audit.repository.ts | 39 ++++++++++++--------- 2 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 server/src/queries/audit.repository.sql diff --git a/server/src/queries/audit.repository.sql b/server/src/queries/audit.repository.sql new file mode 100644 index 0000000000..3c83d2d3e8 --- /dev/null +++ b/server/src/queries/audit.repository.sql @@ -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 diff --git a/server/src/repositories/audit.repository.ts b/server/src/repositories/audit.repository.ts index ac73c3a8b9..5731087aef 100644 --- a/server/src/repositories/audit.repository.ts +++ b/server/src/repositories/audit.repository.ts @@ -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) {} + constructor(@InjectKysely() private db: Kysely) {} + @GenerateSql({ + params: [ + DummyValue.DATE, + { action: DatabaseAction.CREATE, entityType: EntityType.ASSET, userIds: [DummyValue.UUID] }, + ], + }) async getAfter(since: Date, options: AuditSearch): Promise { - 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 { - await this.repository.delete({ createdAt: LessThan(before) }); + await this.db.deleteFrom('audit').where('createdAt', '<', before).execute(); } }