Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1universalStack: sql
databasejoinsn+1redundancydata-modelling
Compute derived per-entity flags in the main query, not in extra methods
Instead of separate service methods that re-fetch relations to answer 'isSharedWithMe' style questions, extend the existing fetch query with joins so each entity arrives with its derived flags already populated.
PR: hegnar-journalist-boost · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codetypescript
| 1 | const articles = await getArticles(userId); |
| 2 | for (const a of articles) { |
| 3 | a.isSharedWithMe = await shareService.isSharedWith(a.id, userId); |
| 4 | a.isSharedWithOthers = await shareService.hasOtherShares(a.id); |
| 5 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | // one query: derive the flags via joins so the entity arrives complete |
| 2 | const articles = await db |
| 3 | .select({ |
| 4 | ...articleColumns, |
| 5 | isSharedWithMe: sql`bool_or(${shares.userId} = ${userId})`, |
| 6 | isSharedWithOthers: sql`bool_or(${shares.userId} <> ${userId})`, |
| 7 | }) |
| 8 | .from(article) |
| 9 | .leftJoin(shares, eq(shares.articleId, article.id)) |
| 10 | .groupBy(article.id); |
Explanation (EN)
Objašnjenje (HR)