Skip to content

Commit 921ef80

Browse files
committed
Add placeholder type
1 parent acc87e8 commit 921ef80

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

server/src/dtos/asset-response.dto.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,6 @@ const hexOrBufferToBase64 = (encoded: string | Buffer) => {
113113
export function mapAsset(entity: AssetEntity, options: AssetMapOptions = {}): AssetResponseDto {
114114
const { stripMetadata = false, withStack = false } = options;
115115

116-
if (entity.localDateTime === null) {
117-
throw new Error(`Asset ${entity.id} has no localDateTime`);
118-
}
119-
if (entity.fileCreatedAt === null) {
120-
throw new Error(`Asset ${entity.id} has no fileCreatedAt`);
121-
}
122-
if (entity.fileModifiedAt === null) {
123-
throw new Error(`Asset ${entity.id} has no fileModifiedAt`);
124-
}
125-
126116
if (stripMetadata) {
127117
const sanitizedAssetResponse: SanitizedAssetResponseDto = {
128118
id: entity.id,

server/src/entities/asset.entity.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ export class AssetEntity {
101101

102102
@Index('idx_asset_file_created_at')
103103
@Column({ type: 'timestamptz', nullable: true, default: null })
104-
fileCreatedAt!: Date | null;
104+
fileCreatedAt!: Date;
105105

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

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

112112
@Column({ type: 'boolean', default: false })
113113
isFavorite!: boolean;
@@ -180,6 +180,12 @@ export class AssetEntity {
180180
duplicateId!: string | null;
181181
}
182182

183+
export type AssetEntityPlaceholder = AssetEntity & {
184+
fileCreatedAt: Date | null;
185+
fileModifiedAt: Date | null;
186+
localDateTime: Date | null;
187+
};
188+
183189
export function withExif<O>(qb: SelectQueryBuilder<DB, 'assets', O>) {
184190
return qb.leftJoin('exif', 'assets.id', 'exif.assetId').select((eb) => eb.fn.toJson(eb.table('exif')).as('exifInfo'));
185191
}

server/src/repositories/asset.repository.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Injectable } from '@nestjs/common';
2-
import { Insertable, Kysely, Updateable, sql } from 'kysely';
2+
import { Insertable, Kysely, NotNull, Updateable, sql } from 'kysely';
33
import { isEmpty, isUndefined, omitBy } from 'lodash';
44
import { InjectKysely } from 'nestjs-kysely';
55
import { ASSET_FILE_CONFLICT_KEYS, EXIF_CONFLICT_KEYS, JOB_STATUS_CONFLICT_KEYS } from 'src/constants';
66
import { AssetFiles, AssetJobStatus, Assets, DB, Exif } from 'src/db';
77
import { Chunked, ChunkedArray, DummyValue, GenerateSql } from 'src/decorators';
88
import {
99
AssetEntity,
10+
AssetEntityPlaceholder,
1011
hasPeople,
1112
searchAssetBuilder,
1213
truncatedDate,
@@ -79,8 +80,12 @@ export class AssetRepository implements IAssetRepository {
7980
.execute();
8081
}
8182

82-
create(asset: Insertable<Assets>): Promise<AssetEntity> {
83-
return this.db.insertInto('assets').values(asset).returningAll().executeTakeFirst() as any as Promise<AssetEntity>;
83+
create(asset: Insertable<Assets>): Promise<AssetEntityPlaceholder> {
84+
return this.db
85+
.insertInto('assets')
86+
.values(asset)
87+
.returningAll()
88+
.executeTakeFirst() as any as Promise<AssetEntityPlaceholder>;
8489
}
8590

8691
@GenerateSql({ params: [DummyValue.UUID, { day: 1, month: 1 }] })
@@ -131,6 +136,9 @@ export class AssetRepository implements IAssetRepository {
131136
)
132137
.innerJoin('exif', 'a.id', 'exif.assetId')
133138
.selectAll('a')
139+
.$narrowType<{ fileCreatedAt: NotNull }>()
140+
.$narrowType<{ fileModifiedAt: NotNull }>()
141+
.$narrowType<{ localDateTime: NotNull }>()
134142
.select((eb) => eb.fn.toJson(eb.table('exif')).as('exifInfo')),
135143
)
136144
.selectFrom('res')
@@ -838,8 +846,6 @@ export class AssetRepository implements IAssetRepository {
838846
.select((eb) => eb.fn.toJson(eb.table('stacked_assets')).as('stack'))
839847
.where('assets.ownerId', '=', anyUuid(options.userIds))
840848
.where('isVisible', '=', true)
841-
.where('assets.fileCreatedAt', 'is not', null)
842-
.where('assets.fileModifiedAt', 'is not', null)
843849
.where('updatedAt', '>', options.updatedAfter)
844850
.limit(options.limit)
845851
.execute() as any as Promise<AssetEntity[]>;

server/src/repositories/view-repository.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export class ViewRepository {
3737
.where('deletedAt', 'is', null)
3838
.where('originalPath', 'like', `%${normalizedPath}/%`)
3939
.where('originalPath', 'not like', `%${normalizedPath}/%/%`)
40+
.$narrowType<{ fileCreatedAt: Date }>()
41+
.$narrowType<{ fileModifiedAt: Date }>()
42+
.$narrowType<{ localDateTime: Date }>()
4043
.orderBy(
4144
(eb) => eb.fn('regexp_replace', ['assets.originalPath', eb.val('.*/(.+)'), eb.val(String.raw`\1`)]),
4245
'asc',

0 commit comments

Comments
 (0)