dyedgreen / deno-sqlite

Deno SQLite module
https://deno.land/x/sqlite
MIT License
409 stars 36 forks source link

Support transactions #164

Closed dyedgreen closed 2 years ago

dyedgreen commented 2 years ago

Closes #162

dyedgreen commented 2 years ago

One open question is whether this should support async functions inside the transaction. I'm tempted to use an API similar to the standard libraries:

async function transaction(closure: () => Promise<V>): Promise<V>
function transactionSync(closure: () => V): V

But there is a big open question there what should happen when you try to run transactions in parallel, e.g.:

let first = db.transaction(async () => {
  / * run queries ... * /
});
let second = db.transaction(async () => {
  / * run queries ... * /
});
let [firstResult, secondResult] = Promise.all(first, second);

Should these transactions be serialized, or should they be nested (and if they are nested, do we guarantee a nesting order)?

Because of this difficulty with the interface, I'd be tempted to say transactions must be synchronous, and if you need to do some async work, conditional on a query result you must run in a transaction, there is always BEGIN TRANSACTION, which gives you more fine grained control.