feat: sync stacks (#19629)

This commit is contained in:
Jason Rasmussen
2025-06-30 15:26:41 -04:00
committed by GitHub
parent 095ace8687
commit 181a7e115f
35 changed files with 646 additions and 62 deletions

View File

@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { ExpressionBuilder, Kysely, Updateable } from 'kysely';
import { ExpressionBuilder, Insertable, Kysely, Updateable } from 'kysely';
import { jsonArrayFrom } from 'kysely/helpers/postgres';
import { InjectKysely } from 'nestjs-kysely';
import { columns } from 'src/database';
@@ -55,12 +55,12 @@ export class StackRepository {
.execute();
}
async create(entity: { ownerId: string; assetIds: string[] }) {
async create(entity: Omit<Insertable<StackTable>, 'primaryAssetId'>, assetIds: string[]) {
return this.db.transaction().execute(async (tx) => {
const stacks = await tx
.selectFrom('asset_stack')
.where('asset_stack.ownerId', '=', entity.ownerId)
.where('asset_stack.primaryAssetId', 'in', entity.assetIds)
.where('asset_stack.primaryAssetId', 'in', assetIds)
.select('asset_stack.id')
.select((eb) =>
jsonArrayFrom(
@@ -73,13 +73,13 @@ export class StackRepository {
)
.execute();
const assetIds = new Set<string>(entity.assetIds);
const uniqueIds = new Set<string>(assetIds);
// children
for (const stack of stacks) {
if (stack.assets && stack.assets.length > 0) {
for (const asset of stack.assets) {
assetIds.add(asset.id);
uniqueIds.add(asset.id);
}
}
}
@@ -97,10 +97,7 @@ export class StackRepository {
const newRecord = await tx
.insertInto('asset_stack')
.values({
ownerId: entity.ownerId,
primaryAssetId: entity.assetIds[0],
})
.values({ ...entity, primaryAssetId: assetIds[0] })
.returning('id')
.executeTakeFirstOrThrow();
@@ -110,7 +107,7 @@ export class StackRepository {
stackId: newRecord.id,
updatedAt: new Date(),
})
.where('id', 'in', [...assetIds])
.where('id', 'in', [...uniqueIds])
.execute();
return tx