refactor: migrate stacks (#17559)

chore: migrate stacks
This commit is contained in:
Daniel Dietzler
2025-04-12 14:33:35 +02:00
committed by GitHub
parent 5dac315af7
commit a373034629
11 changed files with 153 additions and 63 deletions

View File

@@ -2,9 +2,10 @@ import { Injectable } from '@nestjs/common';
import { ExpressionBuilder, Kysely, Updateable } from 'kysely';
import { jsonArrayFrom } from 'kysely/helpers/postgres';
import { InjectKysely } from 'nestjs-kysely';
import { DB } from 'src/db';
import { columns } from 'src/database';
import { AssetStack, DB } from 'src/db';
import { DummyValue, GenerateSql } from 'src/decorators';
import { StackEntity } from 'src/entities/stack.entity';
import { AssetEntity } from 'src/entities/asset.entity';
import { asUuid } from 'src/utils/database';
export interface StackSearch {
@@ -18,7 +19,7 @@ const withAssets = (eb: ExpressionBuilder<DB, 'asset_stack'>, withTags = false)
.selectFrom('assets')
.selectAll('assets')
.innerJoinLateral(
(eb) => eb.selectFrom('exif').selectAll('exif').whereRef('exif.assetId', '=', 'assets.id').as('exifInfo'),
(eb) => eb.selectFrom('exif').select(columns.exif).whereRef('exif.assetId', '=', 'assets.id').as('exifInfo'),
(join) => join.onTrue(),
)
.$if(withTags, (eb) =>
@@ -26,7 +27,7 @@ const withAssets = (eb: ExpressionBuilder<DB, 'asset_stack'>, withTags = false)
jsonArrayFrom(
eb
.selectFrom('tags')
.selectAll('tags')
.select(columns.tag)
.innerJoin('tag_asset', 'tags.id', 'tag_asset.tagsId')
.whereRef('tag_asset.assetsId', '=', 'assets.id'),
).as('tags'),
@@ -35,7 +36,9 @@ const withAssets = (eb: ExpressionBuilder<DB, 'asset_stack'>, withTags = false)
.select((eb) => eb.fn.toJson('exifInfo').as('exifInfo'))
.where('assets.deletedAt', 'is', null)
.whereRef('assets.stackId', '=', 'asset_stack.id'),
).as('assets');
)
.$castTo<AssetEntity[]>()
.as('assets');
};
@Injectable()
@@ -43,17 +46,17 @@ export class StackRepository {
constructor(@InjectKysely() private db: Kysely<DB>) {}
@GenerateSql({ params: [{ ownerId: DummyValue.UUID }] })
search(query: StackSearch): Promise<StackEntity[]> {
search(query: StackSearch) {
return this.db
.selectFrom('asset_stack')
.selectAll('asset_stack')
.select(withAssets)
.where('asset_stack.ownerId', '=', query.ownerId)
.$if(!!query.primaryAssetId, (eb) => eb.where('asset_stack.primaryAssetId', '=', query.primaryAssetId!))
.execute() as unknown as Promise<StackEntity[]>;
.execute();
}
async create(entity: { ownerId: string; assetIds: string[] }): Promise<StackEntity> {
async create(entity: { ownerId: string; assetIds: string[] }) {
return this.db.transaction().execute(async (tx) => {
const stacks = await tx
.selectFrom('asset_stack')
@@ -116,7 +119,7 @@ export class StackRepository {
.selectAll('asset_stack')
.select(withAssets)
.where('id', '=', newRecord.id)
.executeTakeFirst() as unknown as Promise<StackEntity>;
.executeTakeFirstOrThrow();
});
}
@@ -129,23 +132,23 @@ export class StackRepository {
await this.db.deleteFrom('asset_stack').where('id', 'in', ids).execute();
}
update(id: string, entity: Updateable<StackEntity>): Promise<StackEntity> {
update(id: string, entity: Updateable<AssetStack>) {
return this.db
.updateTable('asset_stack')
.set(entity)
.where('id', '=', asUuid(id))
.returningAll('asset_stack')
.returning((eb) => withAssets(eb, true))
.executeTakeFirstOrThrow() as unknown as Promise<StackEntity>;
.executeTakeFirstOrThrow();
}
@GenerateSql({ params: [DummyValue.UUID] })
getById(id: string): Promise<StackEntity | undefined> {
getById(id: string) {
return this.db
.selectFrom('asset_stack')
.selectAll()
.select((eb) => withAssets(eb, true))
.where('id', '=', asUuid(id))
.executeTakeFirst() as Promise<StackEntity | undefined>;
.executeTakeFirst();
}
}

View File

@@ -17,14 +17,14 @@ export class TagRepository {
@GenerateSql({ params: [DummyValue.UUID] })
get(id: string) {
return this.db.selectFrom('tags').select(columns.tagDto).where('id', '=', id).executeTakeFirst();
return this.db.selectFrom('tags').select(columns.tag).where('id', '=', id).executeTakeFirst();
}
@GenerateSql({ params: [DummyValue.UUID, DummyValue.STRING] })
getByValue(userId: string, value: string) {
return this.db
.selectFrom('tags')
.select(columns.tagDto)
.select(columns.tag)
.where('userId', '=', userId)
.where('value', '=', value)
.executeTakeFirst();
@@ -68,12 +68,7 @@ export class TagRepository {
@GenerateSql({ params: [DummyValue.UUID] })
getAll(userId: string) {
return this.db
.selectFrom('tags')
.select(columns.tagDto)
.where('userId', '=', userId)
.orderBy('value asc')
.execute();
return this.db.selectFrom('tags').select(columns.tag).where('userId', '=', userId).orderBy('value asc').execute();
}
@GenerateSql({ params: [{ userId: DummyValue.UUID, color: DummyValue.STRING, value: DummyValue.STRING }] })