forked from immich-app/immich
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate version-history repository to kysely (immich-app#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
- Loading branch information
1 parent
de3c9ef
commit ac517b7
Showing
4 changed files
with
40 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |