@bjornpagen/bumbledb 0.20.2 → 1.0.0

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.
Files changed (192) hide show
  1. package/COOKBOOK.md +283 -1611
  2. package/README.md +156 -118
  3. package/dist/capacity.d.ts +16 -12
  4. package/dist/capacity.d.ts.map +1 -1
  5. package/dist/capacity.js +16 -48
  6. package/dist/capacity.js.map +1 -1
  7. package/dist/changes.d.ts +54 -0
  8. package/dist/changes.d.ts.map +1 -0
  9. package/dist/changes.js +245 -0
  10. package/dist/changes.js.map +1 -0
  11. package/dist/close.d.ts +24 -0
  12. package/dist/close.d.ts.map +1 -0
  13. package/dist/close.js +34 -0
  14. package/dist/close.js.map +1 -0
  15. package/dist/closed.d.ts +1 -3
  16. package/dist/closed.d.ts.map +1 -1
  17. package/dist/closed.js +24 -12
  18. package/dist/closed.js.map +1 -1
  19. package/dist/codec.d.ts +56 -0
  20. package/dist/codec.d.ts.map +1 -0
  21. package/dist/codec.js +289 -0
  22. package/dist/codec.js.map +1 -0
  23. package/dist/compile.d.ts +68 -0
  24. package/dist/compile.d.ts.map +1 -0
  25. package/dist/compile.js +75 -0
  26. package/dist/compile.js.map +1 -0
  27. package/dist/db-native.d.ts +170 -0
  28. package/dist/db-native.d.ts.map +1 -0
  29. package/dist/db-native.js +5 -0
  30. package/dist/db-native.js.map +1 -0
  31. package/dist/db.d.ts +101 -233
  32. package/dist/db.d.ts.map +1 -1
  33. package/dist/db.js +232 -1084
  34. package/dist/db.js.map +1 -1
  35. package/dist/errors.d.ts +57 -0
  36. package/dist/errors.d.ts.map +1 -0
  37. package/dist/errors.js +32 -0
  38. package/dist/errors.js.map +1 -0
  39. package/dist/face.d.ts.map +1 -1
  40. package/dist/face.js +2 -2
  41. package/dist/face.js.map +1 -1
  42. package/dist/fields.d.ts +65 -24
  43. package/dist/fields.d.ts.map +1 -1
  44. package/dist/fields.js +94 -24
  45. package/dist/fields.js.map +1 -1
  46. package/dist/index.d.ts +37 -32
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +17 -21
  49. package/dist/index.js.map +1 -1
  50. package/dist/internal/log.d.ts +24 -0
  51. package/dist/internal/log.d.ts.map +1 -0
  52. package/dist/internal/log.js +10 -0
  53. package/dist/internal/log.js.map +1 -0
  54. package/dist/law.d.ts +8 -13
  55. package/dist/law.d.ts.map +1 -1
  56. package/dist/law.js +6 -59
  57. package/dist/law.js.map +1 -1
  58. package/dist/lower.d.ts +0 -13
  59. package/dist/lower.d.ts.map +1 -1
  60. package/dist/lower.js +9 -17
  61. package/dist/lower.js.map +1 -1
  62. package/dist/migration.d.ts +33 -0
  63. package/dist/migration.d.ts.map +1 -0
  64. package/dist/migration.js +48 -0
  65. package/dist/migration.js.map +1 -0
  66. package/dist/native.d.ts +256 -301
  67. package/dist/native.d.ts.map +1 -1
  68. package/dist/native.js +65 -108
  69. package/dist/native.js.map +1 -1
  70. package/dist/query/atom.d.ts +24 -13
  71. package/dist/query/atom.d.ts.map +1 -1
  72. package/dist/query/atom.js +7 -3
  73. package/dist/query/atom.js.map +1 -1
  74. package/dist/query/compute.d.ts +68 -0
  75. package/dist/query/compute.d.ts.map +1 -0
  76. package/dist/query/compute.js +141 -0
  77. package/dist/query/compute.js.map +1 -0
  78. package/dist/query/find.d.ts +15 -8
  79. package/dist/query/find.d.ts.map +1 -1
  80. package/dist/query/find.js +6 -2
  81. package/dist/query/find.js.map +1 -1
  82. package/dist/query/lower.d.ts +23 -2
  83. package/dist/query/lower.d.ts.map +1 -1
  84. package/dist/query/lower.js +433 -80
  85. package/dist/query/lower.js.map +1 -1
  86. package/dist/query/parse-ir.d.ts.map +1 -1
  87. package/dist/query/parse-ir.js +17 -8
  88. package/dist/query/parse-ir.js.map +1 -1
  89. package/dist/query/run.d.ts +12 -2
  90. package/dist/query/run.d.ts.map +1 -1
  91. package/dist/query/run.js +26 -9
  92. package/dist/query/run.js.map +1 -1
  93. package/dist/query/scope.d.ts +47 -3
  94. package/dist/query/scope.d.ts.map +1 -1
  95. package/dist/query/scope.js +83 -19
  96. package/dist/query/scope.js.map +1 -1
  97. package/dist/relation.d.ts +3 -8
  98. package/dist/relation.d.ts.map +1 -1
  99. package/dist/relation.js +16 -10
  100. package/dist/relation.js.map +1 -1
  101. package/dist/result.d.ts +47 -0
  102. package/dist/result.d.ts.map +1 -0
  103. package/dist/result.js +50 -0
  104. package/dist/result.js.map +1 -0
  105. package/dist/rows.d.ts +69 -0
  106. package/dist/rows.d.ts.map +1 -0
  107. package/dist/rows.js +328 -0
  108. package/dist/rows.js.map +1 -0
  109. package/dist/runtime-codes.d.ts +2 -0
  110. package/dist/runtime-codes.d.ts.map +1 -0
  111. package/dist/runtime-codes.js +19 -0
  112. package/dist/runtime-codes.js.map +1 -0
  113. package/dist/runtime-errors.d.ts +106 -0
  114. package/dist/runtime-errors.d.ts.map +1 -0
  115. package/dist/runtime-errors.js +62 -0
  116. package/dist/runtime-errors.js.map +1 -0
  117. package/dist/runtime-native.d.ts +171 -0
  118. package/dist/runtime-native.d.ts.map +1 -0
  119. package/dist/runtime-native.js +5 -0
  120. package/dist/runtime-native.js.map +1 -0
  121. package/dist/runtime.d.ts +73 -0
  122. package/dist/runtime.d.ts.map +1 -0
  123. package/dist/runtime.js +293 -0
  124. package/dist/runtime.js.map +1 -0
  125. package/dist/scalar.d.ts +165 -0
  126. package/dist/scalar.d.ts.map +1 -0
  127. package/dist/scalar.js +344 -0
  128. package/dist/scalar.js.map +1 -0
  129. package/dist/schema.d.ts +1 -1
  130. package/dist/schema.d.ts.map +1 -1
  131. package/dist/schema.js +34 -37
  132. package/dist/schema.js.map +1 -1
  133. package/dist/shape.d.ts +28 -0
  134. package/dist/shape.d.ts.map +1 -0
  135. package/dist/shape.js +2 -0
  136. package/dist/shape.js.map +1 -0
  137. package/dist/spec.d.ts +27 -3
  138. package/dist/spec.d.ts.map +1 -1
  139. package/dist/spec.js +22 -1
  140. package/dist/spec.js.map +1 -1
  141. package/dist/statements.d.ts +16 -3
  142. package/dist/statements.d.ts.map +1 -1
  143. package/dist/statements.js +54 -28
  144. package/dist/statements.js.map +1 -1
  145. package/dist/uuid.d.ts +23 -0
  146. package/dist/uuid.d.ts.map +1 -0
  147. package/dist/uuid.js +54 -0
  148. package/dist/uuid.js.map +1 -0
  149. package/pack-provenance.json +7 -0
  150. package/package.json +12 -16
  151. package/src/capacity.ts +34 -54
  152. package/src/changes.ts +361 -0
  153. package/src/close.ts +53 -0
  154. package/src/closed.ts +25 -19
  155. package/src/codec.ts +379 -0
  156. package/src/compile.ts +149 -0
  157. package/src/db-native.ts +247 -0
  158. package/src/db.ts +461 -1632
  159. package/src/errors.ts +50 -0
  160. package/src/face.ts +2 -2
  161. package/src/fields.ts +182 -55
  162. package/src/index.ts +62 -126
  163. package/src/internal/log.ts +51 -0
  164. package/src/law.ts +13 -28
  165. package/src/lower.ts +9 -4
  166. package/src/migration.ts +69 -0
  167. package/src/native.ts +326 -487
  168. package/src/query/atom.ts +61 -25
  169. package/src/query/compute.ts +237 -0
  170. package/src/query/find.ts +31 -12
  171. package/src/query/lower.ts +531 -143
  172. package/src/query/parse-ir.ts +18 -13
  173. package/src/query/run.ts +32 -16
  174. package/src/query/scope.ts +140 -8
  175. package/src/relation.ts +17 -21
  176. package/src/result.ts +130 -0
  177. package/src/rows.ts +404 -0
  178. package/src/runtime-codes.ts +18 -0
  179. package/src/runtime-errors.ts +69 -0
  180. package/src/runtime-native.ts +242 -0
  181. package/src/runtime.ts +440 -0
  182. package/src/scalar.ts +560 -0
  183. package/src/schema.ts +35 -37
  184. package/src/shape.ts +31 -0
  185. package/src/spec.ts +33 -3
  186. package/src/statements.ts +75 -68
  187. package/src/uuid.ts +69 -0
  188. package/dist/marshal.d.ts +0 -44
  189. package/dist/marshal.d.ts.map +0 -1
  190. package/dist/marshal.js +0 -165
  191. package/dist/marshal.js.map +0 -1
  192. package/src/marshal.ts +0 -200
