@bjornpagen/bumbledb 0.1.0 → 0.3.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 (112) hide show
  1. package/COOKBOOK.md +1450 -0
  2. package/README.md +69 -30
  3. package/dist/closed.d.ts +157 -38
  4. package/dist/closed.d.ts.map +1 -1
  5. package/dist/closed.js +232 -47
  6. package/dist/closed.js.map +1 -1
  7. package/dist/count.d.ts +64 -17
  8. package/dist/count.d.ts.map +1 -1
  9. package/dist/count.js +37 -14
  10. package/dist/count.js.map +1 -1
  11. package/dist/db.d.ts +27 -8
  12. package/dist/db.d.ts.map +1 -1
  13. package/dist/db.js +33 -10
  14. package/dist/db.js.map +1 -1
  15. package/dist/exhume.d.ts +12 -11
  16. package/dist/exhume.d.ts.map +1 -1
  17. package/dist/exhume.js +7 -6
  18. package/dist/exhume.js.map +1 -1
  19. package/dist/face.d.ts +135 -30
  20. package/dist/face.d.ts.map +1 -1
  21. package/dist/face.js +44 -19
  22. package/dist/face.js.map +1 -1
  23. package/dist/fields.d.ts +130 -130
  24. package/dist/fields.d.ts.map +1 -1
  25. package/dist/fields.js +60 -94
  26. package/dist/fields.js.map +1 -1
  27. package/dist/index.d.ts +29 -24
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +19 -14
  30. package/dist/index.js.map +1 -1
  31. package/dist/law.d.ts +224 -0
  32. package/dist/law.d.ts.map +1 -0
  33. package/dist/law.js +224 -0
  34. package/dist/law.js.map +1 -0
  35. package/dist/lower.d.ts +17 -7
  36. package/dist/lower.d.ts.map +1 -1
  37. package/dist/lower.js +66 -24
  38. package/dist/lower.js.map +1 -1
  39. package/dist/marshal.d.ts +36 -21
  40. package/dist/marshal.d.ts.map +1 -1
  41. package/dist/marshal.js +41 -25
  42. package/dist/marshal.js.map +1 -1
  43. package/dist/native.d.ts +6 -2
  44. package/dist/native.d.ts.map +1 -1
  45. package/dist/native.js +2 -2
  46. package/dist/native.js.map +1 -1
  47. package/dist/query/atom.d.ts +370 -174
  48. package/dist/query/atom.d.ts.map +1 -1
  49. package/dist/query/atom.js +75 -172
  50. package/dist/query/atom.js.map +1 -1
  51. package/dist/query/lower.d.ts +312 -109
  52. package/dist/query/lower.d.ts.map +1 -1
  53. package/dist/query/lower.js +906 -387
  54. package/dist/query/lower.js.map +1 -1
  55. package/dist/query/predicate.d.ts +70 -88
  56. package/dist/query/predicate.d.ts.map +1 -1
  57. package/dist/query/predicate.js +118 -72
  58. package/dist/query/predicate.js.map +1 -1
  59. package/dist/query/run.d.ts +19 -16
  60. package/dist/query/run.d.ts.map +1 -1
  61. package/dist/query/run.js +31 -24
  62. package/dist/query/run.js.map +1 -1
  63. package/dist/query/scope.d.ts +178 -127
  64. package/dist/query/scope.d.ts.map +1 -1
  65. package/dist/query/scope.js +111 -108
  66. package/dist/query/scope.js.map +1 -1
  67. package/dist/query/select.d.ts +102 -80
  68. package/dist/query/select.d.ts.map +1 -1
  69. package/dist/query/select.js +39 -34
  70. package/dist/query/select.js.map +1 -1
  71. package/dist/relation.d.ts +48 -38
  72. package/dist/relation.d.ts.map +1 -1
  73. package/dist/relation.js +24 -19
  74. package/dist/relation.js.map +1 -1
  75. package/dist/schema.d.ts +41 -3
  76. package/dist/schema.d.ts.map +1 -1
  77. package/dist/schema.js +34 -9
  78. package/dist/schema.js.map +1 -1
  79. package/dist/spec.d.ts +7 -5
  80. package/dist/spec.d.ts.map +1 -1
  81. package/dist/spec.js.map +1 -1
  82. package/dist/statements.d.ts +91 -36
  83. package/dist/statements.d.ts.map +1 -1
  84. package/dist/statements.js +42 -12
  85. package/dist/statements.js.map +1 -1
  86. package/package.json +4 -3
  87. package/src/closed.ts +448 -95
  88. package/src/count.ts +112 -18
  89. package/src/db.ts +45 -17
  90. package/src/exhume.ts +12 -11
  91. package/src/face.ts +211 -41
  92. package/src/fields.ts +199 -234
  93. package/src/index.ts +78 -61
  94. package/src/law.ts +519 -0
  95. package/src/lower.ts +82 -27
  96. package/src/marshal.ts +48 -30
  97. package/src/native.ts +8 -4
  98. package/src/query/atom.ts +525 -374
  99. package/src/query/lower.ts +1469 -538
  100. package/src/query/predicate.ts +208 -161
  101. package/src/query/run.ts +35 -25
  102. package/src/query/scope.ts +264 -218
  103. package/src/query/select.ts +168 -93
  104. package/src/relation.ts +47 -47
  105. package/src/schema.ts +69 -18
  106. package/src/spec.ts +7 -5
  107. package/src/statements.ts +124 -41
  108. package/dist/brand.d.ts +0 -59
  109. package/dist/brand.d.ts.map +0 -1
  110. package/dist/brand.js +0 -47
  111. package/dist/brand.js.map +0 -1
  112. package/src/brand.ts +0 -82
