feat(expiry): NoteRepository.trashBatch atomic (#5 v0.2.3)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user