From 3f1f783b94e62e9beb7ab7776c649e49509f5a03 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Fri, 10 Jan 2025 14:20:15 -0500 Subject: [PATCH] refactor: migrate system metadata to kysely (#15231) --- .../queries/system.metadata.repository.sql | 24 ++++++++++++++ .../system-metadata.repository.ts | 33 +++++++++++++------ 2 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 server/src/queries/system.metadata.repository.sql diff --git a/server/src/queries/system.metadata.repository.sql b/server/src/queries/system.metadata.repository.sql new file mode 100644 index 00000000000000..996ac148030921 --- /dev/null +++ b/server/src/queries/system.metadata.repository.sql @@ -0,0 +1,24 @@ +-- NOTE: This file is auto generated by ./sql-generator + +-- SystemMetadataRepository.get +select + "value" +from + "system_metadata" +where + "key" = $1 + +-- SystemMetadataRepository.set +insert into + "system_metadata" ("key", "value") +values + ($1, $2) +on conflict ("key") do +update +set + "value" = $3 + +-- SystemMetadataRepository.delete +delete from "system_metadata" +where + "key" = $1 diff --git a/server/src/repositories/system-metadata.repository.ts b/server/src/repositories/system-metadata.repository.ts index 1c6aaf0517804e..7cd4d715e2fec2 100644 --- a/server/src/repositories/system-metadata.repository.ts +++ b/server/src/repositories/system-metadata.repository.ts @@ -1,31 +1,44 @@ import { Injectable } from '@nestjs/common'; -import { InjectRepository } from '@nestjs/typeorm'; +import { Insertable, Kysely } from 'kysely'; +import { InjectKysely } from 'nestjs-kysely'; import { readFile } from 'node:fs/promises'; -import { SystemMetadata, SystemMetadataEntity } from 'src/entities/system-metadata.entity'; +import { DB, SystemMetadata as DbSystemMetadata } from 'src/db'; +import { GenerateSql } from 'src/decorators'; +import { SystemMetadata } from 'src/entities/system-metadata.entity'; import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface'; -import { Repository } from 'typeorm'; + +type Upsert = Insertable; @Injectable() export class SystemMetadataRepository implements ISystemMetadataRepository { - constructor( - @InjectRepository(SystemMetadataEntity) - private repository: Repository, - ) {} + constructor(@InjectKysely() private db: Kysely) {} + @GenerateSql({ params: ['metadata_key'] }) async get(key: T): Promise { - const metadata = await this.repository.findOne({ where: { key } }); + const metadata = await this.db + .selectFrom('system_metadata') + .select('value') + .where('key', '=', key) + .executeTakeFirst(); + if (!metadata) { return null; } return metadata.value as SystemMetadata[T]; } + @GenerateSql({ params: ['metadata_key', { foo: 'bar' }] }) async set(key: T, value: SystemMetadata[T]): Promise { - await this.repository.upsert({ key, value }, { conflictPaths: { key: true } }); + await this.db + .insertInto('system_metadata') + .values({ key, value } as Upsert) + .onConflict((oc) => oc.columns(['key']).doUpdateSet({ value } as Upsert)) + .execute(); } + @GenerateSql({ params: ['metadata_key'] }) async delete(key: T): Promise { - await this.repository.delete({ key }); + await this.db.deleteFrom('system_metadata').where('key', '=', key).execute(); } readFile(filename: string): Promise {