-
Notifications
You must be signed in to change notification settings - Fork 160
Open
Description
Problem
Today transactions are expressed manually with raw SQL:
await c.db.execute("BEGIN");
try {
// ...queries...
await c.db.execute("COMMIT");
} catch (error) {
await c.db.execute("ROLLBACK");
throw error;
}c.db is a shared actor-local database handle, so other c.db.execute(...) calls can interleave while a transaction is open (for example from concurrent actions). This makes transaction safety easy to get wrong.
Proposal
Add a first-class transaction API:
await c.db.transaction(async (tx) => {
await tx.execute("INSERT INTO todos (title) VALUES (?)", title);
await tx.execute(
"INSERT INTO comments (todo_id, body) VALUES (last_insert_rowid(), ?)",
body,
);
});Expected behavior
transactionacquires exclusive access for the callback duration.- Other
c.dboperations are queued until transaction commit/rollback. - Automatically
COMMITon success. - Automatically
ROLLBACKon throw. txonly exposes transaction-safe operations (e.g.tx.execute).
Why this helps
- Prevents interleaving bugs with
BEGIN/COMMIT. - Makes docs/examples safer and simpler.
- Aligns with ergonomics from other SQLite libraries.
Optional follow-ups
- Decide nested transaction behavior (
SAVEPOINTor disallow nesting). - Add typed return support from callback (
transaction<T>(...) => Promise<T>).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels