Skip to content

Commit

Permalink
chore: migrate version-history repository to kysely (immich-app#15267)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bo0tzz authored and aminesebastian committed Jan 12, 2025
1 parent de3c9ef commit ac517b7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
19 changes: 9 additions & 10 deletions server/src/db.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface Albums {
export interface AlbumsAssetsAssets {
albumsId: string;
assetsId: string;
createdAt: Generated<Timestamp>;
}

export interface AlbumsSharedUsersUsers {
Expand Down Expand Up @@ -201,7 +202,6 @@ export interface GeodataPlaces {
admin2Name: string | null;
alternateNames: string | null;
countryCode: string;
earthCoord: Generated<string | null>;
id: number;
latitude: number;
longitude: number;
Expand Down Expand Up @@ -257,7 +257,7 @@ export interface NaturalearthCountries {
admin: string;
admin_a3: string;
coordinates: string;
id: Generated<number>;
id: number;
type: string;
}

Expand Down Expand Up @@ -311,13 +311,6 @@ export interface SharedLinks {
userId: string;
}

export interface SmartInfo {
assetId: string;
objects: string[] | null;
smartInfoTextSearchableColumn: Generated<string>;
tags: string[] | null;
}

export interface SmartSearch {
assetId: string;
embedding: string;
Expand Down Expand Up @@ -399,6 +392,12 @@ export interface VectorsPgVectorIndexStat {
tablerelid: number | null;
}

export interface VersionHistory {
createdAt: Generated<Timestamp>;
id: Generated<string>;
version: string;
}

export interface DB {
activity: Activity;
albums: Albums;
Expand All @@ -425,7 +424,6 @@ export interface DB {
sessions: Sessions;
shared_link__asset: SharedLinkAsset;
shared_links: SharedLinks;
smart_info: SmartInfo;
smart_search: SmartSearch;
socket_io_attachments: SocketIoAttachments;
system_config: SystemConfig;
Expand All @@ -436,4 +434,5 @@ export interface DB {
user_metadata: UserMetadata;
users: Users;
'vectors.pg_vector_index_stat': VectorsPgVectorIndexStat;
version_history: VersionHistory;
}
2 changes: 1 addition & 1 deletion server/src/interfaces/version-history.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export const IVersionHistoryRepository = 'IVersionHistoryRepository';
export interface IVersionHistoryRepository {
create(version: Omit<VersionHistoryEntity, 'id' | 'createdAt'>): Promise<VersionHistoryEntity>;
getAll(): Promise<VersionHistoryEntity[]>;
getLatest(): Promise<VersionHistoryEntity | null>;
getLatest(): Promise<VersionHistoryEntity | undefined>;
}
17 changes: 17 additions & 0 deletions server/src/queries/version.history.repository.sql
Original file line number Diff line number Diff line change
@@ -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
22 changes: 13 additions & 9 deletions server/src/repositories/version-history.repository.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
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 { IVersionHistoryRepository } from 'src/interfaces/version-history.interface';
import { Repository } from 'typeorm';

@Injectable()
export class VersionHistoryRepository implements IVersionHistoryRepository {
constructor(@InjectRepository(VersionHistoryEntity) private repository: Repository<VersionHistoryEntity>) {}
constructor(@InjectKysely() private db: Kysely<DB>) {}

async getAll(): Promise<VersionHistoryEntity[]> {
return this.repository.find({ order: { createdAt: 'DESC' } });
@GenerateSql()
getAll(): Promise<VersionHistoryEntity[]> {
return this.db.selectFrom('version_history').selectAll().orderBy('createdAt', 'desc').execute();
}

async getLatest(): Promise<VersionHistoryEntity | null> {
const results = await this.repository.find({ order: { createdAt: 'DESC' }, take: 1 });
return results[0] || null;
@GenerateSql()
getLatest(): Promise<VersionHistoryEntity | undefined> {
return this.db.selectFrom('version_history').selectAll().orderBy('createdAt', 'desc').executeTakeFirst();
}

@GenerateSql({ params: [DummyValue.STRING] })
create(version: Omit<VersionHistoryEntity, 'id' | 'createdAt'>): Promise<VersionHistoryEntity> {
return this.repository.save(version);
return this.db.insertInto('version_history').values(version).returningAll().executeTakeFirstOrThrow();
}
}

0 comments on commit ac517b7

Please sign in to comment.