Skip to content

Commit 081e0d3

Browse files
DavidBabinecclaude
andauthored
refactor(loops): collapse data.rows page-slice SQL into one whitelisted query (CoreBunch#159)
The data.rows loop source repeated the same ~35-line SELECT twelve times (6 orderBy/direction branches x 2 table kinds), differing only in the ORDER BY line. Adding a column to the projection meant editing twelve copies — a drift trap. Each table kind now has ONE query. The ORDER BY clause is composed from a closed, compile-time column map (POST_TYPE_ORDER_COLUMN / DATA_KIND_ORDER_COLUMN); orderBy is whitelisted against ALLOWED_ORDER_BY and direction narrowed to 'asc' | 'desc' before either reaches the SQL text, and every runtime value (tableId, limit, offset) rides as a positional parameter via db.unsafe — the same dialect-aware pattern resolveMediaIdsToPaths already used. Both DB adapters run identical row normalization (_json hydration, Date → ISO) on the unsafe path, so read semantics are unchanged. New dataRowsFetch.test.ts locks the behavior against a real migrated SQLite DB: every orderBy x direction ordering for both table kinds, the publishedAt → createdAt mapping on data-kind tables, status/soft-delete filtering, pagination with stable totalItems, the full LoopItem field projection, and unknown-input fallbacks. The suite passed against the old branchy implementation before the swap and passes unchanged after. dataRows.ts drops 903 → 540 lines and graduates from the module-size grandfathered ledger. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 6d06865 commit 081e0d3

3 files changed

Lines changed: 540 additions & 471 deletions

File tree

src/__tests__/architecture/module-size-budgets.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ const GRANDFATHERED: Record<string, number> = {
9696
// extracted into server/repositories/mediaAssetMapping.ts, dropping media.ts
9797
// to 583 lines — under CEILING, so it's now held by the normal ceiling rule.
9898
'server/handlers/cms/auth.ts': 854,
99-
'src/core/loops/sources/dataRows.ts': 903,
99+
// src/core/loops/sources/dataRows.ts graduated: the 12 per-order-branch SQL
100+
// copies collapsed into one query + a whitelisted ORDER BY column map,
101+
// dropping the file to 540 lines — under CEILING, so it's now held by the
102+
// normal ceiling rule.
100103
// The O(1) parentId pointer is a denormalised cache that every
101104
// parentage-changing primitive (insert/move/duplicate/wrap/paste) maintains
102105
// inline. The node deep-clone primitive lives in its own module

0 commit comments

Comments
 (0)