feat(expiry): NoteRepository.trashBatch atomic (#5 v0.2.3)

This commit is contained in:
altair823
2026-05-02 00:01:03 +09:00
parent 00423fb235
commit fec80361dd
2 changed files with 71 additions and 0 deletions

View File

@@ -241,6 +241,32 @@ export class NoteRepository {
tx();
}
/**
* Atomically transition a batch of notes from active → trash.
* Returns the number of notes that actually transitioned (i.e. were active
* before the call). Already-trashed and unknown ids are silent skips —
* counting them would inflate `expired_batch_trash` telemetry.
*
* Reuses `trash(id, deletedAt)` per row to inherit pending_jobs cleanup
* invariant (§9.2 of #4 spec).
*/
trashBatch(ids: string[], deletedAt: string): { trashedCount: number } {
if (ids.length === 0) return { trashedCount: 0 };
let trashedCount = 0;
const tx = this.db.transaction((batch: string[]) => {
for (const id of batch) {
const row = this.db
.prepare(`SELECT deleted_at FROM notes WHERE id = ?`)
.get(id) as { deleted_at: string | null } | undefined;
if (!row || row.deleted_at !== null) continue;
this.trash(id, deletedAt);
trashedCount += 1;
}
});
tx(ids);
return { trashedCount };
}
restore(id: string): void {
const now = new Date().toISOString();
this.db