Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Handle pre-existing pooled resources when patching via a "new resource" event
Initialize every pooled connection through supported lifecycle hooks, including resources created before setup, and handle setup errors before allowing use.
Bad example
| 1 | pool.on('connection', (connection) => { |
| 2 | connection.query(UTC_SESSION_SQL); |
| 3 | }); |
| 4 | // migrations already opened one or more connections before this listener existed |
| 5 | await dataSource.query(UTC_SESSION_SQL); // patches only one connection back from the pool |
Explanation (EN)
A future-connection listener cannot initialize earlier resources, and unobserved setup failures can leave connections misconfigured.
Objašnjenje (HR)
Listener za buduće veze ne inicijalizira već otvorene resurse, a neobrađen kvar pripreme može ostaviti pogrešno konfigurirane veze.
Good example
| 1 | const pool = createConfiguredPool({ |
| 2 | initialize: async (connection) => { |
| 3 | await configureSession(connection); |
| 4 | }, |
| 5 | }); |
| 6 | await pool.start(); |
Explanation (EN)
This schematic pool adapter gates each checkout on successful setup; implement initialize using the actual driver's documented lifecycle.
Objašnjenje (HR)
Ovaj shematski adapter dopušta korištenje veze tek nakon uspješne pripreme. initialize implementiraj prema dokumentiranom životnom ciklusu konkretnog drivera.
Notes (EN)
For per-connection initialization, register the driver-supported initialization hook before the pool opens resources, await setup before use where the API supports it, and handle initialization failures.
Bilješke (HR)
Za inicijalizaciju svake veze registriraj podržani inicijalizacijski hook prije otvaranja resursa poola, pričekaj završetak pripreme prije uporabe kada API to omogućuje i obradi kvar inicijalizacije.
Exceptions / Tradeoffs (EN)
Do not depend on private pool fields such as _allConnections. If resources already exist, use documented pool reinitialization or initialization-on-checkout with a completion gate. API styles differ between promise and EventEmitter drivers.