Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Keep database transactions tightly scoped — no slow work inside
Keep database transactions short while preserving every read/write needed for the atomic invariant; move independent external work outside them.
Bad example
| 1 | await dataSource.transaction(async (manager) => { |
| 2 | for (const item of items) { |
| 3 | // External call + heavy processing keep the transaction (and its locks) open the whole time |
| 4 | const rates = await httpClient.fetchRates(item); |
| 5 | await syncService.process(item, rates, manager); |
| 6 | } |
| 7 | }); |
Explanation (EN)
External I/O held inside a transaction prolongs lock and connection occupancy.
Objašnjenje (HR)
Vanjski I/O unutar transakcije produljuje zadržavanje lockova i veze.
Good example
| 1 | for (const item of items) { |
| 2 | // Slow work happens outside the transaction |
| 3 | const rates = await httpClient.fetchRates(item); |
| 4 |
|
| 5 | // Transaction wraps only the writes that must be atomic |
| 6 | await dataSource.transaction(async (manager) => { |
| 7 | await syncService.process(item, rates, manager); |
| 8 | }); |
| 9 | } |
Explanation (EN)
External fetches run outside the transaction; each transaction is short-lived and covers only the atomic writes for one item, keeping locks and connection hold-time minimal.
Objašnjenje (HR)
Vanjski dohvati izvršavaju se izvan transakcije; svaka transakcija je kratkotrajna i pokriva samo atomarne zapise za jednu stavku, držeći lockove i vrijeme zauzeća konekcije minimalnim.
Notes (EN)
Keep invariant-dependent reads and writes in the same short transaction; perform independent slow external work before it and revalidate any assumptions that can change.
Bilješke (HR)
Čitanja i upise koji održavaju istu invarijantu zadrži u kratkoj zajedničkoj transakciji. Neovisan spor vanjski posao obavi prije nje te ponovno provjeri pretpostavke koje su se mogle promijeniti.
Exceptions / Tradeoffs (EN)
Acceptable for very small, fast batches where the whole operation completes in milliseconds and atomicity across all items is required. Splitting a batch into per-item transactions changes atomicity. Retain a shared transaction when all items must commit together, and do not move a concurrency-sensitive read outside its lock.
Iznimke / Tradeoffi (HR)
Zajednička transakcija opravdana je kada svi elementi moraju uspjeti zajedno, osobito za kratke pakete. Razdvajanje po elementima mijenja atomarnost. Čitanje osjetljivo na konkurentnost ne izdvajaj iz potrebnog zaključavanja.