rewrite as a single statement

This commit is contained in:
Jorge Montejo 2025-10-02 21:19:04 +02:00
parent 0bf848b1e7
commit 6075f5313e

View file

@ -163,19 +163,21 @@ export class TagRepository {
} }
async deleteEmptyTags() { async deleteEmptyTags() {
// TODO rewrite as a single statement
await this.db.transaction().execute(async (tx) => { await this.db.transaction().execute(async (tx) => {
const result = await tx const result = await tx
.selectFrom('tag') .deleteFrom('tag')
.select('tag.id') .where('id', 'in', (eb) =>
.leftJoin('tag_asset', 'tag.id', 'tag_asset.tagsId') eb
.where('tag_asset.assetsId', 'is', null) .selectFrom('tag')
.execute(); .leftJoin('tag_asset', 'tag.id', 'tag_asset.tagsId')
.select('tag.id')
.where('tag_asset.assetsId', 'is', null),
)
.executeTakeFirst();
const ids = result.map(row => row.id); const deletedRows = Number(result.numDeletedRows);
if (ids.length > 0) { if (deletedRows > 0) {
await this.db.deleteFrom('tag').where('id', 'in', ids).execute(); this.logger.log(`Deleted ${deletedRows} empty tags`);
this.logger.log(`Deleted ${ids.length} empty tags`);
} }
}); });
} }