Skip to content

Commit

Permalink
refactor(server): AssetRepository.deleteFiles with Kysely
Browse files Browse the repository at this point in the history
  • Loading branch information
eligao committed Jan 23, 2025
1 parent 4dff398 commit c7914c2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
5 changes: 2 additions & 3 deletions server/src/interfaces/asset.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Insertable, Updateable } from 'kysely';
import { Insertable, Selectable, Updateable } from 'kysely';
import { AssetFiles, AssetJobStatus, Assets, Exif } from 'src/db';
import { AssetEntity } from 'src/entities/asset.entity';
import { AssetFileType, AssetOrder, AssetStatus, AssetType } from 'src/enum';
Expand Down Expand Up @@ -156,7 +156,6 @@ export interface IAssetRepository {
update(asset: Updateable<Assets> & { id: string }): Promise<AssetEntity>;
remove(asset: AssetEntity): Promise<void>;
findLivePhotoMatch(options: LivePhotoSearchOptions): Promise<AssetEntity | undefined>;
removeAssetFile(path: string): Promise<void>;
getStatistics(ownerId: string, options: AssetStatsOptions): Promise<AssetStats>;
getTimeBuckets(options: TimeBucketOptions): Promise<TimeBucketItem[]>;
getTimeBucket(timeBucket: string, options: TimeBucketOptions): Promise<AssetEntity[]>;
Expand All @@ -168,5 +167,5 @@ export interface IAssetRepository {
getChangedDeltaSync(options: AssetDeltaSyncOptions): Promise<AssetEntity[]>;
upsertFile(options: Insertable<AssetFiles>): Promise<void>;
upsertFiles(options: Insertable<AssetFiles>[]): Promise<void>;
deleteFiles(assetIds: string | string[]): Promise<void>;
deleteFiles(options: Selectable<AssetFiles>[]): Promise<void>;
}
14 changes: 10 additions & 4 deletions server/src/repositories/asset.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { Insertable, Kysely, Updateable, sql } from 'kysely';
import { Insertable, Kysely, Selectable, 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';
Expand Down Expand Up @@ -832,8 +832,14 @@ export class AssetRepository implements IAssetRepository {
.execute();
}

@GenerateSql({ params: [[DummyValue.UUID, DummyValue.UUID]] })
async deleteFiles(assetIds: string | string[]): Promise<void> {
await this.fileRepository.delete(assetIds);
async deleteFiles(files: Pick<Selectable<AssetFiles>, 'id'>[]): Promise<void> {
if (files.length === 0) {
return;
}

await this.db
.deleteFrom('asset_files')
.where('id', '=', anyUuid(files.map((file) => file.id)))
.execute();
}
}
1 change: 0 additions & 1 deletion server/src/repositories/media.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
GenerateThumbhashOptions,
GenerateThumbnailOptions,
ImageDimensions,
IMediaRepository,
ProbeOptions,
TranscodeCommand,
VideoInfo,
Expand Down
2 changes: 1 addition & 1 deletion server/src/services/media.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class MediaService extends BaseService {
pathsToDelete.push(fullsizeFile.path);
if (!generated.fullsizePath) {
// did not generate a new fullsize image, delete the existing record
await this.assetRepository.deleteFiles(fullsizeFile.id);
await this.assetRepository.deleteFiles([fullsizeFile]);
}
}

Expand Down

0 comments on commit c7914c2

Please sign in to comment.