foldkit-entity 0.13.0 → 0.15.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/README.md +962 -512
- package/dist/collation.d.ts +33 -0
- package/dist/collation.d.ts.map +1 -0
- package/dist/collation.js +46 -0
- package/dist/collation.js.map +1 -0
- package/dist/conformance/index.d.ts +66 -0
- package/dist/conformance/index.d.ts.map +1 -0
- package/dist/conformance/{index.mjs → index.js} +22 -360
- package/dist/conformance/index.js.map +1 -0
- package/dist/conformance/operator.d.ts +115 -0
- package/dist/conformance/operator.d.ts.map +1 -0
- package/dist/conformance/operator.js +354 -0
- package/dist/conformance/operator.js.map +1 -0
- package/dist/conformance/relational.d.ts +240 -0
- package/dist/conformance/relational.d.ts.map +1 -0
- package/dist/conformance/relational.js +425 -0
- package/dist/conformance/relational.js.map +1 -0
- package/dist/conformance/tuples.d.ts +8 -0
- package/dist/conformance/tuples.d.ts.map +1 -0
- package/dist/conformance/tuples.js +43 -0
- package/dist/conformance/tuples.js.map +1 -0
- package/dist/conformance/variants.d.ts +47 -0
- package/dist/conformance/variants.d.ts.map +1 -0
- package/dist/conformance/variants.js +126 -0
- package/dist/conformance/variants.js.map +1 -0
- package/dist/context.d.ts +60 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +36 -0
- package/dist/context.js.map +1 -0
- package/dist/descriptor.d.ts +33 -0
- package/dist/descriptor.d.ts.map +1 -0
- package/dist/descriptor.js +137 -0
- package/dist/descriptor.js.map +1 -0
- package/dist/entity.d.ts +231 -0
- package/dist/entity.d.ts.map +1 -0
- package/dist/entity.js +435 -0
- package/dist/entity.js.map +1 -0
- package/dist/evaluate.d.ts +204 -0
- package/dist/evaluate.d.ts.map +1 -0
- package/dist/evaluate.js +765 -0
- package/dist/evaluate.js.map +1 -0
- package/dist/expr.d.ts +915 -0
- package/dist/expr.d.ts.map +1 -0
- package/dist/expr.js +1630 -0
- package/dist/expr.js.map +1 -0
- package/dist/held.d.ts +112 -0
- package/dist/held.d.ts.map +1 -0
- package/dist/held.js +155 -0
- package/dist/held.js.map +1 -0
- package/dist/ids.d.ts +8 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +8 -0
- package/dist/ids.js.map +1 -0
- package/dist/index.d.ts +380 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +110 -0
- package/dist/index.js.map +1 -0
- package/dist/invariant.d.ts +123 -0
- package/dist/invariant.d.ts.map +1 -0
- package/dist/invariant.js +236 -0
- package/dist/invariant.js.map +1 -0
- package/dist/jsonSchema.d.ts +3 -0
- package/dist/jsonSchema.d.ts.map +1 -0
- package/dist/jsonSchema.js +17 -0
- package/dist/jsonSchema.js.map +1 -0
- package/dist/law.d.ts +264 -0
- package/dist/law.d.ts.map +1 -0
- package/dist/law.js +111 -0
- package/dist/law.js.map +1 -0
- package/dist/operators.d.ts +170 -0
- package/dist/operators.d.ts.map +1 -0
- package/dist/operators.js +207 -0
- package/dist/operators.js.map +1 -0
- package/dist/readFilter.d.ts +8 -0
- package/dist/readFilter.d.ts.map +1 -0
- package/dist/readFilter.js +23 -0
- package/dist/readFilter.js.map +1 -0
- package/dist/record.d.ts +11 -0
- package/dist/record.d.ts.map +1 -0
- package/dist/record.js +21 -0
- package/dist/record.js.map +1 -0
- package/dist/sets.d.ts +18 -0
- package/dist/sets.d.ts.map +1 -0
- package/dist/sets.js +57 -0
- package/dist/sets.js.map +1 -0
- package/dist/shape.d.ts +23 -0
- package/dist/shape.d.ts.map +1 -0
- package/dist/shape.js +30 -0
- package/dist/shape.js.map +1 -0
- package/dist/tested.d.ts +8 -0
- package/dist/tested.d.ts.map +1 -0
- package/dist/tested.js +25 -0
- package/dist/tested.js.map +1 -0
- package/dist/text.d.ts +27 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +39 -0
- package/dist/text.js.map +1 -0
- package/dist/variants.d.ts +72 -0
- package/dist/variants.d.ts.map +1 -0
- package/dist/variants.js +76 -0
- package/dist/variants.js.map +1 -0
- package/dist/words.d.ts +17 -0
- package/dist/words.d.ts.map +1 -0
- package/dist/words.js +22 -0
- package/dist/words.js.map +1 -0
- package/dist/write.d.ts +312 -0
- package/dist/write.d.ts.map +1 -0
- package/dist/write.js +520 -0
- package/dist/write.js.map +1 -0
- package/package.json +8 -8
- package/dist/conformance/index.d.mts +0 -274
- package/dist/conformance/index.d.mts.map +0 -1
- package/dist/conformance/index.mjs.map +0 -1
- package/dist/index-hu8IYlNc.d.mts +0 -2228
- package/dist/index-hu8IYlNc.d.mts.map +0 -1
- package/dist/index.d.mts +0 -2
- package/dist/index.mjs +0 -1
- package/dist/src-DBAilaDV.mjs +0 -3061
- package/dist/src-DBAilaDV.mjs.map +0 -1
|
@@ -1,2228 +0,0 @@
|
|
|
1
|
-
import { Array, Data, Option, Pipeable, Schema, SchemaAST } from "effect";
|
|
2
|
-
import { Metadata } from "foldkit-metadata";
|
|
3
|
-
type Collation =
|
|
4
|
-
/** By code point: SQLite `BINARY`, Postgres `COLLATE "C"`. */
|
|
5
|
-
{
|
|
6
|
-
readonly _tag: 'Binary';
|
|
7
|
-
} |
|
|
8
|
-
/** ASCII letters folded to lower case, then by code point: SQLite `NOCASE`, Postgres `lower(x COLLATE "C")`. */
|
|
9
|
-
{
|
|
10
|
-
readonly _tag: 'AsciiFold';
|
|
11
|
-
} |
|
|
12
|
-
/** The backend's collation by this name: ordered by the server, never by a client. */
|
|
13
|
-
{
|
|
14
|
-
readonly _tag: 'Locale';
|
|
15
|
-
readonly locale: string;
|
|
16
|
-
};
|
|
17
|
-
declare const Collation: {
|
|
18
|
-
binary: Collation;
|
|
19
|
-
asciiFold: Collation;
|
|
20
|
-
locale: (locale: string) => Collation;
|
|
21
|
-
/** Member metadata: how a text field orders, wherever a query orders by it. */
|
|
22
|
-
of: (collation: Collation) => Metadata;
|
|
23
|
-
/** The collation a member's metadata declares, if any. */
|
|
24
|
-
declared: (metadata: Metadata) => Option.Option<Collation>;
|
|
25
|
-
/**
|
|
26
|
-
* Text as `asciiFold` compares it: ASCII letters lowercased, every other
|
|
27
|
-
* character as it is, which is what SQLite's `lower` does without ICU and so
|
|
28
|
-
* what compiled SQL folds to. `toLowerCase` would fold `É` to `é` here and not
|
|
29
|
-
* there. An operator implementing `asciiFold` folds with this.
|
|
30
|
-
*/
|
|
31
|
-
foldAscii: (text: string) => string;
|
|
32
|
-
};
|
|
33
|
-
/** An operator a law names as a witness: the inverse, or the operator distributed over. */
|
|
34
|
-
type AnyOperator = Operator<ReadonlyArray<Schema.Top>, Schema.Top>;
|
|
35
|
-
/** Which argument a witness stands in. */
|
|
36
|
-
type Side = 'left' | 'right' | 'both';
|
|
37
|
-
/**
|
|
38
|
-
* A law, over its domain: by default every non-null value of the operator's
|
|
39
|
-
* input schemas; narrowed by `Law.within`.
|
|
40
|
-
*/
|
|
41
|
-
type Law = Data.TaggedEnum<{
|
|
42
|
-
/** `op(op(a, b), c) = op(a, op(b, c))` */
|
|
43
|
-
Associative: {
|
|
44
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
45
|
-
};
|
|
46
|
-
/** `op(a, b) = op(b, a)` */
|
|
47
|
-
Commutative: {
|
|
48
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
49
|
-
};
|
|
50
|
-
/** `op(a, a) = a` */
|
|
51
|
-
Idempotent: {
|
|
52
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
53
|
-
};
|
|
54
|
-
/** `op(e, a) = a` (left), `op(a, e) = a` (right), or both. */
|
|
55
|
-
Identity: {
|
|
56
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
57
|
-
readonly value: unknown;
|
|
58
|
-
readonly side: Side;
|
|
59
|
-
};
|
|
60
|
-
/** `op(z, a) = z` (left), `op(a, z) = z` (right), or both. */
|
|
61
|
-
Absorbing: {
|
|
62
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
63
|
-
readonly value: unknown;
|
|
64
|
-
readonly side: Side;
|
|
65
|
-
};
|
|
66
|
-
/** `op(a, other(a)) = identity` */
|
|
67
|
-
Inverse: {
|
|
68
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
69
|
-
readonly other: AnyOperator;
|
|
70
|
-
readonly identity: unknown;
|
|
71
|
-
};
|
|
72
|
-
/** `op(a, over(b, c)) = over(op(a, b), op(a, c))` */
|
|
73
|
-
Distributes: {
|
|
74
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
75
|
-
readonly over: AnyOperator;
|
|
76
|
-
};
|
|
77
|
-
/** `a ≤ b` at `position` implies `op(…a…) ≤ op(…b…)`, or `≥` when decreasing. */
|
|
78
|
-
Monotonic: {
|
|
79
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
80
|
-
readonly direction: 'increasing' | 'decreasing';
|
|
81
|
-
readonly position: number;
|
|
82
|
-
};
|
|
83
|
-
/** Folding the result equals folding the arguments first: its text keeps their collation (§7). */
|
|
84
|
-
PreservesCollation: {
|
|
85
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
86
|
-
};
|
|
87
|
-
}>;
|
|
88
|
-
/**
|
|
89
|
-
* A law as JSON says it: what `Operators.describe` lists and evidence names.
|
|
90
|
-
* Its witnesses are values as stored and operators by name; its domain is a
|
|
91
|
-
* JSON Schema, or `null` for the operator's whole input.
|
|
92
|
-
*/
|
|
93
|
-
declare const LawDescription: Schema.Struct<{
|
|
94
|
-
readonly kind: Schema.String;
|
|
95
|
-
readonly witnesses: Schema.$Record<Schema.String, Schema.Unknown>;
|
|
96
|
-
readonly domain: Schema.Unknown;
|
|
97
|
-
}>;
|
|
98
|
-
type LawDescription = typeof LawDescription.Type;
|
|
99
|
-
declare const Law: {
|
|
100
|
-
associative: {
|
|
101
|
-
readonly _tag: "Associative";
|
|
102
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
103
|
-
};
|
|
104
|
-
commutative: {
|
|
105
|
-
readonly _tag: "Commutative";
|
|
106
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
107
|
-
};
|
|
108
|
-
idempotent: {
|
|
109
|
-
readonly _tag: "Idempotent";
|
|
110
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
111
|
-
};
|
|
112
|
-
preservesCollation: {
|
|
113
|
-
readonly _tag: "PreservesCollation";
|
|
114
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
115
|
-
};
|
|
116
|
-
/** `value` leaves the other argument as it is, from `side` (both by default). */
|
|
117
|
-
identity: (value: unknown, side?: Side) => Law;
|
|
118
|
-
/** `value` is the answer whatever the other argument, from `side` (both by default). */
|
|
119
|
-
absorbing: (value: unknown, side?: Side) => Law;
|
|
120
|
-
/** `other` undoes the operator: `op(a, other(a))` is `identity`. */
|
|
121
|
-
inverse: <In extends ReadonlyArray<Schema.Top>, Out extends Schema.Top>(other: Operator<In, Out>, identity: unknown) => Law;
|
|
122
|
-
/** The operator distributes over `over`, from the left. */
|
|
123
|
-
distributes: <In extends ReadonlyArray<Schema.Top>, Out extends Schema.Top>(over: Operator<In, Out>) => Law;
|
|
124
|
-
/** Moving the argument at `position` up moves the result up (or down, decreasing). */
|
|
125
|
-
monotonic: (direction: 'increasing' | 'decreasing', position: number) => Law;
|
|
126
|
-
/**
|
|
127
|
-
* The law over the values `domain` accepts only: a law that holds of the
|
|
128
|
-
* integers and not of every number, say, narrowed so it is tested where it
|
|
129
|
-
* is claimed.
|
|
130
|
-
*/
|
|
131
|
-
within: (law: Law, domain: Schema.Top) => Law;
|
|
132
|
-
$match: {
|
|
133
|
-
<Cases extends { readonly [Tag in "Absorbing" | "Associative" | "Commutative" | "Distributes" | "Idempotent" | "Identity" | "Inverse" | "Monotonic" | "PreservesCollation"]: (args: Extract<{
|
|
134
|
-
readonly _tag: "Absorbing";
|
|
135
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
136
|
-
readonly value: unknown;
|
|
137
|
-
readonly side: Side;
|
|
138
|
-
} | {
|
|
139
|
-
readonly _tag: "Associative";
|
|
140
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
141
|
-
} | {
|
|
142
|
-
readonly _tag: "Commutative";
|
|
143
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
144
|
-
} | {
|
|
145
|
-
readonly _tag: "Distributes";
|
|
146
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
147
|
-
readonly over: AnyOperator;
|
|
148
|
-
} | {
|
|
149
|
-
readonly _tag: "Idempotent";
|
|
150
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
151
|
-
} | {
|
|
152
|
-
readonly _tag: "Identity";
|
|
153
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
154
|
-
readonly value: unknown;
|
|
155
|
-
readonly side: Side;
|
|
156
|
-
} | {
|
|
157
|
-
readonly _tag: "Inverse";
|
|
158
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
159
|
-
readonly other: AnyOperator;
|
|
160
|
-
readonly identity: unknown;
|
|
161
|
-
} | {
|
|
162
|
-
readonly _tag: "Monotonic";
|
|
163
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
164
|
-
readonly direction: 'increasing' | 'decreasing';
|
|
165
|
-
readonly position: number;
|
|
166
|
-
} | {
|
|
167
|
-
readonly _tag: "PreservesCollation";
|
|
168
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
169
|
-
}, {
|
|
170
|
-
readonly _tag: Tag;
|
|
171
|
-
}>) => any; }>(cases: Cases): (value: {
|
|
172
|
-
readonly _tag: "Absorbing";
|
|
173
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
174
|
-
readonly value: unknown;
|
|
175
|
-
readonly side: Side;
|
|
176
|
-
} | {
|
|
177
|
-
readonly _tag: "Associative";
|
|
178
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
179
|
-
} | {
|
|
180
|
-
readonly _tag: "Commutative";
|
|
181
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
182
|
-
} | {
|
|
183
|
-
readonly _tag: "Distributes";
|
|
184
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
185
|
-
readonly over: AnyOperator;
|
|
186
|
-
} | {
|
|
187
|
-
readonly _tag: "Idempotent";
|
|
188
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
189
|
-
} | {
|
|
190
|
-
readonly _tag: "Identity";
|
|
191
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
192
|
-
readonly value: unknown;
|
|
193
|
-
readonly side: Side;
|
|
194
|
-
} | {
|
|
195
|
-
readonly _tag: "Inverse";
|
|
196
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
197
|
-
readonly other: AnyOperator;
|
|
198
|
-
readonly identity: unknown;
|
|
199
|
-
} | {
|
|
200
|
-
readonly _tag: "Monotonic";
|
|
201
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
202
|
-
readonly direction: 'increasing' | 'decreasing';
|
|
203
|
-
readonly position: number;
|
|
204
|
-
} | {
|
|
205
|
-
readonly _tag: "PreservesCollation";
|
|
206
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
207
|
-
}) => import("effect/Unify").Unify<ReturnType<Cases["Absorbing" | "Associative" | "Commutative" | "Distributes" | "Idempotent" | "Identity" | "Inverse" | "Monotonic" | "PreservesCollation"]>>;
|
|
208
|
-
<Cases extends { readonly [Tag in "Absorbing" | "Associative" | "Commutative" | "Distributes" | "Idempotent" | "Identity" | "Inverse" | "Monotonic" | "PreservesCollation"]: (args: Extract<{
|
|
209
|
-
readonly _tag: "Absorbing";
|
|
210
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
211
|
-
readonly value: unknown;
|
|
212
|
-
readonly side: Side;
|
|
213
|
-
} | {
|
|
214
|
-
readonly _tag: "Associative";
|
|
215
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
216
|
-
} | {
|
|
217
|
-
readonly _tag: "Commutative";
|
|
218
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
219
|
-
} | {
|
|
220
|
-
readonly _tag: "Distributes";
|
|
221
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
222
|
-
readonly over: AnyOperator;
|
|
223
|
-
} | {
|
|
224
|
-
readonly _tag: "Idempotent";
|
|
225
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
226
|
-
} | {
|
|
227
|
-
readonly _tag: "Identity";
|
|
228
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
229
|
-
readonly value: unknown;
|
|
230
|
-
readonly side: Side;
|
|
231
|
-
} | {
|
|
232
|
-
readonly _tag: "Inverse";
|
|
233
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
234
|
-
readonly other: AnyOperator;
|
|
235
|
-
readonly identity: unknown;
|
|
236
|
-
} | {
|
|
237
|
-
readonly _tag: "Monotonic";
|
|
238
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
239
|
-
readonly direction: 'increasing' | 'decreasing';
|
|
240
|
-
readonly position: number;
|
|
241
|
-
} | {
|
|
242
|
-
readonly _tag: "PreservesCollation";
|
|
243
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
244
|
-
}, {
|
|
245
|
-
readonly _tag: Tag;
|
|
246
|
-
}>) => any; }>(value: {
|
|
247
|
-
readonly _tag: "Absorbing";
|
|
248
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
249
|
-
readonly value: unknown;
|
|
250
|
-
readonly side: Side;
|
|
251
|
-
} | {
|
|
252
|
-
readonly _tag: "Associative";
|
|
253
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
254
|
-
} | {
|
|
255
|
-
readonly _tag: "Commutative";
|
|
256
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
257
|
-
} | {
|
|
258
|
-
readonly _tag: "Distributes";
|
|
259
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
260
|
-
readonly over: AnyOperator;
|
|
261
|
-
} | {
|
|
262
|
-
readonly _tag: "Idempotent";
|
|
263
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
264
|
-
} | {
|
|
265
|
-
readonly _tag: "Identity";
|
|
266
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
267
|
-
readonly value: unknown;
|
|
268
|
-
readonly side: Side;
|
|
269
|
-
} | {
|
|
270
|
-
readonly _tag: "Inverse";
|
|
271
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
272
|
-
readonly other: AnyOperator;
|
|
273
|
-
readonly identity: unknown;
|
|
274
|
-
} | {
|
|
275
|
-
readonly _tag: "Monotonic";
|
|
276
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
277
|
-
readonly direction: 'increasing' | 'decreasing';
|
|
278
|
-
readonly position: number;
|
|
279
|
-
} | {
|
|
280
|
-
readonly _tag: "PreservesCollation";
|
|
281
|
-
readonly domain: Option.Option<Schema.Top>;
|
|
282
|
-
}, cases: Cases): import("effect/Unify").Unify<ReturnType<Cases["Absorbing" | "Associative" | "Commutative" | "Distributes" | "Idempotent" | "Identity" | "Inverse" | "Monotonic" | "PreservesCollation"]>>;
|
|
283
|
-
};
|
|
284
|
-
/**
|
|
285
|
-
* The operators a law, as JSON, calls besides its own (`inverse`'s and
|
|
286
|
-
* `distributes`' witnesses), by name: evidence of it is of their
|
|
287
|
-
* implementations too, and goes when one of them is replaced.
|
|
288
|
-
*/
|
|
289
|
-
witnessesNamed: (law: LawDescription) => ReadonlyArray<string>;
|
|
290
|
-
/** The law as JSON: its kind, its witnesses (operators by name) and its domain's JSON Schema. */
|
|
291
|
-
describe: (law: Law) => LawDescription;
|
|
292
|
-
};
|
|
293
|
-
/** A constant the query was written with. */
|
|
294
|
-
interface LiteralExpr<T> {
|
|
295
|
-
readonly _tag: 'Literal';
|
|
296
|
-
readonly value: T;
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* One field of one Entity. `owner` is the Entity's identity rather than its
|
|
300
|
-
* name, so two Entities defined with the same name are not one column.
|
|
301
|
-
*/
|
|
302
|
-
interface FieldExpr<T> {
|
|
303
|
-
readonly _tag: 'Field';
|
|
304
|
-
readonly owner: EntityIdentity<string>;
|
|
305
|
-
readonly key: string;
|
|
306
|
-
readonly schema: Schema.Codec<T, unknown>;
|
|
307
|
-
/**
|
|
308
|
-
* How it compares when ordered by, as the field declares (`Collation.of`).
|
|
309
|
-
* An id is `binary` unless it says otherwise: its order need only be total
|
|
310
|
-
* and agreed, and every order ends on it.
|
|
311
|
-
*/
|
|
312
|
-
readonly collation: Option.Option<Collation>;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* A value the query is given when it runs. A definition's body is built once,
|
|
316
|
-
* so inside it an input is this placeholder and never the value: there is
|
|
317
|
-
* nothing yet to branch on, and a query that wants to depend on what was passed
|
|
318
|
-
* says so with an operation over the placeholder.
|
|
319
|
-
*/
|
|
320
|
-
interface InputExpr<T> {
|
|
321
|
-
readonly _tag: 'Input';
|
|
322
|
-
readonly key: string;
|
|
323
|
-
readonly schema: Schema.Codec<T, unknown>;
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* A field as the row a write would leave holds it: what an invariant reads
|
|
327
|
-
* (`after.status`, beside `before.status` over a change). An interpreter that judges stored
|
|
328
|
-
* rows alone refuses it, as the operation `next`.
|
|
329
|
-
*/
|
|
330
|
-
interface NextExpr<T> {
|
|
331
|
-
readonly _tag: 'Next';
|
|
332
|
-
readonly field: FieldExpr<T>;
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* What an operator is, as the nodes that call it hold it: its identity, the
|
|
336
|
-
* stored form of what it takes and gives, and, for an operator defined by an
|
|
337
|
-
* expression, how to build that expression. It computes nothing itself.
|
|
338
|
-
* Identity is this object; `id` and `version` name it.
|
|
339
|
-
*/
|
|
340
|
-
interface OperatorDescriptor {
|
|
341
|
-
readonly _tag: 'Operator';
|
|
342
|
-
readonly id: string;
|
|
343
|
-
readonly version: number;
|
|
344
|
-
readonly description: Option.Option<string>;
|
|
345
|
-
/** One schema per argument, read as the stored form. */
|
|
346
|
-
readonly input: ReadonlyArray<Schema.Top>;
|
|
347
|
-
readonly output: Schema.Top;
|
|
348
|
-
/**
|
|
349
|
-
* Builds the expression a call means, from its arguments: an operator defined
|
|
350
|
-
* by an expression. None for a primitive operator, which each interpreter
|
|
351
|
-
* implements (`Operators`).
|
|
352
|
-
*/
|
|
353
|
-
readonly definedAs: Option.Option<(...args: ReadonlyArray<Operandish>) => Operandish>;
|
|
354
|
-
/**
|
|
355
|
-
* A primitive operator's null rule. `strict`: a null argument gives null, or
|
|
356
|
-
* unknown for a predicate, and the implementation never sees one. `handled`:
|
|
357
|
-
* the implementation is given nulls and answers for them.
|
|
358
|
-
*/
|
|
359
|
-
readonly nulls: 'strict' | 'handled';
|
|
360
|
-
/**
|
|
361
|
-
* How a call compares text. `operands`: it carries the collation its collated
|
|
362
|
-
* text arguments share, so its text result compares under it. `required`: the
|
|
363
|
-
* same, refused where none is declared (it compares text itself, as
|
|
364
|
-
* `Text.startsWith` does). `none`: it carries none.
|
|
365
|
-
*/
|
|
366
|
-
readonly collation: 'none' | 'operands' | 'required';
|
|
367
|
-
/** The equations every implementation keeps (`Law`), checked where declared, tested by `Conformance.laws`. */
|
|
368
|
-
readonly laws: ReadonlyArray<Law>;
|
|
369
|
-
}
|
|
370
|
-
/** What an interpreter runs, as far as a check of a body needs to know. */
|
|
371
|
-
interface OperatorTable {
|
|
372
|
-
/** Whether this table implements the operator. */
|
|
373
|
-
readonly has: (operator: OperatorDescriptor) => boolean;
|
|
374
|
-
/**
|
|
375
|
-
* The versions of the operator `id` it implements: what a refusal of a call
|
|
376
|
-
* to another version names, since a client and a server on different
|
|
377
|
-
* deploys can disagree on one.
|
|
378
|
-
*/
|
|
379
|
-
readonly versions: (id: string) => ReadonlyArray<number>;
|
|
380
|
-
}
|
|
381
|
-
/** A table implementing nothing: every primitive operator is unsupported by it. */
|
|
382
|
-
declare const noOperators: OperatorTable;
|
|
383
|
-
/**
|
|
384
|
-
* An operator `operators` does not implement, by name, with the versions of it
|
|
385
|
-
* the table does implement: `acme.slug@2 (this table has acme.slug)`.
|
|
386
|
-
*/
|
|
387
|
-
declare const missingName: (operator: OperatorDescriptor, operators: OperatorTable) => string;
|
|
388
|
-
/**
|
|
389
|
-
* A call of an operator whose result is a scalar. `expansion` is the
|
|
390
|
-
* expression it means, built from its arguments when it was called: an
|
|
391
|
-
* interpreter runs that, and needs nothing of its own for the operator.
|
|
392
|
-
*/
|
|
393
|
-
interface CallExpr<T> {
|
|
394
|
-
readonly _tag: 'Call';
|
|
395
|
-
readonly operator: OperatorDescriptor;
|
|
396
|
-
readonly args: ReadonlyArray<Operandish>;
|
|
397
|
-
readonly expansion: Option.Option<Expr<T>>;
|
|
398
|
-
/** How its text result compares, from its operator's `collation` rule. */
|
|
399
|
-
readonly collation: Option.Option<Collation>;
|
|
400
|
-
}
|
|
401
|
-
/** A typed scalar: what a comparison compares. */
|
|
402
|
-
type Expr<T> = LiteralExpr<T> | FieldExpr<T> | InputExpr<T> | NextExpr<T> | CallExpr<T>;
|
|
403
|
-
/** Any scalar, for the places that hold operands without caring what they are. */
|
|
404
|
-
type AnyExpr = Expr<unknown>;
|
|
405
|
-
/**
|
|
406
|
-
* `Expr<boolean>`, in the shape the operations that exist can actually take.
|
|
407
|
-
* The design's kernel is all boolean-valued (`eq`, `and`, `isNull`,
|
|
408
|
-
* `contains`, …), so a predicate is its own type rather than a phantom
|
|
409
|
-
* parameter on a general operation node: `Predicate` and `Expr<string>` cannot
|
|
410
|
-
* then be confused, which a structural phantom would allow. A scalar-valued
|
|
411
|
-
* operation, if one is ever needed, joins `Expr` instead.
|
|
412
|
-
*/
|
|
413
|
-
type Predicate = EqPredicate | NullPredicate | ContainsPredicate | InPredicate | AndPredicate | OrPredicate | NotPredicate | ComparePredicate | TestPredicate;
|
|
414
|
-
/** Two values are the same. */
|
|
415
|
-
interface EqPredicate {
|
|
416
|
-
readonly _tag: 'Eq';
|
|
417
|
-
readonly left: Operandish;
|
|
418
|
-
readonly right: Operandish;
|
|
419
|
-
}
|
|
420
|
-
/**
|
|
421
|
-
* Whether a value is absent. `present` says which answer absence gives, so one
|
|
422
|
-
* node covers `is null` and `is not null` rather than two that differ by a
|
|
423
|
-
* negation nothing else can express.
|
|
424
|
-
*/
|
|
425
|
-
interface NullPredicate {
|
|
426
|
-
readonly _tag: 'Null';
|
|
427
|
-
readonly operand: AnyExpr;
|
|
428
|
-
readonly present: boolean;
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* Whether a text value contains another. Containing the empty string is
|
|
432
|
-
* everything, which is what lets a search box with nothing typed in it be the
|
|
433
|
-
* same query as one with something — see `Expr.contains`.
|
|
434
|
-
*/
|
|
435
|
-
interface ContainsPredicate {
|
|
436
|
-
readonly _tag: 'Contains';
|
|
437
|
-
readonly value: AnyExpr;
|
|
438
|
-
readonly search: Operandish;
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* Whether a value is one of a list. The list is a scalar whose value is an
|
|
442
|
-
* array: a literal, or an input (a principal's `projectIds`, a picker's
|
|
443
|
-
* chosen ids). Read as SQL's `in`: a null value is unknown, and so is no
|
|
444
|
-
* match against a list holding a null; an empty list matches nothing.
|
|
445
|
-
*/
|
|
446
|
-
interface InPredicate {
|
|
447
|
-
readonly _tag: 'In';
|
|
448
|
-
readonly value: AnyExpr;
|
|
449
|
-
readonly among: AnyExpr;
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Every operand holds. `Query.where`'s list is already a conjunction; this is
|
|
453
|
-
* one inside something else, `or(and(a, b), c)`. Never empty, and never holding
|
|
454
|
-
* another `and`: `Expr.and` flattens one into its parent.
|
|
455
|
-
*/
|
|
456
|
-
interface AndPredicate {
|
|
457
|
-
readonly _tag: 'And';
|
|
458
|
-
readonly operands: Array.NonEmptyReadonlyArray<Predicate>;
|
|
459
|
-
}
|
|
460
|
-
/** At least one operand holds. Never empty, and never holding another `or`. */
|
|
461
|
-
interface OrPredicate {
|
|
462
|
-
readonly _tag: 'Or';
|
|
463
|
-
readonly operands: Array.NonEmptyReadonlyArray<Predicate>;
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* The operand does not hold. As SQL's `not`, an unknown stays unknown, so a row
|
|
467
|
-
* whose operand is unknown (a null compared) is matched by neither the operand
|
|
468
|
-
* nor its `not`.
|
|
469
|
-
*/
|
|
470
|
-
interface NotPredicate {
|
|
471
|
-
readonly _tag: 'Not';
|
|
472
|
-
readonly operand: Predicate;
|
|
473
|
-
}
|
|
474
|
-
/** Which way a comparison orders its two sides: `left < right` for `lt`. */
|
|
475
|
-
type CompareOperator = 'lt' | 'lte' | 'gt' | 'gte';
|
|
476
|
-
/**
|
|
477
|
-
* One scalar orders before, or after, the other. A null on either side is
|
|
478
|
-
* unknown. `collation` is how text compares, decided where the comparison is
|
|
479
|
-
* written (see `Expr.lt`); none for values that order intrinsically, numbers.
|
|
480
|
-
*/
|
|
481
|
-
interface ComparePredicate {
|
|
482
|
-
readonly _tag: 'Compare';
|
|
483
|
-
readonly operator: CompareOperator;
|
|
484
|
-
readonly left: AnyExpr;
|
|
485
|
-
readonly right: AnyExpr;
|
|
486
|
-
readonly collation: Option.Option<Collation>;
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* A call of an operator whose result is a boolean, so it stands where any
|
|
490
|
-
* predicate does: in `and`, `or` and `not`, a guard, an invariant. `expansion`
|
|
491
|
-
* is the predicate it means, built when it was called.
|
|
492
|
-
*/
|
|
493
|
-
interface TestPredicate {
|
|
494
|
-
readonly _tag: 'Test';
|
|
495
|
-
readonly operator: OperatorDescriptor;
|
|
496
|
-
readonly args: ReadonlyArray<Operandish>;
|
|
497
|
-
readonly expansion: Option.Option<Predicate>;
|
|
498
|
-
/** The collation it compares text under, from its operator's `collation` rule. */
|
|
499
|
-
readonly collation: Option.Option<Collation>;
|
|
500
|
-
}
|
|
501
|
-
/**
|
|
502
|
-
* Anything an operation can be given: a scalar, or a predicate where a boolean
|
|
503
|
-
* is wanted. `Expr.eq(Expr.isNotNull(archivedAt), input.archived)` is the
|
|
504
|
-
* second case, and it is how a query depends on an input without branching on
|
|
505
|
-
* it.
|
|
506
|
-
*/
|
|
507
|
-
type Operandish = AnyExpr | Predicate;
|
|
508
|
-
/** A field, a scalar already built from one, or a predicate used as a boolean. */
|
|
509
|
-
type Operand<T> = EntityField<string, string, Schema.Constraint> | Expr<T> | Predicate;
|
|
510
|
-
/**
|
|
511
|
-
* Type-only: the Entities a predicate's fields belong to, by name, so
|
|
512
|
-
* `Query.where` can refuse one over another Entity where it is written. Never
|
|
513
|
-
* set at run time; a predicate built without it (typed plain `Predicate`)
|
|
514
|
-
* reads as unknown and is checked at run time only.
|
|
515
|
-
*/
|
|
516
|
-
interface Reads<Names extends string> {
|
|
517
|
-
readonly '~reads'?: Names;
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
|
-
* The Entity names an operand reads: a field's own, a marked node's, none for
|
|
521
|
-
* a value or an input. A node with no marker (a `Predicate` typed plainly)
|
|
522
|
-
* reads as `string`, unknown.
|
|
523
|
-
*/
|
|
524
|
-
type ReadsOf<X> = X extends EntityField<infer Name, string, Schema.Constraint> ? Name : X extends PlainValue | {
|
|
525
|
-
readonly _tag: 'Literal' | 'Input';
|
|
526
|
-
} ? never : X extends {
|
|
527
|
-
readonly '~reads'?: infer Names;
|
|
528
|
-
} ? unknown extends Names ? string : Names extends string ? Names : never : never;
|
|
529
|
-
/**
|
|
530
|
-
* Nothing when a query's predicates read its own Entity, none, or one unknown
|
|
531
|
-
* to the type (checked at run time instead); else a failure naming both.
|
|
532
|
-
*/
|
|
533
|
-
type OverEntity<Names extends string, Name extends string, What extends string> = string extends Names ? unknown : [Names] extends [Name] ? unknown : Invalid<`foldkit-entity: ${What} reads ${Exclude<Names, Name>}, but the query is from ${Name}`>;
|
|
534
|
-
/** A plain value, which `Expr.eq` takes on the right only. */
|
|
535
|
-
type PlainValue = string | number | boolean | bigint | null;
|
|
536
|
-
/** A compile-time failure whose type is the sentence that says what to do. */
|
|
537
|
-
interface Invalid<Message extends string> {
|
|
538
|
-
readonly '~invalid': Message;
|
|
539
|
-
}
|
|
540
|
-
/** What an operand is worth, so a comparison's other side is checked against it. */
|
|
541
|
-
type ValueOf<E> = E extends EntityField<string, string, infer S> ? Schema.Schema.Type<S> : E extends Predicate ? boolean : E extends Expr<infer T> ? T : never;
|
|
542
|
-
/**
|
|
543
|
-
* A field or scalar whose value is text, or absent text.
|
|
544
|
-
*
|
|
545
|
-
* `Expr.contains` is a case-insensitive **text** search that compiles to
|
|
546
|
-
* `lower(column) like lower(?)`. Over a number that is nonsense which reaches
|
|
547
|
-
* the database — SQLite coerces and answers something, Postgres raises — so
|
|
548
|
-
* the operand is constrained here rather than discovered there.
|
|
549
|
-
*
|
|
550
|
-
* Null belongs in the type, because a nullable text column is a real and
|
|
551
|
-
* documented case: a null contains nothing, not even the empty string, so its
|
|
552
|
-
* rows drop out of a search that would otherwise match everything.
|
|
553
|
-
*
|
|
554
|
-
* Written as a **constraint on the type parameter** rather than as a check
|
|
555
|
-
* intersected onto the parameter (the `Invalid` idiom `foldkit-remote` uses for
|
|
556
|
-
* an unregistered descriptor). That idiom compares an already-resolved indexed
|
|
557
|
-
* access; here the check would be a conditional over the type being inferred,
|
|
558
|
-
* and inference falls back to the constraint — whose `Expr<any>` branch makes
|
|
559
|
-
* the check vacuously true, so every operand passes. A constraint cannot be
|
|
560
|
-
* defeated that way.
|
|
561
|
-
*/
|
|
562
|
-
type TextOperand = EntityField<string, string, Schema.Constraint & Schema.Codec<string | null, any, any, any>> | Expr<string> | Expr<string | null>;
|
|
563
|
-
declare const literal: <T>(value: T) => LiteralExpr<T>;
|
|
564
|
-
/** Whether a value is a predicate, and so usable where a boolean is wanted. */
|
|
565
|
-
declare const isPredicate: (value: unknown) => value is Predicate;
|
|
566
|
-
/**
|
|
567
|
-
* Every operation the kernel has. An interpreter declares which of them it
|
|
568
|
-
* runs, and a body needing one it does not is refused rather than answered
|
|
569
|
-
* wrongly — see `Query.unsupported`.
|
|
570
|
-
*/
|
|
571
|
-
type Operation = 'eq' | 'isNull' | 'isNotNull' | 'contains' | 'in' | 'and' | 'or' | 'not' | 'compare' | 'next';
|
|
572
|
-
/** One field an expression reads: its Entity's name and key, and the identity that owns it. */
|
|
573
|
-
interface FieldDependency {
|
|
574
|
-
/** The owning Entity's name, for display; two Entities may share one. */
|
|
575
|
-
readonly entity: string;
|
|
576
|
-
readonly key: string;
|
|
577
|
-
/** Which Entity it is. Two defined with the same name are two entries. */
|
|
578
|
-
readonly owner: EntityIdentity<string>;
|
|
579
|
-
}
|
|
580
|
-
/** Which fields and inputs an expression reads, and which operations it uses. */
|
|
581
|
-
interface Dependencies {
|
|
582
|
-
/** One entry per distinct field of each Entity identity. */
|
|
583
|
-
readonly fields: ReadonlyArray<FieldDependency>;
|
|
584
|
-
/** Each field read as the row a write would leave holds it (`Next`), one entry each. */
|
|
585
|
-
readonly next: ReadonlyArray<FieldDependency>;
|
|
586
|
-
/** One entry per distinct input key. */
|
|
587
|
-
readonly inputs: ReadonlyArray<string>;
|
|
588
|
-
/** One entry per distinct operation, so an interpreter can refuse what it cannot run. */
|
|
589
|
-
readonly operations: ReadonlyArray<Operation>;
|
|
590
|
-
/** Each operator it calls, once per descriptor, defined or not. */
|
|
591
|
-
readonly operators: ReadonlyArray<OperatorDescriptor>;
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
* What a query reads, with each field's role. A change to a `predicate` field
|
|
595
|
-
* can move a row into or out of the result; a change to an `order` field can
|
|
596
|
-
* move it within. A field in both is in both; `fields` is their union.
|
|
597
|
-
*/
|
|
598
|
-
interface QueryDependencies extends Dependencies {
|
|
599
|
-
readonly predicate: ReadonlyArray<FieldDependency>;
|
|
600
|
-
readonly order: ReadonlyArray<FieldDependency>;
|
|
601
|
-
}
|
|
602
|
-
/**
|
|
603
|
-
* A field or scalar whose value orders: a number, text, a date (which orders by
|
|
604
|
-
* its encoded form), or one of those absent. A boolean does not order here.
|
|
605
|
-
* A constraint on the type parameter, as `TextOperand` is, so inference cannot
|
|
606
|
-
* fall back past it.
|
|
607
|
-
*/
|
|
608
|
-
type OrderedOperand = EntityField<string, string, Schema.Constraint & Schema.Codec<number | string | Date | null, any, any, any>> | Expr<number> | Expr<number | null> | Expr<string> | Expr<string | null> | Expr<Date> | Expr<Date | null>;
|
|
609
|
-
/** `Expr.and` and `Expr.or`: one operand is that operand, and two or more are one node. */
|
|
610
|
-
interface Connective<Node extends Predicate> {
|
|
611
|
-
<P extends Predicate>(only: P): P;
|
|
612
|
-
<A extends Predicate, B extends Predicate, Rest extends ReadonlyArray<Predicate>>(first: A, second: B, ...rest: Rest): Node & Reads<ReadsOf<A> | ReadsOf<B> | ReadsOf<Rest[number]>>;
|
|
613
|
-
(...operands: Array.NonEmptyReadonlyArray<Predicate>): Predicate;
|
|
614
|
-
}
|
|
615
|
-
/** A schema's stored form, which is what an operator takes and gives. */
|
|
616
|
-
type StoredOf$1<S extends Schema.Top> = Schema.Codec.Encoded<S>;
|
|
617
|
-
/**
|
|
618
|
-
* One argument an operator may be called with: a field or scalar stored as the
|
|
619
|
-
* input's stored type, a predicate where it takes a boolean, or a plain value.
|
|
620
|
-
*/
|
|
621
|
-
type ArgumentOf<S extends Schema.Top> = EntityField<string, string, Schema.Constraint & Schema.Codec<any, StoredOf$1<S>, any, any>> | Expr<StoredOf$1<S>> | (boolean extends StoredOf$1<S> ? Predicate : never) | StoredOf$1<S>;
|
|
622
|
-
/** What `definedAs` is handed for one argument: the argument as a node. */
|
|
623
|
-
type ParameterOf<S extends Schema.Top> = Expr<StoredOf$1<S>> | (boolean extends StoredOf$1<S> ? Predicate : never);
|
|
624
|
-
/** A call's node: a predicate for a boolean result, a scalar for anything else. */
|
|
625
|
-
type CallOf<Out extends Schema.Top> = [StoredOf$1<Out>] extends [boolean] ? TestPredicate : CallExpr<StoredOf$1<Out>>;
|
|
626
|
-
/** What `definedAs` builds: a predicate for a boolean result, a scalar otherwise. */
|
|
627
|
-
type BodyOf<Out extends Schema.Top> = [StoredOf$1<Out>] extends [boolean] ? Predicate : Expr<StoredOf$1<Out>>;
|
|
628
|
-
/**
|
|
629
|
-
* An operator: called like a function, it builds a node that holds `descriptor`
|
|
630
|
-
* and its arguments. See `Expr.operator`.
|
|
631
|
-
*/
|
|
632
|
-
interface Operator<In extends ReadonlyArray<Schema.Top>, Out extends Schema.Top> {
|
|
633
|
-
<const A extends { readonly [K in keyof In]: ArgumentOf<In[K]>; }>(...args: A): CallOf<Out> & Reads<ReadsOf<A[number]>>;
|
|
634
|
-
/**
|
|
635
|
-
* A call comparing text under the collation given, rather than its text
|
|
636
|
-
* arguments' declared one: for an operator whose `collation` is `operands`
|
|
637
|
-
* or `required`, as `Expr.lt(field, value, { collation })` is.
|
|
638
|
-
*/
|
|
639
|
-
<const A extends { readonly [K in keyof In]: ArgumentOf<In[K]>; }>(...args: [...A, CallOptions]): CallOf<Out> & Reads<ReadsOf<A[number]>>;
|
|
640
|
-
readonly descriptor: OperatorDescriptor;
|
|
641
|
-
}
|
|
642
|
-
/** What a call may say past its arguments. */
|
|
643
|
-
interface CallOptions {
|
|
644
|
-
/** The collation it compares text under, in place of its arguments' declared one. */
|
|
645
|
-
readonly collation: Collation;
|
|
646
|
-
}
|
|
647
|
-
declare const Expr: {
|
|
648
|
-
/**
|
|
649
|
-
* An operator, defined by what a call of it means: `definedAs` builds that
|
|
650
|
-
* expression from the call's arguments, once per call, and every interpreter
|
|
651
|
-
* runs it, so the operator needs no implementation anywhere. What it adds is
|
|
652
|
-
* a name: a query, a guard and an invariant that mean the same thing say so,
|
|
653
|
-
* and print it.
|
|
654
|
-
*
|
|
655
|
-
* ```ts
|
|
656
|
-
* const published = Expr.operator('cms.published', {
|
|
657
|
-
* input: [Schema.Unknown],
|
|
658
|
-
* output: Schema.Boolean,
|
|
659
|
-
* definedAs: publishedAt => Expr.isNotNull(publishedAt),
|
|
660
|
-
* })
|
|
661
|
-
* Query.where(published(Post.fields.publishedAt))
|
|
662
|
-
* ```
|
|
663
|
-
*
|
|
664
|
-
* Arguments are checked by their stored form: a field, scalar or plain value
|
|
665
|
-
* of the input's stored type, or a predicate where it takes a boolean. A
|
|
666
|
-
* boolean output makes a call a predicate. `definedAs` may read only what its
|
|
667
|
-
* arguments hold: a field or input it closes over instead is refused when
|
|
668
|
-
* called, since nothing asking what the call reads would see it. The id is
|
|
669
|
-
* namespaced (`cms.published`); `foldkit.` is reserved; `version` (default 1)
|
|
670
|
-
* changes when what it means does.
|
|
671
|
-
*
|
|
672
|
-
* **Without `definedAs` it is primitive**: each interpreter implements it
|
|
673
|
-
* (`Operators.implement` in memory), and one without an implementation
|
|
674
|
-
* refuses the body by the operator's id. `nulls` (default `strict`: a null
|
|
675
|
-
* argument gives null, the implementation never sees one) and `collation`
|
|
676
|
-
* (`operands`: a text result carries its text arguments' shared collation;
|
|
677
|
-
* `required`: it compares text itself and needs one) are a primitive
|
|
678
|
-
* operator's; a defined one means its body for both.
|
|
679
|
-
*/
|
|
680
|
-
operator: <const In extends ReadonlyArray<Schema.Top>, Out extends Schema.Top>(id: string, options: {
|
|
681
|
-
readonly input: In;
|
|
682
|
-
readonly output: Out;
|
|
683
|
-
readonly version?: number | undefined;
|
|
684
|
-
readonly description?: string | undefined;
|
|
685
|
-
/** Equations every implementation keeps (`Law.commutative`, `Law.identity('')`, …). */
|
|
686
|
-
readonly laws?: ReadonlyArray<Law> | undefined;
|
|
687
|
-
} & ({
|
|
688
|
-
readonly definedAs: (...args: { readonly [K in keyof In]: ParameterOf<In[K]>; }) => BodyOf<Out>;
|
|
689
|
-
readonly nulls?: undefined;
|
|
690
|
-
readonly collation?: undefined;
|
|
691
|
-
} | {
|
|
692
|
-
readonly definedAs?: undefined;
|
|
693
|
-
readonly nulls?: OperatorDescriptor['nulls'] | undefined;
|
|
694
|
-
readonly collation?: OperatorDescriptor['collation'] | undefined;
|
|
695
|
-
})) => Operator<In, Out>;
|
|
696
|
-
/**
|
|
697
|
-
* The predicate or scalar (an order term's, say) with every call of a
|
|
698
|
-
* defined operator replaced by what it means, deeply. An interpreter that
|
|
699
|
-
* compiles a body expands it first, and compiles exactly what was written
|
|
700
|
-
* out. A call with no definition stays, for the interpreter to run or refuse
|
|
701
|
-
* by name.
|
|
702
|
-
*/
|
|
703
|
-
/**
|
|
704
|
-
* Whether a constant written in these expressions is text holding a NUL, or
|
|
705
|
-
* a list holding one: SQL refuses such a parameter wherever it compiles, so
|
|
706
|
-
* an interpreter refuses the run, not one row.
|
|
707
|
-
*/
|
|
708
|
-
givesNul: (...nodes: ReadonlyArray<AnyExpr | Predicate>) => boolean;
|
|
709
|
-
expanded: <N extends Predicate | AnyExpr>(node: N) => N extends Predicate ? Predicate : AnyExpr;
|
|
710
|
-
/**
|
|
711
|
-
* What these expressions need that an interpreter lacks, as `Query.unsupported`
|
|
712
|
-
* says it for a query: each kernel operation `supported` does not list, then
|
|
713
|
-
* each primitive operator `operators` does not implement, by name (`id`, or
|
|
714
|
-
* `id@version` past the first). For anything holding expressions that is not a
|
|
715
|
-
* query: a guard's rule, an invariant, a write's `when`.
|
|
716
|
-
*/
|
|
717
|
-
unsupported: (nodes: ReadonlyArray<AnyExpr | Predicate | Ordering>, interpreter: {
|
|
718
|
-
readonly operations: ReadonlyArray<Operation>;
|
|
719
|
-
readonly operators: OperatorTable;
|
|
720
|
-
}) => ReadonlyArray<string>;
|
|
721
|
-
/** A constant. Comparisons coerce a plain value, so this is rarely written. */
|
|
722
|
-
literal: typeof literal;
|
|
723
|
-
/**
|
|
724
|
-
* A value the query is given when it runs. A definition builds these from its
|
|
725
|
-
* `Input` schema; written by hand only when constructing a body directly.
|
|
726
|
-
*/
|
|
727
|
-
input: <T>(key: string, schema: Schema.Codec<T, unknown>) => InputExpr<T>;
|
|
728
|
-
/**
|
|
729
|
-
* A field as the row a write would leave holds it: what `Entity.invariant`'s
|
|
730
|
-
* builder hands out as `after`.
|
|
731
|
-
*/
|
|
732
|
-
next: <Name extends string, Key extends string, S extends Schema.Constraint>(field: EntityField<Name, Key, S>) => NextExpr<Schema.Schema.Type<S>> & Reads<Name>;
|
|
733
|
-
/** One field of one Entity, as a scalar. */
|
|
734
|
-
field: <Name extends string, Key extends string, S extends Schema.Constraint>(field: EntityField<Name, Key, S>) => FieldExpr<Schema.Schema.Type<S>> & Reads<Name>;
|
|
735
|
-
/**
|
|
736
|
-
* Two scalars are the same value: a field (or an expression) on the left,
|
|
737
|
-
* and the value on the right, typed from it. Fields and plain values are
|
|
738
|
-
* coerced, so the common form reads as it means:
|
|
739
|
-
*
|
|
740
|
-
* ```ts
|
|
741
|
-
* Expr.eq(Post.fields.slug, input.slug)
|
|
742
|
-
* Expr.eq(Post.fields.status, 'active')
|
|
743
|
-
* ```
|
|
744
|
-
*/
|
|
745
|
-
eq: <L extends Operand<any> | PlainValue, R extends (L extends PlainValue ? Invalid<'foldkit-entity: Expr.eq takes the field on the left and the value on the right'> : ValueOf<L> | Expr<ValueOf<L>> | (boolean extends ValueOf<L> ? Predicate : never))>(left: L, right: R) => EqPredicate & Reads<ReadsOf<L> | ReadsOf<R>>;
|
|
746
|
-
/**
|
|
747
|
-
* Whether a field holds no value. Its opposite is `isNotNull`; both are the
|
|
748
|
-
* one node, since a query asks which of the two it means, and `is not null`
|
|
749
|
-
* has no unknown for a `not` to keep.
|
|
750
|
-
*/
|
|
751
|
-
isNull: <L extends Operand<any>>(operand: L) => NullPredicate & Reads<ReadsOf<L>>;
|
|
752
|
-
/** Whether a field holds a value. */
|
|
753
|
-
isNotNull: <L extends Operand<any>>(operand: L) => NullPredicate & Reads<ReadsOf<L>>;
|
|
754
|
-
/**
|
|
755
|
-
* Whether a text value contains `search`, anywhere in it. Containing the
|
|
756
|
-
* empty string is everything, so a search box with nothing typed in it is the
|
|
757
|
-
* same query as one with something — no branch, and no second query.
|
|
758
|
-
*
|
|
759
|
-
* **Case-insensitive**, which is what a search means, and which has to be
|
|
760
|
-
* said rather than left to the interpreter: SQLite's `like` ignores case and
|
|
761
|
-
* Postgres's does not, so a body that left it open would mean two things.
|
|
762
|
-
* Folding is ASCII-only, because that is what `lower` does in SQLite without
|
|
763
|
-
* ICU.
|
|
764
|
-
*
|
|
765
|
-
* **Over a column that can be null this is not the same as no filter.** A
|
|
766
|
-
* null contains nothing, not even the empty string, so its rows drop out. A
|
|
767
|
-
* nullable column that should match everything on an empty search says so:
|
|
768
|
-
* `Expr.or(Expr.isNull(field), Expr.contains(field, input.search))`.
|
|
769
|
-
*/
|
|
770
|
-
contains: <L extends TextOperand>(value: L, search: string | LiteralExpr<string> | InputExpr<string> | InputExpr<string | null>) => ContainsPredicate & Reads<ReadsOf<L>>;
|
|
771
|
-
/**
|
|
772
|
-
* Whether a value is one of `among`: a list written out, or an input whose
|
|
773
|
-
* value is one. Each element is compared as `eq` compares.
|
|
774
|
-
*
|
|
775
|
-
* ```ts
|
|
776
|
-
* Expr.in(Todo.fields.projectId, input.projectIds)
|
|
777
|
-
* Expr.in(Todo.fields.status, ['open', 'blocked'])
|
|
778
|
-
* ```
|
|
779
|
-
*
|
|
780
|
-
* As SQL's `in`: a null value is unknown, as is no match against a list
|
|
781
|
-
* holding a null, and an empty list matches no row. A list written out is
|
|
782
|
-
* copied, so changing the array afterwards does not change the query.
|
|
783
|
-
*/
|
|
784
|
-
in: <L extends Operand<any>>(value: L, among: ReadonlyArray<ValueOf<L>> | Expr<ReadonlyArray<ValueOf<L>>>) => InPredicate & Reads<ReadsOf<L>>;
|
|
785
|
-
/**
|
|
786
|
-
* `left` orders before `right`. Numbers compare as numbers; text compares
|
|
787
|
-
* under a collation, the one given or the field's declared one, which must be
|
|
788
|
-
* `binary` or `asciiFold` (so every backend agrees), and text needs a field on
|
|
789
|
-
* one side. A date compares by its encoded form: as text when it is stored as
|
|
790
|
-
* an ISO string, so its field declares `Collation.binary`. A null on either
|
|
791
|
-
* side is unknown, so the row is matched by neither this nor its `not`.
|
|
792
|
-
*
|
|
793
|
-
* ```ts
|
|
794
|
-
* Expr.lt(fields.cents, 1000)
|
|
795
|
-
* Expr.and(Expr.gte(fields.at, input.from), Expr.lt(fields.at, input.until))
|
|
796
|
-
* ```
|
|
797
|
-
*/
|
|
798
|
-
lt: <L extends OrderedOperand, R extends ValueOf<L> | Expr<ValueOf<L>>>(left: L, right: R, options?: {
|
|
799
|
-
readonly collation?: Collation | undefined;
|
|
800
|
-
}) => ComparePredicate & Reads<ReadsOf<L> | ReadsOf<R>>;
|
|
801
|
-
/** `left` orders before `right`, or with it; see `Expr.lt`. */
|
|
802
|
-
lte: <L extends OrderedOperand, R extends ValueOf<L> | Expr<ValueOf<L>>>(left: L, right: R, options?: {
|
|
803
|
-
readonly collation?: Collation | undefined;
|
|
804
|
-
}) => ComparePredicate & Reads<ReadsOf<L> | ReadsOf<R>>;
|
|
805
|
-
/** `left` orders after `right`; see `Expr.lt`. */
|
|
806
|
-
gt: <L extends OrderedOperand, R extends ValueOf<L> | Expr<ValueOf<L>>>(left: L, right: R, options?: {
|
|
807
|
-
readonly collation?: Collation | undefined;
|
|
808
|
-
}) => ComparePredicate & Reads<ReadsOf<L> | ReadsOf<R>>;
|
|
809
|
-
/** `left` orders after `right`, or with it; see `Expr.lt`. */
|
|
810
|
-
gte: <L extends OrderedOperand, R extends ValueOf<L> | Expr<ValueOf<L>>>(left: L, right: R, options?: {
|
|
811
|
-
readonly collation?: Collation | undefined;
|
|
812
|
-
}) => ComparePredicate & Reads<ReadsOf<L> | ReadsOf<R>>;
|
|
813
|
-
/**
|
|
814
|
-
* Every operand holds, as one predicate to place inside another: `where`'s
|
|
815
|
-
* list is already a conjunction, so this is for `or(and(a, b), c)`.
|
|
816
|
-
* Unknown as SQL's `and` is: false wins over unknown, which wins over true.
|
|
817
|
-
* Needs an operand; a list built at run time is checked non-empty first
|
|
818
|
-
* (`Array.isReadonlyArrayNonEmpty`), since an empty `and` would match every row.
|
|
819
|
-
*/
|
|
820
|
-
and: Connective<AndPredicate>;
|
|
821
|
-
/**
|
|
822
|
-
* At least one operand holds:
|
|
823
|
-
*
|
|
824
|
-
* ```ts
|
|
825
|
-
* Expr.or(Expr.contains(fields.description, input.search), Expr.contains(fields.upc, input.search))
|
|
826
|
-
* ```
|
|
827
|
-
*
|
|
828
|
-
* Unknown as SQL's `or` is: true wins over unknown, which wins over false.
|
|
829
|
-
* Needs an operand, since an empty `or` would match no row.
|
|
830
|
-
*/
|
|
831
|
-
or: Connective<OrPredicate>;
|
|
832
|
-
/**
|
|
833
|
-
* The operand does not hold. **A null is matched by neither side**:
|
|
834
|
-
* `not(eq(status, 'draft'))` leaves out a row whose `status` is null, since
|
|
835
|
-
* `null = 'draft'` is unknown and so is its `not`, as in SQL. "Everything
|
|
836
|
-
* but drafts" over a nullable column is
|
|
837
|
-
* `Expr.or(Expr.isNull(status), Expr.not(Expr.eq(status, 'draft')))`.
|
|
838
|
-
*/
|
|
839
|
-
not: <P extends Predicate>(operand: P) => NotPredicate & Reads<ReadsOf<P>>;
|
|
840
|
-
/**
|
|
841
|
-
* A filter that applies only when its input is given: `or(isNull($x), …)`,
|
|
842
|
-
* with the predicate built from the input as never null.
|
|
843
|
-
*
|
|
844
|
-
* ```ts
|
|
845
|
-
* // Every product, or those of the chosen status.
|
|
846
|
-
* Expr.whenGiven(input.status, status => Expr.eq(fields.status, status))
|
|
847
|
-
* ```
|
|
848
|
-
*
|
|
849
|
-
* An interpreter that runs per request, as the Drizzle compiler does, decides
|
|
850
|
-
* `isNull($x)` before writing SQL, so this compiles to `true`, which filters
|
|
851
|
-
* nothing, or to the predicate alone.
|
|
852
|
-
*/
|
|
853
|
-
whenGiven: <T, P extends Predicate>(input: InputExpr<T>, build: (given: InputExpr<NonNullable<T>>) => P) => Predicate & Reads<ReadsOf<P>>;
|
|
854
|
-
/** An expression as readable text; see `showExpr` for what it is and is not. */
|
|
855
|
-
show: (node: Operandish) => string;
|
|
856
|
-
/**
|
|
857
|
-
* The predicate with each field after a write (`Next`) that `values` names,
|
|
858
|
-
* by key, read as that value: how an invariant is judged where the
|
|
859
|
-
* write's new values are known. A field it does not name stays `Next`, for
|
|
860
|
-
* the interpreter to read as the row after holds it.
|
|
861
|
-
*/
|
|
862
|
-
substituteNext: (predicate: Predicate, values: Readonly<Record<string, unknown>>) => Predicate;
|
|
863
|
-
};
|
|
864
|
-
/** One term of an ordering: a scalar and the direction to read it in. */
|
|
865
|
-
interface OrderTerm {
|
|
866
|
-
readonly _tag: 'Term';
|
|
867
|
-
readonly direction: 'asc' | 'desc';
|
|
868
|
-
readonly expr: AnyExpr;
|
|
869
|
-
/**
|
|
870
|
-
* Where rows without a value go, in the term's own direction. Databases
|
|
871
|
-
* disagree when it is left unsaid (Postgres puts them last ascending, SQLite
|
|
872
|
-
* first), so it is always said: last ascending and first descending unless
|
|
873
|
-
* a term asks otherwise, which is Postgres's.
|
|
874
|
-
*/
|
|
875
|
-
readonly nulls: 'first' | 'last';
|
|
876
|
-
/**
|
|
877
|
-
* How text compares, when the term orders text: the term's own, or its
|
|
878
|
-
* field's. None leaves it to the backend, which a client cannot match.
|
|
879
|
-
*/
|
|
880
|
-
readonly collation: Option.Option<Collation>;
|
|
881
|
-
}
|
|
882
|
-
/** What a term may say besides its operand. */
|
|
883
|
-
interface TermOptions {
|
|
884
|
-
readonly nulls?: 'first' | 'last' | undefined;
|
|
885
|
-
/** Overrides the field's declared collation for this term. */
|
|
886
|
-
readonly collation?: Collation | undefined;
|
|
887
|
-
}
|
|
888
|
-
/** How a list is sorted, as its input holds it: one of its named orders, which way, or none. */
|
|
889
|
-
type ChosenSort<By extends string> = {
|
|
890
|
-
readonly by: By;
|
|
891
|
-
readonly direction: 'asc' | 'desc';
|
|
892
|
-
} | null;
|
|
893
|
-
/**
|
|
894
|
-
* An ordering the input chooses: one of several named fields, which way, or
|
|
895
|
-
* none. It is how a list sorts by what its reader picked, said in the body, so
|
|
896
|
-
* the server and the client read one declaration of what each name means.
|
|
897
|
-
*/
|
|
898
|
-
interface ChosenOrder {
|
|
899
|
-
readonly _tag: 'Chosen';
|
|
900
|
-
readonly sort: InputExpr<ChosenSort<string>>;
|
|
901
|
-
readonly choices: Readonly<Record<string, FieldExpr<unknown> | CallExpr<unknown>>>;
|
|
902
|
-
}
|
|
903
|
-
/** One entry of a body's ordering: fixed, or chosen by the input. */
|
|
904
|
-
type Ordering = OrderTerm | ChosenOrder;
|
|
905
|
-
declare const Order: {
|
|
906
|
-
/** Smallest first; rows without a value last, unless `nulls` says first. */
|
|
907
|
-
asc: <E extends Operand<any>>(expr: E, options?: TermOptions) => OrderTerm & Reads<ReadsOf<E>>;
|
|
908
|
-
/** Largest first; rows without a value first, unless `nulls` says last. */
|
|
909
|
-
desc: <E extends Operand<any>>(expr: E, options?: TermOptions) => OrderTerm & Reads<ReadsOf<E>>;
|
|
910
|
-
/**
|
|
911
|
-
* The order the input names, among `choices`: every name the sort can hold
|
|
912
|
-
* maps to a field or a call that orders, so a name left out is a type error
|
|
913
|
-
* here, not a query that silently orders by nothing. A `null` sort chooses none, and the terms after
|
|
914
|
-
* it decide.
|
|
915
|
-
*
|
|
916
|
-
* ```ts
|
|
917
|
-
* Query.orderBy(Order.chosen(input.sort, { title: Post.fields.title }))
|
|
918
|
-
* ```
|
|
919
|
-
*/
|
|
920
|
-
chosen: <By extends string, const Choices extends { readonly [K in By]: Operand<any>; }>(sort: InputExpr<ChosenSort<By>>, choices: Choices) => ChosenOrder & Reads<ReadsOf<Choices[keyof Choices]>>;
|
|
921
|
-
};
|
|
922
|
-
/**
|
|
923
|
-
* Whether a client can order rows the way the server does, for one input: the
|
|
924
|
-
* answer `Query.placement` gives, and the reason when it cannot.
|
|
925
|
-
*/
|
|
926
|
-
type Placement = {
|
|
927
|
-
readonly _tag: 'Placeable';
|
|
928
|
-
} | {
|
|
929
|
-
readonly _tag: 'NotPlaceable';
|
|
930
|
-
readonly reason: string;
|
|
931
|
-
};
|
|
932
|
-
/**
|
|
933
|
-
* What an expression reads and uses, for the planner and for an interpreter
|
|
934
|
-
* deciding whether it can run this query at all. Distinct entries only, in the
|
|
935
|
-
* order first seen, so the answer is stable enough to assert on.
|
|
936
|
-
*/
|
|
937
|
-
declare const dependenciesOf: (...nodes: ReadonlyArray<AnyExpr | Predicate | Ordering>) => Dependencies;
|
|
938
|
-
/**
|
|
939
|
-
* Which rows a query is about, as a value: an Entity to read, the predicates
|
|
940
|
-
* every row must satisfy, and the order to read them in. It says nothing about
|
|
941
|
-
* which fields to return — that is a Selection — and nothing about pagination,
|
|
942
|
-
* liveness, or whether absence is an error, which belong to the consumer's read
|
|
943
|
-
* rather than to the relation.
|
|
944
|
-
*
|
|
945
|
-
* Composing one performs no work. `Query.from(Post)` names no table and opens
|
|
946
|
-
* no connection; an interpreter turns the value into a query when something
|
|
947
|
-
* asks it to.
|
|
948
|
-
*/
|
|
949
|
-
interface Query<E extends AnyEntity> extends Pipeable.Pipeable {
|
|
950
|
-
readonly _tag: 'Query';
|
|
951
|
-
readonly entity: E;
|
|
952
|
-
/**
|
|
953
|
-
* Every predicate holds: the list *is* the conjunction. Keeping it a list
|
|
954
|
-
* rather than folding it into one `and` is what makes each `where` a reusable
|
|
955
|
-
* fragment, and it means no `Expr.and` exists until a query needs a
|
|
956
|
-
* conjunction nested inside something else.
|
|
957
|
-
*/
|
|
958
|
-
readonly where: ReadonlyArray<Predicate>;
|
|
959
|
-
/** In order of significance; a later `orderBy` appends less significant terms. */
|
|
960
|
-
readonly orderBy: ReadonlyArray<Ordering>;
|
|
961
|
-
}
|
|
962
|
-
/** A `Query` over any Entity, for the places that hold one without caring which. */
|
|
963
|
-
type AnyQuery = Query<AnyEntity>;
|
|
964
|
-
/**
|
|
965
|
-
* The most nodes a query's predicates may hold once every shared node is
|
|
966
|
-
* counted on each path that reaches it. Interpreters read a predicate as a
|
|
967
|
-
* tree: `evaluate` once per row, and the SQL compiler into text that repeats a
|
|
968
|
-
* shared operand wherever it appears. So `eq(n, n)` nested thirty deep is
|
|
969
|
-
* thirty-one objects and a billion nodes of work. Real queries are tens.
|
|
970
|
-
*/
|
|
971
|
-
declare const maxQueryNodes = 1000;
|
|
972
|
-
declare const Query: {
|
|
973
|
-
/** Every row of an Entity: the query each transformation narrows. */
|
|
974
|
-
from: <E extends AnyEntity>(entity: E) => Query<E>;
|
|
975
|
-
/**
|
|
976
|
-
* Narrows to the rows the predicate holds for. Two `where`s conjoin — the
|
|
977
|
-
* second never replaces the first — so a fragment can be written once and
|
|
978
|
-
* piped into any query over the same Entity:
|
|
979
|
-
*
|
|
980
|
-
* ```ts
|
|
981
|
-
* const published = Query.where(Expr.eq(Post.fields.published, true))
|
|
982
|
-
* Query.from(Post).pipe(published, Query.where(byTitle))
|
|
983
|
-
* ```
|
|
984
|
-
*/
|
|
985
|
-
where: <const P extends ReadonlyArray<Predicate>>(...predicates: P) => <E extends AnyEntity>(self: Query<E> & OverEntity<ReadsOf<P[number]>, E['name'], 'a predicate'>) => Query<E>;
|
|
986
|
-
/**
|
|
987
|
-
* Reads the rows in this order. Two `orderBy`s append, so an earlier term
|
|
988
|
-
* stays the more significant one and a later call adds a tie-breaker rather
|
|
989
|
-
* than silently winning.
|
|
990
|
-
*/
|
|
991
|
-
orderBy: <const T extends ReadonlyArray<Ordering>>(...terms: T) => <E extends AnyEntity>(self: Query<E> & OverEntity<ReadsOf<T[number]>, E['name'], 'an ordering term'>) => Query<E>;
|
|
992
|
-
/**
|
|
993
|
-
* The fixed terms this query orders by for one input: each chosen order
|
|
994
|
-
* resolved to the field its input names, or to nothing for a `null` sort,
|
|
995
|
-
* and the Entity's id last, ascending, unless the terms already read it. So
|
|
996
|
-
* the order is total, and every interpreter breaks ties the same way: one
|
|
997
|
-
* that broke them its own way would place rows a step apart from another.
|
|
998
|
-
*/
|
|
999
|
-
orderFor: (self: AnyQuery, input: Readonly<Record<string, unknown>>) => ReadonlyArray<OrderTerm>;
|
|
1000
|
-
/**
|
|
1001
|
-
* Whether a client can order this query's rows exactly as the server does,
|
|
1002
|
-
* for one input: every term over a number or a boolean (a row may lack it), or over text with a
|
|
1003
|
-
* portable collation (`binary`, `asciiFold`), or a call whose primitive
|
|
1004
|
-
* operators the client's table (`operators`; none when not given) runs. A
|
|
1005
|
-
* body that names no order leaves it to the server, and is never placeable.
|
|
1006
|
-
*/
|
|
1007
|
-
placement: (self: AnyQuery, input: Readonly<Record<string, unknown>>, options?: {
|
|
1008
|
-
readonly operators?: OperatorTable | undefined;
|
|
1009
|
-
}) => Placement;
|
|
1010
|
-
/**
|
|
1011
|
-
* Each operator the body calls, by name (`id`, or `id@version` past the
|
|
1012
|
-
* first), in the order it first calls them: a defined one, and each
|
|
1013
|
-
* primitive one its expansion calls.
|
|
1014
|
-
*/
|
|
1015
|
-
operators: (self: AnyQuery) => ReadonlyArray<string>;
|
|
1016
|
-
/**
|
|
1017
|
-
* What the whole query reads: the fields and inputs of every predicate and
|
|
1018
|
-
* every ordering term, and the operations it uses, with the fields also
|
|
1019
|
-
* split by role.
|
|
1020
|
-
*/
|
|
1021
|
-
dependencies: (self: AnyQuery) => QueryDependencies;
|
|
1022
|
-
/**
|
|
1023
|
-
* The operations this query needs that `supported` does not list, in the
|
|
1024
|
-
* order the query uses them, then each primitive operator `operators` does
|
|
1025
|
-
* not implement, by name. Without `operators`, every primitive operator is
|
|
1026
|
-
* named: a list of operations implements none. Empty means an interpreter
|
|
1027
|
-
* declaring both can answer it.
|
|
1028
|
-
*
|
|
1029
|
-
* An interpreter declares what it runs and refuses the rest, rather than
|
|
1030
|
-
* ignoring an operation it does not implement — which would answer a
|
|
1031
|
-
* different question and say nothing. The refusal is the interpreter's to
|
|
1032
|
-
* raise: this only says what is missing, because what to do about it differs
|
|
1033
|
-
* between one that compiles at registration and one that runs a body
|
|
1034
|
-
* directly.
|
|
1035
|
-
*
|
|
1036
|
-
* ```ts
|
|
1037
|
-
* const missing = Query.unsupported(body, ['eq', 'isNull', 'isNotNull'])
|
|
1038
|
-
* if (missing.length > 0) throw new Error(`cannot run ${missing.join(', ')}`)
|
|
1039
|
-
* ```
|
|
1040
|
-
*/
|
|
1041
|
-
unsupported: (self: AnyQuery, supported: ReadonlyArray<Operation>, operators?: OperatorTable) => ReadonlyArray<string>;
|
|
1042
|
-
/**
|
|
1043
|
-
* The whole query as readable text, one clause per line:
|
|
1044
|
-
*
|
|
1045
|
-
* ```text
|
|
1046
|
-
* FROM Post
|
|
1047
|
-
* WHERE Post.slug = $slug
|
|
1048
|
-
* AND Post.published = true
|
|
1049
|
-
* ORDER BY Post.updatedAt DESC
|
|
1050
|
-
* ```
|
|
1051
|
-
*
|
|
1052
|
-
* For an explanation or a diagnostic, never for an interpreter — see
|
|
1053
|
-
* `Expr.show`. A query with no `where` or no `orderBy` omits that line rather
|
|
1054
|
-
* than printing an empty one, so what is shown is what was written.
|
|
1055
|
-
*/
|
|
1056
|
-
show: (self: AnyQuery) => string;
|
|
1057
|
-
};
|
|
1058
|
-
declare const testedByRunner: (record: Evidence) => boolean;
|
|
1059
|
-
/** What an implementation is told besides its arguments. */
|
|
1060
|
-
interface OperatorContext {
|
|
1061
|
-
/** The collation the call compares text under, from its operator's `collation` rule. */
|
|
1062
|
-
readonly collation: Option.Option<Collation>;
|
|
1063
|
-
}
|
|
1064
|
-
/** One operator's in-memory implementation: a pure function of stored values. */
|
|
1065
|
-
interface MemoryImplementation {
|
|
1066
|
-
readonly operator: OperatorDescriptor;
|
|
1067
|
-
readonly run: (args: ReadonlyArray<unknown>, context: OperatorContext) => unknown;
|
|
1068
|
-
}
|
|
1069
|
-
/**
|
|
1070
|
-
* What is known of a law of an implementation, past its operator claiming it
|
|
1071
|
-
* (expr-operations-DESIGN §10.3): `Tested`, it passed `Conformance.laws`'
|
|
1072
|
-
* cases; `Proven`, a proof elsewhere says so under its assumptions, recorded
|
|
1073
|
-
* and never checked here.
|
|
1074
|
-
*/
|
|
1075
|
-
type Evidence = Data.TaggedEnum<{
|
|
1076
|
-
Tested: {
|
|
1077
|
-
readonly operator: string;
|
|
1078
|
-
readonly law: LawDescription;
|
|
1079
|
-
readonly implementation: string;
|
|
1080
|
-
readonly cases: number;
|
|
1081
|
-
readonly seed: number;
|
|
1082
|
-
};
|
|
1083
|
-
Proven: {
|
|
1084
|
-
readonly operator: string;
|
|
1085
|
-
readonly law: LawDescription;
|
|
1086
|
-
readonly reference: string;
|
|
1087
|
-
readonly assumptions: ReadonlyArray<string>;
|
|
1088
|
-
};
|
|
1089
|
-
}>;
|
|
1090
|
-
declare const Evidence: {
|
|
1091
|
-
readonly $is: <Tag extends "Proven" | "Tested">(tag: Tag) => (u: unknown) => u is Extract<{
|
|
1092
|
-
readonly _tag: "Proven";
|
|
1093
|
-
readonly operator: string;
|
|
1094
|
-
readonly law: LawDescription;
|
|
1095
|
-
readonly reference: string;
|
|
1096
|
-
readonly assumptions: ReadonlyArray<string>;
|
|
1097
|
-
}, {
|
|
1098
|
-
readonly _tag: Tag;
|
|
1099
|
-
}> | Extract<{
|
|
1100
|
-
readonly _tag: "Tested";
|
|
1101
|
-
readonly operator: string;
|
|
1102
|
-
readonly law: LawDescription;
|
|
1103
|
-
readonly implementation: string;
|
|
1104
|
-
readonly cases: number;
|
|
1105
|
-
readonly seed: number;
|
|
1106
|
-
}, {
|
|
1107
|
-
readonly _tag: Tag;
|
|
1108
|
-
}>;
|
|
1109
|
-
readonly $match: {
|
|
1110
|
-
<Cases extends { readonly [Tag in "Proven" | "Tested"]: (args: Extract<{
|
|
1111
|
-
readonly _tag: "Proven";
|
|
1112
|
-
readonly operator: string;
|
|
1113
|
-
readonly law: LawDescription;
|
|
1114
|
-
readonly reference: string;
|
|
1115
|
-
readonly assumptions: ReadonlyArray<string>;
|
|
1116
|
-
} | {
|
|
1117
|
-
readonly _tag: "Tested";
|
|
1118
|
-
readonly operator: string;
|
|
1119
|
-
readonly law: LawDescription;
|
|
1120
|
-
readonly implementation: string;
|
|
1121
|
-
readonly cases: number;
|
|
1122
|
-
readonly seed: number;
|
|
1123
|
-
}, {
|
|
1124
|
-
readonly _tag: Tag;
|
|
1125
|
-
}>) => any; }>(cases: Cases): (value: {
|
|
1126
|
-
readonly _tag: "Proven";
|
|
1127
|
-
readonly operator: string;
|
|
1128
|
-
readonly law: LawDescription;
|
|
1129
|
-
readonly reference: string;
|
|
1130
|
-
readonly assumptions: ReadonlyArray<string>;
|
|
1131
|
-
} | {
|
|
1132
|
-
readonly _tag: "Tested";
|
|
1133
|
-
readonly operator: string;
|
|
1134
|
-
readonly law: LawDescription;
|
|
1135
|
-
readonly implementation: string;
|
|
1136
|
-
readonly cases: number;
|
|
1137
|
-
readonly seed: number;
|
|
1138
|
-
}) => import("effect/Unify").Unify<ReturnType<Cases["Proven" | "Tested"]>>;
|
|
1139
|
-
<Cases extends { readonly [Tag in "Proven" | "Tested"]: (args: Extract<{
|
|
1140
|
-
readonly _tag: "Proven";
|
|
1141
|
-
readonly operator: string;
|
|
1142
|
-
readonly law: LawDescription;
|
|
1143
|
-
readonly reference: string;
|
|
1144
|
-
readonly assumptions: ReadonlyArray<string>;
|
|
1145
|
-
} | {
|
|
1146
|
-
readonly _tag: "Tested";
|
|
1147
|
-
readonly operator: string;
|
|
1148
|
-
readonly law: LawDescription;
|
|
1149
|
-
readonly implementation: string;
|
|
1150
|
-
readonly cases: number;
|
|
1151
|
-
readonly seed: number;
|
|
1152
|
-
}, {
|
|
1153
|
-
readonly _tag: Tag;
|
|
1154
|
-
}>) => any; }>(value: {
|
|
1155
|
-
readonly _tag: "Proven";
|
|
1156
|
-
readonly operator: string;
|
|
1157
|
-
readonly law: LawDescription;
|
|
1158
|
-
readonly reference: string;
|
|
1159
|
-
readonly assumptions: ReadonlyArray<string>;
|
|
1160
|
-
} | {
|
|
1161
|
-
readonly _tag: "Tested";
|
|
1162
|
-
readonly operator: string;
|
|
1163
|
-
readonly law: LawDescription;
|
|
1164
|
-
readonly implementation: string;
|
|
1165
|
-
readonly cases: number;
|
|
1166
|
-
readonly seed: number;
|
|
1167
|
-
}, cases: Cases): import("effect/Unify").Unify<ReturnType<Cases["Proven" | "Tested"]>>;
|
|
1168
|
-
};
|
|
1169
|
-
readonly Proven: Data.TaggedEnum.ConstructorFrom<{
|
|
1170
|
-
readonly _tag: "Proven";
|
|
1171
|
-
readonly operator: string;
|
|
1172
|
-
readonly law: LawDescription;
|
|
1173
|
-
readonly reference: string;
|
|
1174
|
-
readonly assumptions: ReadonlyArray<string>;
|
|
1175
|
-
}, "_tag">;
|
|
1176
|
-
readonly Tested: Data.TaggedEnum.ConstructorFrom<{
|
|
1177
|
-
readonly _tag: "Tested";
|
|
1178
|
-
readonly operator: string;
|
|
1179
|
-
readonly law: LawDescription;
|
|
1180
|
-
readonly implementation: string;
|
|
1181
|
-
readonly cases: number;
|
|
1182
|
-
readonly seed: number;
|
|
1183
|
-
}, "_tag">;
|
|
1184
|
-
};
|
|
1185
|
-
/** A record of a law passing `Conformance.laws`' cases. */
|
|
1186
|
-
type TestedEvidence = Extract<Evidence, {
|
|
1187
|
-
readonly _tag: 'Tested';
|
|
1188
|
-
}>;
|
|
1189
|
-
/**
|
|
1190
|
-
* An operator as JSON, by its signature and never its body (a defined
|
|
1191
|
-
* operator says `defined`, not what it means): what a server can publish of
|
|
1192
|
-
* what a body may call, and a client compare with what it runs. Its schemas
|
|
1193
|
-
* are each argument's and the result's stored side, as JSON Schemas.
|
|
1194
|
-
*/
|
|
1195
|
-
declare const OperatorDescription: Schema.Struct<{
|
|
1196
|
-
/** By id, and `@version` past the first: what tables, evidence and refusals name. */
|
|
1197
|
-
readonly name: Schema.String;
|
|
1198
|
-
readonly id: Schema.String;
|
|
1199
|
-
readonly version: Schema.Int;
|
|
1200
|
-
readonly description: Schema.NullOr<Schema.String>;
|
|
1201
|
-
readonly input: Schema.$Array<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
1202
|
-
readonly output: Schema.$Record<Schema.String, Schema.Unknown>;
|
|
1203
|
-
readonly nulls: Schema.Literals<readonly ["strict", "handled"]>;
|
|
1204
|
-
readonly collation: Schema.Literals<readonly ["none", "operands", "required"]>;
|
|
1205
|
-
readonly laws: Schema.$Array<Schema.Struct<{
|
|
1206
|
-
readonly kind: Schema.String;
|
|
1207
|
-
readonly witnesses: Schema.$Record<Schema.String, Schema.Unknown>;
|
|
1208
|
-
readonly domain: Schema.Unknown;
|
|
1209
|
-
}>>;
|
|
1210
|
-
readonly defined: Schema.Boolean;
|
|
1211
|
-
}>;
|
|
1212
|
-
type OperatorDescription = typeof OperatorDescription.Type;
|
|
1213
|
-
/** One operator a table implements, as `Operators.describeTable` lists it: described, with its evidence. */
|
|
1214
|
-
interface ImplementedOperator extends OperatorDescription {
|
|
1215
|
-
readonly evidence: ReadonlyArray<Evidence>;
|
|
1216
|
-
}
|
|
1217
|
-
/** The in-memory interpreter's implementations, by operator. */
|
|
1218
|
-
interface MemoryOperators extends OperatorTable {
|
|
1219
|
-
readonly _tag: 'MemoryOperators';
|
|
1220
|
-
readonly get: (operator: OperatorDescriptor) => Option.Option<MemoryImplementation['run']>;
|
|
1221
|
-
/**
|
|
1222
|
-
* The operator it implements under a name (`id`, or `id@version` past the
|
|
1223
|
-
* first): how a computed assignment carried as data finds what to run.
|
|
1224
|
-
*/
|
|
1225
|
-
readonly named: (name: string) => Option.Option<OperatorDescriptor>;
|
|
1226
|
-
/** Whether each answer is checked against its operator's output schema: for tests. */
|
|
1227
|
-
readonly checked: boolean;
|
|
1228
|
-
readonly implementations: ReadonlyArray<MemoryImplementation>;
|
|
1229
|
-
/** What is known of its implementations' laws: `Operators.withEvidence`. */
|
|
1230
|
-
readonly evidence: ReadonlyArray<Evidence>;
|
|
1231
|
-
}
|
|
1232
|
-
type StoredOf<S extends Schema.Top> = Schema.Codec.Encoded<S>;
|
|
1233
|
-
declare const Operators: {
|
|
1234
|
-
/**
|
|
1235
|
-
* An implementation of a strict operator: a pure function of its arguments'
|
|
1236
|
-
* stored values, never given a null (the kernel answers null, or unknown, for
|
|
1237
|
-
* one first), and told the call's collation last.
|
|
1238
|
-
*
|
|
1239
|
-
* ```ts
|
|
1240
|
-
* Operators.implement(startsWith, (text, prefix, { collation }) => text.startsWith(prefix))
|
|
1241
|
-
* ```
|
|
1242
|
-
*/
|
|
1243
|
-
implement: <In extends ReadonlyArray<Schema.Top>, Out extends Schema.Top>(operator: Operator<In, Out>, run: (...args: [...{ readonly [K in keyof In]: NonNullable<StoredOf<In[K]>>; }, OperatorContext]) => StoredOf<Out>) => MemoryImplementation;
|
|
1244
|
-
/**
|
|
1245
|
-
* An implementation of an operator declared `nulls: 'handled'`: given nulls
|
|
1246
|
-
* as they are, it answers for them; a predicate answers `null` for unknown.
|
|
1247
|
-
*/
|
|
1248
|
-
implementHandlingNulls: <In extends ReadonlyArray<Schema.Top>, Out extends Schema.Top>(operator: Operator<In, Out>, run: (...args: [...{ readonly [K in keyof In]: StoredOf<In[K]>; }, OperatorContext]) => StoredOf<Out> | null) => MemoryImplementation;
|
|
1249
|
-
/** A table of implementations. One operator implemented twice, or two under one name, is refused. */
|
|
1250
|
-
memory: (...implementations: ReadonlyArray<MemoryImplementation>) => MemoryOperators;
|
|
1251
|
-
/** Both tables' implementations. An operator both implement is refused: say which wins with `override`. */
|
|
1252
|
-
merge: (left: MemoryOperators, right: MemoryOperators) => MemoryOperators;
|
|
1253
|
-
/**
|
|
1254
|
-
* The table with these implementations in place of its own for the same
|
|
1255
|
-
* operators. Evidence of a replaced implementation goes with it: it was
|
|
1256
|
-
* that implementation's.
|
|
1257
|
-
*/
|
|
1258
|
-
override: (base: MemoryOperators, ...implementations: ReadonlyArray<MemoryImplementation>) => MemoryOperators;
|
|
1259
|
-
/**
|
|
1260
|
-
* The table with what is known of its implementations' laws recorded: each
|
|
1261
|
-
* record of an operator it implements and a law that operator declares.
|
|
1262
|
-
* `Conformance.laws` gives `Tested` records, each of the implementation it
|
|
1263
|
-
* ran, and a table takes only those of its own; `Evidence.Proven` says a
|
|
1264
|
-
* proof elsewhere covers one.
|
|
1265
|
-
*/
|
|
1266
|
-
withEvidence: (base: MemoryOperators, evidence: ReadonlyArray<Evidence>) => MemoryOperators;
|
|
1267
|
-
/**
|
|
1268
|
-
* The memory implementation `Conformance.laws` tested a record on; none for
|
|
1269
|
-
* a record of a runner (`Conformance.lawsBy`) or a proof.
|
|
1270
|
-
*/
|
|
1271
|
-
testedOn: (record: Evidence) => Option.Option<MemoryImplementation>;
|
|
1272
|
-
/**
|
|
1273
|
-
* Whether `Conformance.lawsBy` made a record, through a runner: what an SQL
|
|
1274
|
-
* table takes. Which runner it was, the database's or another, is the
|
|
1275
|
-
* caller's word; a record built by hand, copied, or tested in memory is not.
|
|
1276
|
-
*/
|
|
1277
|
-
testedByRunner: typeof testedByRunner;
|
|
1278
|
-
/**
|
|
1279
|
-
* Each operator the table implements, by name, with the laws it declares and
|
|
1280
|
-
* the evidence the table records of them: what a server can publish of how
|
|
1281
|
-
* it runs a body. JSON as it is.
|
|
1282
|
-
*/
|
|
1283
|
-
describeTable: (base: MemoryOperators) => ReadonlyArray<ImplementedOperator>;
|
|
1284
|
-
/**
|
|
1285
|
-
* An operator as JSON (`OperatorDescription`): its name, version and
|
|
1286
|
-
* description, each argument's and its result's stored side as a JSON
|
|
1287
|
-
* Schema (as JSON carries it: a number also admits the strings `"Infinity"`,
|
|
1288
|
-
* `"-Infinity"` and `"NaN"`), its null and collation rules, its laws, and
|
|
1289
|
-
* whether it is defined. Its signature, never its body.
|
|
1290
|
-
*/
|
|
1291
|
-
describe: (operator: {
|
|
1292
|
-
readonly descriptor: OperatorDescriptor;
|
|
1293
|
-
}) => OperatorDescription;
|
|
1294
|
-
/**
|
|
1295
|
-
* The table, checking every answer against its operator's output schema, so
|
|
1296
|
-
* an implementation that answers the wrong kind fails where it answered. For
|
|
1297
|
-
* tests: it costs a decode per call.
|
|
1298
|
-
*/
|
|
1299
|
-
checked: (base: MemoryOperators) => MemoryOperators;
|
|
1300
|
-
/** The kernel's own operators (`Text.startsWith`): every memory interpreter's default. */
|
|
1301
|
-
kernel: MemoryOperators;
|
|
1302
|
-
};
|
|
1303
|
-
/** A row as this interpreter reads one: values by field key. */
|
|
1304
|
-
type Row = Readonly<Record<string, unknown>>;
|
|
1305
|
-
declare class QueryEvaluateError extends Error {
|
|
1306
|
-
constructor(message: string);
|
|
1307
|
-
}
|
|
1308
|
-
/**
|
|
1309
|
-
* A call of a primitive operator the table does not implement. Its own class,
|
|
1310
|
-
* because a form judging an invariant reads it as a judgement it cannot make
|
|
1311
|
-
* (`Undecided`), not as a failure.
|
|
1312
|
-
*/
|
|
1313
|
-
declare class OperatorMissingError extends QueryEvaluateError {
|
|
1314
|
-
readonly operator: OperatorDescriptor;
|
|
1315
|
-
/** The operator by its id, and its version past the first. */
|
|
1316
|
-
readonly named: string;
|
|
1317
|
-
/** `operators`, the table that lacks it, names the versions of it that it does hold. */
|
|
1318
|
-
constructor(operator: OperatorDescriptor, operators?: OperatorTable);
|
|
1319
|
-
}
|
|
1320
|
-
/**
|
|
1321
|
-
* A primitive operator over stored values, as a call of it is run: what a
|
|
1322
|
-
* computed assignment comes to (`Write.apply`). Refused, as a call is, when
|
|
1323
|
-
* `operators` does not implement it.
|
|
1324
|
-
*/
|
|
1325
|
-
declare const applyOperator: (operator: OperatorDescriptor, values: ReadonlyArray<unknown>, options: {
|
|
1326
|
-
readonly collation: Option.Option<Collation>;
|
|
1327
|
-
readonly operators: MemoryOperators;
|
|
1328
|
-
}) => unknown;
|
|
1329
|
-
/** SQL's unknown: neither true nor false, and a row is kept only on true. */
|
|
1330
|
-
type Truth = boolean | 'unknown';
|
|
1331
|
-
/**
|
|
1332
|
-
* What an operand comes to over no row: a call of literals and inputs, run by
|
|
1333
|
-
* `operators` (the kernel's when not given), a predicate as its truth with
|
|
1334
|
-
* unknown as null. How a conformance check asks an implementation one
|
|
1335
|
-
* question; refuses what `evaluate` refuses (an operator the table lacks,
|
|
1336
|
-
* text holding NUL).
|
|
1337
|
-
*/
|
|
1338
|
-
declare const valueOfOperand: (node: Operandish, options?: {
|
|
1339
|
-
readonly input?: Row | undefined;
|
|
1340
|
-
readonly operators?: MemoryOperators | undefined;
|
|
1341
|
-
}) => unknown;
|
|
1342
|
-
/** What a predicate comes to: SQL's three values, of which a row is kept only on `true`. */
|
|
1343
|
-
type PredicateTruth = Truth;
|
|
1344
|
-
/**
|
|
1345
|
-
* A predicate's truth from the input alone, when it reads no field; none when
|
|
1346
|
-
* it reads one. An interpreter that runs per request decides such a predicate
|
|
1347
|
-
* before it builds anything: `isNull($status)` is known before any row is.
|
|
1348
|
-
* Refuses what `evaluate` refuses (text holding a NUL).
|
|
1349
|
-
*/
|
|
1350
|
-
declare const decided: (node: Predicate, input: Row, operators?: MemoryOperators) => Option.Option<PredicateTruth>;
|
|
1351
|
-
/** A predicate's truth over one row (and, for an invariant, the row after a write): SQL's three values. */
|
|
1352
|
-
declare const truthOver: (node: Predicate, row: Row, input?: Row, next?: Row, operators?: MemoryOperators) => PredicateTruth;
|
|
1353
|
-
/**
|
|
1354
|
-
* Where `left` sorts against `right` under these terms (`Query.orderFor`'s),
|
|
1355
|
-
* as `evaluate` sorts: negative when it comes first. A call among them runs
|
|
1356
|
-
* over each row with `input` and `operators` (the kernel's when not given).
|
|
1357
|
-
* Refuses what `evaluate` refuses, for these two rows: a term over neither a
|
|
1358
|
-
* field nor a call, a locale collation, keys of different kinds, or an
|
|
1359
|
-
* operator the table lacks. For a client placing one row among others it
|
|
1360
|
-
* holds.
|
|
1361
|
-
*/
|
|
1362
|
-
declare const compareRows: (terms: ReadonlyArray<OrderTerm>, left: Row, right: Row, query: string, options?: {
|
|
1363
|
-
readonly input?: Row | undefined;
|
|
1364
|
-
readonly operators?: MemoryOperators | undefined;
|
|
1365
|
-
}) => number;
|
|
1366
|
-
/**
|
|
1367
|
-
* The operations this interpreter runs. It is the reference, so this is the
|
|
1368
|
-
* whole kernel — but it is declared rather than assumed, because an
|
|
1369
|
-
* interpreter that gains an operator and forgets to say so is exactly what
|
|
1370
|
-
* `Query.unsupported` exists to catch.
|
|
1371
|
-
*/
|
|
1372
|
-
declare const supported: ReadonlyArray<Operation>;
|
|
1373
|
-
/**
|
|
1374
|
-
* Refuses a body needing an operation this interpreter does not run, rather
|
|
1375
|
-
* than skipping it and answering a different question. Called by `evaluate`;
|
|
1376
|
-
* call it directly to check a body once instead of on every run.
|
|
1377
|
-
*/
|
|
1378
|
-
declare const assertSupported: (body: AnyQuery, operators?: MemoryOperators) => void;
|
|
1379
|
-
/**
|
|
1380
|
-
* The rows the body matches, in the order it asks for. Pure: it reads the rows
|
|
1381
|
-
* it is given and nothing else.
|
|
1382
|
-
*
|
|
1383
|
-
* `input` holds the values this run was given, by the keys the body's
|
|
1384
|
-
* placeholders were built with — the same record the query would be called
|
|
1385
|
-
* with anywhere else.
|
|
1386
|
-
*/
|
|
1387
|
-
declare const evaluate: (body: AnyQuery, input: Row, rows: ReadonlyArray<Row>, options?: {
|
|
1388
|
-
readonly operators?: MemoryOperators | undefined;
|
|
1389
|
-
}) => ReadonlyArray<Row>;
|
|
1390
|
-
/** What `evaluateEach` made of each row: those it matched, in order, and those it refused. */
|
|
1391
|
-
interface Evaluated {
|
|
1392
|
-
readonly matched: ReadonlyArray<Row>;
|
|
1393
|
-
readonly refused: ReadonlyArray<Row>;
|
|
1394
|
-
}
|
|
1395
|
-
/**
|
|
1396
|
-
* As `evaluate`, with each row judged alone: a row whose own values are
|
|
1397
|
-
* refused (text holding a NUL, a value an operator fails on) is set aside in
|
|
1398
|
-
* `refused` and the others are still answered. A client judging the rows it
|
|
1399
|
-
* holds wants this, since one bad stored row says nothing about the rest.
|
|
1400
|
-
*
|
|
1401
|
-
* What no single row causes still refuses the run: an operation or operator
|
|
1402
|
-
* the table lacks, an order by a locale collation, an input the body reads
|
|
1403
|
-
* holding a NUL (every row would refuse, and no row is the cause), and order
|
|
1404
|
-
* keys of different kinds across rows.
|
|
1405
|
-
*/
|
|
1406
|
-
declare const evaluateEach: (body: AnyQuery, input: Row, rows: ReadonlyArray<Row>, options?: {
|
|
1407
|
-
readonly operators?: MemoryOperators | undefined;
|
|
1408
|
-
}) => Evaluated;
|
|
1409
|
-
/** One invariant of an Entity, as its interpreters read it. */
|
|
1410
|
-
interface Invariant {
|
|
1411
|
-
/** Stable, unique within its Entity: what a refusal names, and forms and logs key on. */
|
|
1412
|
-
readonly id: string;
|
|
1413
|
-
readonly predicate: Predicate;
|
|
1414
|
-
/** What someone the rules let write is told when a write would break it. */
|
|
1415
|
-
readonly message: string;
|
|
1416
|
-
/** The fields a form shows the failure beside. */
|
|
1417
|
-
readonly path: ReadonlyArray<string>;
|
|
1418
|
-
/** The fields it reads of the row before a write; none for a state invariant. */
|
|
1419
|
-
readonly before: ReadonlyArray<string>;
|
|
1420
|
-
/** The fields it reads of the row after. */
|
|
1421
|
-
readonly after: ReadonlyArray<string>;
|
|
1422
|
-
}
|
|
1423
|
-
/** Each field as the row before a write holds it: what an invariant reads as `before`. */
|
|
1424
|
-
type BeforeRow<E extends AnyEntity> = { readonly [K in keyof E['fields'] & string]: FieldExpr<Schema.Schema.Type<E['fields'][K]['schema']>> & Reads<E['name']>; };
|
|
1425
|
-
/** Each field as the row after a write holds it: what an invariant reads as `after`. */
|
|
1426
|
-
type AfterRow<E extends AnyEntity> = { readonly [K in keyof E['fields'] & string]: NextExpr<Schema.Schema.Type<E['fields'][K]['schema']>> & Reads<E['name']>; };
|
|
1427
|
-
/** A row, or a change from one row to another, as an invariant is judged over it. */
|
|
1428
|
-
interface Change {
|
|
1429
|
-
readonly after: Row;
|
|
1430
|
-
/** The row before an update; none for a create, which judges no transition invariant. */
|
|
1431
|
-
readonly before?: Row;
|
|
1432
|
-
}
|
|
1433
|
-
/** What a row, or a change, comes to under an Entity's invariants. */
|
|
1434
|
-
type Judgement = {
|
|
1435
|
-
readonly _tag: 'Valid';
|
|
1436
|
-
} | {
|
|
1437
|
-
readonly _tag: 'Invalid';
|
|
1438
|
-
readonly failures: ReadonlyArray<Invariant>;
|
|
1439
|
-
} |
|
|
1440
|
-
/**
|
|
1441
|
-
* Validity is not known: some invariant reads a field the row does not have
|
|
1442
|
-
* (`fields`), or calls an operator the table does not run (`operators`, by name).
|
|
1443
|
-
*/
|
|
1444
|
-
{
|
|
1445
|
-
readonly _tag: 'Undecided';
|
|
1446
|
-
readonly fields: ReadonlyArray<string>;
|
|
1447
|
-
readonly operators: ReadonlyArray<string>;
|
|
1448
|
-
};
|
|
1449
|
-
/** Whether an invariant reads the row before, and so is judged only where there is one. */
|
|
1450
|
-
declare const readsBefore: (held: Invariant) => boolean;
|
|
1451
|
-
/**
|
|
1452
|
-
* An invariant as declared: the step that attaches it to its Entity, carrying
|
|
1453
|
-
* its id, so a refusal is matched against the declaration rather than a
|
|
1454
|
-
* string spelled again (`Refusal.isInvalidBy(refusal, reservedWithinQuantity)`).
|
|
1455
|
-
*/
|
|
1456
|
-
interface DeclaredInvariant<E extends AnyEntity> {
|
|
1457
|
-
(entity: E): E;
|
|
1458
|
-
readonly id: string;
|
|
1459
|
-
}
|
|
1460
|
-
declare const invariant: <E extends AnyEntity>(id: string, build: (rows: {
|
|
1461
|
-
readonly before: BeforeRow<E>;
|
|
1462
|
-
readonly after: AfterRow<E>;
|
|
1463
|
-
}) => Predicate & Reads<E['name']>, options: {
|
|
1464
|
-
readonly message: string;
|
|
1465
|
-
readonly path?: ReadonlyArray<keyof E['fields'] & string>;
|
|
1466
|
-
}) => DeclaredInvariant<E>;
|
|
1467
|
-
declare const invariantsOf: (entity: AnyEntity) => ReadonlyArray<Invariant>;
|
|
1468
|
-
/** `judge` with the invariants given, as a binding that keeps them holds them. */
|
|
1469
|
-
declare const judgeAgainst: (invariants: ReadonlyArray<Invariant>, change: Change, operators?: MemoryOperators) => Judgement;
|
|
1470
|
-
/**
|
|
1471
|
-
* A row, or a change, under every invariant (each row as the store holds it,
|
|
1472
|
-
* encoded): valid only when each is true. SQL's unknown, from a null, fails
|
|
1473
|
-
* one, as a `WHERE` drops the row; a field a row does not have leaves it
|
|
1474
|
-
* undecided, never valid. Without `before` (a create), transition invariants
|
|
1475
|
-
* are not judged.
|
|
1476
|
-
*/
|
|
1477
|
-
declare const judge: (entity: AnyEntity, change: Change, operators?: MemoryOperators) => Judgement;
|
|
1478
|
-
/**
|
|
1479
|
-
* The row an input's value describes, judged under its Entity's state
|
|
1480
|
-
* invariants: each key mapped to a field, encoded as the store holds it. An
|
|
1481
|
-
* invariant over a field the input does not set (an `Option` holding nothing, a
|
|
1482
|
-
* key left out) is undecided, so a partial input is never called valid on its
|
|
1483
|
-
* account. It has no row before, so it judges no transition.
|
|
1484
|
-
*/
|
|
1485
|
-
declare const judgeInput: (input: EntityInput<AnyEntity, any, any>, value: Readonly<Record<string, unknown>>, operators?: MemoryOperators) => Judgement;
|
|
1486
|
-
/**
|
|
1487
|
-
* The Entity's state invariants as an Effect Schema check over its row type:
|
|
1488
|
-
* each failure at its path, with its message. The value is encoded first,
|
|
1489
|
-
* field by field, so it is judged as the store would hold it.
|
|
1490
|
-
*/
|
|
1491
|
-
declare const check: <E extends AnyEntity>(entity: E, options?: {
|
|
1492
|
-
readonly operators?: MemoryOperators | undefined;
|
|
1493
|
-
}) => SchemaAST.Filter<Schema.Schema.Type<E['schema']>>;
|
|
1494
|
-
declare const Text: {
|
|
1495
|
-
/**
|
|
1496
|
-
* Whether the first text begins with the second, under the first's collation.
|
|
1497
|
-
*
|
|
1498
|
-
* | Case | Answer |
|
|
1499
|
-
* | --- | --- |
|
|
1500
|
-
* | either is null | unknown |
|
|
1501
|
-
* | the prefix is `''` | true |
|
|
1502
|
-
* | otherwise | whether it begins so, compared under the collation |
|
|
1503
|
-
*
|
|
1504
|
-
* The collation is the one its text operands share (a field's declared one,
|
|
1505
|
-
* or a call's), or one given to the call. `binary` compares exactly;
|
|
1506
|
-
* `asciiFold` folds ASCII letters on both sides. Operands declaring
|
|
1507
|
-
* different collations, text with none, or a backend's locale are refused
|
|
1508
|
-
* where the call is written.
|
|
1509
|
-
* Prefixes are by character, so non-ASCII and astral text begin as they read.
|
|
1510
|
-
*
|
|
1511
|
-
* ```ts
|
|
1512
|
-
* Query.where(Text.startsWith(Product.fields.upc, input.search))
|
|
1513
|
-
* ```
|
|
1514
|
-
*/
|
|
1515
|
-
startsWith: Operator<readonly [Schema.NullOr<Schema.String>, Schema.NullOr<Schema.String>], Schema.Boolean>;
|
|
1516
|
-
};
|
|
1517
|
-
declare const ReadFilter: {
|
|
1518
|
-
/** Entity metadata: the rows the server lists depend on these members. */
|
|
1519
|
-
of: (members: ReadonlyArray<string>) => Metadata;
|
|
1520
|
-
/** The members an Entity's metadata names, every attachment's together. */
|
|
1521
|
-
members: (metadata: Metadata) => ReadonlyArray<string>;
|
|
1522
|
-
};
|
|
1523
|
-
/** What a schema says of itself to a person: its `title` and `description`. */
|
|
1524
|
-
interface SchemaWords {
|
|
1525
|
-
readonly title: Option.Option<string>;
|
|
1526
|
-
readonly description: Option.Option<string>;
|
|
1527
|
-
}
|
|
1528
|
-
declare const Words: {
|
|
1529
|
-
/**
|
|
1530
|
-
* A schema's words, as a label and its help text. Under Effect 4 a checked
|
|
1531
|
-
* schema resolves to its last check's annotations, which say what the check
|
|
1532
|
-
* expects and carry no `title`, so a title given before the check is read from
|
|
1533
|
-
* the schema's own annotations. Read words through this, never through
|
|
1534
|
-
* `Schema.resolveAnnotations` directly.
|
|
1535
|
-
*/
|
|
1536
|
-
of: (schema: Schema.Top) => SchemaWords;
|
|
1537
|
-
};
|
|
1538
|
-
declare const isNullish: (ast: SchemaAST.AST) => boolean;
|
|
1539
|
-
declare const isText: (ast: SchemaAST.AST) => boolean;
|
|
1540
|
-
/** What a schema's AST holds, read through Effect's own guards. */
|
|
1541
|
-
declare const SchemaShape: {
|
|
1542
|
-
/** True for the `null` and `undefined` members that make a schema optional. */
|
|
1543
|
-
isNullish: typeof isNullish;
|
|
1544
|
-
/**
|
|
1545
|
-
* The members of a schema with `null` and `undefined` set aside, so optional
|
|
1546
|
-
* text is still text; a schema that is not a union is its one member.
|
|
1547
|
-
*/
|
|
1548
|
-
present: (ast: SchemaAST.AST) => ReadonlyArray<SchemaAST.AST>;
|
|
1549
|
-
/** True when every value the schema admits is a string. */
|
|
1550
|
-
isText: typeof isText;
|
|
1551
|
-
/**
|
|
1552
|
-
* True when the AST is checked to hold integers (`Schema.isInt`, alone or in
|
|
1553
|
-
* a group such as `isInt32`). Read it from a schema's encoded side to ask
|
|
1554
|
-
* what the store holds.
|
|
1555
|
-
*/
|
|
1556
|
-
holdsIntegers: (ast: SchemaAST.AST) => boolean;
|
|
1557
|
-
};
|
|
1558
|
-
type AnyField = EntityField<string, string, Schema.Constraint>;
|
|
1559
|
-
type OneRelation = EntityRelation<string, string, AnyEntity, 'one', boolean>;
|
|
1560
|
-
/** One input key the write sets, and the field it sets. */
|
|
1561
|
-
interface WriteSet {
|
|
1562
|
-
readonly key: string;
|
|
1563
|
-
readonly field: AnyField;
|
|
1564
|
-
}
|
|
1565
|
-
/**
|
|
1566
|
-
* One input key holding an amount, and the field it moves. `integer` is whether
|
|
1567
|
-
* the amount's schema holds it to an integer (`Schema.isInt`): a writer over an
|
|
1568
|
-
* integer column refuses an increment without it, which the Entity cannot see.
|
|
1569
|
-
*/
|
|
1570
|
-
interface WriteIncrement extends WriteSet {
|
|
1571
|
-
readonly integer: boolean;
|
|
1572
|
-
}
|
|
1573
|
-
/**
|
|
1574
|
-
* One field the write computes (`label := concat(title, ' draft')`): an
|
|
1575
|
-
* expression over the row before the write and the input, every defined
|
|
1576
|
-
* operator in it expanded, so what is carried is primitive calls alone.
|
|
1577
|
-
*/
|
|
1578
|
-
interface WriteAssignment {
|
|
1579
|
-
readonly field: AnyField;
|
|
1580
|
-
readonly expr: AnyExpr;
|
|
1581
|
-
}
|
|
1582
|
-
/**
|
|
1583
|
-
* A computed assignment as data, bound to one input: what travels to a server
|
|
1584
|
-
* and sits in a client's pending patch, which holds no descriptor and no
|
|
1585
|
-
* function. A field of the row before the write, a stored value, or a call of
|
|
1586
|
-
* a primitive operator by name (`id`, or `id@version`), with the collation it
|
|
1587
|
-
* compares text under (`null` for none: this is JSON).
|
|
1588
|
-
*/
|
|
1589
|
-
type Computation = Data.TaggedEnum<{
|
|
1590
|
-
Field: {
|
|
1591
|
-
readonly key: string;
|
|
1592
|
-
};
|
|
1593
|
-
Value: {
|
|
1594
|
-
readonly value: unknown;
|
|
1595
|
-
};
|
|
1596
|
-
Call: {
|
|
1597
|
-
readonly operator: string;
|
|
1598
|
-
readonly args: ReadonlyArray<Computation>;
|
|
1599
|
-
readonly collation: Collation | null;
|
|
1600
|
-
};
|
|
1601
|
-
}>;
|
|
1602
|
-
declare const Computation: {
|
|
1603
|
-
readonly $is: <Tag extends "Call" | "Field" | "Value">(tag: Tag) => (u: unknown) => u is Extract<{
|
|
1604
|
-
readonly _tag: "Call";
|
|
1605
|
-
readonly operator: string;
|
|
1606
|
-
readonly args: ReadonlyArray<Computation>;
|
|
1607
|
-
readonly collation: Collation | null;
|
|
1608
|
-
}, {
|
|
1609
|
-
readonly _tag: Tag;
|
|
1610
|
-
}> | Extract<{
|
|
1611
|
-
readonly _tag: "Field";
|
|
1612
|
-
readonly key: string;
|
|
1613
|
-
}, {
|
|
1614
|
-
readonly _tag: Tag;
|
|
1615
|
-
}> | Extract<{
|
|
1616
|
-
readonly _tag: "Value";
|
|
1617
|
-
readonly value: unknown;
|
|
1618
|
-
}, {
|
|
1619
|
-
readonly _tag: Tag;
|
|
1620
|
-
}>;
|
|
1621
|
-
readonly $match: {
|
|
1622
|
-
<Cases extends { readonly [Tag in "Call" | "Field" | "Value"]: (args: Extract<{
|
|
1623
|
-
readonly _tag: "Call";
|
|
1624
|
-
readonly operator: string;
|
|
1625
|
-
readonly args: ReadonlyArray<Computation>;
|
|
1626
|
-
readonly collation: Collation | null;
|
|
1627
|
-
} | {
|
|
1628
|
-
readonly _tag: "Field";
|
|
1629
|
-
readonly key: string;
|
|
1630
|
-
} | {
|
|
1631
|
-
readonly _tag: "Value";
|
|
1632
|
-
readonly value: unknown;
|
|
1633
|
-
}, {
|
|
1634
|
-
readonly _tag: Tag;
|
|
1635
|
-
}>) => any; }>(cases: Cases): (value: {
|
|
1636
|
-
readonly _tag: "Call";
|
|
1637
|
-
readonly operator: string;
|
|
1638
|
-
readonly args: ReadonlyArray<Computation>;
|
|
1639
|
-
readonly collation: Collation | null;
|
|
1640
|
-
} | {
|
|
1641
|
-
readonly _tag: "Field";
|
|
1642
|
-
readonly key: string;
|
|
1643
|
-
} | {
|
|
1644
|
-
readonly _tag: "Value";
|
|
1645
|
-
readonly value: unknown;
|
|
1646
|
-
}) => import("effect/Unify").Unify<ReturnType<Cases["Call" | "Field" | "Value"]>>;
|
|
1647
|
-
<Cases extends { readonly [Tag in "Call" | "Field" | "Value"]: (args: Extract<{
|
|
1648
|
-
readonly _tag: "Call";
|
|
1649
|
-
readonly operator: string;
|
|
1650
|
-
readonly args: ReadonlyArray<Computation>;
|
|
1651
|
-
readonly collation: Collation | null;
|
|
1652
|
-
} | {
|
|
1653
|
-
readonly _tag: "Field";
|
|
1654
|
-
readonly key: string;
|
|
1655
|
-
} | {
|
|
1656
|
-
readonly _tag: "Value";
|
|
1657
|
-
readonly value: unknown;
|
|
1658
|
-
}, {
|
|
1659
|
-
readonly _tag: Tag;
|
|
1660
|
-
}>) => any; }>(value: {
|
|
1661
|
-
readonly _tag: "Call";
|
|
1662
|
-
readonly operator: string;
|
|
1663
|
-
readonly args: ReadonlyArray<Computation>;
|
|
1664
|
-
readonly collation: Collation | null;
|
|
1665
|
-
} | {
|
|
1666
|
-
readonly _tag: "Field";
|
|
1667
|
-
readonly key: string;
|
|
1668
|
-
} | {
|
|
1669
|
-
readonly _tag: "Value";
|
|
1670
|
-
readonly value: unknown;
|
|
1671
|
-
}, cases: Cases): import("effect/Unify").Unify<ReturnType<Cases["Call" | "Field" | "Value"]>>;
|
|
1672
|
-
};
|
|
1673
|
-
readonly Call: Data.TaggedEnum.ConstructorFrom<{
|
|
1674
|
-
readonly _tag: "Call";
|
|
1675
|
-
readonly operator: string;
|
|
1676
|
-
readonly args: ReadonlyArray<Computation>;
|
|
1677
|
-
readonly collation: Collation | null;
|
|
1678
|
-
}, "_tag">;
|
|
1679
|
-
readonly Field: Data.TaggedEnum.ConstructorFrom<{
|
|
1680
|
-
readonly _tag: "Field";
|
|
1681
|
-
readonly key: string;
|
|
1682
|
-
}, "_tag">;
|
|
1683
|
-
readonly Value: Data.TaggedEnum.ConstructorFrom<{
|
|
1684
|
-
readonly _tag: "Value";
|
|
1685
|
-
readonly value: unknown;
|
|
1686
|
-
}, "_tag">;
|
|
1687
|
-
};
|
|
1688
|
-
/** One input key the write points at another row with: a `one` relation, by the target's id. */
|
|
1689
|
-
interface WriteLink {
|
|
1690
|
-
readonly key: string;
|
|
1691
|
-
readonly relation: OneRelation;
|
|
1692
|
-
}
|
|
1693
|
-
/**
|
|
1694
|
-
* One row of an Entity, by id, from an operation's input: updated, inserted
|
|
1695
|
-
* with an id the client chose, or deleted.
|
|
1696
|
-
*/
|
|
1697
|
-
interface Write<E extends AnyEntity, Fields extends Schema.Struct.Fields> {
|
|
1698
|
-
readonly _tag: 'Update' | 'Insert' | 'Delete';
|
|
1699
|
-
readonly input: EntityInput<E, Fields, any>;
|
|
1700
|
-
/** The input key holding the row's id, and the Entity's `id` field it is. */
|
|
1701
|
-
readonly id: WriteSet & {
|
|
1702
|
-
readonly key: keyof Fields & string;
|
|
1703
|
-
};
|
|
1704
|
-
/**
|
|
1705
|
-
* The input key holding the revision the row was read at, and its field, when
|
|
1706
|
-
* the write is refused for a row that moved on since.
|
|
1707
|
-
*/
|
|
1708
|
-
readonly expect: (WriteSet & {
|
|
1709
|
-
readonly key: keyof Fields & string;
|
|
1710
|
-
}) | undefined;
|
|
1711
|
-
/**
|
|
1712
|
-
* What the row must hold for the write to land, over the Entity, read
|
|
1713
|
-
* before the write and checked where it commits: an interpreter writes only
|
|
1714
|
-
* a row it holds of, and refuses the rest as unmet.
|
|
1715
|
-
*/
|
|
1716
|
-
readonly when: Option.Option<AnyQuery>;
|
|
1717
|
-
/** The keys it sets, in the input's order. */
|
|
1718
|
-
readonly sets: ReadonlyArray<WriteSet>;
|
|
1719
|
-
/**
|
|
1720
|
-
* The fields it moves by an amount the input holds (`stock + quantity`),
|
|
1721
|
-
* computed where the write commits: the input key, and the field it moves.
|
|
1722
|
-
*/
|
|
1723
|
-
readonly increments: ReadonlyArray<WriteIncrement>;
|
|
1724
|
-
/** The fields it computes, each from the row before the write and the input. */
|
|
1725
|
-
readonly assignments: ReadonlyArray<WriteAssignment>;
|
|
1726
|
-
/** The `one` relations it points elsewhere, in the input's order. */
|
|
1727
|
-
readonly links: ReadonlyArray<WriteLink>;
|
|
1728
|
-
}
|
|
1729
|
-
type AnyWrite = Write<AnyEntity, any>;
|
|
1730
|
-
/** A write declared with a precondition, so it may be refused as unmet. */
|
|
1731
|
-
type ConditionedWrite<E extends AnyEntity, Fields extends Schema.Struct.Fields> = Write<E, Fields> & {
|
|
1732
|
-
readonly when: Option.Some<AnyQuery>;
|
|
1733
|
-
};
|
|
1734
|
-
interface RowOptions<Fields extends Schema.Struct.Fields> {
|
|
1735
|
-
readonly id: keyof Fields & string;
|
|
1736
|
-
readonly expect?: keyof Fields & string;
|
|
1737
|
-
}
|
|
1738
|
-
/** The keys of `Fields` whose values are numbers. */
|
|
1739
|
-
type NumberKeys<Fields> = { [K in keyof Fields & string]: Fields[K] extends Schema.Top ? Schema.Schema.Type<Fields[K]> extends number ? K : never : never; }[keyof Fields & string];
|
|
1740
|
-
/** An update's increments: a number field of the Entity, by the input key holding the amount. */
|
|
1741
|
-
type WriteIncrements<E extends AnyEntity, Fields extends Schema.Struct.Fields> = { readonly [K in NumberKeys<{ [F in keyof E['fields']]: E['fields'][F]['schema']; }>]?: NumberKeys<Fields>; };
|
|
1742
|
-
/**
|
|
1743
|
-
* An update's computed fields: each a field of the Entity, by what it comes
|
|
1744
|
-
* to over the row before the write (`row`, each field as `Expr.field`) and
|
|
1745
|
-
* the input. Its kind is checked against the field's where it is declared.
|
|
1746
|
-
*/
|
|
1747
|
-
type WriteComputations<E extends AnyEntity, Fields extends Schema.Struct.Fields> = { readonly [F in keyof E['fields'] & string]?: (context: {
|
|
1748
|
-
readonly row: BeforeRow<E>;
|
|
1749
|
-
readonly input: { readonly [K in keyof Fields & string]: InputExpr<Schema.Schema.Type<Fields[K]>>; };
|
|
1750
|
-
}) => AnyExpr; };
|
|
1751
|
-
/** An update's options: a row's, what it moves by an amount, and what it computes. */
|
|
1752
|
-
type UpdateOptions<E extends AnyEntity, Fields extends Schema.Struct.Fields> = RowOptions<Fields> & {
|
|
1753
|
-
readonly increment?: WriteIncrements<E, Fields>;
|
|
1754
|
-
readonly compute?: WriteComputations<E, Fields>;
|
|
1755
|
-
};
|
|
1756
|
-
/** `update`: conditioned when given `when`, which the overloads tell apart. */
|
|
1757
|
-
interface UpdateWrite {
|
|
1758
|
-
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: UpdateOptions<E, Fields> & {
|
|
1759
|
-
readonly when: WriteCondition<E, Fields>;
|
|
1760
|
-
}): ConditionedWrite<E, Fields>;
|
|
1761
|
-
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: UpdateOptions<E, Fields> & {
|
|
1762
|
-
readonly when?: undefined;
|
|
1763
|
-
}): Write<E, Fields>;
|
|
1764
|
-
}
|
|
1765
|
-
/** `delete`: conditioned when given `when`, as an update is. */
|
|
1766
|
-
interface DeleteWrite {
|
|
1767
|
-
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: RowOptions<Fields> & {
|
|
1768
|
-
readonly when: WriteCondition<E, Fields>;
|
|
1769
|
-
}): ConditionedWrite<E, Fields>;
|
|
1770
|
-
<E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: RowOptions<Fields> & {
|
|
1771
|
-
readonly when?: undefined;
|
|
1772
|
-
}): Write<E, Fields>;
|
|
1773
|
-
}
|
|
1774
|
-
/**
|
|
1775
|
-
* A write's precondition: a predicate over its Entity's row, given one
|
|
1776
|
-
* placeholder per input key, as a `Query.define` body is. It is built once,
|
|
1777
|
-
* so `input` stands for what the write will be given.
|
|
1778
|
-
*/
|
|
1779
|
-
type WriteCondition<E extends AnyEntity, Fields extends Schema.Struct.Fields> = (context: {
|
|
1780
|
-
readonly input: { readonly [K in keyof Fields & string]: InputExpr<Schema.Schema.Type<Fields[K]>>; };
|
|
1781
|
-
}) => Predicate & Reads<E['name']>;
|
|
1782
|
-
/** One row's new values, in the store's wire shape: what a client patches and a server writes. */
|
|
1783
|
-
interface BoundWrite {
|
|
1784
|
-
readonly entity: string;
|
|
1785
|
-
readonly id: string;
|
|
1786
|
-
readonly values: Readonly<Record<string, unknown>>;
|
|
1787
|
-
/**
|
|
1788
|
-
* Each `one` relation it points, by the relation's key: the row it points at,
|
|
1789
|
-
* or none for an optional relation set to nothing. How a ref is carried (a
|
|
1790
|
-
* key on the wire, a foreign key in a table) is each interpreter's.
|
|
1791
|
-
*/
|
|
1792
|
-
readonly links: Readonly<Record<string, Option.Option<EntityRef>>>;
|
|
1793
|
-
/** Each field it moves, by the amount, encoded: added by the server, and predicted over what a client holds. */
|
|
1794
|
-
readonly increments: Readonly<Record<string, number>>;
|
|
1795
|
-
/** Each field it computes, as data: run by the server where it writes, predicted by a client. */
|
|
1796
|
-
readonly assignments: Readonly<Record<string, Computation>>;
|
|
1797
|
-
}
|
|
1798
|
-
declare const Write: {
|
|
1799
|
-
/**
|
|
1800
|
-
* Sets, on the row whose id is the input's `id` key, every field the input
|
|
1801
|
-
* maps a key to. `expect` names the key holding the revision the row was
|
|
1802
|
-
* read at: an interpreter writes only a row still at that revision.
|
|
1803
|
-
*
|
|
1804
|
-
* ```ts
|
|
1805
|
-
* const EditPost = Write.update(EditPostInput, { id: 'id' })
|
|
1806
|
-
* ```
|
|
1807
|
-
*
|
|
1808
|
-
* A key mapped to a `one` relation (`Relation.input`) points the row at the
|
|
1809
|
-
* id it holds. Refused where it is written: an `id` that is not the Entity's
|
|
1810
|
-
* `id` field, a `many` relation or a nested key (not written by an update
|
|
1811
|
-
* yet), or nothing to set.
|
|
1812
|
-
*
|
|
1813
|
-
* `when` is what the row must hold for the update to land, checked where it
|
|
1814
|
-
* commits (`({ input }) => Expr.eq(Post.fields.status, 'draft')`); a row it
|
|
1815
|
-
* does not hold of is refused as unmet.
|
|
1816
|
-
*
|
|
1817
|
-
* `compute` sets a field to what an expression comes to over the row before
|
|
1818
|
-
* the write and the input, run where it commits
|
|
1819
|
-
* (`{ label: ({ row }) => concat(row.title, ' draft') }`). It is refused for
|
|
1820
|
-
* a field another key writes, an expression over the row after the write or
|
|
1821
|
-
* another Entity, or one of a kind the field does not store.
|
|
1822
|
-
*/
|
|
1823
|
-
update: UpdateWrite;
|
|
1824
|
-
/**
|
|
1825
|
-
* Inserts the row whose id is the input's `id` key, an id the client chose,
|
|
1826
|
-
* with every field and `one` relation the input maps a key to. An insert
|
|
1827
|
-
* expects no revision: there is no row to have read. A row already there
|
|
1828
|
-
* under that id (a retry) is left as it is.
|
|
1829
|
-
*/
|
|
1830
|
-
insert: <E extends AnyEntity, Fields extends Schema.Struct.Fields>(input: EntityInput<E, Fields, any>, options: {
|
|
1831
|
-
readonly id: keyof Fields & string;
|
|
1832
|
-
}) => Write<E, Fields>;
|
|
1833
|
-
/**
|
|
1834
|
-
* Deletes the row whose id is the input's `id` key, only while it is still at
|
|
1835
|
-
* the revision `expect` names, when it names one. It sets nothing, so a key
|
|
1836
|
-
* mapped to a member is refused where it is written. A row already gone (a
|
|
1837
|
-
* retry) is gone. `when`, as an update's, is what the row must hold.
|
|
1838
|
-
*/
|
|
1839
|
-
delete: DeleteWrite;
|
|
1840
|
-
/**
|
|
1841
|
-
* The revision this input says the row was read at, encoded, with its field:
|
|
1842
|
-
* what an interpreter writes only a row still at. None for a write that
|
|
1843
|
-
* expects none.
|
|
1844
|
-
*/
|
|
1845
|
-
expected: <Fields extends Schema.Struct.Fields>(write: Write<AnyEntity, Fields>, value: Schema.Struct.Type<Fields>) => Option.Option<{
|
|
1846
|
-
readonly field: string;
|
|
1847
|
-
readonly revision: unknown;
|
|
1848
|
-
}>;
|
|
1849
|
-
/** The members any invocation may write: what a query reading none of them cannot be changed by. */
|
|
1850
|
-
writes: (write: AnyWrite) => ReadonlyArray<AnyField | OneRelation>;
|
|
1851
|
-
/**
|
|
1852
|
-
* The fields a write reads from the row it writes: those its `when` reads,
|
|
1853
|
-
* each it increments, each its assignments read, and the revision it
|
|
1854
|
-
* expects. What an interpreter must read, and what a startup check judges
|
|
1855
|
-
* it could disclose.
|
|
1856
|
-
*/
|
|
1857
|
-
reads: (write: AnyWrite) => ReadonlyArray<AnyField>;
|
|
1858
|
-
/**
|
|
1859
|
-
* The row this input writes and its new values, encoded as the store holds
|
|
1860
|
-
* them: the patch a client shows before the server answers. `keys`, when
|
|
1861
|
-
* given, narrows it to those input keys (what an author changed); the rest of
|
|
1862
|
-
* the input is still the whole, validated value.
|
|
1863
|
-
*/
|
|
1864
|
-
bind: <Fields extends Schema.Struct.Fields>(write: Write<AnyEntity, Fields>, value: Schema.Struct.Type<Fields>, keys?: ReadonlyArray<string>) => BoundWrite;
|
|
1865
|
-
/**
|
|
1866
|
-
* The fields of the row before it that a bound write's amounts and
|
|
1867
|
-
* assignments read: what to read to work out the row it leaves.
|
|
1868
|
-
*/
|
|
1869
|
-
boundReads: (bound: Pick<BoundWrite, 'increments' | 'assignments'>) => ReadonlyArray<string>;
|
|
1870
|
-
/**
|
|
1871
|
-
* The row a bound write leaves over `row`, the row before it: its values
|
|
1872
|
-
* set, each amount added (a null stays null, as in SQL), and each
|
|
1873
|
-
* assignment computed by `operators` over the row before. `unknown` names
|
|
1874
|
-
* each field it could not work out: one `row` lacks, or holds as no number
|
|
1875
|
-
* for an amount, or an assignment calling an operator the table lacks. That
|
|
1876
|
-
* field keeps what `row` holds. The one computation of this row: a guard
|
|
1877
|
-
* judges it, a writer explains a miss with it, a client predicts it.
|
|
1878
|
-
*/
|
|
1879
|
-
apply: (row: Readonly<Record<string, unknown>>, bound: Pick<BoundWrite, 'values' | 'increments' | 'assignments'>, options: {
|
|
1880
|
-
readonly operators: MemoryOperators;
|
|
1881
|
-
}) => {
|
|
1882
|
-
readonly row: Readonly<Record<string, unknown>>;
|
|
1883
|
-
readonly unknown: ReadonlyArray<string>;
|
|
1884
|
-
};
|
|
1885
|
-
};
|
|
1886
|
-
declare const EntityTypeId: unique symbol;
|
|
1887
|
-
type EntityTypeId = typeof EntityTypeId;
|
|
1888
|
-
/**
|
|
1889
|
-
* What makes two descriptors the same Entity. A pipe step returns a new
|
|
1890
|
-
* descriptor with the same identity, so compare `identity`, never the objects.
|
|
1891
|
-
*/
|
|
1892
|
-
interface EntityIdentity<Name extends string> {
|
|
1893
|
-
readonly name: Name;
|
|
1894
|
-
/** Distinguishes two Entities that were defined with the same name. */
|
|
1895
|
-
readonly token: symbol;
|
|
1896
|
-
}
|
|
1897
|
-
/** An intrinsic value of the Entity: one property of `entity.schema`. */
|
|
1898
|
-
interface EntityField<Name extends string, Key extends string, S extends Schema.Constraint> {
|
|
1899
|
-
readonly _tag: 'Field';
|
|
1900
|
-
readonly owner: EntityIdentity<Name>;
|
|
1901
|
-
readonly key: Key;
|
|
1902
|
-
readonly schema: S;
|
|
1903
|
-
readonly metadata: Metadata;
|
|
1904
|
-
}
|
|
1905
|
-
type Cardinality = 'one' | 'many';
|
|
1906
|
-
/** What `Relation.one` / `Relation.many` return, before `Entity.relate` binds it to an owner. */
|
|
1907
|
-
interface RelationSpec<Target extends AnyEntity, C extends Cardinality, Optional extends boolean> {
|
|
1908
|
-
readonly target: Target;
|
|
1909
|
-
readonly cardinality: C;
|
|
1910
|
-
readonly optional: Optional;
|
|
1911
|
-
}
|
|
1912
|
-
/**
|
|
1913
|
-
* A navigation edge as its owner sees it: one or many of `target`. How the edge
|
|
1914
|
-
* is stored (a foreign key, a join table, a nested document) is the
|
|
1915
|
-
* interpreter's concern.
|
|
1916
|
-
*/
|
|
1917
|
-
interface EntityRelation<Name extends string, Key extends string, Target extends AnyEntity, C extends Cardinality, Optional extends boolean> {
|
|
1918
|
-
readonly _tag: 'Relation';
|
|
1919
|
-
readonly owner: EntityIdentity<Name>;
|
|
1920
|
-
readonly key: Key;
|
|
1921
|
-
/** The target as `Entity.relate` returned it, so its own relations can be followed. */
|
|
1922
|
-
readonly target: () => Target;
|
|
1923
|
-
readonly cardinality: C;
|
|
1924
|
-
readonly optional: Optional;
|
|
1925
|
-
readonly metadata: Metadata;
|
|
1926
|
-
}
|
|
1927
|
-
/** What `Derived.make` returns, before an Entity owns it. */
|
|
1928
|
-
interface DerivedSpec<S extends Schema.Constraint> {
|
|
1929
|
-
readonly schema: S;
|
|
1930
|
-
}
|
|
1931
|
-
/**
|
|
1932
|
-
* A value consumers may read that is not part of `entity.schema`. It says
|
|
1933
|
-
* nothing about how the value is computed; an interpreter supplies it.
|
|
1934
|
-
*/
|
|
1935
|
-
interface EntityDerived<Name extends string, Key extends string, S extends Schema.Constraint> {
|
|
1936
|
-
readonly _tag: 'Derived';
|
|
1937
|
-
readonly owner: EntityIdentity<Name>;
|
|
1938
|
-
readonly key: Key;
|
|
1939
|
-
readonly schema: S;
|
|
1940
|
-
readonly metadata: Metadata;
|
|
1941
|
-
}
|
|
1942
|
-
type EntityMember = EntityField<string, string, Schema.Constraint> | EntityRelation<string, string, AnyEntity, Cardinality, boolean> | EntityDerived<string, string, Schema.Constraint>;
|
|
1943
|
-
type RelationSpecs = Readonly<Record<string, RelationSpec<AnyEntity, Cardinality, boolean>>>;
|
|
1944
|
-
type DerivedSpecs = Readonly<Record<string, DerivedSpec<Schema.Constraint>>>;
|
|
1945
|
-
type FieldsOf<Name extends string, Fields extends Schema.Struct.Fields> = { readonly [K in keyof Fields & string]: EntityField<Name, K, Fields[K]>; };
|
|
1946
|
-
type RelationsOf<Name extends string, Relations extends RelationSpecs> = { readonly [K in keyof Relations & string]: Relations[K] extends RelationSpec<infer Target, infer C, infer Optional> ? EntityRelation<Name, K, Target, C, Optional> : never; };
|
|
1947
|
-
type DerivedOf<Name extends string, Derived extends DerivedSpecs> = { readonly [K in keyof Derived & string]: EntityDerived<Name, K, Derived[K]['schema']>; };
|
|
1948
|
-
interface Entity<Name extends string, Fields extends Schema.Struct.Fields, Relations extends RelationSpecs = {}, Derived extends DerivedSpecs = {}> extends Pipeable.Pipeable {
|
|
1949
|
-
readonly [EntityTypeId]: EntityTypeId;
|
|
1950
|
-
readonly identity: EntityIdentity<Name>;
|
|
1951
|
-
readonly name: Name;
|
|
1952
|
-
/** The intrinsic value only: no relations, no derived members. */
|
|
1953
|
-
readonly schema: Schema.Struct<Fields>;
|
|
1954
|
-
readonly fields: FieldsOf<Name, Fields>;
|
|
1955
|
-
readonly relations: RelationsOf<Name, Relations>;
|
|
1956
|
-
readonly derived: DerivedOf<Name, Derived>;
|
|
1957
|
-
/** Every readable member under one namespace; keys never collide. */
|
|
1958
|
-
readonly members: FieldsOf<Name, Fields> & RelationsOf<Name, Relations> & DerivedOf<Name, Derived>;
|
|
1959
|
-
readonly metadata: Metadata;
|
|
1960
|
-
}
|
|
1961
|
-
type AnyEntity = Entity<string, any, any, any>;
|
|
1962
|
-
/** Unsatisfiable when a new key is already a member, so the pipe step is the error site. */
|
|
1963
|
-
type Free<E extends AnyEntity, New> = keyof New & keyof E['members'] extends never ? unknown : {
|
|
1964
|
-
readonly alreadyAMember: keyof New & keyof E['members'];
|
|
1965
|
-
};
|
|
1966
|
-
type WithDerived<E, New extends DerivedSpecs> = E extends Entity<infer Name, infer Fields, infer R, infer D> ? Entity<Name, Fields, R, D & New> : never;
|
|
1967
|
-
type Entities = Readonly<Record<string, AnyEntity>>;
|
|
1968
|
-
type RelationMap<Es extends Entities> = { readonly [K in keyof Es]?: RelationSpecs; };
|
|
1969
|
-
/** The key in `Es` of the Entity a spec targets, matched by name. */
|
|
1970
|
-
type TargetKey<Es extends Entities, Target extends AnyEntity> = { [K in keyof Es]: [Es[K]['name'], Target['name']] extends [Target['name'], Es[K]['name']] ? K : never; }[keyof Es];
|
|
1971
|
-
type RelatedSpecs<Es extends Entities, Rs extends RelationMap<Es>, K extends keyof Es> = { readonly [R in keyof Rs[K]]: Rs[K][R] extends RelationSpec<infer Target, infer C, infer Optional> ? RelationSpec<Related<Es, Rs, TargetKey<Es, Target>>, C, Optional> : never; };
|
|
1972
|
-
/**
|
|
1973
|
-
* One Entity of an `Entity.relate` result. A relation's target is again a
|
|
1974
|
-
* `Related`, so relations can be followed through a cycle; the recursion goes
|
|
1975
|
-
* through type arguments, which TypeScript resolves only when asked.
|
|
1976
|
-
*/
|
|
1977
|
-
type Related<Es extends Entities, Rs extends RelationMap<Es>, K extends keyof Es> = Es[K] extends Entity<infer Name, infer Fields, infer R, infer D> ? Entity<Name, Fields, R & RelatedSpecs<Es, Rs, K>, D> : never;
|
|
1978
|
-
/** Marks a relation key that is already a member of its owner, or an owner that is not listed. */
|
|
1979
|
-
type CheckRelations<Es extends Entities, Rs> = { readonly [K in keyof Rs]: K extends keyof Es ? { readonly [R in keyof Rs[K]]: R extends keyof Es[K]['members'] ? `"${R & string}" is already a member of ${Es[K]['name']}` : Rs[K][R]; } : 'not one of the entities being related'; };
|
|
1980
|
-
declare const SelectionTypeId: unique symbol;
|
|
1981
|
-
type SelectionTypeId = typeof SelectionTypeId;
|
|
1982
|
-
/**
|
|
1983
|
-
* The schema that identifies one of an Entity: its `id` field when that is text
|
|
1984
|
-
* (a plain string, a brand, a pattern), and plain text otherwise. An id is text
|
|
1985
|
-
* because a ref travels and is stored as text; an Entity keyed by a number still
|
|
1986
|
-
* has refs, and their ids are the number as text.
|
|
1987
|
-
*/
|
|
1988
|
-
type IdSchemaOf<E> = E extends {
|
|
1989
|
-
readonly fields: {
|
|
1990
|
-
readonly id: {
|
|
1991
|
-
readonly schema: infer S;
|
|
1992
|
-
};
|
|
1993
|
-
};
|
|
1994
|
-
} ? S extends Schema.Constraint ? [S['Type']] extends [string] ? S : Schema.String : Schema.String : Schema.String;
|
|
1995
|
-
/** What identifies one of an Entity: `IdOf<typeof Blog.Post>` is `PostId` when its `id` field is one. */
|
|
1996
|
-
type IdOf<E> = IdSchemaOf<E>['Type'];
|
|
1997
|
-
/**
|
|
1998
|
-
* What a relation selected with `true` yields: which Entity, and which one. `Id`
|
|
1999
|
-
* is the target's own id type, so a ref to an Author cannot be used as a Post's.
|
|
2000
|
-
*/
|
|
2001
|
-
interface EntityRef<Name extends string = string, Id extends string = string> {
|
|
2002
|
-
readonly entity: Name;
|
|
2003
|
-
readonly id: Id;
|
|
2004
|
-
}
|
|
2005
|
-
type RefSchema<Target extends AnyEntity> = Schema.Struct<{
|
|
2006
|
-
readonly entity: Schema.Literal<Target['name']>;
|
|
2007
|
-
readonly id: IdSchemaOf<Target>;
|
|
2008
|
-
}>;
|
|
2009
|
-
/**
|
|
2010
|
-
* A view of the Entity graph: which members to read, and the schema of the
|
|
2011
|
-
* value that results. It says nothing about where the value comes from.
|
|
2012
|
-
*/
|
|
2013
|
-
interface Selection<Name extends string, Members, S extends Schema.Constraint, Id extends string = string> {
|
|
2014
|
-
readonly [SelectionTypeId]: SelectionTypeId;
|
|
2015
|
-
/** Type-only: the id type of the Entity selected, so a reader can ask for the right one. Never set. */
|
|
2016
|
-
readonly Id?: Id;
|
|
2017
|
-
readonly entity: Entity<Name, any, any, any>;
|
|
2018
|
-
/** What was selected, by member key: `true`, or a relation's nested Selection. */
|
|
2019
|
-
readonly members: Members;
|
|
2020
|
-
readonly schema: S;
|
|
2021
|
-
}
|
|
2022
|
-
type AnySelection<Name extends string = string> = Selection<Name, any, any>;
|
|
2023
|
-
/** The value a Selection reads: `Selected<typeof PostCard>` is one card's fields. */
|
|
2024
|
-
type Selected<S extends Selection<string, unknown, Schema.Constraint>> = S['schema']['Type'];
|
|
2025
|
-
/** `true` for any member; a relation also takes a Selection of its target. */
|
|
2026
|
-
/**
|
|
2027
|
-
* Which part of a `many` relation to read: the first or last so many, from a
|
|
2028
|
-
* cursor or from the end. A cursor is whatever the interpreter handed out with an
|
|
2029
|
-
* earlier page; nothing here looks inside it.
|
|
2030
|
-
*/
|
|
2031
|
-
interface PageWindow {
|
|
2032
|
-
readonly first?: number | undefined;
|
|
2033
|
-
readonly last?: number | undefined;
|
|
2034
|
-
readonly after?: string | undefined;
|
|
2035
|
-
readonly before?: string | undefined;
|
|
2036
|
-
}
|
|
2037
|
-
declare const SelectionPageTypeId: unique symbol;
|
|
2038
|
-
type SelectionPageTypeId = typeof SelectionPageTypeId;
|
|
2039
|
-
/** A Selection of a `many` relation's target, read a page at a time. */
|
|
2040
|
-
interface SelectionPage<Name extends string, S extends Schema.Constraint> {
|
|
2041
|
-
readonly [SelectionPageTypeId]: SelectionPageTypeId;
|
|
2042
|
-
readonly selection: Selection<Name, any, S>;
|
|
2043
|
-
readonly window: PageWindow;
|
|
2044
|
-
}
|
|
2045
|
-
type PageSchema<S extends Schema.Constraint> = Schema.Struct<{
|
|
2046
|
-
readonly items: Schema.$Array<S>;
|
|
2047
|
-
readonly hasNext: Schema.Boolean;
|
|
2048
|
-
readonly hasPrevious: Schema.Boolean;
|
|
2049
|
-
}>;
|
|
2050
|
-
type SelectionSpec<E extends AnyEntity, Spec> = { readonly [K in keyof Spec]: K extends keyof E['relations'] ? E['relations'][K] extends EntityRelation<any, any, infer Target, infer C, any> ? true | AnySelection<Target['name']> | (C extends 'many' ? SelectionPage<Target['name'], any> : never) : never : K extends keyof E['members'] ? true : `"${K & string}" is not a member of ${E['name']}`; };
|
|
2051
|
-
type RelationShape<C extends Cardinality, Optional extends boolean, S extends Schema.Constraint> = C extends 'many' ? Schema.$Array<S> : Optional extends true ? Schema.NullOr<S> : S;
|
|
2052
|
-
type SelectedSchema<Member, Selected> = Member extends EntityField<any, any, infer S> | EntityDerived<any, any, infer S> ? S : Member extends EntityRelation<any, any, infer Target, infer C, infer Optional> ? Selected extends SelectionPage<any, infer S> ? PageSchema<S> : RelationShape<C, Optional, Selected extends Selection<any, any, infer S> ? S : RefSchema<Target>> : never;
|
|
2053
|
-
type SelectionSchema<E extends AnyEntity, Spec> = Schema.Struct<{ readonly [K in keyof Spec & keyof E['members']]: SelectedSchema<E['members'][K], Spec[K]>; }>;
|
|
2054
|
-
/** An input key that carries a relation's target: the id of a `one`, the ids of a `many`. */
|
|
2055
|
-
interface RelationInput<R extends EntityRelation<any, any, any, any, any>> {
|
|
2056
|
-
readonly _tag: 'RelationInput';
|
|
2057
|
-
readonly relation: R;
|
|
2058
|
-
}
|
|
2059
|
-
/**
|
|
2060
|
-
* An input key that carries the relation's target itself, written through an
|
|
2061
|
-
* input of its own: a post created with a new author, an order with its lines.
|
|
2062
|
-
* A `one` holds that input's value, a `many` a list of them.
|
|
2063
|
-
*/
|
|
2064
|
-
interface NestedInput<R extends EntityRelation<any, any, any, any, any>, I extends EntityInput<any, any, any>> {
|
|
2065
|
-
readonly _tag: 'NestedInput';
|
|
2066
|
-
readonly relation: R;
|
|
2067
|
-
readonly input: I;
|
|
2068
|
-
}
|
|
2069
|
-
/** An input key that is about the operation, not the Entity: a reason, a flag, a confirmation. */
|
|
2070
|
-
interface Unmapped {
|
|
2071
|
-
readonly _tag: 'Unmapped';
|
|
2072
|
-
}
|
|
2073
|
-
/** What one key of an operation's input means in terms of the Entity. */
|
|
2074
|
-
type InputMember = EntityField<string, string, Schema.Constraint> | RelationInput<EntityRelation<string, string, AnyEntity, Cardinality, boolean>> | NestedInput<EntityRelation<string, string, AnyEntity, Cardinality, boolean>, EntityInput<AnyEntity, any, any>> | Unmapped;
|
|
2075
|
-
/** The ids a relation takes as input: one id, a nullable id, or a list of ids. */
|
|
2076
|
-
type RelationIds<R> = R extends EntityRelation<any, any, infer Target, infer C, infer Optional> ? C extends 'many' ? ReadonlyArray<IdOf<Target>> : Optional extends true ? IdOf<Target> | null : IdOf<Target> : never;
|
|
2077
|
-
/** What a nested input takes: the value of the target's input, or a list of them for a `many`. */
|
|
2078
|
-
type NestedValues<R, I> = I extends EntityInput<any, infer Fields, any> ? R extends EntityRelation<any, any, any, 'many', any> ? ReadonlyArray<Schema.Struct.Type<Fields>> : Schema.Struct.Type<Fields> : never;
|
|
2079
|
-
type Present<T> = Exclude<T, null | undefined>;
|
|
2080
|
-
/** What an `Option` holds, else the value itself. */
|
|
2081
|
-
type Held<T> = T extends Option.Some<infer A> ? Present<A> : T extends Option.None<any> ? never : Present<T>;
|
|
2082
|
-
/**
|
|
2083
|
-
* Whether an input value fits a field. Only the value that is present has to
|
|
2084
|
-
* fit: an input may leave a key out (a partial update), send `null` to clear
|
|
2085
|
-
* it, or hold an `Option` (an id absent for something new), whose value fits as
|
|
2086
|
-
* it is (an `Option` field) or as what it holds. Whether it may is the input
|
|
2087
|
-
* schema's rule; the fit only guards against mapping the wrong member.
|
|
2088
|
-
*/
|
|
2089
|
-
type Fits<Input, Field> = [Present<Input>] extends [Field] ? true : [Held<Input>] extends [Field] ? true : false;
|
|
2090
|
-
type MappingFor<Name extends string, Input, M> = M extends Unmapped ? M : M extends EntityField<infer Owner, any, any> | RelationInput<EntityRelation<infer Owner, any, any, any, any>> | NestedInput<EntityRelation<infer Owner, any, any, any, any>, any> ? [Owner] extends [Name] ? M extends EntityField<any, any, infer S> ? Fits<Input, Schema.Schema.Type<S>> extends true ? M : 'the input value does not fit this field' : M extends RelationInput<infer R> ? Present<Input> extends RelationIds<R> ? M : 'the input value is not the id shape of this relation' : M extends NestedInput<infer R, infer I> ? R extends EntityRelation<any, any, infer Target, any, any> ? I extends EntityInput<infer Of, any, any> ? [Of['name']] extends [Target['name']] ? Present<Input> extends NestedValues<R, I> ? M : 'the input value is not the value of the nested input' : `the nested input is of ${Of['name']}, not ${Target['name']}` : never : never : never : `this member belongs to ${Owner}` : 'map an input key to a Field, Relation.input(relation), Relation.nested(relation, input), or Entity.unmapped';
|
|
2091
|
-
/** A key that names a field whose value it fits maps itself; every other key needs an entry. */
|
|
2092
|
-
type SelfMapped<E extends AnyEntity, Fields extends Schema.Struct.Fields> = { [K in keyof Fields]: K extends keyof E['fields'] ? Fits<Schema.Schema.Type<Fields[K]>, Schema.Schema.Type<E['fields'][K]['schema']>> extends true ? K : never : never; }[keyof Fields];
|
|
2093
|
-
/**
|
|
2094
|
-
* A mapping written as a member's key: `'title'` is that Field, and `'author'` is
|
|
2095
|
-
* `Relation.input` of that relation. Nothing is inferred from the input key's own
|
|
2096
|
-
* name; the member is still named, only more briefly.
|
|
2097
|
-
*/
|
|
2098
|
-
type Named<E extends AnyEntity, M> = M extends string ? M extends keyof E['fields'] ? E['fields'][M] : M extends keyof E['relations'] ? RelationInput<E['relations'][M]> : `"${M}" names no field or relation of ${E['name']}` : M;
|
|
2099
|
-
type InputMapping<E extends AnyEntity, Fields extends Schema.Struct.Fields, Mapping> = { readonly [K in Exclude<keyof Fields, SelfMapped<E, Fields>>]: unknown; } & { readonly [K in keyof Mapping]: K extends keyof Fields ? Named<E, Mapping[K]> extends (infer Member) ? Member extends string ? Member : MappingFor<E['name'], Schema.Schema.Type<Fields[K]>, Member> extends Member ? Mapping[K] : MappingFor<E['name'], Schema.Schema.Type<Fields[K]>, Member> : never : `"${K & string}" is not a key of the input`; };
|
|
2100
|
-
type InputMembers<E extends AnyEntity, Fields extends Schema.Struct.Fields, Mapping> = { readonly [K in keyof Fields]: K extends keyof Mapping ? Named<E, Mapping[K]> : K extends keyof E['fields'] ? E['fields'][K] : never; };
|
|
2101
|
-
/**
|
|
2102
|
-
* An operation's input read against an Entity: which member each input key
|
|
2103
|
-
* writes. The operation decides what may be submitted; the Entity only says
|
|
2104
|
-
* what each submitted key means.
|
|
2105
|
-
*/
|
|
2106
|
-
interface EntityInput<E extends AnyEntity, Fields extends Schema.Struct.Fields, Members> {
|
|
2107
|
-
readonly entity: E;
|
|
2108
|
-
readonly schema: Schema.Struct<Fields>;
|
|
2109
|
-
readonly members: Members;
|
|
2110
|
-
}
|
|
2111
|
-
/** The Entity member an input key writes, by its key; nothing for an unmapped key. */
|
|
2112
|
-
type WrittenKey<M> = M extends EntityField<any, infer Key, any> ? Key : M extends RelationInput<EntityRelation<any, infer Key, any, any, any>> | NestedInput<EntityRelation<any, infer Key, any, any, any>, any> ? Key : never;
|
|
2113
|
-
/** A nested input reads what it writes of the target; every other member reads as it is. */
|
|
2114
|
-
type WrittenAs<M> = M extends NestedInput<any, EntityInput<infer Of, any, infer Members>> ? Selection<Of['name'], WrittenSpec<Members>, SelectionSchema<Of, WrittenSpec<Members>>> : true;
|
|
2115
|
-
/** Every member the input writes: what to read to show the input's current values. */
|
|
2116
|
-
type WrittenSpec<Members> = { readonly [K in keyof Members as WrittenKey<Members[K]>]: WrittenAs<Members[K]>; };
|
|
2117
|
-
declare const isSameEntity: (left: AnyEntity, right: AnyEntity) => boolean;
|
|
2118
|
-
declare const isEntity: (value: unknown) => value is AnyEntity;
|
|
2119
|
-
declare const Entity: {
|
|
2120
|
-
/** Declares an Entity from its intrinsic fields. Each call is a new identity. */
|
|
2121
|
-
define: <const Name extends string, Fields extends Schema.Struct.Fields>(name: Name, schema: Schema.Struct<Fields>) => Entity<Name, Fields>;
|
|
2122
|
-
/**
|
|
2123
|
-
* Declares the relations between a set of Entities in one step, and returns
|
|
2124
|
-
* them related. One step, because Entities that point at each other cannot
|
|
2125
|
-
* each be declared in terms of the other.
|
|
2126
|
-
*/
|
|
2127
|
-
relate: <const Es extends Entities, const Rs extends RelationMap<Es>>(entities: Es, relations: Rs & CheckRelations<Es, Rs>) => { readonly [K in keyof Es]: Related<Es, Rs, K>; };
|
|
2128
|
-
/**
|
|
2129
|
-
* Selects members of an Entity and assembles the schema of the result. A
|
|
2130
|
-
* relation selected with `true` yields an `EntityRef`; with a Selection of its
|
|
2131
|
-
* target, that Selection's value. `many` yields an array, an optional `one`
|
|
2132
|
-
* is nullable.
|
|
2133
|
-
*/
|
|
2134
|
-
select: <E extends AnyEntity, const Spec>(entity: E, spec: Spec & SelectionSpec<E, Spec>) => Selection<E['name'], Spec, SelectionSchema<E, Spec>, IdOf<E>>;
|
|
2135
|
-
/**
|
|
2136
|
-
* A `many` relation read a page at a time, in place of its Selection:
|
|
2137
|
-
* `comments: Entity.page(CommentSummary, { first: 10 })`. The value is a page
|
|
2138
|
-
* of the Selection's values: `items`, `hasNext`, `hasPrevious`. Reading on from a cursor is another window.
|
|
2139
|
-
*/
|
|
2140
|
-
page: <Name extends string, S extends Schema.Constraint>(selection: Selection<Name, any, S>, window: PageWindow) => SelectionPage<Name, S>;
|
|
2141
|
-
/** Adds readable members that are not part of `entity.schema`. */
|
|
2142
|
-
derived: <const New extends DerivedSpecs>(specs: New) => <E extends AnyEntity>(entity: E & Free<E, New>) => WithDerived<E, New>;
|
|
2143
|
-
/** Attaches an interpreter's metadata to the Entity itself. */
|
|
2144
|
-
annotate: (metadata: Metadata) => <E extends AnyEntity>(entity: E) => E;
|
|
2145
|
-
/**
|
|
2146
|
-
* Declares what every stored row, or every change of one, must hold:
|
|
2147
|
-
* `Entity.invariant('reservedWithinQuantity', ({ after }) =>
|
|
2148
|
-
* Expr.lte(after.reserved, after.quantity), { message })` over the row a
|
|
2149
|
-
* write leaves, or with `before` too over the change an update makes.
|
|
2150
|
-
* Refused where it is declared: an id declared twice, a predicate over another
|
|
2151
|
-
* Entity, one reading an input or nothing of `after` (that is a `when`), a path
|
|
2152
|
-
* naming no field.
|
|
2153
|
-
*/
|
|
2154
|
-
invariant: typeof invariant;
|
|
2155
|
-
/** The Entity's invariants, in the order they were declared. */
|
|
2156
|
-
invariants: typeof invariantsOf;
|
|
2157
|
-
/**
|
|
2158
|
-
* A row (`{ after }`, a create's) or a change (`{ before, after }`, an
|
|
2159
|
-
* update's) under the Entity's invariants: `Valid`, `Invalid` with the ones
|
|
2160
|
-
* that fail (a null fails, as SQL's unknown), or `Undecided` with the fields
|
|
2161
|
-
* one reads that a row lacks.
|
|
2162
|
-
*/
|
|
2163
|
-
judge: typeof judge;
|
|
2164
|
-
/** Whether an invariant reads the row before a write: judged on updates only, and no CHECK. */
|
|
2165
|
-
readsBefore: typeof readsBefore;
|
|
2166
|
-
/** `Entity.judge` against invariants already read, such as those a database binding keeps. */
|
|
2167
|
-
judgeAgainst: typeof judgeAgainst;
|
|
2168
|
-
/**
|
|
2169
|
-
* The row an `Entity.input`'s value describes, judged: what a form can say
|
|
2170
|
-
* before the server does. Undecided for an invariant over a field it leaves unset.
|
|
2171
|
-
*/
|
|
2172
|
-
judgeInput: typeof judgeInput;
|
|
2173
|
-
/** The state invariants as a Schema check over the row type, each failure at its path. */
|
|
2174
|
-
check: typeof check;
|
|
2175
|
-
/** Attaches an interpreter's metadata to members, by key. */
|
|
2176
|
-
annotateMembers: <E extends AnyEntity>(metadata: { readonly [K in keyof E['members']]?: Metadata; }) => (entity: E) => E;
|
|
2177
|
-
/**
|
|
2178
|
-
* Reads an operation's input schema against an Entity. A key that names a
|
|
2179
|
-
* field maps to it; any other key is mapped explicitly, to a Field, to
|
|
2180
|
-
* `Relation.input(relation)`, or to `Entity.unmapped`. Nothing is inferred
|
|
2181
|
-
* from a naming convention such as `authorId`.
|
|
2182
|
-
*/
|
|
2183
|
-
input: <E extends AnyEntity, Fields extends Schema.Struct.Fields, const Mapping = {}>(entity: E, schema: Schema.Struct<Fields>, ...mapping: keyof InputMapping<E, Fields, Mapping> extends never ? [mapping?: Mapping & InputMapping<E, Fields, Mapping>] : [mapping: Mapping & InputMapping<E, Fields, Mapping>]) => EntityInput<E, Fields, InputMembers<E, Fields, Mapping>>;
|
|
2184
|
-
/** For an input key that is about the operation rather than the Entity. */
|
|
2185
|
-
unmapped: Unmapped;
|
|
2186
|
-
/**
|
|
2187
|
-
* The schemas of some of an Entity's fields, by key, to spread into an input's
|
|
2188
|
-
* struct: `Schema.Struct({ ...Entity.fields(Post, 'id', 'title'), editorId: ... })`.
|
|
2189
|
-
* The input then keeps the field's own rules, and its keys map themselves.
|
|
2190
|
-
*/
|
|
2191
|
-
fields: <E extends AnyEntity, const Keys extends ReadonlyArray<keyof E['fields'] & string>>(entity: E, ...keys: Keys) => { readonly [K in Keys[number]]: E['fields'][K]['schema']; };
|
|
2192
|
-
/**
|
|
2193
|
-
* The Selection of every member an input writes, with each relation as refs:
|
|
2194
|
-
* what an edit screen loads before it shows the input's current values.
|
|
2195
|
-
*/
|
|
2196
|
-
selectFor: <E extends AnyEntity, Fields extends Schema.Struct.Fields, Members>(input: EntityInput<E, Fields, Members>) => Selection<E['name'], WrittenSpec<Members>, SelectionSchema<E, WrittenSpec<Members>>, IdOf<E>>;
|
|
2197
|
-
/**
|
|
2198
|
-
* The input values that reproduce `value`, a value read through `selectFor`
|
|
2199
|
-
* (or any Selection with those members): a field as it is, a relation as the
|
|
2200
|
-
* id or ids of its refs. A key whose member `value` lacks is left out, as is
|
|
2201
|
-
* an unmapped key, which the Entity knows nothing about.
|
|
2202
|
-
*/
|
|
2203
|
-
valuesFor: <E extends AnyEntity, Fields extends Schema.Struct.Fields, Members>(input: EntityInput<E, Fields, Members>, value: Readonly<Record<string, unknown>>) => Partial<Schema.Struct.Type<Fields>>;
|
|
2204
|
-
is: typeof isEntity;
|
|
2205
|
-
/** True when both descriptors are versions of one Entity. */
|
|
2206
|
-
same: typeof isSameEntity;
|
|
2207
|
-
};
|
|
2208
|
-
declare function one<Target extends AnyEntity>(target: Target): RelationSpec<Target, 'one', false>;
|
|
2209
|
-
declare function one<Target extends AnyEntity, const Optional extends boolean>(target: Target, options: {
|
|
2210
|
-
readonly optional: Optional;
|
|
2211
|
-
}): RelationSpec<Target, 'one', Optional>;
|
|
2212
|
-
/** What the owner sees: one or many of `target`. Used inside `Entity.relate`. */
|
|
2213
|
-
declare const Relation: {
|
|
2214
|
-
one: typeof one;
|
|
2215
|
-
/** For `Entity.input`: the input key holds this relation's target id, or ids for a `many`. */
|
|
2216
|
-
input: <R extends EntityRelation<any, any, any, any, any>>(relation: R) => RelationInput<R>;
|
|
2217
|
-
/**
|
|
2218
|
-
* For `Entity.input`: the input key holds the relation's target itself, written
|
|
2219
|
-
* through `input`, an `Entity.input` of the target. A `many` holds a list.
|
|
2220
|
-
*/
|
|
2221
|
-
nested: <R extends EntityRelation<any, any, any, any, any>, I extends EntityInput<any, any, any>>(relation: R, input: I) => NestedInput<R, I>;
|
|
2222
|
-
many: <Target extends AnyEntity>(target: Target) => RelationSpec<Target, 'many', false>;
|
|
2223
|
-
};
|
|
2224
|
-
declare const Derived: {
|
|
2225
|
-
make: <S extends Schema.Constraint>(schema: S) => DerivedSpec<S>;
|
|
2226
|
-
};
|
|
2227
|
-
export { Text as $, Operator as $t, SelectionPage as A, AnyQuery as At, Computation as B, EqPredicate as Bt, Relation as C, Law as Cn, MemoryOperators as Ct, RelationSpecs as D, TestedEvidence as Dt, RelationSpec as E, Collation as En, Operators as Et, WrittenAs as F, ChosenSort as Ft, WriteCondition as G, InputExpr as Gt, Write as H, FieldDependency as Ht, WrittenKey as I, CompareOperator as It, WriteSet as J, NextExpr as Jt, WriteIncrement as K, Invalid as Kt, WrittenSpec as L, ComparePredicate as Lt, SelectionTypeId as M, CallExpr as Mt, TargetKey as N, CallOptions as Nt, Selected as O, AndPredicate as Ot, Unmapped as P, ChosenOrder as Pt, ReadFilter as Q, Operation as Qt, AnyWrite as R, ContainsPredicate as Rt, RelatedSpecs as S, noOperators as Sn, MemoryImplementation as St, RelationMap as T, Side as Tn, OperatorDescription as Tt, WriteAssignment as U, FieldExpr as Ut, ConditionedWrite as V, Expr as Vt, WriteComputations as W, InPredicate as Wt, SchemaWords as X, NullPredicate as Xt, SchemaShape as Y, NotPredicate as Yt, Words as Z, Operandish as Zt, IdSchemaOf as _, ValueOf as _n, supported as _t, DerivedSpecs as a, OrderedOperand as an, Judgement as at, PageWindow as b, maxQueryNodes as bn, Evidence as bt, EntityDerived as c, Placement as cn, PredicateTruth as ct, EntityInput as d, QueryDependencies as dn, applyOperator as dt, OperatorDescriptor as en, AfterRow as et, EntityMember as f, Reads as fn, assertSupported as ft, IdOf as g, TextOperand as gn, evaluateEach as gt, EntityTypeId as h, TestPredicate as hn, evaluate as ht, DerivedSpec as i, OrderTerm as in, Invariant as it, SelectionPageTypeId as j, ArgumentOf as jt, Selection as k, AnyExpr as kt, EntityField as l, Predicate as ln, QueryEvaluateError as lt, EntityRelation as m, TermOptions as mn, decided as mt, Cardinality as n, OrPredicate as nn, Change as nt, Entities as o, Ordering as on, Evaluated as ot, EntityRef as p, ReadsOf as pn, compareRows as pt, WriteLink as q, LiteralExpr as qt, Derived as r, Order as rn, DeclaredInvariant as rt, Entity as s, ParameterOf as sn, OperatorMissingError as st, AnyEntity as t, OperatorTable as tn, BeforeRow as tt, EntityIdentity as u, Query as un, Row as ut, InputMember as v, dependenciesOf as vn, truthOver as vt, RelationInput as w, LawDescription as wn, OperatorContext as wt, Related as x, missingName as xn, ImplementedOperator as xt, NestedInput as y, isPredicate as yn, valueOfOperand as yt, BoundWrite as z, Dependencies as zt };
|
|
2228
|
-
//# sourceMappingURL=index-hu8IYlNc.d.mts.map
|