1
0
mirror of https://github.com/immich-app/immich.git synced 2025-01-14 15:45:55 +02:00

refactor: migrate system metadata to kysely (#15231)

This commit is contained in:
Jason Rasmussen 2025-01-10 14:20:15 -05:00 committed by GitHub
parent 930f979960
commit cc6a8b0c74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 10 deletions

View File

@ -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

View File

@ -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> {