/** * @factory/database-engine/query — EAV pivot (TASK-0066 R1 "EAV pivot in one * indexed query" — the in-memory half). * * The API fetches `record_properties WHERE record_id IN (page)` in one query; * `pivotValues` maps those flat rows into `recordId → propertyId → value`. * The value JSON is passed through the codec's `fromWire` when a property set * is supplied (so cells always see normalized values). */ import type { PropertyValue } from "../values/types.js"; import type { PropertyInfo } from "../values/types.js"; import { codec } from "../values/registry.js"; export interface EavRow { recordId: string; propertyId: string; value: unknown; // JSON (the record_properties.value column) } /** Pivot flat EAV rows into a recordId → propertyId → value map. */ export function pivotValues( eav: readonly EavRow[], properties?: Map, ): Map> { const out = new Map>(); for (const row of eav) { let rec = out.get(row.recordId); if (!rec) { rec = {}; out.set(row.recordId, rec); } const property = properties?.get(row.propertyId); rec[row.propertyId] = property ? codec(property.type).fromWire(row.value) : (row.value as PropertyValue); } return out; } /** Assemble RecordRows from pages + pivoted values + the db's property set. */ export function assembleRows< T extends { id: string; pageId: string; position: string; icon?: string; coverUrl?: string; archivedAt?: number | null; createdTime: number; lastEditedTime: number; }, >( pages: readonly T[], values: Map>, titlePropertyId?: string, ): import("../values/types.js").RecordRow[] { return pages.map((p) => { const vals = values.get(p.id) ?? {}; const row: import("../values/types.js").RecordRow = { id: p.id, pageId: p.pageId, position: p.position, values: vals, createdTime: p.createdTime, lastEditedTime: p.lastEditedTime, }; if (p.icon !== undefined) row.icon = p.icon; if (p.coverUrl !== undefined) row.coverUrl = p.coverUrl; if (p.archivedAt !== null && p.archivedAt !== undefined) row.archivedAt = p.archivedAt; if (titlePropertyId && typeof vals[titlePropertyId] === "string") row.title = vals[titlePropertyId]; return row; }); }