Skip to content

Commit

Permalink
refactor: migrate system metadata to kysely (#15231)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 authored Jan 10, 2025
1 parent 930f979 commit cc6a8b0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
24 changes: 24 additions & 0 deletions server/src/queries/system.metadata.repository.sql
Original file line number Diff line number Diff line change
@@ -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
33 changes: 23 additions & 10 deletions server/src/repositories/system-metadata.repository.ts
Original file line number Diff line number Diff line change
@@ -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<DbSystemMetadata>;

@Injectable()
export class SystemMetadataRepository implements ISystemMetadataRepository {
constructor(
@InjectRepository(SystemMetadataEntity)
private repository: Repository<SystemMetadataEntity>,
) {}
constructor(@InjectKysely() private db: Kysely<DB>) {}

@GenerateSql({ params: ['metadata_key'] })
async get<T extends keyof SystemMetadata>(key: T): Promise<SystemMetadata[T] | null> {
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<T extends keyof SystemMetadata>(key: T, value: SystemMetadata[T]): Promise<void> {
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<T extends keyof SystemMetadata>(key: T): Promise<void> {
await this.repository.delete({ key });
await this.db.deleteFrom('system_metadata').where('key', '=', key).execute();
}

readFile(filename: string): Promise<string> {
Expand Down

0 comments on commit cc6a8b0

Please sign in to comment.