package/src/face.ts CHANGED
@@ -1,24 +1,48 @@
1
1
  /**
2
2
  * Faces — the projection-with-selection value both containments and
3
- * windows consume: `on(Account, "holder")`, `on(Account.where({ kind:
4
- * Kind.Savings }), "id")`, `on(Kind, "id")` (a closed relation's sealed
5
- * shape opens with its synthetic `id`). Projection is positional: tuple
6
- * order is preserved in the type, and the statement constructors pair the
7
- * two sides' tuples by arity (`SameArity`).
3
+ * windows consume: `on(Account, "holder")` the common single-field
4
+ * position, `on(Booking, ["room", "during"])` the composite/pointwise
5
+ * position (one spelling, arity-generic), `on(Account.where({ kind:
6
+ * Kind.Savings }), "id")` the σ-carrying source, `on(Kind, "id")` a closed
7
+ * relation's sealed shape opened through its synthetic `id`,
8
+ * `on(Kind.where({ mastered: true }), "id")` the ψ-selected closed source
9
+ * (the selection lowered as-is — the ENGINE folds it against the sealed
10
+ * extension at validate, never the SDK). Projection is
11
+ * positional: tuple order is preserved in the type, and the statement
12
+ * constructors pair the two sides' tuples by arity ({@link SameArity}) AND
13
+ * by structural shape ({@link SameShapes}) — every projected field's
14
+ * kind/width/element triple is read off the schema type (the minimal
15
+ * kernel: descriptors are pure structure) and compared positionwise. There
16
+ * is no domain to compare at construction — domains are LAW-BORN: the
17
+ * statements themselves define the equivalence classes, and `schema()` is
18
+ * where they aggregate and get judged (the one-generator-per-class wall).
8
19
  */
9
20
 
10
- import type { AnyClosed } from "#closed.ts"
11
- import type { AnyRelation, AnySelected, SelectionBinding } from "#relation.ts"
21
+ import * as errors from "@superbuilders/errors"
22
+ import type { AnyClosed, AnySelectedClosed, PayloadField } from "#closed.ts"
23
+ import type { AnyField } from "#fields.ts"
24
+ import type { AnyRelation, AnySelected, FieldsShape, RelationFields, SelectionBinding } from "#relation.ts"
12
25
  import { renderLiteralSet } from "#spec.ts"
13
26
 
14
27
  /** The empty σ of a selection-free face, shared by every bare projection. */
15
28
  const emptySelection: readonly SelectionBinding[] = Object.freeze([])
16
29
 
17
30
  /**
18
- * Splits a face source into its owner and σ: a selected relation carries
19
- * its own bindings (`relation` is the discriminant the property exists on
20
- * no relation or closed value, and `closed()` reserves the name against
21
- * handle collisions); a bare relation or closed relation carries none.
31
+ * The OWNER a face source resolves to: a selected relation — ordinary σ or
32
+ * closed ψ, one shape projects from its underlying relation; a bare
33
+ * relation or closed relation is its own owner. The type-level twin of
34
+ * {@link faceParts}'s split, and what a statement's face `data` carries at
35
+ * its EXACT type — `schema()`'s law-typing reads the owner's literal name
36
+ * (and the projection tuple) straight off the statement type.
37
+ */
38
+ type OwnerOf<S extends FaceSource> = S extends AnySelected | AnySelectedClosed ? S["relation"] : S
39
+
40
+ /**
41
+ * Splits a face source into its owner and σ: a selected relation — ordinary
42
+ * σ or closed ψ, one shape — carries its own bindings (`relation` is the
43
+ * discriminant — the property exists on no relation or closed value, and
44
+ * `closed()` reserves the name against handle collisions); a bare relation
45
+ * or closed relation carries none.
22
46
  */
