stack as tuple

This commit is contained in:
mertalev 2025-05-04 20:11:48 -04:00
parent 8837f5b4fb
commit a3a2ced3a9
No known key found for this signature in database
GPG Key ID: DF6ABC77AAD98C95
4 changed files with 19 additions and 17 deletions

View File

@ -83,10 +83,17 @@ export class TimeBucketAssetResponseDto implements TimeBucketAssets {
@ApiProperty({ type: 'array', items: { type: 'string', nullable: true } })
duration!: (string | null)[];
stackCount?: number[];
@ApiProperty({ type: 'array', items: { type: 'string', nullable: true } })
stackId?: (string | null)[];
// id, count
@ApiProperty({
type: 'array',
items: {
type: 'array',
nullable: true,
items: { oneOf: [{ type: 'string' }, { type: 'number' }], minItems: 2, maxItems: 2 },
},
description: 'The stack ID and stack asset count as a tuple',
})
stack?: ([string, number] | null)[];
@ApiProperty({ type: 'array', items: { type: 'string', nullable: true } })
projectionType!: (string | null)[];

View File

@ -652,14 +652,15 @@ export class AssetRepository {
(eb) =>
eb
.selectFrom('assets as stacked')
.select((eb) => eb.fn.coalesce(eb.fn.count(eb.table('stacked')), eb.lit(0)).as('stackCount'))
.select(sql`json_build_array(stacked."stackId", count('stacked'))`.as('stack'))
.whereRef('stacked.stackId', '=', 'assets.stackId')
.where('stacked.deletedAt', 'is', null)
.where('stacked.isArchived', '=', false)
.groupBy('stacked.stackId')
.as('stacked_assets'),
(join) => join.onTrue(),
)
.select(['assets.stackId', 'stackCount']),
.select('stack'),
)
.$if(!!options.assetType, (qb) => qb.where('assets.type', '=', options.assetType!))
.$if(options.isDuplicate !== undefined, (qb) =>
@ -690,12 +691,7 @@ export class AssetRepository {
eb.fn.coalesce(eb.fn('array_agg', ['status']), sql.lit('{}')).as('status'),
eb.fn.coalesce(eb.fn('array_agg', ['thumbhash']), sql.lit('{}')).as('thumbhash'),
])
.$if(!!options.withStacked, (qb) =>
qb.select((eb) => [
eb.fn('array_agg', ['stackCount']).as('stackCount'),
eb.fn('array_agg', ['stackId']).as('stackId'),
]),
),
.$if(!!options.withStacked, (qb) => qb.select((eb) => eb.fn('array_agg', ['stack']).as('stack'))),
)
.selectFrom('agg')
.select(sql<string>`to_json(agg)::text`.as('assets'));

View File

@ -19,8 +19,7 @@ export type TimeBucketAssets = {
isImage: number[];
thumbhash: (string | null)[];
localDateTime: string[];
stackCount?: number[];
stackId?: (string | null)[];
stack?: ([string, number] | null)[];
duration: (string | null)[];
projectionType: (string | null)[];
livePhotoVideoId: (string | null)[];

View File

@ -437,11 +437,11 @@ export class AssetBucket {
people: [],
projectionType: bucketAssets.projectionType[i],
ratio: bucketAssets.ratio[i],
stack: bucketAssets.stackId?.[i]
stack: bucketAssets.stack?.[i]
? {
id: bucketAssets.stackId[i]!,
id: bucketAssets.stack[i]![0] as string,
primaryAssetId: bucketAssets.id[i],
assetCount: bucketAssets.stackCount![i],
assetCount: bucketAssets.stack[i]![1] as number,
}
: null,
thumbhash: bucketAssets.thumbhash[i],