Transactions
Group related operations into one atomic unit with deterministic commit and rollback behavior.
Transactions
Use transactions for business flows where partial success is unacceptable.
Contract
db.transaction<R>(fn: (tx: Database) => Promise<R>): Promise<R>Inside the callback:
- all operations run in a single transaction
- any thrown error rolls back the full unit
- returned value becomes the transaction result
Canonical example
const transfer = await db.transaction(async (tx) => {
await tx.wallets.update({ userId: fromId }, { balance: fromBalance - amount })
await tx.wallets.update({ userId: toId }, { balance: toBalance + amount })
const rows = await tx.transfers.insert({ fromId, toId, amount: String(amount) })
return rows
})Usage rules
- Use
tx.*inside the callback, not outerdb.*. - Keep blocks short and database-focused.
- Do not call slow external services inside open transactions.
- Validate request payload before entering
transaction().
Failure behavior
- Any thrown error causes rollback.
- Kadak rethrows the error for your service layer to handle.