Transactions
Group several writes so they all land or none do.
Interactive transactions
await db.$transaction(async (tx) => {
const from = await tx.account.where({ id: a }).first();
if (!from || from.balance < amount) throw new Error("Insufficient funds");
await tx.account.where({ id: a }).update({ data: { balance: { decrement: amount } } });
await tx.account.where({ id: b }).update({ data: { balance: { increment: amount } } });
});Isolation levels
Level | PostgreSQL | SQLite |
|---|---|---|
| default | — |
| supported | — |
| supported | default |
Interactive transactions time out after 5 seconds by default. Pass { timeout: 15000 } as the second argument for longer work, and keep network calls out of them.
Updated
Was this page helpful?