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).
Push filtering predicates into the query, not into in-memory loops
When the data store can express a filter, add it to the query instead of fetching everything and filtering in application code.
Bad example
| 1 | // Fetch all records, then filter in memory |
| 2 | const all = await searchClient.query({ index: 'instruments' }); |
| 3 | const filtered = all.filter((i) => countries.includes(i.countryCode)); |
Explanation (EN)
The store returns every record over the wire and the process discards most of them. This wastes bandwidth and memory, ignores available indexes, and is easy to get wrong as the dataset grows.
Objašnjenje (HR)
Baza vraca sve zapise preko mreze, a proces vecinu odbaci. Time se trosi propusnost i memorija, ignoriraju se postojeci indeksi i lako se uvuce greska kako skup podataka raste.
Good example
| 1 | // Express the filter as part of the query |
| 2 | const filtered = await searchClient.query({ |
| 3 | index: 'instruments', |
| 4 | filter: { terms: { countryCode: countries } }, |
| 5 | }); |
Explanation (EN)
The predicate is sent to the data store, which can use indexes and return only matching rows. Less data crosses the wire and there is no hand-written in-memory filtering to maintain.
Objašnjenje (HR)
Predikat se salje bazi, koja moze koristiti indekse i vratiti samo odgovarajuce retke. Manje podataka prolazi mrezom i nema rucnog filtriranja u memoriji koje treba odrzavati.
Notes (EN)
Applies to SQL WHERE clauses, ElasticSearch filters, MongoDB queries, and any API that accepts server-side filter params. Pass the filter value down through the call chain so the lowest data-access layer can apply it.
Bilješke (HR)
Vrijedi za SQL WHERE klauzule, ElasticSearch filtere, MongoDB upite i svaki API koji prima parametre filtriranja na posluzitelju. Proslijedi vrijednost filtera kroz lanac poziva do najnizeg sloja pristupa podacima.
Exceptions / Tradeoffs (EN)
Filtering in memory is acceptable when the predicate cannot be expressed by the store, the result set is already small and bounded, or you need post-fetch logic that depends on derived/aggregated values.
Iznimke / Tradeoffi (HR)
Filtriranje u memoriji je prihvatljivo kada predikat nije moguce izraziti u bazi, kada je rezultat vec malen i ogranicen, ili kada treba logika nakon dohvata koja ovisi o izvedenim/agregiranim vrijednostima.