Rules Hub

Coding Rules Library

← Back to all rules
backend ruleStack: node
architecturebackendrepository-patterndata-fetchingconsistency

Enforce consistent query behavior across data sources

Ensure primary and fallback data providers use identical matching logic (exact vs. fuzzy) to prevent unpredictable results.

PR: Feat/FCK-1888 - Create new articles by tag endpoint #1291Created: Dec 8, 2025

Bad example

Old codets
1class ArticleRepository {
2 async findByTag(tag: string) {
3 // Primary source (Elasticsearch): Configured for EXACT match
4 const fromSearch = await this.searchEngine.query({ term: { tag } });
5 if (fromSearch.length > 0) return fromSearch;
6
7 // Fallback source (Database): Configured for FUZZY match
8 // If primary misses, users suddenly get broad, loosely matched results.
9 return this.db.query(`SELECT * FROM articles WHERE tag LIKE ?`, [`%${tag}%`]);
10 }
11}

Explanation (EN)

The primary source uses strict exact matching, while the fallback uses loose fuzzy matching. This creates inconsistent behavior where the result set's quality changes drastically depending solely on which provider answers the request.

Objašnjenje (HR)

Primarni izvor podataka koristi strogo točno podudaranje, dok rezervni (fallback) koristi približno (fuzzy) podudaranje. To stvara nekonzistentno ponašanje gdje se kvaliteta i opseg rezultata drastično mijenjaju ovisno o tome koji servis odgovori na zahtjev.

Good example

New codets
1class ArticleRepository {
2 async findByTag(tag: string) {
3 // Primary source: EXACT match
4 const fromSearch = await this.searchEngine.query({ term: { tag } });
5 if (fromSearch.length > 0) return fromSearch;
6
7 // Fallback source: Enforced EXACT match
8 // Both sources now adhere to the same strict contract.
9 return this.db.query(`SELECT * FROM articles WHERE tag = ?`, [tag]);
10 }
11}

Explanation (EN)

Both data providers are constrained to use the same matching logic (exact match). The repository guarantees consistent behavior and result quality regardless of which underlying data source resolves the request.

Objašnjenje (HR)

Oba izvora podataka ograničena su na korištenje iste logike podudaranja (točno podudaranje). Repozitorij time jamči konzistentno ponašanje i kvalitetu rezultata bez obzira na to koji izvor podataka obradi zahtjev.

Notes (EN)

This often occurs when falling back from a specialized search engine (like Elasticsearch) to a general-purpose database (like SQL). Explicitly test the fallback path to ensure semantics align.

Bilješke (HR)

Ovo se često događa pri prebacivanju sa specijalizirane tražilice (poput Elasticsearcha) na opću bazu podataka (poput SQL-a). Eksplicitno testirajte 'fallback' putanju kako biste osigurali da su semantike upita usklađene.