Transactions
Run multiple writes safely as one unit.
Transactions
Problem: one write succeeds and the next write fails, leaving inconsistent data.
Solution: wrap related writes in db.transaction().
Basic transaction
await db.transaction(async (tx) => {
await tx.users.insert({
email: 'new@example.com',
name: 'New User',
isActive: true,
balance: '10.00',
})
await tx.users.update({ email: 'new@example.com' }, { isActive: false })
})If any query inside throws, KadakORM rolls back the transaction.
Important: use the
txclient inside the callback, not the outerdbclient.