foldkit-entity 0.2.0 → 0.4.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 +84 -6
- package/dist/conformance/index.d.mts +46 -0
- package/dist/conformance/index.d.mts.map +1 -0
- package/dist/conformance/index.mjs +293 -0
- package/dist/conformance/index.mjs.map +1 -0
- package/dist/index.d.mts +93 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +191 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/expr.ts","../src/index.ts"],"mappings":";;;;iBAiBiB,YAAY;WAClB;WACA,OAAO;;;;;;iBAOD,UAAU;WAChB;WACA,OAAO;WACP;WACA,QAAQ,OAAO,MAAM;;;;;;;;iBASf,UAAU;WAChB;WACA;WACA,QAAQ,OAAO,MAAM;;;YAIpB,KAAK,KAAK,YAAY,KAAK,UAAU,KAAK,UAAU;;YAGpD,UAAU;;;;;;;;;YAUV,YAAY,cAAc,gBAAgB;;iBAGrC;WACN;WACA,MAAM;WACN,OAAO;;;;;;;iBAQD;WACN;WACA,SAAS;WACT;;;;;;;iBAQM;WACN;WACA,OAAO;WACP,QAAQ;;;;;;;;YASP,aAAa,UAAU;;KAG9B,QAAQ,KAAK,4BAA4B,OAAO,cAAc,KAAK,KAAK;;YAGjE,QAAQ,KAClB,UAAU,kCAAkC,KACxC,OAAO,OAAO,KAAK,KACnB,UAAU,sBAER,UAAU,WAAW,KACnB;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/expr.ts","../src/evaluate.ts","../src/index.ts"],"mappings":";;;;iBAiBiB,YAAY;WAClB;WACA,OAAO;;;;;;iBAOD,UAAU;WAChB;WACA,OAAO;WACP;WACA,QAAQ,OAAO,MAAM;;;;;;;;iBASf,UAAU;WAChB;WACA;WACA,QAAQ,O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cAAc;;;;;EAO9E,SAAA,UAAU,yCAAyC,UAAU,4BAA0B,UACpF,GAAC,OACJ,MACN,YAAY,GAAG;EAEX,OAAA,eAAe,WAAS,QAAU,WAAS,aAAa;;qBAOpD;EACJ,OAAA,UAAU,OAAO,YAAU,QAAU,MAAI,YAAY"}
|
package/dist/index.mjs
CHANGED
|
@@ -119,7 +119,9 @@ const Expr = {
|
|
|
119
119
|
_tag: "Contains",
|
|
120
120
|
value: toExpr(value, "contains"),
|
|
121
121
|
search: toOperand(search)
|
|
122
|
-
})
|
|
122
|
+
}),
|
|
123
|
+
/** An expression as readable text; see `showExpr` for what it is and is not. */
|
|
124
|
+
show: (node) => showExpr(node)
|
|
123
125
|
};
|
|
124
126
|
const Order = {
|
|
125
127
|
asc: (expr) => ({
|
|
@@ -248,7 +250,193 @@ const Query = {
|
|
|
248
250
|
* What the whole query reads: the fields and inputs of every predicate and
|
|
249
251
|
* every ordering term, and the operations it uses.
|
|
250
252
|
*/
|
|
251
|
-
dependencies: (self) => dependenciesOf(...self.where, ...self.orderBy)
|
|
253
|
+
dependencies: (self) => dependenciesOf(...self.where, ...self.orderBy),
|
|
254
|
+
/**
|
|
255
|
+
* The operations this query needs that `supported` does not list, in the
|
|
256
|
+
* order the query uses them. Empty means an interpreter declaring
|
|
257
|
+
* `supported` can answer it.
|
|
258
|
+
*
|
|
259
|
+
* An interpreter declares what it runs and refuses the rest, rather than
|
|
260
|
+
* ignoring an operation it does not implement — which would answer a
|
|
261
|
+
* different question and say nothing. The refusal is the interpreter's to
|
|
262
|
+
* raise: this only says what is missing, because what to do about it differs
|
|
263
|
+
* between one that compiles at registration and one that runs a body
|
|
264
|
+
* directly.
|
|
265
|
+
*
|
|
266
|
+
* ```ts
|
|
267
|
+
* const missing = Query.unsupported(body, ['eq', 'isNull', 'isNotNull'])
|
|
268
|
+
* if (missing.length > 0) throw new Error(`cannot run ${missing.join(', ')}`)
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
unsupported: (self, supported) => {
|
|
272
|
+
const runs = new Set(supported);
|
|
273
|
+
return Query.dependencies(self).operations.filter((operation) => !runs.has(operation));
|
|
274
|
+
},
|
|
275
|
+
/**
|
|
276
|
+
* The whole query as readable text, one clause per line:
|
|
277
|
+
*
|
|
278
|
+
* ```text
|
|
279
|
+
* FROM Post
|
|
280
|
+
* WHERE Post.slug = $slug
|
|
281
|
+
* AND Post.published = true
|
|
282
|
+
* ORDER BY Post.updatedAt DESC
|
|
283
|
+
* ```
|
|
284
|
+
*
|
|
285
|
+
* For an explanation or a diagnostic, never for an interpreter — see
|
|
286
|
+
* `Expr.show`. A query with no `where` or no `orderBy` omits that line rather
|
|
287
|
+
* than printing an empty one, so what is shown is what was written.
|
|
288
|
+
*/
|
|
289
|
+
show: (self) => [
|
|
290
|
+
`FROM ${self.entity.name}`,
|
|
291
|
+
...self.where.map((predicate, index) => `${index === 0 ? "WHERE" : " AND"} ${Expr.show(predicate)}`),
|
|
292
|
+
...self.orderBy.length === 0 ? [] : [`ORDER BY ${self.orderBy.map((term) => `${Expr.show(term.expr)} ${term.direction.toUpperCase()}`).join(", ")}`]
|
|
293
|
+
].join("\n")
|
|
294
|
+
};
|
|
295
|
+
/**
|
|
296
|
+
* An expression as readable text, in the IR's own terms.
|
|
297
|
+
*
|
|
298
|
+
* This is for a human — an explanation, a diagnostic, a test that wants to
|
|
299
|
+
* assert on a whole predicate at once — and deliberately **not** for an
|
|
300
|
+
* interpreter. It is close enough to SQL to read at a glance and unlike it
|
|
301
|
+
* everywhere that matters: an input is `$slug` rather than a bound parameter,
|
|
302
|
+
* `contains` is named rather than rendered as somebody's `like`, and no
|
|
303
|
+
* dialect's escaping or collation is implied. What actually ran is whatever
|
|
304
|
+
* that backend compiled, which is not this.
|
|
305
|
+
*
|
|
306
|
+
* A predicate standing where a value is wanted is parenthesised, because
|
|
307
|
+
* `x is not null = $archived` reads as three operands and is two. A predicate
|
|
308
|
+
* that *is* the whole expression is not: nothing encloses it.
|
|
309
|
+
*/
|
|
310
|
+
const showExpr = (node) => {
|
|
311
|
+
switch (node._tag) {
|
|
312
|
+
case "Literal": return JSON.stringify(node.value) ?? String(node.value);
|
|
313
|
+
case "Field": return `${node.owner.name}.${node.key}`;
|
|
314
|
+
case "Input": return `$${node.key}`;
|
|
315
|
+
case "Eq": return `${showOperand(node.left)} = ${showOperand(node.right)}`;
|
|
316
|
+
case "Null": return `${showOperand(node.operand)} is ${node.present ? "not null" : "null"}`;
|
|
317
|
+
case "Contains": return `contains(${showOperand(node.value)}, ${showOperand(node.search)})`;
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
const showOperand = (node) => isPredicate(node) ? `(${showExpr(node)})` : showExpr(node);
|
|
321
|
+
//#endregion
|
|
322
|
+
//#region src/evaluate.ts
|
|
323
|
+
/**
|
|
324
|
+
* The reference interpreter: a query body run over rows already in memory.
|
|
325
|
+
*
|
|
326
|
+
* `foldkit-remote-drizzle` compiles a body to SQL. This runs the same body
|
|
327
|
+
* directly, which is what makes "source-neutral" a fact rather than a claim —
|
|
328
|
+
* the two are checked against each other over a real database, and a body that
|
|
329
|
+
* means one thing here and another there is a bug in one of them.
|
|
330
|
+
*
|
|
331
|
+
* It is also the interpreter to reach for when there is no database: a test
|
|
332
|
+
* that wants rows, a server assembling a page from values it already holds, or
|
|
333
|
+
* a client judging rows it already has.
|
|
334
|
+
*
|
|
335
|
+
* It lives in `foldkit-entity` because it is the **reference semantics** of the
|
|
336
|
+
* IR this package owns — what §6.0.1 means operationally rather than in prose —
|
|
337
|
+
* and a specification's reference implementation belongs with the
|
|
338
|
+
* specification. The rule that keeps it here: **it may depend on the IR and
|
|
339
|
+
* nothing else.** The moment it wants a Remote concept it has moved to the
|
|
340
|
+
* wrong place and should leave.
|
|
341
|
+
*
|
|
342
|
+
* **It follows SQL, not JavaScript.** Where the two disagree this follows SQL,
|
|
343
|
+
* because SQL is what the other interpreter runs. The important case is null:
|
|
344
|
+
* `null = null` is unknown in SQL and a row is not matched by it, where
|
|
345
|
+
* JavaScript would happily call the two equal.
|
|
346
|
+
*/
|
|
347
|
+
var QueryEvaluateError = class extends Error {
|
|
348
|
+
constructor(message) {
|
|
349
|
+
super(`[foldkit-entity] ${message}`);
|
|
350
|
+
this.name = "QueryEvaluateError";
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
const valueOf = (node, row, input) => {
|
|
354
|
+
switch (node._tag) {
|
|
355
|
+
case "Field": return row[node.key];
|
|
356
|
+
case "Literal": return node.value;
|
|
357
|
+
case "Input": return input[node.key];
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
const isNull = (value) => value === null || value === void 0;
|
|
361
|
+
/** A predicate used as a value is its own truth; anything else is its value. */
|
|
362
|
+
const sideOf = (node, row, input) => isPredicate(node) ? truthValue(holds(node, row, input)) : valueOf(node, row, input);
|
|
363
|
+
/**
|
|
364
|
+
* A predicate compared as a value. SQL's unknown is null there too, so an
|
|
365
|
+
* unknown compared to anything stays unknown rather than becoming false.
|
|
366
|
+
*/
|
|
367
|
+
const truthValue = (truth) => truth === "unknown" ? null : truth;
|
|
368
|
+
const holds = (node, row, input) => {
|
|
369
|
+
switch (node._tag) {
|
|
370
|
+
case "Eq": {
|
|
371
|
+
const left = sideOf(node.left, row, input);
|
|
372
|
+
const right = sideOf(node.right, row, input);
|
|
373
|
+
if (isNull(left) || isNull(right)) return "unknown";
|
|
374
|
+
return left === right;
|
|
375
|
+
}
|
|
376
|
+
case "Null": return isNull(valueOf(node.operand, row, input)) !== node.present;
|
|
377
|
+
case "Contains": {
|
|
378
|
+
const value = valueOf(node.value, row, input);
|
|
379
|
+
const search = sideOf(node.search, row, input);
|
|
380
|
+
if (isNull(value) || isNull(search)) return "unknown";
|
|
381
|
+
if (typeof value !== "string" || typeof search !== "string") throw new QueryEvaluateError("a containment test was given something that is not text");
|
|
382
|
+
return value.toLowerCase().includes(search.toLowerCase());
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
/** Every predicate must hold: the list is the conjunction, as it is in a `Query`. */
|
|
387
|
+
const matches = (body, row, input) => body.where.every((node) => holds(node, row, input) === true);
|
|
388
|
+
const compare = (left, right, query) => {
|
|
389
|
+
if (typeof left === "string" && typeof right === "string") return left < right ? -1 : left > right ? 1 : 0;
|
|
390
|
+
if (typeof left === "number" && typeof right === "number") return left - right;
|
|
391
|
+
if (typeof left === "boolean" && typeof right === "boolean") return Number(left) - Number(right);
|
|
392
|
+
throw new QueryEvaluateError(`query "${query}" orders by values this interpreter cannot compare (${typeof left} and ${typeof right})`);
|
|
393
|
+
};
|
|
394
|
+
const ordered = (rows, terms, query) => {
|
|
395
|
+
if (terms.length === 0) return [...rows];
|
|
396
|
+
return [...rows].sort((left, right) => {
|
|
397
|
+
for (const term of terms) {
|
|
398
|
+
if (term.expr._tag !== "Field") throw new QueryEvaluateError(`query "${query}" orders by something that is not a field, which this interpreter cannot run yet`);
|
|
399
|
+
const a = left[term.expr.key];
|
|
400
|
+
const b = right[term.expr.key];
|
|
401
|
+
if (isNull(a) || isNull(b)) throw new QueryEvaluateError(`query "${query}" orders by "${term.expr.key}", which is null in a row; where nulls sort is a thing databases disagree about, so it is outside what this interpreter will answer for`);
|
|
402
|
+
const sign = compare(a, b, query);
|
|
403
|
+
if (sign !== 0) return term.direction === "asc" ? sign : -sign;
|
|
404
|
+
}
|
|
405
|
+
return 0;
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
/**
|
|
409
|
+
* The operations this interpreter runs. It is the reference, so this is the
|
|
410
|
+
* whole kernel — but it is declared rather than assumed, because an
|
|
411
|
+
* interpreter that gains an operator and forgets to say so is exactly what
|
|
412
|
+
* `Query.unsupported` exists to catch.
|
|
413
|
+
*/
|
|
414
|
+
const supported = [
|
|
415
|
+
"eq",
|
|
416
|
+
"isNull",
|
|
417
|
+
"isNotNull",
|
|
418
|
+
"contains"
|
|
419
|
+
];
|
|
420
|
+
/**
|
|
421
|
+
* Refuses a body needing an operation this interpreter does not run, rather
|
|
422
|
+
* than skipping it and answering a different question. Called by `evaluate`;
|
|
423
|
+
* call it directly to check a body once instead of on every run.
|
|
424
|
+
*/
|
|
425
|
+
const assertSupported = (body) => {
|
|
426
|
+
const missing = Query.unsupported(body, supported);
|
|
427
|
+
if (missing.length > 0) throw new QueryEvaluateError(`this interpreter does not run ${missing.join(", ")}, which query "${body.entity.name}" needs`);
|
|
428
|
+
};
|
|
429
|
+
/**
|
|
430
|
+
* The rows the body matches, in the order it asks for. Pure: it reads the rows
|
|
431
|
+
* it is given and nothing else.
|
|
432
|
+
*
|
|
433
|
+
* `input` holds the values this run was given, by the keys the body's
|
|
434
|
+
* placeholders were built with — the same record the query would be called
|
|
435
|
+
* with anywhere else.
|
|
436
|
+
*/
|
|
437
|
+
const evaluate = (body, input, rows) => {
|
|
438
|
+
assertSupported(body);
|
|
439
|
+
return ordered(rows.filter((row) => matches(body, row, input)), body.orderBy, body.entity.name);
|
|
252
440
|
};
|
|
253
441
|
//#endregion
|
|
254
442
|
//#region src/index.ts
|
|
@@ -584,6 +772,6 @@ const Relation = {
|
|
|
584
772
|
};
|
|
585
773
|
const Derived = { make: (schema) => ({ schema }) };
|
|
586
774
|
//#endregion
|
|
587
|
-
export { Derived, Entity, EntityTypeId, Expr, Order, Query, Relation, SelectionPageTypeId, SelectionTypeId, dependenciesOf, isPredicate };
|
|
775
|
+
export { Derived, Entity, EntityTypeId, Expr, Order, Query, QueryEvaluateError, Relation, SelectionPageTypeId, SelectionTypeId, assertSupported, dependenciesOf, evaluate, isPredicate, supported };
|
|
588
776
|
|
|
589
777
|
//# sourceMappingURL=index.mjs.map
|