Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Use transactions for multi-step writes
Wrap multi-step write operations in a transaction to prevent partial updates.
Bad example
| 1 | export async function placeOrder(userId: string, items: Item[]) { |
| 2 | const order = await db.order.create({ data: { userId } }); |
| 3 |
|
| 4 | for (const item of items) { |
| 5 | await db.inventory.update({ |
| 6 | where: { id: item.id }, |
| 7 | data: { stock: { decrement: item.qty } }, |
| 8 | }); |
| 9 | } |
| 10 |
|
| 11 | await db.payment.create({ data: { orderId: order.id } }); |
| 12 | return order; |
| 13 | } |
Explanation (EN)
If any write fails, earlier writes remain, leaving the system in an inconsistent state.
Objašnjenje (HR)
Ako neki zapis ne uspije, prethodni zapisi ostaju i sustav postaje nekonzistentan.
Good example
| 1 | export async function placeOrder(userId: string, items: Item[]) { |
| 2 | return db.$transaction(async (tx) => { |
| 3 | const order = await tx.order.create({ data: { userId } }); |
| 4 |
|
| 5 | for (const item of items) { |
| 6 | await tx.inventory.update({ |
| 7 | where: { id: item.id }, |
| 8 | data: { stock: { decrement: item.qty } }, |
| 9 | }); |
| 10 | } |
| 11 |
|
| 12 | await tx.payment.create({ data: { orderId: order.id } }); |
| 13 | return order; |
| 14 | }); |
| 15 | } |
Explanation (EN)
Transactions make the operation atomic: either everything succeeds or everything rolls back.
Objašnjenje (HR)
Transakcije cine operaciju atomarnom: ili sve uspije ili se sve vrati.
Exceptions / Tradeoffs (EN)
If you intentionally use a saga/outbox with compensating actions, document it and ensure consistency guarantees.
Iznimke / Tradeoffi (HR)
Ako namjerno koristis saga/outbox s kompenzacijama, dokumentiraj to i osiguraj garancije konzistencije.