@konneal/engine 0.2.191 → 0.2.193

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,92 @@
1
+ // Typed-unit payloads are per DOCUMENT: a unit id is unique only within
2
+ // its publication — `u:table-1` exists in eleven of them. The legacy
3
+ // unit_payloads table is keyed by unit_id alone, so every MKO load
4
+ // overwrote the earlier documents' payloads and a reference resolved to
5
+ // whichever document loaded last (observed 2026-12-05: an answer citing
6
+ // OIML R 60-1 §5.1.2 Table 1 rendered OIML R 91-2's influence-factors
7
+ // table under it). unit_payloads_doc is keyed by (unit_id, doc_id); a
8
+ // legacy row stands only for the document that cited the unit.
9
+ import type { StoreQuery } from "./ports/store.ts";
10
+
11
+ export interface UnitRow {
12
+ unit_id: string;
13
+ doc_id: string | null;
14
+ type: string;
15
+ docidentifier: string | null;
16
+ edition: string | null;
17
+ payload: string;
18
+ }
19
+
20
+ /** A unit as its citing passage names it: the id and the document. */
21
+ export interface UnitKey {
22
+ unit_id: string;
23
+ doc_id?: string | null;
24
+ docidentifier?: string | null;
25
+ }
26
+
27
+ const COLS = "unit_id, doc_id, type, docidentifier, edition, payload";
28
+
29
+ /** May a legacy row stand for this key? A key that names no document
30
+ * keeps the legacy reading (the by-id tool contract); one that names a
31
+ * document accepts only that document's row — another publication's
32
+ * payload is never rendered in its place. */
33
+ function belongs(row: UnitRow, key: UnitKey): boolean {
34
+ if (!key.doc_id && !key.docidentifier) return true;
35
+ if (key.doc_id) return row.doc_id === key.doc_id;
36
+ return row.docidentifier === key.docidentifier;
37
+ }
38
+
39
+ async function byIds(db: StoreQuery, table: string, ids: string[]): Promise<UnitRow[] | null> {
40
+ const ph = ids.map((_, i) => `?${i + 1}`).join(",");
41
+ try {
42
+ const res = await db.prepare(`SELECT ${COLS} FROM ${table} WHERE unit_id IN (${ph})`).bind(...ids).all<UnitRow>();
43
+ return res.results ?? [];
44
+ } catch {
45
+ return null; // a deployment without that table
46
+ }
47
+ }
48
+
49
+ /** The payload rows for the given unit keys, aligned with them: null
50
+ * where no row belongs to the citing document. The document-scoped
51
+ * table answers first; the legacy table fills a key only with its own
52
+ * document's row. */
53
+ export async function unitPayloadRows(db: StoreQuery, keys: readonly UnitKey[]): Promise<(UnitRow | null)[]> {
54
+ const out: (UnitRow | null)[] = new Array(keys.length).fill(null);
55
+ if (!keys.length) return out;
56
+ const ids = [...new Set(keys.map((k) => k.unit_id))];
57
+ const scoped = keys.some((k) => k.doc_id) ? await byIds(db, "unit_payloads_doc", ids) : null;
58
+ const byDoc = new Map<string, UnitRow>();
59
+ for (const r of scoped ?? []) byDoc.set(`${r.unit_id}|${r.doc_id}`, r);
60
+ const legacy = new Map<string, UnitRow>();
61
+ for (const r of (await byIds(db, "unit_payloads", ids)) ?? []) if (!legacy.has(r.unit_id)) legacy.set(r.unit_id, r);
62
+ keys.forEach((key, i) => {
63
+ let row: UnitRow | null = key.doc_id ? (byDoc.get(`${key.unit_id}|${key.doc_id}`) ?? null) : null;
64
+ const l = legacy.get(key.unit_id);
65
+ if (!row && l && belongs(l, key)) row = l;
66
+ out[i] = row;
67
+ });
68
+ return out;
69
+ }
70
+
71
+ /** Every unit of one type under a publication (by docidentifier
72
+ * pattern), from the document-scoped table — or the legacy one where
73
+ * that deployment has none. */
74
+ export async function publicationUnitRows(
75
+ db: StoreQuery,
76
+ type: string,
77
+ docidentifierLike: string,
78
+ limit: number,
79
+ ): Promise<UnitRow[]> {
80
+ for (const table of ["unit_payloads_doc", "unit_payloads"]) {
81
+ try {
82
+ const res = await db
83
+ .prepare(`SELECT ${COLS} FROM ${table} WHERE type = ?1 AND docidentifier LIKE ?2 LIMIT ${limit}`)
84
+ .bind(type, docidentifierLike)
85
+ .all<UnitRow>();
86
+ return res.results ?? [];
87
+ } catch {
88
+ // the document-scoped table is absent — the legacy one stands
89
+ }
90
+ }
91
+ return [];
92
+ }
@@ -1,16 +0,0 @@
1
- import {
2
- handleAsk
3
- } from "./chunk-SWCKO4OB.js";
4
- import "./chunk-IRCDOFP4.js";
5
- import "./chunk-ECNBEZGK.js";
6
- import "./chunk-3SGGVZZ4.js";
7
- import "./chunk-AVNHVFEB.js";
8
- import "./chunk-GJQT4IMD.js";
9
- import "./chunk-ETLSWSZ7.js";
10
- import "./chunk-7YMIUTPY.js";
11
- import "./chunk-22WB6IVC.js";
12
- import "./chunk-3FYJM7LH.js";
13
- import "./chunk-H6ZULBW2.js";
14
- export {
15
- handleAsk
16
- };
@@ -1,14 +0,0 @@
1
- import {
2
- handleSearch
3
- } from "./chunk-CW6J3HTM.js";
4
- import "./chunk-3SGGVZZ4.js";
5
- import "./chunk-AVNHVFEB.js";
6
- import "./chunk-GJQT4IMD.js";
7
- import "./chunk-ETLSWSZ7.js";
8
- import "./chunk-7YMIUTPY.js";
9
- import "./chunk-22WB6IVC.js";
10
- import "./chunk-3FYJM7LH.js";
11
- import "./chunk-H6ZULBW2.js";
12
- export {
13
- handleSearch
14
- };