@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.
- package/COOKBOOK.md +1450 -0
- package/README.md +69 -30
- package/dist/closed.d.ts +157 -38
- package/dist/closed.d.ts.map +1 -1
- package/dist/closed.js +232 -47
- package/dist/closed.js.map +1 -1
- package/dist/count.d.ts +64 -17
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +37 -14
- package/dist/count.js.map +1 -1
- package/dist/db.d.ts +27 -8
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +33 -10
- package/dist/db.js.map +1 -1
- package/dist/exhume.d.ts +12 -11
- package/dist/exhume.d.ts.map +1 -1
- package/dist/exhume.js +7 -6
- package/dist/exhume.js.map +1 -1
- package/dist/face.d.ts +135 -30
- package/dist/face.d.ts.map +1 -1
- package/dist/face.js +44 -19
- package/dist/face.js.map +1 -1
- package/dist/fields.d.ts +130 -130
- package/dist/fields.d.ts.map +1 -1
- package/dist/fields.js +60 -94
- package/dist/fields.js.map +1 -1
- package/dist/index.d.ts +29 -24
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -14
- package/dist/index.js.map +1 -1
- package/dist/law.d.ts +224 -0
- package/dist/law.d.ts.map +1 -0
- package/dist/law.js +224 -0
- package/dist/law.js.map +1 -0
- package/dist/lower.d.ts +17 -7
- package/dist/lower.d.ts.map +1 -1
- package/dist/lower.js +66 -24
- package/dist/lower.js.map +1 -1
- package/dist/marshal.d.ts +36 -21
- package/dist/marshal.d.ts.map +1 -1
- package/dist/marshal.js +41 -25
- package/dist/marshal.js.map +1 -1
- package/dist/native.d.ts +6 -2
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +2 -2
- package/dist/native.js.map +1 -1
- package/dist/query/atom.d.ts +370 -174
- package/dist/query/atom.d.ts.map +1 -1
- package/dist/query/atom.js +75 -172
- package/dist/query/atom.js.map +1 -1
- package/dist/query/lower.d.ts +312 -109
- package/dist/query/lower.d.ts.map +1 -1
- package/dist/query/lower.js +906 -387
- package/dist/query/lower.js.map +1 -1
- package/dist/query/predicate.d.ts +70 -88
- package/dist/query/predicate.d.ts.map +1 -1
- package/dist/query/predicate.js +118 -72
- package/dist/query/predicate.js.map +1 -1
- package/dist/query/run.d.ts +19 -16
- package/dist/query/run.d.ts.map +1 -1
- package/dist/query/run.js +31 -24
- package/dist/query/run.js.map +1 -1
- package/dist/query/scope.d.ts +178 -127
- package/dist/query/scope.d.ts.map +1 -1
- package/dist/query/scope.js +111 -108
- package/dist/query/scope.js.map +1 -1
- package/dist/query/select.d.ts +102 -80
- package/dist/query/select.d.ts.map +1 -1
- package/dist/query/select.js +39 -34
- package/dist/query/select.js.map +1 -1
- package/dist/relation.d.ts +48 -38
- package/dist/relation.d.ts.map +1 -1
- package/dist/relation.js +24 -19
- package/dist/relation.js.map +1 -1
- package/dist/schema.d.ts +41 -3
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +34 -9
- package/dist/schema.js.map +1 -1
- package/dist/spec.d.ts +7 -5
- package/dist/spec.d.ts.map +1 -1
- package/dist/spec.js.map +1 -1
- package/dist/statements.d.ts +91 -36
- package/dist/statements.d.ts.map +1 -1
- package/dist/statements.js +42 -12
- package/dist/statements.js.map +1 -1
- package/package.json +4 -3
- package/src/closed.ts +448 -95
- package/src/count.ts +112 -18
- package/src/db.ts +45 -17
- package/src/exhume.ts +12 -11
- package/src/face.ts +211 -41
- package/src/fields.ts +199 -234
- package/src/index.ts +78 -61
- package/src/law.ts +519 -0
- package/src/lower.ts +82 -27
- package/src/marshal.ts +48 -30
- package/src/native.ts +8 -4
- package/src/query/atom.ts +525 -374
- package/src/query/lower.ts +1469 -538
- package/src/query/predicate.ts +208 -161
- package/src/query/run.ts +35 -25
- package/src/query/scope.ts +264 -218
- package/src/query/select.ts +168 -93
- package/src/relation.ts +47 -47
- package/src/schema.ts +69 -18
- package/src/spec.ts +7 -5
- package/src/statements.ts +124 -41
- package/dist/brand.d.ts +0 -59
- package/dist/brand.d.ts.map +0 -1
- package/dist/brand.js +0 -47
- package/dist/brand.js.map +0 -1
- package/src/brand.ts +0 -82
package/src/query/select.ts
CHANGED
|
@@ -1,106 +1,87 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Select entries and aggregates
|
|
3
|
-
* the IR's aggregate roster exactly
|
|
4
|
-
* `
|
|
5
|
-
* `
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* maximal segment), the result
|
|
2
|
+
* Select entries and aggregates, STRUCTURAL edition — the head vocabulary,
|
|
3
|
+
* mirroring the IR's aggregate roster exactly
|
|
4
|
+
* (`bumbledb/crates/bumbledb/src/ir.rs` `AggOp`/`FindTerm`;
|
|
5
|
+
* `docs/architecture/20-query-ir.md` § aggregation): `count` (nullary),
|
|
6
|
+
* `countDistinct`, `sum`/`min`/`max` (over an orderable variable or the
|
|
7
|
+
* measure), `argMax`/`argMin` (arg-restriction: carried value + orderable
|
|
8
|
+
* key; a tie yields every attaining row), and `pack` (the coalescing fold —
|
|
9
|
+
* RELATION-SHAPED: one answer row per (group, maximal segment), the result
|
|
10
|
+
* position interval-typed). Aggregates name their variables — `r.sum("m")`
|
|
11
|
+
* — and are typed by the rule environment at `.select`. Grouping is
|
|
10
12
|
* implicit: the non-aggregate select entries are the group key; over empty
|
|
11
13
|
* input an all-aggregate select yields the EMPTY SET, never a zero row.
|
|
14
|
+
* The creation quarantine is representational: a head position is a var
|
|
15
|
+
* name, the measure, or an aggregate — no minting or arithmetic term
|
|
16
|
+
* exists to spell (permanent law).
|
|
12
17
|
*/
|
|
13
18
|
|
|
14
|
-
import type {
|
|
15
|
-
import {
|
|
16
|
-
import type { Duration } from "#query/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/** The three folds the measure admits (and the plain-variable folds). */
|
|
21
|
-
type FoldOp = "sum" | "min" | "max"
|
|
22
|
-
|
|
23
|
-
/** One aggregate's runtime description. */
|
|
24
|
-
type AggregateData =
|
|
25
|
-
| { readonly op: "count" }
|
|
26
|
-
| { readonly op: "countDistinct"; readonly over: AnyVar }
|
|
27
|
-
| { readonly op: "fold"; readonly fold: FoldOp; readonly over: AnyVar | Duration }
|
|
28
|
-
| {
|
|
29
|
-
readonly op: "arg"
|
|
30
|
-
readonly direction: "argMax" | "argMin"
|
|
31
|
-
readonly key: AnyVar
|
|
32
|
-
readonly over: AnyVar
|
|
33
|
-
}
|
|
34
|
-
| { readonly op: "pack"; readonly over: AnyVar }
|
|
19
|
+
import type { Infer } from "#fields.ts"
|
|
20
|
+
import type { IntervalVarOk, OrderVarOk } from "#query/atom.ts"
|
|
21
|
+
import type { Duration, EnvShape, ShapeOf } from "#query/scope.ts"
|
|
22
|
+
|
|
23
|
+
/** One aggregate operator name of the select vocabulary. */
|
|
24
|
+
type AggOpName = "count" | "countDistinct" | "sum" | "min" | "max" | "argMax" | "argMin" | "pack"
|
|
35
25
|
|
|
36
26
|
/**
|
|
37
|
-
* One aggregate select
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
27
|
+
* One aggregate select VALUE: the op, the variable name (or measure) it
|
|
28
|
+
* folds, and — for the Arg forms — the orderable key's variable name. The
|
|
29
|
+
* runtime representation carries the types; the rule environment types the
|
|
30
|
+
* result at `.select`.
|
|
41
31
|
*/
|
|
42
|
-
interface
|
|
43
|
-
|
|
44
|
-
|
|
32
|
+
interface Agg<
|
|
33
|
+
Op extends AggOpName,
|
|
34
|
+
Over extends string | Duration<string> | undefined,
|
|
35
|
+
Key extends string | undefined = undefined
|
|
36
|
+
> {
|
|
37
|
+
readonly agg: Op
|
|
38
|
+
readonly over: Over
|
|
39
|
+
readonly key: Key
|
|
45
40
|
}
|
|
46
41
|
|
|
47
|
-
/**
|
|
48
|
-
type
|
|
49
|
-
|
|
50
|
-
/** The select record: answer column name to entry, written order = column order. */
|
|
51
|
-
type SelectShape = Readonly<Record<string, SelectEntryInput>>
|
|
42
|
+
/** Any aggregate select value. */
|
|
43
|
+
type AnyAgg = Agg<AggOpName, string | Duration<string> | undefined, string | undefined>
|
|
52
44
|
|
|
53
|
-
/** One select entry
|
|
54
|
-
type
|
|
55
|
-
? Exclude<R, undefined>
|
|
56
|
-
: T extends { readonly measure: AnyVar }
|
|
57
|
-
? bigint
|
|
58
|
-
: T extends { readonly [term]: "var"; readonly [phantom]?: infer V }
|
|
59
|
-
? Exclude<V, undefined>
|
|
60
|
-
: never
|
|
45
|
+
/** One select entry: a projected var name, the measure, or an aggregate. */
|
|
46
|
+
type SelectEntry = string | Duration<string> | AnyAgg
|
|
61
47
|
|
|
62
|
-
/**
|
|
63
|
-
|
|
48
|
+
/** Builds one aggregate value. */
|
|
49
|
+
function aggregate<
|
|
50
|
+
Op extends AggOpName,
|
|
51
|
+
Over extends string | Duration<string> | undefined,
|
|
52
|
+
Key extends string | undefined
|
|
53
|
+
>(op: Op, over: Over, key: Key): Agg<Op, Over, Key> {
|
|
54
|
+
return Object.freeze({ agg: op, over, key })
|
|
55
|
+
}
|
|
64
56
|
|
|
65
|
-
/** Nullary count: |the group's set of distinct full bindings|, `bigint`. */
|
|
66
|
-
function count():
|
|
67
|
-
return
|
|
57
|
+
/** Nullary count: |the group's set of distinct full bindings|, `bigint`; the answer column is named `count`. */
|
|
58
|
+
function count(): Agg<"count", undefined> {
|
|
59
|
+
return aggregate("count", undefined, undefined)
|
|
68
60
|
}
|
|
69
61
|
|
|
70
|
-
/** |the distinct values of
|
|
71
|
-
function countDistinct<
|
|
72
|
-
return
|
|
62
|
+
/** |the distinct values of the named variable across the group|, `bigint`; legal over every type. */
|
|
63
|
+
function countDistinct<const N extends string>(over: N): Agg<"countDistinct", N> {
|
|
64
|
+
return aggregate("countDistinct", over, undefined)
|
|
73
65
|
}
|
|
74
66
|
|
|
75
67
|
/**
|
|
76
|
-
* Exact checked sum over
|
|
77
|
-
* finalize range check
|
|
78
|
-
* never a wrap
|
|
68
|
+
* Exact checked sum over an orderable (u64/i64) variable — wide
|
|
69
|
+
* accumulator, one finalize range check; overflow is the engine's typed
|
|
70
|
+
* runtime error, never a wrap — or over the measure
|
|
71
|
+
* (`r.sum(r.duration("w"))`).
|
|
79
72
|
*/
|
|
80
|
-
function sum<
|
|
81
|
-
|
|
82
|
-
function sum(over: Var<bigint> | Duration): Aggregate<bigint> {
|
|
83
|
-
return Object.freeze({
|
|
84
|
-
aggregate: Object.freeze({ op: "fold" as const, fold: "sum" as const, over })
|
|
85
|
-
})
|
|
73
|
+
function sum<const N extends string | Duration<string>>(over: N): Agg<"sum", N> {
|
|
74
|
+
return aggregate("sum", over, undefined)
|
|
86
75
|
}
|
|
87
76
|
|
|
88
|
-
/** Minimum over
|
|
89
|
-
function min<
|
|
90
|
-
|
|
91
|
-
function min(over: Var<bigint> | Duration): Aggregate<bigint> {
|
|
92
|
-
return Object.freeze({
|
|
93
|
-
aggregate: Object.freeze({ op: "fold" as const, fold: "min" as const, over })
|
|
94
|
-
})
|
|
77
|
+
/** Minimum over an orderable variable or the measure (orderable types only). */
|
|
78
|
+
function min<const N extends string | Duration<string>>(over: N): Agg<"min", N> {
|
|
79
|
+
return aggregate("min", over, undefined)
|
|
95
80
|
}
|
|
96
81
|
|
|
97
|
-
/** Maximum over
|
|
98
|
-
function max<
|
|
99
|
-
|
|
100
|
-
function max(over: Var<bigint> | Duration): Aggregate<bigint> {
|
|
101
|
-
return Object.freeze({
|
|
102
|
-
aggregate: Object.freeze({ op: "fold" as const, fold: "max" as const, over })
|
|
103
|
-
})
|
|
82
|
+
/** Maximum over an orderable variable or the measure (orderable types only). */
|
|
83
|
+
function max<const N extends string | Duration<string>>(over: N): Agg<"max", N> {
|
|
84
|
+
return aggregate("max", over, undefined)
|
|
104
85
|
}
|
|
105
86
|
|
|
106
87
|
/**
|
|
@@ -111,17 +92,13 @@ function max(over: Var<bigint> | Duration): Aggregate<bigint> {
|
|
|
111
92
|
* direction; Arg and fold aggregates never mix (both the engine's typed
|
|
112
93
|
* rules).
|
|
113
94
|
*/
|
|
114
|
-
function
|
|
115
|
-
return
|
|
116
|
-
aggregate: Object.freeze({ op: "arg" as const, direction: "argMax" as const, key, over: value })
|
|
117
|
-
})
|
|
95
|
+
function argMax<const V extends string, const K extends string>(value: V, key: K): Agg<"argMax", V, K> {
|
|
96
|
+
return aggregate("argMax", value, key)
|
|
118
97
|
}
|
|
119
98
|
|
|
120
|
-
/** Arg-restriction toward the minimum of `key`; rules as {@link
|
|
121
|
-
function
|
|
122
|
-
return
|
|
123
|
-
aggregate: Object.freeze({ op: "arg" as const, direction: "argMin" as const, key, over: value })
|
|
124
|
-
})
|
|
99
|
+
/** Arg-restriction toward the minimum of `key`; rules as {@link argMax}. */
|
|
100
|
+
function argMin<const V extends string, const K extends string>(value: V, key: K): Agg<"argMin", V, K> {
|
|
101
|
+
return aggregate("argMin", value, key)
|
|
125
102
|
}
|
|
126
103
|
|
|
127
104
|
/**
|
|
@@ -132,9 +109,107 @@ function argmin<K extends bigint, V>(key: Var<K>, value: Var<V>): Aggregate<V> {
|
|
|
132
109
|
* input's element type. At most one `pack` per select, never beside a
|
|
133
110
|
* fold or an Arg entry (the engine's typed rules).
|
|
134
111
|
*/
|
|
135
|
-
function pack<
|
|
136
|
-
return
|
|
112
|
+
function pack<const N extends string>(over: N): Agg<"pack", N> {
|
|
113
|
+
return aggregate("pack", over, undefined)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** A fold input's judgment: an orderable variable, or the measure of an interval variable. */
|
|
117
|
+
type FoldOverOk<Env extends EnvShape, O> = O extends string
|
|
118
|
+
? OrderVarOk<Env, O>
|
|
119
|
+
: O extends Duration<infer N extends string>
|
|
120
|
+
? IntervalVarOk<Env, N>
|
|
121
|
+
: false
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* One select entry's judgment against the rule environment: projected
|
|
125
|
+
* names must be bound, the measure and `pack` demand interval-typed
|
|
126
|
+
* variables, folds and Arg keys demand orderable ones.
|
|
127
|
+
*/
|
|
128
|
+
type SelectEntryOk<Env extends EnvShape, E> = E extends string
|
|
129
|
+
? E extends keyof Env
|
|
130
|
+
? true
|
|
131
|
+
: false
|
|
132
|
+
: E extends Duration<infer N extends string>
|
|
133
|
+
? IntervalVarOk<Env, N>
|
|
134
|
+
: E extends Agg<"count", undefined>
|
|
135
|
+
? true
|
|
136
|
+
: E extends Agg<"countDistinct", infer O extends string>
|
|
137
|
+
? O extends keyof Env
|
|
138
|
+
? true
|
|
139
|
+
: false
|
|
140
|
+
: E extends Agg<"sum" | "min" | "max", infer O>
|
|
141
|
+
? FoldOverOk<Env, O>
|
|
142
|
+
: E extends Agg<"argMax" | "argMin", infer O extends string, infer K extends string>
|
|
143
|
+
? [O extends keyof Env ? true : false, OrderVarOk<Env, K>] extends [true, true]
|
|
144
|
+
? true
|
|
145
|
+
: false
|
|
146
|
+
: E extends Agg<"pack", infer O extends string>
|
|
147
|
+
? IntervalVarOk<Env, O>
|
|
148
|
+
: false
|
|
149
|
+
|
|
150
|
+
/** The validated select tuple (intersect with the inferred entries — errors land on the offending argument). */
|
|
151
|
+
type CheckSelect<Env extends EnvShape, S> = {
|
|
152
|
+
readonly [I in keyof S]: SelectEntryOk<Env, S[I]> extends true ? S[I] : never
|
|
137
153
|
}
|
|
138
154
|
|
|
139
|
-
|
|
140
|
-
|
|
155
|
+
/** The validated names-only select tuple of a RECURSIVE rule (aggregates and the measure are unwritable there). */
|
|
156
|
+
type CheckNameSelect<Env extends EnvShape, S> = {
|
|
157
|
+
readonly [I in keyof S]: S[I] extends keyof Env ? S[I] : never
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* One select entry's answer-column fragment: the column is named by the
|
|
162
|
+
* variable it projects or folds (`count` names its column `count`), and
|
|
163
|
+
* its type reflects the entry — `count`/`countDistinct` are `bigint`
|
|
164
|
+
* whatever they counted, folds carry their input's type, the measure is
|
|
165
|
+
* `bigint`, the Arg forms carry the payload's type, `pack` its interval
|
|
166
|
+
* type.
|
|
167
|
+
*/
|
|
168
|
+
type SelectEntryRow<Env extends EnvShape, E> = E extends string
|
|
169
|
+
? { readonly [K in E]: Infer<Env[E & keyof Env]["field"]> }
|
|
170
|
+
: E extends Duration<infer N extends string>
|
|
171
|
+
? { readonly [K in N]: bigint }
|
|
172
|
+
: E extends Agg<"count", undefined>
|
|
173
|
+
? { readonly count: bigint }
|
|
174
|
+
: E extends Agg<"countDistinct", infer O extends string>
|
|
175
|
+
? { readonly [K in O]: bigint }
|
|
176
|
+
: E extends Agg<"sum" | "min" | "max", infer O>
|
|
177
|
+
? O extends string
|
|
178
|
+
? { readonly [K in O]: Infer<Env[O & keyof Env]["field"]> }
|
|
179
|
+
: O extends Duration<infer N extends string>
|
|
180
|
+
? { readonly [K in N]: bigint }
|
|
181
|
+
: never
|
|
182
|
+
: E extends Agg<"argMax" | "argMin", infer O extends string, string>
|
|
183
|
+
? { readonly [K in O]: Infer<Env[O & keyof Env]["field"]> }
|
|
184
|
+
: E extends Agg<"pack", infer O extends string>
|
|
185
|
+
? { readonly [K in O]: Infer<Env[O & keyof Env]["field"]> }
|
|
186
|
+
: never
|
|
187
|
+
|
|
188
|
+
/** The inferred answer-row object type of a select tuple. */
|
|
189
|
+
type RowOfSelect<Env extends EnvShape, S extends readonly SelectEntry[]> = ShapeOf<SelectEntryRow<Env, S[number]>>
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* One projected name's answer-column fragment — a NAKED parameter, so the
|
|
193
|
+
* judgment distributes per name (the union of a multi-name select never
|
|
194
|
+
* smears into one column's type), mirroring {@link SelectEntryRow}.
|
|
195
|
+
*/
|
|
196
|
+
type NameSelectRow<Env extends EnvShape, N> = N extends string
|
|
197
|
+
? { readonly [K in N]: Infer<Env[K & keyof Env]["field"]> }
|
|
198
|
+
: never
|
|
199
|
+
|
|
200
|
+
/** The inferred answer-row object type of a names-only (recursive-rule) select tuple. */
|
|
201
|
+
type RowOfNameSelect<Env extends EnvShape, S extends readonly string[]> = ShapeOf<NameSelectRow<Env, S[number]>>
|
|
202
|
+
|
|
203
|
+
export type {
|
|
204
|
+
Agg,
|
|
205
|
+
AggOpName,
|
|
206
|
+
AnyAgg,
|
|
207
|
+
CheckNameSelect,
|
|
208
|
+
CheckSelect,
|
|
209
|
+
RowOfNameSelect,
|
|
210
|
+
RowOfSelect,
|
|
211
|
+
SelectEntry,
|
|
212
|
+
SelectEntryOk,
|
|
213
|
+
SelectEntryRow
|
|
214
|
+
}
|
|
215
|
+
export { argMax, argMin, count, countDistinct, max, min, pack, sum }
|
package/src/relation.ts
CHANGED
|
@@ -1,31 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `relation()` — the ordinary-relation half of the theory's signature. A
|
|
3
3
|
* relation value is a frozen plain object carrying its name, its ordered
|
|
4
|
-
* field
|
|
5
|
-
* field references (`R.fields.holder`), and — since selections are
|
|
6
|
-
* relation's own vocabulary — `where()`, which resolves a selection
|
|
7
|
-
* lowered bindings eagerly (handles
|
|
4
|
+
* field descriptors (declaration order = ordinal ids, the macro's law),
|
|
5
|
+
* typed field references (`R.fields.holder`), and — since selections are
|
|
6
|
+
* the relation's own vocabulary — `where()`, which resolves a selection
|
|
7
|
+
* into lowered bindings eagerly (handles verified against their roster at
|
|
8
8
|
* construction). `Fact<>`/`InsertFact<>` are the inferred row object
|
|
9
|
-
* types
|
|
10
|
-
*
|
|
9
|
+
* types at BARE structural value types (no brands): fresh fields are
|
|
10
|
+
* optional on insert input (omit-to-mint) and present on read
|
|
11
|
+
* (resupply-to-preserve-identity), typed exactly.
|
|
11
12
|
*/
|
|
12
13
|
|
|
13
14
|
import * as errors from "@superbuilders/errors"
|
|
14
|
-
import { phantom } from "#brand.ts"
|
|
15
15
|
import type { OneOf } from "#face.ts"
|
|
16
|
-
import { type AnyField, assertDeclarationOrderKey, type
|
|
16
|
+
import { type AnyField, assertDeclarationOrderKey, type Infer, literalOf } from "#fields.ts"
|
|
17
17
|
import type { LiteralSetSpec, LiteralSpec } from "#spec.ts"
|
|
18
18
|
|
|
19
19
|
/** Flattens an intersection into one displayed object type (hover legibility). */
|
|
20
20
|
type Flatten<T> = { [K in keyof T]: T[K] }
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* The one
|
|
23
|
+
* The one trusted seam of `relation()`: the reference record is built by
|
|
24
24
|
* iterating the declared fields, and this guard verifies the checkable
|
|
25
25
|
* facts — one reference per declared field, each carrying its own name —
|
|
26
|
-
* before the record is admitted as the typed {@link FieldRefs}
|
|
27
|
-
*
|
|
28
|
-
* module's single trusted seam, the macro-emission analog.
|
|
26
|
+
* before the record is admitted as the typed {@link FieldRefs} (the
|
|
27
|
+
* macro-emission analog).
|
|
29
28
|
*/
|
|
30
29
|
function refsComplete<RName extends string, Fields extends FieldsShape>(
|
|
31
30
|
refs: Record<string, unknown>,
|
|
@@ -44,7 +43,7 @@ function refsComplete<RName extends string, Fields extends FieldsShape>(
|
|
|
44
43
|
* `oneOf` signature — the one-element set is unwritable); anything else is
|
|
45
44
|
* the bare literal.
|
|
46
45
|
*/
|
|
47
|
-
function resolveEntry(field:
|
|
46
|
+
function resolveEntry(field: AnyField, entry: unknown): LiteralSetSpec {
|
|
48
47
|
if (typeof entry === "object" && entry !== null && "literals" in entry && Array.isArray(entry.literals)) {
|
|
49
48
|
const literals: LiteralSpec[] = entry.literals.map(function lowerSetLiteral(literal: unknown) {
|
|
50
49
|
return Object.freeze(literalOf(field, literal))
|
|
@@ -58,7 +57,10 @@ function resolveEntry(field: FieldData, entry: unknown): LiteralSetSpec {
|
|
|
58
57
|
* Resolves a whole `where()` selection against the declared fields, in the
|
|
59
58
|
* selection's written order (macro parity: σ is spelled, not sorted). An
|
|
60
59
|
* empty selection is the bare relation respelled and rejected (the
|
|
61
|
-
* canonical-utterance law).
|
|
60
|
+
* canonical-utterance law). THE one selection resolver — `closed()`'s
|
|
61
|
+
* `where()` resolves its payload columns through this same machine (a
|
|
62
|
+
* `ClosedColumn` is structurally a {@link RelationField}), so both surfaces
|
|
63
|
+
* share one vocabulary and one error voice.
|
|
62
64
|
*/
|
|
63
65
|
function resolveSelection(
|
|
64
66
|
name: string,
|
|
@@ -86,29 +88,29 @@ function resolveSelection(
|
|
|
86
88
|
return Object.freeze(bindings)
|
|
87
89
|
}
|
|
88
90
|
|
|
89
|
-
/** The field block of a relation: field name to field
|
|
91
|
+
/** The field block of a relation: field name to field descriptor. */
|
|
90
92
|
type FieldsShape = Record<string, AnyField>
|
|
91
93
|
|
|
92
94
|
/**
|
|
93
95
|
* A typed field reference (`Account.fields.holder`) — the value statements,
|
|
94
|
-
* selections, and queries address a field through
|
|
95
|
-
* field's
|
|
96
|
+
* selections, and queries address a field through. Purely positional
|
|
97
|
+
* (relation name + field name); the field's descriptor is read off the
|
|
98
|
+
* relation's schema type structurally.
|
|
96
99
|
*/
|
|
97
|
-
interface FieldRef<Rel extends string, Name extends string
|
|
100
|
+
interface FieldRef<Rel extends string, Name extends string> {
|
|
98
101
|
readonly relation: Rel
|
|
99
102
|
readonly field: Name
|
|
100
|
-
readonly [phantom]?: V
|
|
101
103
|
}
|
|
102
104
|
|
|
103
105
|
/** The typed field-reference record of a relation. */
|
|
104
106
|
type FieldRefs<RName extends string, Fields extends FieldsShape> = {
|
|
105
|
-
readonly [K in keyof Fields & string]: FieldRef<RName, K
|
|
107
|
+
readonly [K in keyof Fields & string]: FieldRef<RName, K>
|
|
106
108
|
}
|
|
107
109
|
|
|
108
|
-
/** One declared field: name plus its
|
|
110
|
+
/** One declared field: name plus its descriptor, in declaration order. */
|
|
109
111
|
interface RelationField {
|
|
110
112
|
readonly name: string
|
|
111
|
-
readonly field:
|
|
113
|
+
readonly field: AnyField
|
|
112
114
|
}
|
|
113
115
|
|
|
114
116
|
/** A relation's runtime description. */
|
|
@@ -128,15 +130,14 @@ interface SelectionBinding {
|
|
|
128
130
|
}
|
|
129
131
|
|
|
130
132
|
/**
|
|
131
|
-
* The `where()` argument: per field, a
|
|
132
|
-
* type (a closed handle constant IS such a literal —
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
* parameter exists anywhere.
|
|
133
|
+
* The `where()` argument: per field, a bare structural literal of that
|
|
134
|
+
* field's value type (a closed handle constant IS such a literal — a
|
|
135
|
+
* bigint verified against the roster at construction), an `oneOf(a, b,
|
|
136
|
+
* ...)` literal set, or a `span(start, end)` interval literal.
|
|
137
|
+
* Equality-only by construction: no operator parameter exists anywhere.
|
|
137
138
|
*/
|
|
138
139
|
type SelectionInput<Fields extends FieldsShape> = {
|
|
139
|
-
readonly [K in keyof Fields]?:
|
|
140
|
+
readonly [K in keyof Fields]?: Infer<Fields[K]> | OneOf<Infer<Fields[K]>>
|
|
140
141
|
}
|
|
141
142
|
|
|
142
143
|
/** A relation with a selection applied — what `on()` consumes as a σ-carrying source. */
|
|
@@ -163,25 +164,21 @@ interface AnySelected {
|
|
|
163
164
|
}
|
|
164
165
|
|
|
165
166
|
/** Extracts a relation's field block type. */
|
|
166
|
-
type RelationFields<R extends AnyRelation> = R extends Relation<string, infer F> ? F : never
|
|
167
|
+
type RelationFields<R extends AnyRelation> = R extends Relation<string, infer F extends FieldsShape> ? F : never
|
|
167
168
|
|
|
168
169
|
/**
|
|
169
170
|
* The inferred row object type of a relation as READ: every field present,
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
* shape.
|
|
171
|
+
* at its BARE structural value type ({@link Infer}). Closed relations have
|
|
172
|
+
* no `Fact` — they are unwritable, and the type constraint refuses them
|
|
173
|
+
* because a closed value lacks the relation shape.
|
|
173
174
|
*/
|
|
174
175
|
type Fact<R extends AnyRelation> = {
|
|
175
|
-
[K in keyof RelationFields<R>]:
|
|
176
|
+
[K in keyof RelationFields<R>]: Infer<RelationFields<R>[K]>
|
|
176
177
|
}
|
|
177
178
|
|
|
178
|
-
/** The field names of `R`
|
|
179
|
+
/** The field names of `R` whose descriptor type carries the fresh mint mark. */
|
|
179
180
|
type FreshKeys<R extends AnyRelation> = {
|
|
180
|
-
[K in keyof RelationFields<R>]: RelationFields<R>[K] extends {
|
|
181
|
-
readonly data: { readonly minted: true }
|
|
182
|
-
}
|
|
183
|
-
? K
|
|
184
|
-
: never
|
|
181
|
+
[K in keyof RelationFields<R>]: RelationFields<R>[K] extends { readonly fresh: true } ? K : never
|
|
185
182
|
}[keyof RelationFields<R>]
|
|
186
183
|
|
|
187
184
|
/**
|
|
@@ -192,11 +189,14 @@ type FreshKeys<R extends AnyRelation> = {
|
|
|
192
189
|
type InsertFact<R extends AnyRelation> = Flatten<Omit<Fact<R>, FreshKeys<R>> & Partial<Pick<Fact<R>, FreshKeys<R>>>>
|
|
193
190
|
|
|
194
191
|
/**
|
|
195
|
-
* Declares one relation: `relation("Account", { id:
|
|
196
|
-
* holder:
|
|
197
|
-
* (
|
|
198
|
-
*
|
|
199
|
-
*
|
|
192
|
+
* Declares one relation: `relation("Account", { id: u64.fresh,
|
|
193
|
+
* holder: u64, kind: Kind.id, ... })` — every field is a pure-structure
|
|
194
|
+
* descriptor (the constructor values themselves; domains are never
|
|
195
|
+
* declared: `schema()` computes them from the statements). Field
|
|
196
|
+
* declaration order is ordinal-id order (macro parity), carried at BOTH
|
|
197
|
+
* levels: the type level by the fields object, the value level by the
|
|
198
|
+
* frozen `data.fields` list — the law the schema-level class naming leans
|
|
199
|
+
* on. The returned value is frozen and side-effect free.
|
|
200
200
|
*/
|
|
201
201
|
function relation<const Name extends string, Fields extends FieldsShape>(
|
|
202
202
|
name: Name,
|
|
@@ -205,7 +205,7 @@ function relation<const Name extends string, Fields extends FieldsShape>(
|
|
|
205
205
|
const ordered: RelationField[] = []
|
|
206
206
|
for (const [fieldName, field] of Object.entries(fields)) {
|
|
207
207
|
assertDeclarationOrderKey(`relation ${name} field`, fieldName)
|
|
208
|
-
ordered.push(Object.freeze({ name: fieldName, field
|
|
208
|
+
ordered.push(Object.freeze({ name: fieldName, field }))
|
|
209
209
|
}
|
|
210
210
|
const data: RelationData = Object.freeze({ name, fields: Object.freeze(ordered) })
|
|
211
211
|
const refs: Record<string, unknown> = {}
|
|
@@ -249,4 +249,4 @@ export type {
|
|
|
249
249
|
SelectionBinding,
|
|
250
250
|
SelectionInput
|
|
251
251
|
}
|
|
252
|
-
export { relation }
|
|
252
|
+
export { relation, resolveSelection }
|