kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.22k stars 259 forks source link

Ensuring that queries use their transaction #895

Closed alexgleason closed 6 months ago

alexgleason commented 6 months ago

Kysely has a transaction API that wraps a block of code in a callback. But from what I can tell, it just sends a begin query at the start, and a commit query at the end. Does it do anything else to keep queries in a transaction together? What prevents async code from putting queries in the wrong transaction?

I am using a custom SQLite adapter (kysely-deno-sqlite) I modified from the stock SQLite adapter, and I now need to extend it to use a worker pool. How can I detect that queries are part of a transaction in my adapter and ensure I send them to the right worker?

koskimas commented 6 months ago

It also reserves a single connection. The transaction object passed to the callback is bound to that single connection. begin and commit/rollback is called on that connection automatically.

As long as you use the object passed to the callback, queries are run inside the transaction. If you use another transaction object or the "global" db object, you run things outside the transaction.