mirror of
https://github.com/immich-app/immich.git
synced 2026-03-01 18:19:10 +03:00
refactor: migrate move repository to kysely (#15327)
* refactor: migrate move repository to kysely * fix: tests * fix: tests
This commit is contained in:
@@ -1,29 +1,49 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Insertable, Kysely, Updateable } from 'kysely';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { DB, MoveHistory } from 'src/db';
|
||||
import { DummyValue, GenerateSql } from 'src/decorators';
|
||||
import { MoveEntity } from 'src/entities/move.entity';
|
||||
import { PathType } from 'src/enum';
|
||||
import { IMoveRepository, MoveCreate } from 'src/interfaces/move.interface';
|
||||
import { Repository } from 'typeorm';
|
||||
import { IMoveRepository } from 'src/interfaces/move.interface';
|
||||
|
||||
@Injectable()
|
||||
export class MoveRepository implements IMoveRepository {
|
||||
constructor(@InjectRepository(MoveEntity) private repository: Repository<MoveEntity>) {}
|
||||
constructor(@InjectKysely() private db: Kysely<DB>) {}
|
||||
|
||||
create(entity: MoveCreate): Promise<MoveEntity> {
|
||||
return this.repository.save(entity);
|
||||
create(entity: Insertable<MoveHistory>): Promise<MoveEntity> {
|
||||
return this.db
|
||||
.insertInto('move_history')
|
||||
.values(entity)
|
||||
.returningAll()
|
||||
.executeTakeFirstOrThrow() as Promise<MoveEntity>;
|
||||
}
|
||||
|
||||
@GenerateSql({ params: [DummyValue.UUID, DummyValue.STRING] })
|
||||
getByEntity(entityId: string, pathType: PathType): Promise<MoveEntity | null> {
|
||||
return this.repository.findOne({ where: { entityId, pathType } });
|
||||
getByEntity(entityId: string, pathType: PathType): Promise<MoveEntity | undefined> {
|
||||
return this.db
|
||||
.selectFrom('move_history')
|
||||
.selectAll()
|
||||
.where('entityId', '=', entityId)
|
||||
.where('pathType', '=', pathType)
|
||||
.executeTakeFirst() as Promise<MoveEntity | undefined>;
|
||||
}
|
||||
|
||||
update(entity: Partial<MoveEntity>): Promise<MoveEntity> {
|
||||
return this.repository.save(entity);
|
||||
update(id: string, entity: Updateable<MoveHistory>): Promise<MoveEntity> {
|
||||
return this.db
|
||||
.updateTable('move_history')
|
||||
.set(entity)
|
||||
.where('id', '=', id)
|
||||
.returningAll()
|
||||
.executeTakeFirstOrThrow() as unknown as Promise<MoveEntity>;
|
||||
}
|
||||
|
||||
delete(move: MoveEntity): Promise<MoveEntity> {
|
||||
return this.repository.remove(move);
|
||||
@GenerateSql({ params: [DummyValue.UUID] })
|
||||
delete(id: string): Promise<MoveEntity> {
|
||||
return this.db
|
||||
.deleteFrom('move_history')
|
||||
.where('id', '=', id)
|
||||
.returningAll()
|
||||
.executeTakeFirstOrThrow() as unknown as Promise<MoveEntity>;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user