package/dist/marshal.js DELETED
@@ -1,165 +0,0 @@
1
- /**
2
- * The marshal layer: fact object ⇄ positional `FactValue[]` by field
3
- * ordinal, schema-directed, in ONE place only. The write side lowers named
4
- * host objects to rows in the relation's field-declaration order
5
- * (declaration order = ordinal ids, the macro's law); the read side decodes
6
- * rows back to named objects of BARE structural values — the marshal
7
- * boundary is pure both ways. CAST-FREE, LITERALLY: with structural values
8
- * there is no brand to assert on the way out (the historical "one
9
- * sanctioned marshal cast" died with the brand era), so product code
10
- * carries zero casts — the only trusted seams are the completeness
11
- * PREDICATES below, which verify the checkable half (every declared field
12
- * present) and rely on the store as the proof carrier for the rest: a row
13
- * the engine admitted IS a legal fact of its relation (the same trust
14
- * direction as Rust's typed readback). Shape mismatches here are genuine
15
- * failures and THROW typed; they are never domain data.
16
- *
17
- * THE CLOSED BIJECTION (0.4.0): a closed-referencing cell crosses this
18
- * boundary as its handle NAME — the write side lowers name → u64 row id
19
- * (declaration order = row ids, the sealed roster's own law, ≤ 256 rows),
20
- * the read side lifts id → name, and both directions are total and static
21
- * over the roster. An unknown name is a pointed THROW at the write seam —
22
- * a deliberate UPGRADE over 0.3.0, where any bigint sailed through the
23
- * marshal to a commit-time containment violation; the wrong spelling now
24
- * dies here, before the engine ever sees the row. An out-of-roster id on
25
- * the read side (reachable only in a store whose closed-typed column was
26
- * never pinned by its containment law) is equally pointed — never a
27
- * silent fallback, never `undefined`.
28
- */
29
- import * as errors from "@superbuilders/errors";
30
- import { isIntervalValue, literalShapeError, rosterOf } from "#fields.ts";
31
- function isFreshField(field) {
32
- return "fresh" in field && field.fresh === true;
33
- }
34
- function recordOf(fact) {
35
- if (!isStringIndexed(fact)) {
36
- throw errors.new("fact object is not string-indexable");
37
- }
38
- return fact;
39
- }
40
- function isStringIndexed(value) {
41
- return typeof value === "object" || typeof value === "function";
42
- }
43
- /**
44
- * The write half of the closed bijection: one handle NAME to its u64 row
45
- * id (declaration order = row ids — the engine's own minting of the
46
- * sealed extension). An unknown name is a pointed refusal naming the
47
- * vocabulary and its roster — the 0.4.0 upgrade over any-bigint-compiles:
48
- * the wrong spelling dies at the marshal, never as a commit-time
49
- * violation. `indexOf` is the whole machine (the roster is ≤ 256 rows,
50
- * engine law — no map is warranted).
51
- */
52
- function closedCellOf(context, closed, name) {
53
- const id = closed.handles.indexOf(name);
54
- if (id === -1) {
55
- throw errors.new(`${context}: "${name}" is not a handle of ${closed.name} — the roster is ${closed.handles.join(", ")}`);
56
- }
57
- return BigInt(id);
58
- }
59
- function handleOf(context, closed, cell) {
60
- if (typeof cell !== "bigint") {
61
- throw literalShapeError(context, `a ${closed.name} handle id (bigint)`, cell);
62
- }
63
- const handle = closed.handles[Number(cell)];
64
- if (handle === undefined) {
65
- throw errors.new(`${context}: id ${cell} is outside the ${closed.name} roster (${closed.handles.join(", ")}) — the column types ${closed.name} but no law pins it — a containment statement is the missing piece`);
66
- }
67
- return handle;
68
- }
69
- function cellOf(context, field, value) {
70
- const roster = rosterOf(field);
71
- if (roster !== undefined) {
72
- if (typeof value !== "string") {
73
- throw literalShapeError(context, `a ${roster.name} handle name (string)`, value);
74
- }
75
- return closedCellOf(context, roster, value);
76
- }
77
- switch (field.kind) {
78
- case "bool": {
79
- if (typeof value !== "boolean") {
80
- throw literalShapeError(context, "boolean", value);
81
- }
82
- return value;
83
- }
84
- case "u64":
85
- case "i64": {
86
- if (typeof value !== "bigint") {
87
- throw literalShapeError(context, "bigint", value);
88
- }
89
- return value;
90
- }
91
- case "str": {
92
- if (typeof value !== "string") {
93
- throw literalShapeError(context, "string", value);
94
- }
95
- if (!value.isWellFormed()) {
96
- throw literalShapeError(context, "well-formed string", value);
97
- }
98
- return value;
99
- }
100
- case "bytes": {
101
- if (!(value instanceof Uint8Array)) {
102
- throw literalShapeError(context, "Uint8Array", value);
103
- }
104
- return value;
105
- }
106
- case "interval": {
107
- if (!isIntervalValue(value)) {
108
- throw literalShapeError(context, "interval ({ start, end } bigints)", value);
109
- }
110
- return { start: value.start, end: value.end };
111
- }
112
- }
113
- }
114
- function rowOf(relation, fact) {
115
- return relation.fields.map(function marshalCell(declared) {
116
- const value = fact[declared.name];
117
- if (value === undefined) {
118
- throw errors.new(`relation ${relation.name}: fact is missing field ${declared.name}`);
119
- }
120
- return cellOf(`relation ${relation.name} field ${declared.name}`, declared.field, value);
121
- });
122
- }
123
- function keyRowOf(relation, projection, key) {
124
- return projection.map(function marshalKeyCell(fieldName) {
125
- const declared = relation.fields.find(function byName(candidate) {
126
- return candidate.name === fieldName;
127
- });
128
- if (declared === undefined) {
129
- throw errors.new(`relation ${relation.name}: key projection cites unknown field ${fieldName}`);
130
- }
131
- const value = key[fieldName];
132
- if (value === undefined) {
133
- throw errors.new(`relation ${relation.name}: key object is missing field ${fieldName} — get reads through the primary (first-declared) key, whose projection is (${projection.join(", ")})`);
134
- }
135
- return cellOf(`relation ${relation.name} key field ${fieldName}`, declared.field, value);
136
- });
137
- }
138
- function isCompleteFact(relation, decoded) {
139
- return relation.data.fields.every(function present(declared) {
140
- return decoded[declared.name] !== undefined;
141
- });
142
- }
143
- function factOf(relation, row) {
144
- const data = relation.data;
145
- if (row.length !== data.fields.length) {
146
- throw errors.new(`relation ${data.name}: row arity ${row.length} does not match the ${data.fields.length} declared fields`);
147
- }
148
- const decoded = {};
149
- data.fields.forEach(function decodeCell(declared, ordinal) {
150
- const cell = row[ordinal];
151
- if (cell === undefined) {
152
- throw errors.new(`relation ${data.name}: row cell ${ordinal} (${declared.name}) is absent`);
153
- }
154
- const roster = rosterOf(declared.field);
155
- decoded[declared.name] =
156
- roster !== undefined ? handleOf(`relation ${data.name} field ${declared.name}`, roster, cell) : cell;
157
- });
158
- Object.freeze(decoded);
159
- if (!isCompleteFact(relation, decoded)) {
160
- throw errors.new(`relation ${data.name}: decoded row is not a complete fact`);
161
- }
162
- return decoded;
163
- }
164
- export { cellOf, factOf, handleOf, isFreshField, keyRowOf, recordOf, rowOf };
165
- //# sourceMappingURL=marshal.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"marshal.js","sourceRoot":"","sources":["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,QAAQ,EAAE,KAAK,EAAE,CAAA"}
package/src/marshal.ts DELETED
@@ -1,200 +0,0 @@
1
- /**
2
- * The marshal layer: fact object ⇄ positional `FactValue[]` by field
3
- * ordinal, schema-directed, in ONE place only. The write side lowers named
4
- * host objects to rows in the relation's field-declaration order
5
- * (declaration order = ordinal ids, the macro's law); the read side decodes
6
- * rows back to named objects of BARE structural values — the marshal
7
- * boundary is pure both ways. CAST-FREE, LITERALLY: with structural values
8
- * there is no brand to assert on the way out (the historical "one
9
- * sanctioned marshal cast" died with the brand era), so product code
10
- * carries zero casts — the only trusted seams are the completeness
11
- * PREDICATES below, which verify the checkable half (every declared field
12
- * present) and rely on the store as the proof carrier for the rest: a row
13
- * the engine admitted IS a legal fact of its relation (the same trust
14
- * direction as Rust's typed readback). Shape mismatches here are genuine
15
- * failures and THROW typed; they are never domain data.
16
- *
17
- * THE CLOSED BIJECTION (0.4.0): a closed-referencing cell crosses this
18
- * boundary as its handle NAME — the write side lowers name → u64 row id
19
- * (declaration order = row ids, the sealed roster's own law, ≤ 256 rows),
20
- * the read side lifts id → name, and both directions are total and static
21
- * over the roster. An unknown name is a pointed THROW at the write seam —
22
- * a deliberate UPGRADE over 0.3.0, where any bigint sailed through the
23
- * marshal to a commit-time containment violation; the wrong spelling now
24
- * dies here, before the engine ever sees the row. An out-of-roster id on
25
- * the read side (reachable only in a store whose closed-typed column was
26
- * never pinned by its containment law) is equally pointed — never a
27
- * silent fallback, never `undefined`.
28
- */
29
-
30
- import * as errors from "@superbuilders/errors"
31
- import type { AnyClosedRoster, AnyField } from "#fields.ts"
32
- import { isIntervalValue, literalShapeError, rosterOf } from "#fields.ts"
33
- import type { FactValue } from "#native.ts"
34
- import type { AnyRelation, Fact, FreshKeys, RelationData } from "#relation.ts"
35
-
36
- function isFreshField(field: AnyField): boolean {
37
- return "fresh" in field && field.fresh === true
38
- }
39
-
40
- type KeyFact<R extends AnyRelation> = [FreshKeys<R>] extends [never]
41
- ? Partial<Fact<R>>
42
- : { [K in FreshKeys<R>]: Fact<R>[K] }
43
-
44
- function recordOf(fact: object): Readonly<Record<string, unknown>> {
45
- if (!isStringIndexed(fact)) {
46
- throw errors.new("fact object is not string-indexable")
47
- }
48
- return fact
49
- }
50
-
51
- function isStringIndexed(value: object): value is Readonly<Record<string, unknown>> {
52
- return typeof value === "object" || typeof value === "function"
53
- }
54
-
55
- /**
56
- * The write half of the closed bijection: one handle NAME to its u64 row
57
- * id (declaration order = row ids — the engine's own minting of the
58
- * sealed extension). An unknown name is a pointed refusal naming the
59
- * vocabulary and its roster — the 0.4.0 upgrade over any-bigint-compiles:
60
- * the wrong spelling dies at the marshal, never as a commit-time
61
- * violation. `indexOf` is the whole machine (the roster is ≤ 256 rows,
62
- * engine law — no map is warranted).
63
- */
64
- function closedCellOf(context: string, closed: AnyClosedRoster, name: string): FactValue {
65
- const id = closed.handles.indexOf(name)
66
- if (id === -1) {
67
- throw errors.new(
68
- `${context}: "${name}" is not a handle of ${closed.name} — the roster is ${closed.handles.join(", ")}`
69
- )
70
- }
71
- return BigInt(id)
72
- }
73
-
74
- function handleOf(context: string, closed: AnyClosedRoster, cell: FactValue): string {
75
- if (typeof cell !== "bigint") {
76
- throw literalShapeError(context, `a ${closed.name} handle id (bigint)`, cell)
77
- }
78
- const handle = closed.handles[Number(cell)]
79
- if (handle === undefined) {
80
- throw errors.new(
81
- `${context}: id ${cell} is outside the ${closed.name} roster (${closed.handles.join(", ")}) — the column types ${closed.name} but no law pins it — a containment statement is the missing piece`
82
- )
83
- }
84
- return handle
85
- }
86
-
87
- function cellOf(context: string, field: AnyField, value: unknown): FactValue {
88
- const roster = rosterOf(field)
89
- if (roster !== undefined) {
90
- if (typeof value !== "string") {
91
- throw literalShapeError(context, `a ${roster.name} handle name (string)`, value)
92
- }
93
- return closedCellOf(context, roster, value)
94
- }
95
- switch (field.kind) {
96
- case "bool": {
97
- if (typeof value !== "boolean") {
98
- throw literalShapeError(context, "boolean", value)
99
- }
100
- return value
101
- }
102
- case "u64":
103
- case "i64": {
104
- if (typeof value !== "bigint") {
105
- throw literalShapeError(context, "bigint", value)
106
- }
107
- return value
108
- }
109
- case "str": {
110
- if (typeof value !== "string") {
111
- throw literalShapeError(context, "string", value)
112
- }
113
-
114
- if (!value.isWellFormed()) {
115
- throw literalShapeError(context, "well-formed string", value)
116
- }
117
- return value
118
- }
119
- case "bytes": {
120
- if (!(value instanceof Uint8Array)) {
121
- throw literalShapeError(context, "Uint8Array", value)
122
- }
123
- return value
124
- }
125
- case "interval": {
126
- if (!isIntervalValue(value)) {
127
- throw literalShapeError(context, "interval ({ start, end } bigints)", value)
128
- }
129
- return { start: value.start, end: value.end }
130
- }
131
- }
132
- }
133
-
134
- function rowOf(relation: RelationData, fact: Readonly<Record<string, unknown>>): FactValue[] {
135
- return relation.fields.map(function marshalCell(declared) {
136
- const value = fact[declared.name]
137
- if (value === undefined) {
138
- throw errors.new(`relation ${relation.name}: fact is missing field ${declared.name}`)
139
- }
140
- return cellOf(`relation ${relation.name} field ${declared.name}`, declared.field, value)
141
- })
142
- }
143
-
144
- function keyRowOf(
145
- relation: RelationData,
146
- projection: readonly string[],
147
- key: Readonly<Record<string, unknown>>
148
- ): FactValue[] {
149
- return projection.map(function marshalKeyCell(fieldName) {
150
- const declared = relation.fields.find(function byName(candidate) {
151
- return candidate.name === fieldName
152
- })
153
- if (declared === undefined) {
154
- throw errors.new(`relation ${relation.name}: key projection cites unknown field ${fieldName}`)
155
- }
156
- const value = key[fieldName]
157
- if (value === undefined) {
158
- throw errors.new(
159
- `relation ${relation.name}: key object is missing field ${fieldName} — get reads through the primary (first-declared) key, whose projection is (${projection.join(", ")})`
160
- )
161
- }
162
- return cellOf(`relation ${relation.name} key field ${fieldName}`, declared.field, value)
163
- })
164
- }
165
-
166
- function isCompleteFact<R extends AnyRelation>(
167
- relation: R,
168
- decoded: Readonly<Record<string, FactValue>>
169
- ): decoded is Readonly<Record<string, FactValue>> & Fact<R> {
170
- return relation.data.fields.every(function present(declared) {
171
- return decoded[declared.name] !== undefined
172
- })
173
- }
174
-
175
- function factOf<R extends AnyRelation>(relation: R, row: readonly FactValue[]): Fact<R> {
176
- const data = relation.data
177
- if (row.length !== data.fields.length) {
178
- throw errors.new(
179
- `relation ${data.name}: row arity ${row.length} does not match the ${data.fields.length} declared fields`
180
- )
181
- }
182
- const decoded: Record<string, FactValue> = {}
183
- data.fields.forEach(function decodeCell(declared, ordinal) {
184
- const cell = row[ordinal]
185
- if (cell === undefined) {
186
- throw errors.new(`relation ${data.name}: row cell ${ordinal} (${declared.name}) is absent`)
187
- }
188
- const roster = rosterOf(declared.field)
189
- decoded[declared.name] =
190
- roster !== undefined ? handleOf(`relation ${data.name} field ${declared.name}`, roster, cell) : cell
191
- })
192
- Object.freeze(decoded)
193
- if (!isCompleteFact(relation, decoded)) {
194
- throw errors.new(`relation ${data.name}: decoded row is not a complete fact`)
195
- }
196
- return decoded
197
- }
198
-
199
- export type { KeyFact }
200
- export { cellOf, factOf, handleOf, isFreshField, keyRowOf, recordOf, rowOf }