mirror of
https://github.com/immich-app/immich.git
synced 2025-01-23 20:22:45 +01:00
refactor: migrate move repository to kysely (#15327)
* refactor: migrate move repository to kysely * fix: tests * fix: tests
This commit is contained in:
parent
fc99c5f530
commit
a35af2b242
5 changed files with 56 additions and 35 deletions
server/src
cores
interfaces
queries
repositories
services
|
@ -183,7 +183,7 @@ export class StorageCore {
|
|||
return;
|
||||
}
|
||||
|
||||
move = await this.moveRepository.update({ id: move.id, oldPath: actualPath, newPath });
|
||||
move = await this.moveRepository.update(move.id, { id: move.id, oldPath: actualPath, newPath });
|
||||
} else {
|
||||
move = await this.moveRepository.create({ entityId, pathType, oldPath, newPath });
|
||||
}
|
||||
|
@ -225,7 +225,7 @@ export class StorageCore {
|
|||
}
|
||||
|
||||
await this.savePath(pathType, entityId, newPath);
|
||||
await this.moveRepository.delete(move);
|
||||
await this.moveRepository.delete(move.id);
|
||||
}
|
||||
|
||||
private async verifyNewPathContentsMatchesExpected(
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { Insertable, Updateable } from 'kysely';
|
||||
import { MoveHistory } from 'src/db';
|
||||
import { MoveEntity } from 'src/entities/move.entity';
|
||||
import { PathType } from 'src/enum';
|
||||
|
||||
|
@ -6,8 +8,8 @@ export const IMoveRepository = 'IMoveRepository';
|
|||
export type MoveCreate = Pick<MoveEntity, 'oldPath' | 'newPath' | 'entityId' | 'pathType'> & Partial<MoveEntity>;
|
||||
|
||||
export interface IMoveRepository {
|
||||
create(entity: MoveCreate): Promise<MoveEntity>;
|
||||
getByEntity(entityId: string, pathType: PathType): Promise<MoveEntity | null>;
|
||||
update(entity: Partial<MoveEntity>): Promise<MoveEntity>;
|
||||
delete(move: MoveEntity): Promise<MoveEntity>;
|
||||
create(entity: Insertable<MoveHistory>): Promise<MoveEntity>;
|
||||
getByEntity(entityId: string, pathType: PathType): Promise<MoveEntity | undefined>;
|
||||
update(id: string, entity: Updateable<MoveHistory>): Promise<MoveEntity>;
|
||||
delete(id: string): Promise<MoveEntity>;
|
||||
}
|
||||
|
|
|
@ -1,18 +1,17 @@
|
|||
-- NOTE: This file is auto generated by ./sql-generator
|
||||
|
||||
-- MoveRepository.getByEntity
|
||||
SELECT
|
||||
"MoveEntity"."id" AS "MoveEntity_id",
|
||||
"MoveEntity"."entityId" AS "MoveEntity_entityId",
|
||||
"MoveEntity"."pathType" AS "MoveEntity_pathType",
|
||||
"MoveEntity"."oldPath" AS "MoveEntity_oldPath",
|
||||
"MoveEntity"."newPath" AS "MoveEntity_newPath"
|
||||
FROM
|
||||
"move_history" "MoveEntity"
|
||||
WHERE
|
||||
(
|
||||
("MoveEntity"."entityId" = $1)
|
||||
AND ("MoveEntity"."pathType" = $2)
|
||||
)
|
||||
LIMIT
|
||||
1
|
||||
select
|
||||
*
|
||||
from
|
||||
"move_history"
|
||||
where
|
||||
"entityId" = $1
|
||||
and "pathType" = $2
|
||||
|
||||
-- MoveRepository.delete
|
||||
delete from "move_history"
|
||||
where
|
||||
"id" = $1
|
||||
returning
|
||||
*
|
||||
|
|
|
@ -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>;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -235,7 +235,7 @@ describe(StorageTemplateService.name, () => {
|
|||
expect(assetMock.getByIds).toHaveBeenCalledWith([assetStub.image.id], { exifInfo: true });
|
||||
expect(storageMock.checkFileExists).toHaveBeenCalledTimes(3);
|
||||
expect(storageMock.rename).toHaveBeenCalledWith(assetStub.image.originalPath, newPath);
|
||||
expect(moveMock.update).toHaveBeenCalledWith({
|
||||
expect(moveMock.update).toHaveBeenCalledWith('123', {
|
||||
id: '123',
|
||||
oldPath: assetStub.image.originalPath,
|
||||
newPath,
|
||||
|
@ -277,7 +277,7 @@ describe(StorageTemplateService.name, () => {
|
|||
expect(storageMock.stat).toHaveBeenCalledWith(previousFailedNewPath);
|
||||
expect(storageMock.rename).toHaveBeenCalledWith(previousFailedNewPath, newPath);
|
||||
expect(storageMock.copyFile).not.toHaveBeenCalled();
|
||||
expect(moveMock.update).toHaveBeenCalledWith({
|
||||
expect(moveMock.update).toHaveBeenCalledWith('123', {
|
||||
id: '123',
|
||||
oldPath: previousFailedNewPath,
|
||||
newPath,
|
||||
|
|
Loading…
Reference in a new issue