Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate system metadata to kysely #15231

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
mertalev marked this conversation as resolved.
Show resolved Hide resolved
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
Loading