feat: auto-cleanup orphaned tags when removed from entries
Tags that are no longer associated with any entry are automatically deleted from the database when a tag is removed from an entry. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -158,6 +158,7 @@ export interface TagRepository {
|
|||||||
getById(id: string): Tag | undefined;
|
getById(id: string): Tag | undefined;
|
||||||
getByEntryId(entryId: string): Tag[];
|
getByEntryId(entryId: string): Tag[];
|
||||||
updateEntryTags(entryId: string, tagNames: string[]): void;
|
updateEntryTags(entryId: string, tagNames: string[]): void;
|
||||||
|
cleanupOrphanedTags(): number;
|
||||||
}
|
}
|
||||||
|
|
||||||
class SQLiteTagRepository implements TagRepository {
|
class SQLiteTagRepository implements TagRepository {
|
||||||
@@ -215,6 +216,15 @@ class SQLiteTagRepository implements TagRepository {
|
|||||||
db.insert(entryTags).values({ entryId, tagId: tag.id }).run();
|
db.insert(entryTags).values({ entryId, tagId: tag.id }).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleanupOrphanedTags(): number {
|
||||||
|
// Delete tags that are not associated with any entry
|
||||||
|
const result = db.run(sql`
|
||||||
|
DELETE FROM tags
|
||||||
|
WHERE id NOT IN (SELECT DISTINCT tag_id FROM entry_tags)
|
||||||
|
`);
|
||||||
|
return result.changes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const tagRepository: TagRepository = new SQLiteTagRepository();
|
export const tagRepository: TagRepository = new SQLiteTagRepository();
|
||||||
|
|||||||
@@ -342,6 +342,9 @@ export const actions: Actions = {
|
|||||||
);
|
);
|
||||||
tagRepository.updateEntryTags(id, updatedTags);
|
tagRepository.updateEntryTags(id, updatedTags);
|
||||||
|
|
||||||
|
// Clean up orphaned tags
|
||||||
|
tagRepository.cleanupOrphanedTags();
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user