Skip to content

Commit

Permalink
Add placeholder type
Browse files Browse the repository at this point in the history
  • Loading branch information
etnoy committed Feb 5, 2025
1 parent acc87e8 commit 921ef80
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
10 changes: 0 additions & 10 deletions server/src/dtos/asset-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,6 @@ const hexOrBufferToBase64 = (encoded: string | Buffer) => {
export function mapAsset(entity: AssetEntity, options: AssetMapOptions = {}): AssetResponseDto {
const { stripMetadata = false, withStack = false } = options;

if (entity.localDateTime === null) {
throw new Error(`Asset ${entity.id} has no localDateTime`);
}
if (entity.fileCreatedAt === null) {
throw new Error(`Asset ${entity.id} has no fileCreatedAt`);
}
if (entity.fileModifiedAt === null) {
throw new Error(`Asset ${entity.id} has no fileModifiedAt`);
}

if (stripMetadata) {
const sanitizedAssetResponse: SanitizedAssetResponseDto = {
id: entity.id,
Expand Down
12 changes: 9 additions & 3 deletions server/src/entities/asset.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ export class AssetEntity {

@Index('idx_asset_file_created_at')
@Column({ type: 'timestamptz', nullable: true, default: null })
fileCreatedAt!: Date | null;
fileCreatedAt!: Date;

@Column({ type: 'timestamptz', nullable: true, default: null })
localDateTime!: Date | null;
localDateTime!: Date;

@Column({ type: 'timestamptz', nullable: true, default: null })
fileModifiedAt!: Date | null;
fileModifiedAt!: Date;

@Column({ type: 'boolean', default: false })
isFavorite!: boolean;
Expand Down Expand Up @@ -180,6 +180,12 @@ export class AssetEntity {
duplicateId!: string | null;
}

export type AssetEntityPlaceholder = AssetEntity & {
fileCreatedAt: Date | null;
fileModifiedAt: Date | null;
localDateTime: Date | null;
};

export function withExif<O>(qb: SelectQueryBuilder<DB, 'assets', O>) {
return qb.leftJoin('exif', 'assets.id', 'exif.assetId').select((eb) => eb.fn.toJson(eb.table('exif')).as('exifInfo'));
}
Expand Down
16 changes: 11 additions & 5 deletions server/src/repositories/asset.repository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Injectable } from '@nestjs/common';
import { Insertable, Kysely, Updateable, sql } from 'kysely';
import { Insertable, Kysely, NotNull, Updateable, sql } from 'kysely';
import { isEmpty, isUndefined, omitBy } from 'lodash';
import { InjectKysely } from 'nestjs-kysely';
import { ASSET_FILE_CONFLICT_KEYS, EXIF_CONFLICT_KEYS, JOB_STATUS_CONFLICT_KEYS } from 'src/constants';
import { AssetFiles, AssetJobStatus, Assets, DB, Exif } from 'src/db';
import { Chunked, ChunkedArray, DummyValue, GenerateSql } from 'src/decorators';
import {
AssetEntity,
AssetEntityPlaceholder,
hasPeople,
searchAssetBuilder,
truncatedDate,
Expand Down Expand Up @@ -79,8 +80,12 @@ export class AssetRepository implements IAssetRepository {
.execute();
}

create(asset: Insertable<Assets>): Promise<AssetEntity> {
return this.db.insertInto('assets').values(asset).returningAll().executeTakeFirst() as any as Promise<AssetEntity>;
create(asset: Insertable<Assets>): Promise<AssetEntityPlaceholder> {
return this.db
.insertInto('assets')
.values(asset)
.returningAll()
.executeTakeFirst() as any as Promise<AssetEntityPlaceholder>;
}

@GenerateSql({ params: [DummyValue.UUID, { day: 1, month: 1 }] })
Expand Down Expand Up @@ -131,6 +136,9 @@ export class AssetRepository implements IAssetRepository {
)
.innerJoin('exif', 'a.id', 'exif.assetId')
.selectAll('a')
.$narrowType<{ fileCreatedAt: NotNull }>()
.$narrowType<{ fileModifiedAt: NotNull }>()
.$narrowType<{ localDateTime: NotNull }>()
.select((eb) => eb.fn.toJson(eb.table('exif')).as('exifInfo')),
)
.selectFrom('res')
Expand Down Expand Up @@ -838,8 +846,6 @@ export class AssetRepository implements IAssetRepository {
.select((eb) => eb.fn.toJson(eb.table('stacked_assets')).as('stack'))
.where('assets.ownerId', '=', anyUuid(options.userIds))
.where('isVisible', '=', true)
.where('assets.fileCreatedAt', 'is not', null)
.where('assets.fileModifiedAt', 'is not', null)
.where('updatedAt', '>', options.updatedAfter)
.limit(options.limit)
.execute() as any as Promise<AssetEntity[]>;
Expand Down
3 changes: 3 additions & 0 deletions server/src/repositories/view-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export class ViewRepository {
.where('deletedAt', 'is', null)
.where('originalPath', 'like', `%${normalizedPath}/%`)
.where('originalPath', 'not like', `%${normalizedPath}/%/%`)
.$narrowType<{ fileCreatedAt: Date }>()
.$narrowType<{ fileModifiedAt: Date }>()
.$narrowType<{ localDateTime: Date }>()
.orderBy(
(eb) => eb.fn('regexp_replace', ['assets.originalPath', eb.val('.*/(.+)'), eb.val(String.raw`\1`)]),
'asc',
Expand Down

0 comments on commit 921ef80

Please sign in to comment.