Missing Transaction
Multiple Prisma writes without $transaction
warningReliability
missing-transactionWhy this matters
Multiple writes without a transaction can leave your database half-updated when one of them fails: credits taken from one account and never given to the other. prodlint looks for two or more writes on a client named prisma in one function.
Example
lib/credits.tsasync function transferCredits(from: string, to: string, amount: number) {
await prisma.account.update({
where: { id: from },
data: { credits: { decrement: amount } },
});
// If this fails, the credits are gone and never arrive
await prisma.account.update({
where: { id: to },
data: { credits: { increment: amount } },
});
}lib/credits.tsasync function transferCredits(from: string, to: string, amount: number) {
await prisma.$transaction([
prisma.account.update({
where: { id: from },
data: { credits: { decrement: amount } },
}),
prisma.account.update({
where: { id: to },
data: { credits: { increment: amount } },
}),
]);
}How to fix
Wrap related writes in prisma.$transaction([...]) so they all succeed or all roll back.
Reference
- In the output
- Findings carry
"ruleId": "missing-transaction"in--jsonand the same id in SARIF and in the terminal. - Silence one line
// prodlint-disable-next-line missing-transaction- Silence a whole file
// prodlint-disable missing-transactionat the top of the file- How it works
- Reads the syntax tree, with a text fallback for files that don't parse. Source on GitHub