23
47
  function faceParts(source: FaceSource): {
24
48
  readonly owner: FaceOwner
@@ -54,42 +78,111 @@ function oneOf<V>(first: V, second: V, ...rest: V[]): OneOf<V> {
54
78
  /** The relation a face projects from — ordinary or closed. */
55
79
  type FaceOwner = AnyRelation | AnyClosed
56
80
 
57
- /** A face's runtime description: owner, π (written order), σ (resolved bindings). */
58
- interface FaceData {
59
- readonly owner: FaceOwner
60
- readonly projection: readonly string[]
81
+ /**
82
+ * A face's runtime description: owner, π (written order), σ (resolved
83
+ * bindings). Generic over the owner and projection so a STATEMENT value
84
+ * carries its paired coordinates at their exact types — the honest runtime
85
+ * properties (`owner.name`, `projection`) ARE the type-level carrier
86
+ * `schema()`'s law-typing reads; the defaults are the wide shape every
87
+ * renderer and lowering walk consumes.
88
+ */
89
+ interface FaceData<O extends FaceOwner = FaceOwner, P extends readonly string[] = readonly string[]> {
90
+ readonly owner: O
91
+ readonly projection: P
61
92
  readonly selection: readonly SelectionBinding[]
62
93
  }
63
94
 
64
95
  /**
65
- * A face value. `P` is the projection tuple as written the statement
66
- * constructors read its length for positional-arity pairing.
96
+ * A face value. `S` is the source exactly as written (the relation,
97
+ * selected relation, or closed relation `on()` was handed — the statement
98
+ * constructors resolve each projected field's DOMAIN through it), and `P`
99
+ * is the projection tuple as written (its length is the positional-pairing
100
+ * arity). Both are honest runtime properties, not phantoms — and `data`
101
+ * carries the resolved owner at its exact type, which is what a statement
102
+ * value hands to `schema()`'s law-typing.
67
103
  */
68
- interface Face<P extends readonly string[]> {
104
+ interface Face<S extends FaceSource, P extends readonly string[]> {
105
+ readonly source: S
69
106
  readonly projection: P
70
- readonly data: FaceData
107
+ readonly data: FaceData<OwnerOf<S>, P>
71
108
  }
72
109
 
73
- /** Any face value, whatever its projection. */
74
- type AnyFace = Face<readonly string[]>
110
+ /** Any face value, whatever its source and projection. */
111
+ type AnyFace = Face<FaceSource, readonly string[]>
75
112
 
76
- /** What `on()` accepts: a relation, a selected relation, or a closed relation. */
77
- type FaceSource = AnyRelation | AnyClosed | AnySelected
113
+ /** What `on()` accepts: a relation, a closed relation, or either with a selection applied. */
114
+ type FaceSource = AnyRelation | AnyClosed | AnySelected | AnySelectedClosed
78
115
 
79
116
  /**
80
117
  * The field names a face over `S` may project: a relation's declared
81
118
  * fields; a selected relation's underlying fields; a closed relation's
82
- * SEALED shape — the synthetic `id` plus its declared payload columns
83
- * (`docs/architecture/70-api.md`: statement field names address the sealed
84
- * shape).
119
+ * SEALED shape — the synthetic `id` plus its declared payload columns,
120
+ * bare or ψ-selected alike (`docs/architecture/70-api.md`: statement field
121
+ * names address the sealed shape).
85
122
  */
86
123
  type FaceFields<S extends FaceSource> = S extends AnySelected
87
124
  ? keyof S["relation"]["fields"] & string
88
- : S extends AnyRelation
89
- ? keyof S["fields"] & string
90
- : S extends { readonly axioms: Readonly<Record<string, infer Row>> }
91
- ? "id" | (keyof Row & string)
92
- : never
125
+ : S extends AnySelectedClosed
126
+ ? "id" | (keyof S["relation"]["columns"] & string)
127
+ : S extends AnyRelation
128
+ ? keyof S["fields"] & string
129
+ : S extends { readonly axioms: Readonly<Record<string, infer Row>> }
130
+ ? "id" | (keyof Row & string)
131
+ : never
132
+
133
+ /**
134
+ * One descriptor's structural comparand: the kind/width/element triple —
135
+ * exactly the structure the minimal kernel carries, compared exactly as the
136
+ * engine's Q1 law pairs positions (`schema/validate.rs`): a `bytes` width is
137
+ * bound (bytes<16> vs bytes<32> mismatch), while an INTERVAL width is FREE —
138
+ * the pointwise judgments quantify over points, which carry an element
139
+ * domain and not a width, so `interval(u64)` pairs with `interval(u64, 1n)`
140
+ * (recipe 9's extent/slot mirrors, recipe 29's mixed-width zones) and the
141
+ * width slot reads `undefined` for every interval. Elements stay bound:
142
+ * u64-vs-i64 interval pairs still mismatch.
143
+ */
144
+ type ShapeOf<F extends AnyField> = readonly [
145
+ F["kind"],
146
+ F extends { readonly element: unknown } ? undefined : F extends { readonly width: infer W } ? W : undefined,
147
+ F extends { readonly element: infer E } ? E : undefined
148
+ ]
149
+
150
+ /** One field's structural shape within a declared field block (`undefined` when the name is foreign). */
151
+ type ShapeIn<Fields extends FieldsShape, K extends string> = K extends keyof Fields ? ShapeOf<Fields[K]> : undefined
152
+
153
+ /**
154
+ * The structural SHAPE of one projected field, read off the source's
155
+ * schema type — an ordinary or selected relation's field contributes its
156
+ * descriptor's triple; a closed relation (bare or ψ-selected) contributes
157
+ * its synthetic `id` as a u64 and its payload columns' declared
158
+ * descriptors' triples through the closed value's typed `columns` carrier
159
+ * (whose runtime twin is the frozen `columns` record the mint carries).
160
+ * The engine stays the final authority at `Db.create`/`Db.open`.
161
+ */
162
+ type ProjectedShape<S extends FaceSource, K extends string> = S extends AnySelected
163
+ ? ShapeIn<RelationFields<S["relation"]>, K>
164
+ : S extends AnySelectedClosed
165
+ ? K extends "id"
166
+ ? ShapeOf<S["relation"]["id"]>
167
+ : ShapeIn<S["relation"]["columns"], K>
168
+ : S extends AnyRelation
169
+ ? ShapeIn<RelationFields<S>, K>
170
+ : S extends {
171
+ readonly id: infer Id extends AnyField
172
+ readonly columns: infer Cols extends Record<string, PayloadField>
173
+ }
174
+ ? K extends "id"
175
+ ? ShapeOf<Id>
176
+ : ShapeIn<Cols, K>
177
+ : undefined
178
+
179
+ /** The positionwise structural-shape tuple of a projection over `S`. */
180
+ type ShapesOf<S extends FaceSource, P extends readonly string[]> = {
181
+ readonly [I in keyof P]: ProjectedShape<S, P[I] & string>
182
+ }
183
+
184
+ /** The shape tuple a face projects, positionwise — the comparand of {@link SameShapes}. */
185
+ type FaceShapes<F extends AnyFace> = F extends Face<infer S, infer P> ? ShapesOf<S, P> : never
93
186
 
94
187
  /** The projection arity of a face. */
95
188
  type Arity<F extends AnyFace> = F["projection"]["length"]
@@ -119,24 +212,86 @@ type SameArity<A extends AnyFace, B extends AnyFace> =
119
212
  : FaceArityMismatch<Arity<A>, Arity<B>>
120
213
 
121
214
  /**
122
- * Projects a face: `on(Account, "holder")`, `on(Account.where({...}),
123
- * "id")`. Field names are typechecked against the source; tuple order is
124
- * preserved (positional pairing with the other side, macro parity). At
125
- * least one field by signature an empty projection has no meaning in the
126
- * statement grammar.
215
+ * The legible shape-mismatch verdict: when the two faces of a containment,
216
+ * bijection, or window project structurally incompatible fields at any
217
+ * position, this type is intersected into the second face's parameter and
218
+ * names both shape tuplesa u64 face against a str face, a bytes width
219
+ * mismatch, or an interval element mismatch is a COMPILE error.
220
+ */
221
+ interface FaceShapeMismatch<Left, Right> {
222
+ readonly "face shape mismatch — positionwise kind, width, and element must be equal on both sides": readonly [
223
+ Left,
224
+ Right
225
+ ]
226
+ }
227
+
228
+ /**
229
+ * Resolves to `unknown` (a no-op intersection) when the two faces project
230
+ * positionwise-equal structural shapes, and to {@link FaceShapeMismatch}
231
+ * otherwise. Equality is mutual tuple assignability over the
232
+ * kind/width/element triples. This is the whole construction-time wall —
233
+ * deliberately: there is no domain to compare here. The domain wall lives
234
+ * where domains are BORN: `schema()` computes every field's class from the
235
+ * statement list and holds the one-generator-per-class law, and query
236
+ * joins compare class names off the schema type.
237
+ */
238
+ type SameShapes<A extends AnyFace, B extends AnyFace> =
239
+ FaceShapes<A> extends FaceShapes<B>
240
+ ? FaceShapes<B> extends FaceShapes<A>
241
+ ? unknown
242
+ : FaceShapeMismatch<FaceShapes<A>, FaceShapes<B>>
243
+ : FaceShapeMismatch<FaceShapes<A>, FaceShapes<B>>
244
+
245
+ /**
246
+ * Projects a face — one spelling, arity-generic: `on(Account, "holder")`
247
+ * for the common single-field position, `on(Booking, ["room", "during"])`
248
+ * for the composite/pointwise position (the interval-pointwise `==` and
249
+ * coverage recipes), `on(Account.where({...}), "id")` for a σ-carrying
250
+ * source. Field names are typechecked against the source (unknown field =
251
+ * type error, names autocomplete); tuple order is preserved (positional
252
+ * pairing with the other side, macro parity). The empty projection is
253
+ * unwritable by signature — it has no meaning in the statement grammar.
127
254
  */
255
+ function on<S extends FaceSource, const F extends FaceFields<S>>(source: S, field: F): Face<S, readonly [F]>
128
256
  function on<S extends FaceSource, const P extends readonly [FaceFields<S>, ...FaceFields<S>[]]>(
129
257
  source: S,
130
- ...projection: P
131
- ): Face<P> {
258
+ fields: P
259
+ ): Face<S, P>
260
+ function on<S extends FaceSource>(source: S, fields: string | readonly string[]): Face<S, readonly string[]> {
261
+ const projection: readonly string[] = Object.freeze(typeof fields === "string" ? [fields] : [...fields])
132
262
  const parts = faceParts(source)
133
- Object.freeze(projection)
134
263
  const data: FaceData = Object.freeze({
135
264
  owner: parts.owner,
136
265
  projection,
137
266
  selection: parts.selection
138
267
  })
139
- return Object.freeze({ projection, data })
268
+ const value = Object.freeze({ source, projection, data })
269
+ if (!faceMinted<S, readonly string[]>(value, source, projection)) {
270
+ throw errors.new(`face over ${parts.owner.name}: face construction incomplete`)
271
+ }
272
+ return value
273
+ }
274
+
275
+ /**
276
+ * The trusted seam of the face mint (the `refsComplete` pattern): the
277
+ * checkable facts — the value carries exactly the source and projection it
278
+ * was built from, and `data.owner` is exactly the owner {@link faceParts}
279
+ * resolves for that source — are verified before the wide construction is
280
+ * admitted at the exact {@link Face} type (whose `data` claims the owner at
281
+ * its precise type, the carrier the schema-level law-typing reads).
282
+ */
283
+ function faceMinted<S extends FaceSource, P extends readonly string[]>(
284
+ value: { readonly source: FaceSource; readonly projection: readonly string[]; readonly data: FaceData },
285
+ source: S,
286
+ projection: P
287
+ ): value is Face<S, P> {
288
+ const owner = "relation" in source ? source.relation : source
289
+ return (
290
+ value.source === source &&
291
+ value.projection === projection &&
292
+ value.data.owner === owner &&
293
+ value.data.projection === projection
294
+ )
140
295
  }
141
296
 
142
297
  /**
@@ -157,5 +312,20 @@ function renderFace(face: FaceData): string {
157
312
  return `${face.owner.name}(${projection} | ${bindings})`
158
313
  }
159
314
 
160
- export type { AnyFace, Arity, Face, FaceArityMismatch, FaceData, FaceFields, FaceOwner, FaceSource, OneOf, SameArity }
315
+ export type {
316
+ AnyFace,
317
+ Arity,
318
+ Face,
319
+ FaceArityMismatch,
320
+ FaceData,
321
+ FaceFields,
322
+ FaceOwner,
323
+ FaceShapeMismatch,
324
+ FaceShapes,
325
+ FaceSource,
326
+ OneOf,
327
+ OwnerOf,
328
+ SameArity,
329
+ SameShapes
330
+ }
161
331
  export { on, oneOf, renderFace }