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
4 changed files with 40 additions and 20 deletions

View File

@@ -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();
}
}