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).
Keep database transactions tightly scoped — no slow work inside
Scope DB transactions around the atomic writes only. Don't hold a transaction open across external HTTP calls, long loops, or other slow work — it ties up locks and connections and invites timeouts.
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)
Wrapping the entire loop — including remote fetches — in one transaction holds a DB connection and locks for as long as all the I/O takes, throttling other writers and risking transaction timeouts.
Objašnjenje (HR)
Omotavanje cijele petlje — uključujući udaljene dohvate — u jednu transakciju drži DB konekciju i lockove cijelo vrijeme trajanja I/O-a, koči ostale pisce i riskira timeout transakcije.
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)
Per-item transactions also give you per-item failure isolation: one item failing doesn't roll back the whole batch. If multiple items truly must commit atomically together, that's a different requirement — but then keep the slow fetches out of the transaction body.
Bilješke (HR)
Transakcije po stavci daju i izolaciju grešaka po stavci: pad jedne stavke ne poništava cijelu seriju. Ako više stavki stvarno mora atomarno commitati zajedno, to je drugačiji zahtjev — ali tada i dalje držite spore dohvate izvan tijela transakcije.
Exceptions / Tradeoffs (EN)
Acceptable for very small, fast batches where the whole operation completes in milliseconds and atomicity across all items is required.
Iznimke / Tradeoffi (HR)
Prihvatljivo za vrlo male, brze serije gdje cijela operacija završi u milisekundama i potrebna je atomarnost preko svih stavki.