chore: migrate version-history repository to kysely (#15267)

* chore: generate sql for version-history repository

* chore: run kysely-codegen

* chore: migrate version-history repository to kysely

* fix: change `| null` to `| undefined`

* chore: clean up unneeded async
This commit is contained in:
bo0tzz 2025-01-11 21:12:34 +01:00 committed by GitHub
parent beb31cebed
commit cab201270c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 20 deletions

19
server/src/db.d.ts vendored
View File

@ -62,6 +62,7 @@ export interface Albums {
export interface AlbumsAssetsAssets { export interface AlbumsAssetsAssets {
albumsId: string; albumsId: string;
assetsId: string; assetsId: string;
createdAt: Generated<Timestamp>;
} }
export interface AlbumsSharedUsersUsers { export interface AlbumsSharedUsersUsers {
@ -201,7 +202,6 @@ export interface GeodataPlaces {
admin2Name: string | null; admin2Name: string | null;
alternateNames: string | null; alternateNames: string | null;
countryCode: string; countryCode: string;
earthCoord: Generated<string | null>;
id: number; id: number;
latitude: number; latitude: number;
longitude: number; longitude: number;
@ -257,7 +257,7 @@ export interface NaturalearthCountries {
admin: string; admin: string;
admin_a3: string; admin_a3: string;
coordinates: string; coordinates: string;
id: Generated<number>; id: number;
type: string; type: string;
} }
@ -311,13 +311,6 @@ export interface SharedLinks {
userId: string; userId: string;
} }
export interface SmartInfo {
assetId: string;
objects: string[] | null;
smartInfoTextSearchableColumn: Generated<string>;
tags: string[] | null;
}
export interface SmartSearch { export interface SmartSearch {
assetId: string; assetId: string;
embedding: string; embedding: string;
@ -399,6 +392,12 @@ export interface VectorsPgVectorIndexStat {
tablerelid: number | null; tablerelid: number | null;
} }
export interface VersionHistory {
createdAt: Generated<Timestamp>;
id: Generated<string>;
version: string;
}
export interface DB { export interface DB {
activity: Activity; activity: Activity;
albums: Albums; albums: Albums;
@ -425,7 +424,6 @@ export interface DB {
sessions: Sessions; sessions: Sessions;
shared_link__asset: SharedLinkAsset; shared_link__asset: SharedLinkAsset;
shared_links: SharedLinks; shared_links: SharedLinks;
smart_info: SmartInfo;
smart_search: SmartSearch; smart_search: SmartSearch;
socket_io_attachments: SocketIoAttachments; socket_io_attachments: SocketIoAttachments;
system_config: SystemConfig; system_config: SystemConfig;
@ -436,4 +434,5 @@ export interface DB {
user_metadata: UserMetadata; user_metadata: UserMetadata;
users: Users; users: Users;
'vectors.pg_vector_index_stat': VectorsPgVectorIndexStat; 'vectors.pg_vector_index_stat': VectorsPgVectorIndexStat;
version_history: VersionHistory;
} }

View File

@ -5,5 +5,5 @@ export const IVersionHistoryRepository = 'IVersionHistoryRepository';
export interface IVersionHistoryRepository { export interface IVersionHistoryRepository {
create(version: Omit<VersionHistoryEntity, 'id' | 'createdAt'>): Promise<VersionHistoryEntity>; create(version: Omit<VersionHistoryEntity, 'id' | 'createdAt'>): Promise<VersionHistoryEntity>;
getAll(): Promise<VersionHistoryEntity[]>; getAll(): Promise<VersionHistoryEntity[]>;
getLatest(): Promise<VersionHistoryEntity | null>; getLatest(): Promise<VersionHistoryEntity | undefined>;
} }

View File

@ -0,0 +1,17 @@
-- NOTE: This file is auto generated by ./sql-generator
-- VersionHistoryRepository.getAll
select
*
from
"version_history"
order by
"createdAt" desc
-- VersionHistoryRepository.getLatest
select
*
from
"version_history"
order by
"createdAt" desc

View File

@ -1,23 +1,27 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { Kysely } from 'kysely';
import { InjectKysely } from 'nestjs-kysely';
import { DB } from 'src/db';
import { DummyValue, GenerateSql } from 'src/decorators';
import { VersionHistoryEntity } from 'src/entities/version-history.entity'; import { VersionHistoryEntity } from 'src/entities/version-history.entity';
import { IVersionHistoryRepository } from 'src/interfaces/version-history.interface'; import { IVersionHistoryRepository } from 'src/interfaces/version-history.interface';
import { Repository } from 'typeorm';
@Injectable() @Injectable()
export class VersionHistoryRepository implements IVersionHistoryRepository { export class VersionHistoryRepository implements IVersionHistoryRepository {
constructor(@InjectRepository(VersionHistoryEntity) private repository: Repository<VersionHistoryEntity>) {} constructor(@InjectKysely() private db: Kysely<DB>) {}
async getAll(): Promise<VersionHistoryEntity[]> { @GenerateSql()
return this.repository.find({ order: { createdAt: 'DESC' } }); getAll(): Promise<VersionHistoryEntity[]> {
return this.db.selectFrom('version_history').selectAll().orderBy('createdAt', 'desc').execute();
} }
async getLatest(): Promise<VersionHistoryEntity | null> { @GenerateSql()
const results = await this.repository.find({ order: { createdAt: 'DESC' }, take: 1 }); getLatest(): Promise<VersionHistoryEntity | undefined> {
return results[0] || null; return this.db.selectFrom('version_history').selectAll().orderBy('createdAt', 'desc').executeTakeFirst();
} }
@GenerateSql({ params: [DummyValue.STRING] })
create(version: Omit<VersionHistoryEntity, 'id' | 'createdAt'>): Promise<VersionHistoryEntity> { create(version: Omit<VersionHistoryEntity, 'id' | 'createdAt'>): Promise<VersionHistoryEntity> {
return this.repository.save(version); return this.db.insertInto('version_history').values(version).returningAll().executeTakeFirstOrThrow();
} }
} }