@atscript/db 0.1.146 → 0.1.147
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/dist/agg.cjs +1 -1
- package/dist/agg.d.cts +1 -1
- package/dist/agg.d.mts +1 -1
- package/dist/agg.mjs +1 -1
- package/dist/{aggregate-fns-CfsveE1w.mjs → aggregate-fns-C0BymEqa.mjs} +3 -1
- package/dist/{aggregate-fns-CGBv3E8S.cjs → aggregate-fns-CtsRbZn9.cjs} +8 -0
- package/dist/{buckets-DYFu0eZ8.d.cts → buckets-CjdPipCC.d.cts} +522 -17
- package/dist/{buckets-DRycmhOW.d.mts → buckets-DYopfd2Y.d.mts} +522 -17
- package/dist/{column-diff-e2oHc71_.mjs → column-diff-CO06iQvI.mjs} +862 -115
- package/dist/{column-diff-D_Kyuh0S.cjs → column-diff-Dt7EDJsA.cjs} +872 -113
- package/dist/{column-diff-Q9UmWn5x.d.mts → fk-diff-BtOmL4OW.d.mts} +48 -2
- package/dist/{column-diff-w-Mym_3w.d.cts → fk-diff-DLqwVkKG.d.cts} +48 -2
- package/dist/index.cjs +28 -33
- package/dist/index.d.cts +40 -15
- package/dist/index.d.mts +40 -15
- package/dist/index.mjs +5 -33
- package/dist/{nested-writer-CnOOAehr.mjs → nested-writer-D35UsDBT.mjs} +542 -2
- package/dist/{nested-writer-xfQwxplL.cjs → nested-writer-SqtYJHF0.cjs} +666 -0
- package/dist/plugin.cjs +266 -5
- package/dist/plugin.mjs +267 -6
- package/dist/rel.cjs +2 -2
- package/dist/rel.d.cts +2 -2
- package/dist/rel.d.mts +2 -2
- package/dist/rel.mjs +2 -2
- package/dist/{relation-helpers-B-0NRKat.d.mts → relation-helpers-C2bLtE-t.d.mts} +1 -1
- package/dist/{relation-helpers-BOMm_HUI.d.cts → relation-helpers-DH3LvwLV.d.cts} +1 -1
- package/dist/relation-loader-BFhVuJ-B.cjs +369 -0
- package/dist/relation-loader-C0qE8xgR.mjs +370 -0
- package/dist/shared.cjs +1 -1
- package/dist/shared.d.cts +4 -3
- package/dist/shared.d.mts +4 -3
- package/dist/shared.mjs +1 -1
- package/dist/sync.cjs +10 -6
- package/dist/sync.d.cts +2 -25
- package/dist/sync.d.mts +2 -25
- package/dist/sync.mjs +10 -6
- package/dist/{validation-utils-DOsB4e6G.cjs → validation-utils-Da2GjobR.cjs} +35 -24
- package/dist/{validation-utils-CMR4fe2M.mjs → validation-utils-Dq0uZ7ef.mjs} +35 -24
- package/dist/{validator-Bw6ks9Hy.d.mts → validator-CMnTI6r7.d.cts} +9 -6
- package/dist/{validator-Bw6ks9Hy.d.cts → validator-CMnTI6r7.d.mts} +9 -6
- package/dist/validator.d.cts +1 -1
- package/dist/validator.d.mts +1 -1
- package/package.json +8 -8
- package/dist/relation-loader-CBPY6kM7.cjs +0 -461
- package/dist/relation-loader-D9XuXaMv.mjs +0 -462
package/dist/plugin.cjs
CHANGED
|
@@ -33,10 +33,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
33
33
|
enumerable: true
|
|
34
34
|
}) : target, mod));
|
|
35
35
|
//#endregion
|
|
36
|
-
const require_aggregate_fns = require("./aggregate-fns-
|
|
36
|
+
const require_aggregate_fns = require("./aggregate-fns-CtsRbZn9.cjs");
|
|
37
37
|
const require_derived_rules = require("./derived-rules-YstgIxG-.cjs");
|
|
38
38
|
require("./consts-BzRfCcH2.cjs");
|
|
39
|
-
const require_validation_utils = require("./validation-utils-
|
|
39
|
+
const require_validation_utils = require("./validation-utils-Da2GjobR.cjs");
|
|
40
40
|
let node_path = require("node:path");
|
|
41
41
|
node_path = __toESM(node_path, 1);
|
|
42
42
|
let node_url = require("node:url");
|
|
@@ -174,6 +174,27 @@ function viewHavingScope(view) {
|
|
|
174
174
|
unqualifiedTarget: view.id
|
|
175
175
|
} : void 0;
|
|
176
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* The ordering (4th argument) of a first-row `@db.view.joins`: the join
|
|
179
|
+
* target only — an unqualified key is a field of the target.
|
|
180
|
+
* @since 0.1.147
|
|
181
|
+
*/
|
|
182
|
+
function joinOrderScope(join) {
|
|
183
|
+
const target = join.args[0]?.text;
|
|
184
|
+
return target ? {
|
|
185
|
+
allowedTypes: [target],
|
|
186
|
+
unqualifiedTarget: target
|
|
187
|
+
} : void 0;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The expression of a `@db.compute` on a view field: the view's own fields,
|
|
191
|
+
* unqualified — the `@db.view.having` scope.
|
|
192
|
+
* @since 0.1.147
|
|
193
|
+
*/
|
|
194
|
+
function computeScope(propToken) {
|
|
195
|
+
const view = require_validation_utils.getDbTableOwner(propToken);
|
|
196
|
+
return view ? viewHavingScope(view) : void 0;
|
|
197
|
+
}
|
|
177
198
|
/** The condition of a `@db.agg.*` on a view field: the scope of the view's `@db.view.filter`. */
|
|
178
199
|
function aggConditionScope(propToken) {
|
|
179
200
|
const view = require_validation_utils.getDbTableOwner(propToken);
|
|
@@ -208,16 +229,25 @@ function relFilterScope(field) {
|
|
|
208
229
|
unqualifiedTarget: target
|
|
209
230
|
};
|
|
210
231
|
}
|
|
232
|
+
/** The `@db.view.joins` annotation one of whose arguments is `arg`. */
|
|
233
|
+
function joinOfArg(joins, arg) {
|
|
234
|
+
return joins.find((a) => a.args.includes(arg));
|
|
235
|
+
}
|
|
211
236
|
/** The `fieldScope` hooks (argument token → scope) the annotation specs declare. */
|
|
212
237
|
const fieldScopes = {
|
|
213
238
|
viewFilter: (arg) => arg.parentNode ? viewFilterScope(arg.parentNode) : void 0,
|
|
214
239
|
viewJoin: (arg) => {
|
|
215
240
|
const view = arg.parentNode;
|
|
216
241
|
const joins = view ? require_validation_utils.viewJoins(view) : [];
|
|
217
|
-
const join = joins
|
|
242
|
+
const join = joinOfArg(joins, arg);
|
|
218
243
|
return view && join ? viewJoinScope(view, join, require_validation_utils.earlierJoinTargets(joins, join)) : void 0;
|
|
219
244
|
},
|
|
245
|
+
joinOrder: (arg) => {
|
|
246
|
+
const join = arg.parentNode ? joinOfArg(require_validation_utils.viewJoins(arg.parentNode), arg) : void 0;
|
|
247
|
+
return join ? joinOrderScope(join) : void 0;
|
|
248
|
+
},
|
|
220
249
|
viewHaving: (arg) => arg.parentNode ? viewHavingScope(arg.parentNode) : void 0,
|
|
250
|
+
compute: computeScope,
|
|
221
251
|
aggCondition: aggConditionScope,
|
|
222
252
|
aggField: aggFieldScope,
|
|
223
253
|
relFilter: (arg) => arg.parentNode ? relFilterScope(arg.parentNode) : void 0
|
|
@@ -868,6 +898,127 @@ function columnCapability(capability, verb) {
|
|
|
868
898
|
});
|
|
869
899
|
}
|
|
870
900
|
//#endregion
|
|
901
|
+
//#region src/plugin/annotations/compute.ts
|
|
902
|
+
/** What a `@db.compute` field cannot also carry (VC1). */
|
|
903
|
+
const EXCLUSIVE_WITH = [
|
|
904
|
+
...require_aggregate_fns.AGG_ANNOTATIONS,
|
|
905
|
+
"db.json",
|
|
906
|
+
"db.ignore"
|
|
907
|
+
].map((key) => ({ key }));
|
|
908
|
+
/** Primitive tags of timestamps (MySQL may store them as native `TIMESTAMP`). */
|
|
909
|
+
const TIMESTAMP_TAGS = [
|
|
910
|
+
"timestamp",
|
|
911
|
+
"created",
|
|
912
|
+
"updated"
|
|
913
|
+
];
|
|
914
|
+
/** The `@db.compute` expression token of a prop, if any. */
|
|
915
|
+
function computeArg(prop) {
|
|
916
|
+
return prop?.annotations?.find((a) => a.name === "db.compute")?.args[0];
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* Whether a prop is typed exactly `number` (a plain primitive ref — no chain
|
|
920
|
+
* ref, no `number.*` extension).
|
|
921
|
+
*/
|
|
922
|
+
function isPlainNumber(prop, doc) {
|
|
923
|
+
const def = prop.getDefinition();
|
|
924
|
+
if (!def || !(0, _atscript_core.isRef)(def) || def.hasChain || def.id !== "number") return false;
|
|
925
|
+
return (0, _atscript_core.isPrimitive)(doc.unwindType("number")?.def);
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* Why an operand prop cannot be computed with, or `undefined` when it can:
|
|
929
|
+
* its resolved type must be a `number` primitive — not `decimal`, not a
|
|
930
|
+
* timestamp — and it must have storage (no `@db.ignore`).
|
|
931
|
+
*/
|
|
932
|
+
function operandProblem(prop, doc) {
|
|
933
|
+
if (prop.countAnnotations("db.ignore") > 0) return "is @db.ignore'd";
|
|
934
|
+
if (computeArg(prop)) return void 0;
|
|
935
|
+
const def = prop.getDefinition();
|
|
936
|
+
const leaf = def && (0, _atscript_core.isRef)(def) ? doc.unwindType(def.id, def.chain)?.def : def;
|
|
937
|
+
const base = require_validation_utils.primitiveBaseType(leaf);
|
|
938
|
+
if (base === void 0) return void 0;
|
|
939
|
+
if (base !== "number") return `is ${base === "decimal" ? "a decimal" : `a ${base}`}`;
|
|
940
|
+
if ((0, _atscript_core.isPrimitive)(leaf) && TIMESTAMP_TAGS.some((t) => leaf.tags.has(t))) return "is a timestamp";
|
|
941
|
+
}
|
|
942
|
+
/** VC6 — whether an expression may be NULL (`/` by zero, an optional operand). */
|
|
943
|
+
function exprNullable(node, props) {
|
|
944
|
+
if ("value" in node) return false;
|
|
945
|
+
if ("left" in node) return node.op === "/" || exprNullable(node.left, props) || exprNullable(node.right, props);
|
|
946
|
+
if ("operand" in node) return exprNullable(node.operand, props);
|
|
947
|
+
if ("args" in node) return node.args.every((a) => exprNullable(a, props));
|
|
948
|
+
return props.get(node.fieldRef.text)?.has("optional") ?? false;
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* VC4 — the first `@db.compute` cycle through `start`, as field names
|
|
952
|
+
* (`rank → priority → rank`), or `undefined`.
|
|
953
|
+
*/
|
|
954
|
+
function findComputeCycle(start, props) {
|
|
955
|
+
const path = [];
|
|
956
|
+
const done = /* @__PURE__ */ new Set();
|
|
957
|
+
const visit = (name) => {
|
|
958
|
+
const at = path.indexOf(name);
|
|
959
|
+
if (at !== -1) return name === start ? [...path.slice(at), name] : void 0;
|
|
960
|
+
if (done.has(name)) return void 0;
|
|
961
|
+
const refs = computeArg(props.get(name))?.exprNode?.fieldRefs() ?? [];
|
|
962
|
+
path.push(name);
|
|
963
|
+
for (const ref of refs) {
|
|
964
|
+
if (ref.typeRef) continue;
|
|
965
|
+
const cycle = visit(ref.fieldRef.text);
|
|
966
|
+
if (cycle) return cycle;
|
|
967
|
+
}
|
|
968
|
+
path.pop();
|
|
969
|
+
done.add(name);
|
|
970
|
+
};
|
|
971
|
+
return visit(start);
|
|
972
|
+
}
|
|
973
|
+
function validateCompute(token, args, doc) {
|
|
974
|
+
const errors = [];
|
|
975
|
+
const prop = token.parentNode;
|
|
976
|
+
const field = prop.id ?? "field";
|
|
977
|
+
const view = require_validation_utils.getDbTableOwner(token);
|
|
978
|
+
const error = (message, range = token.range) => errors.push({
|
|
979
|
+
message,
|
|
980
|
+
severity: 1,
|
|
981
|
+
range
|
|
982
|
+
});
|
|
983
|
+
if (!view || view.countAnnotations("db.view.for") === 0) {
|
|
984
|
+
error("@db.compute is only valid on a field of a @db.view.for view");
|
|
985
|
+
return errors;
|
|
986
|
+
}
|
|
987
|
+
errors.push(...require_validation_utils.validateExclusiveWith(token, "@db.compute", EXCLUSIVE_WITH));
|
|
988
|
+
if (!isPlainNumber(prop, doc)) error(`Field "${field}" has a @db.compute and must be typed \`number\``);
|
|
989
|
+
const expr = args[0]?.exprNode;
|
|
990
|
+
if (!expr) return errors;
|
|
991
|
+
const refs = expr.fieldRefs();
|
|
992
|
+
const props = viewProps(view) ?? /* @__PURE__ */ new Map();
|
|
993
|
+
if (refs.length === 0) error("@db.compute needs at least one view field — a constant column is not supported", args[0].range);
|
|
994
|
+
const scope = viewHavingScope(view);
|
|
995
|
+
if (scope) errors.push(...require_validation_utils.validateQueryScope(args[0], scope, doc, "@db.compute references the view's own fields — declare it on the view first (`field: Type.field`)"));
|
|
996
|
+
for (const ref of refs) {
|
|
997
|
+
if (ref.typeRef) continue;
|
|
998
|
+
const operand = props.get(ref.fieldRef.text);
|
|
999
|
+
const why = operand && operandProblem(operand, doc);
|
|
1000
|
+
if (why) error(`@db.compute operand '${ref.fieldRef.text}' ${why} — operands must be number fields`, ref.fieldRef.range);
|
|
1001
|
+
}
|
|
1002
|
+
if (prop.id) {
|
|
1003
|
+
const cycle = findComputeCycle(prop.id, props);
|
|
1004
|
+
if (cycle) error(`@db.compute of "${field}" depends on itself: ${cycle.join(" → ")}`, args[0].range);
|
|
1005
|
+
}
|
|
1006
|
+
if (!prop.has("optional") && exprNullable(expr.expression, props)) error(`Field "${field}" has a @db.compute that may be NULL and must be optional (${field}?: …) — it is NULL when an operand is NULL or a divisor is 0`);
|
|
1007
|
+
return errors;
|
|
1008
|
+
}
|
|
1009
|
+
const dbComputeAnnotations = { compute: new _atscript_core.AnnotationSpec({
|
|
1010
|
+
description: "Declares a **computed view column** (since 0.1.147): closed arithmetic over the view's own fields — dimensions, aggregates, first-row-join fields and other computed fields — evaluated by the database, so the column sorts, filters and pages like any other.\n\nGrammar: `+ - * /`, unary `-`, parentheses, numeric literals, `coalesce(a, b, …)`. Every value is a double (`7 / 2 = 3.5`); a NULL operand yields NULL; division by zero yields NULL — so an expression with `/` or an optional operand needs an optional field.\n\n**Example:**\n```atscript\n@db.agg.count 'id', `Issue.status = 'open'`\nopenCount: Issue.id\n\n@db.compute `openCount * 10 + coalesce(oldestSeverity, 0)`\nrank: number\n```\n",
|
|
1011
|
+
nodeType: ["prop"],
|
|
1012
|
+
passedWhenReferred: false,
|
|
1013
|
+
argument: {
|
|
1014
|
+
name: "expression",
|
|
1015
|
+
type: "expr",
|
|
1016
|
+
description: "Arithmetic over the view's own fields (unqualified): `+ - * /`, unary `-`, parentheses, numbers, `coalesce(a, b, …)`.",
|
|
1017
|
+
fieldScope: fieldScopes.compute
|
|
1018
|
+
},
|
|
1019
|
+
validate: validateCompute
|
|
1020
|
+
}) };
|
|
1021
|
+
//#endregion
|
|
871
1022
|
//#region src/plugin/annotations/index-ann.ts
|
|
872
1023
|
const dbIndexAnnotations = { index: {
|
|
873
1024
|
plain: new _atscript_core.AnnotationSpec({
|
|
@@ -1258,8 +1409,23 @@ const dbRelAnnotations = { rel: {
|
|
|
1258
1409
|
return errors;
|
|
1259
1410
|
}
|
|
1260
1411
|
}),
|
|
1412
|
+
filterable: new _atscript_core.AnnotationSpec({
|
|
1413
|
+
description: "Lets HTTP clients filter the parent rows by this relation's related rows — `ticket=$some(status=open)` / `ticket=$none(...)` in a moost-db query (`{ ticket: { $some: { status: 'open' } } }`). Off by default: a client predicate filters the PARENT set and runs a correlated subquery. Server-side code (`transformFilter`, `actionRowScope`, direct table calls) never needs it. The related table's own field rules (visibility, encryption, write-only) apply inside the predicate.\n\n**Example:**\n```atscript\n@db.rel.to\n@db.rel.filterable\nticket?: Ticket\n```\n",
|
|
1414
|
+
nodeType: ["prop"],
|
|
1415
|
+
passedWhenReferred: false,
|
|
1416
|
+
validate(token) {
|
|
1417
|
+
const errors = [];
|
|
1418
|
+
const field = token.parentNode;
|
|
1419
|
+
if (!(field.countAnnotations("db.rel.to") > 0 || field.countAnnotations("db.rel.from") > 0 || field.countAnnotations("db.rel.via") > 0)) errors.push({
|
|
1420
|
+
message: "@db.rel.filterable is only valid on navigational fields (@db.rel.to, @db.rel.from, or @db.rel.via)",
|
|
1421
|
+
severity: 1,
|
|
1422
|
+
range: token.range
|
|
1423
|
+
});
|
|
1424
|
+
return errors;
|
|
1425
|
+
}
|
|
1426
|
+
}),
|
|
1261
1427
|
filter: new _atscript_core.AnnotationSpec({
|
|
1262
|
-
description: "Applies a filter to a navigational property, restricting which related records are loaded.\n\n**Example:**\n```atscript\n@db.rel.from\n@db.rel.filter `Post.published = true`\npublishedPosts: Post[]\n```\n",
|
|
1428
|
+
description: "Applies a filter to a navigational property, restricting which related records are loaded (`$with`) and which count in relational predicates (`$some` / `$none`).\n\n**Example:**\n```atscript\n@db.rel.from\n@db.rel.filter `Post.published = true`\npublishedPosts: Post[]\n```\n",
|
|
1263
1429
|
nodeType: ["prop"],
|
|
1264
1430
|
passedWhenReferred: false,
|
|
1265
1431
|
argument: {
|
|
@@ -1285,10 +1451,40 @@ const dbRelAnnotations = { rel: {
|
|
|
1285
1451
|
if (!args[0]?.queryNode) return errors;
|
|
1286
1452
|
const scope = relFilterScope(field);
|
|
1287
1453
|
if (scope) errors.push(...require_validation_utils.validateQueryScope(args[0], scope, doc));
|
|
1454
|
+
const expression = args[0].queryNode.expression;
|
|
1455
|
+
forEachComparison(expression, (cmp) => {
|
|
1456
|
+
if (cmp.right && "fieldRef" in cmp.right) errors.push({
|
|
1457
|
+
message: "@db.rel.filter compares two fields — only field-to-value conditions are supported",
|
|
1458
|
+
severity: 1,
|
|
1459
|
+
range: cmp.right.fieldRef.range
|
|
1460
|
+
});
|
|
1461
|
+
});
|
|
1462
|
+
const junction = hasVia ? require_validation_utils.getAnnotationAlias(field, "db.rel.via") : void 0;
|
|
1463
|
+
if (junction) {
|
|
1464
|
+
const conjuncts = (0, _atscript_core.isQueryLogical)(expression) && expression.operator === "and" ? expression.operands : [expression];
|
|
1465
|
+
for (const conjunct of conjuncts) {
|
|
1466
|
+
let junctionRef;
|
|
1467
|
+
let targetRef = false;
|
|
1468
|
+
require_validation_utils.forEachFieldRef(conjunct, (ref) => {
|
|
1469
|
+
if (ref.typeRef?.text === junction) junctionRef ??= ref.typeRef.range;
|
|
1470
|
+
else targetRef = true;
|
|
1471
|
+
});
|
|
1472
|
+
if (junctionRef && targetRef) errors.push({
|
|
1473
|
+
message: `@db.rel.filter has a condition reading both the junction '${junction}' and the related type — split it into separate top-level "and" conditions`,
|
|
1474
|
+
severity: 1,
|
|
1475
|
+
range: junctionRef
|
|
1476
|
+
});
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1288
1479
|
return errors;
|
|
1289
1480
|
}
|
|
1290
1481
|
})
|
|
1291
1482
|
} };
|
|
1483
|
+
/** Every comparison of a query expression (through and / or / not). */
|
|
1484
|
+
function forEachComparison(expr, fn) {
|
|
1485
|
+
if ((0, _atscript_core.isQueryLogical)(expr)) for (const operand of expr.operands) forEachComparison(operand, fn);
|
|
1486
|
+
else if ((0, _atscript_core.isQueryComparison)(expr)) fn(expr);
|
|
1487
|
+
}
|
|
1292
1488
|
//#endregion
|
|
1293
1489
|
//#region src/plugin/annotations/search.ts
|
|
1294
1490
|
const dbSearchAnnotations = { search: {
|
|
@@ -1638,6 +1834,61 @@ const dbUnitAnnotations = { unit: {
|
|
|
1638
1834
|
} };
|
|
1639
1835
|
//#endregion
|
|
1640
1836
|
//#region src/plugin/annotations/view.ts
|
|
1837
|
+
/**
|
|
1838
|
+
* VJ6–VJ8 — the ordering of a first-row join: every key is a scalar field of
|
|
1839
|
+
* the join target (no object, array, `@db.json`, `@db.encrypted`, `@db.writeOnly` or
|
|
1840
|
+
* navigation field), no key repeats, and the target (through `@db.alias`)
|
|
1841
|
+
* declares exactly one `@meta.id` field — the anchor of the join's
|
|
1842
|
+
* correlated subquery.
|
|
1843
|
+
*/
|
|
1844
|
+
function validateJoinOrder(orderToken, scope, doc) {
|
|
1845
|
+
const target = scope.unqualifiedTarget;
|
|
1846
|
+
const errors = require_validation_utils.validateQueryScope(orderToken, scope, doc, `a first-row join orders by fields of its target '${target}'`);
|
|
1847
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1848
|
+
for (const item of orderToken.orderNode.items) {
|
|
1849
|
+
const { ref } = item;
|
|
1850
|
+
if (ref.typeRef && ref.typeRef.text !== target) continue;
|
|
1851
|
+
const path = ref.fieldRef.text;
|
|
1852
|
+
if (seen.has(path)) {
|
|
1853
|
+
errors.push({
|
|
1854
|
+
message: `Order key '${path}' appears more than once`,
|
|
1855
|
+
severity: 1,
|
|
1856
|
+
range: ref.fieldRef.range
|
|
1857
|
+
});
|
|
1858
|
+
continue;
|
|
1859
|
+
}
|
|
1860
|
+
seen.add(path);
|
|
1861
|
+
const why = orderKeyProblem(doc, target, path.split("."));
|
|
1862
|
+
if (why) errors.push({
|
|
1863
|
+
message: `Order key '${path}' ${why} — order by a scalar field of '${target}'`,
|
|
1864
|
+
severity: 1,
|
|
1865
|
+
range: ref.fieldRef.range
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1868
|
+
const ids = (0, _atscript_core.getFieldsForType)(doc, target).filter((p) => p.countAnnotations("meta.id") > 0);
|
|
1869
|
+
if (ids.length !== 1) errors.push({
|
|
1870
|
+
message: `A first-row join needs a target with exactly one @meta.id field — '${target}' has ${ids.length === 0 ? "none" : `${ids.length} (a composite key)`}`,
|
|
1871
|
+
severity: 1,
|
|
1872
|
+
range: orderToken.range
|
|
1873
|
+
});
|
|
1874
|
+
return errors;
|
|
1875
|
+
}
|
|
1876
|
+
/** Why `path` of `target` cannot order a first-row join, or `undefined` when it can. */
|
|
1877
|
+
function orderKeyProblem(doc, target, path) {
|
|
1878
|
+
for (let i = 1; i <= path.length; i++) {
|
|
1879
|
+
const step = doc.unwindType(target, path.slice(0, i));
|
|
1880
|
+
if (!step) return void 0;
|
|
1881
|
+
const node = step.node;
|
|
1882
|
+
if (node && (0, _atscript_core.isProp)(node)) {
|
|
1883
|
+
if (node.countAnnotations("db.json") > 0) return "reads a @db.json field";
|
|
1884
|
+
if (node.countAnnotations("db.encrypted") > 0) return "is @db.encrypted";
|
|
1885
|
+
if (node.countAnnotations("db.writeOnly") > 0) return "is @db.writeOnly";
|
|
1886
|
+
if (node.countAnnotations("db.ignore") > 0) return "is @db.ignore'd";
|
|
1887
|
+
}
|
|
1888
|
+
if ((0, _atscript_core.isArray)(step.def)) return "is an array";
|
|
1889
|
+
if (i === path.length && !(0, _atscript_core.isPrimitive)(step.def)) return "is not a scalar";
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1641
1892
|
const dbViewAnnotations = { view: {
|
|
1642
1893
|
$self: new _atscript_core.AnnotationSpec({
|
|
1643
1894
|
description: "Marks an interface as a **database view**. Optionally takes a view name argument.\n\n**Example:**\n```atscript\n@db.view \"active_premium_users\"\n@db.view.for User\nexport interface ActivePremiumUser { ... }\n```\n",
|
|
@@ -1689,7 +1940,7 @@ const dbViewAnnotations = { view: {
|
|
|
1689
1940
|
}
|
|
1690
1941
|
}),
|
|
1691
1942
|
joins: new _atscript_core.AnnotationSpec({
|
|
1692
|
-
description: "Declares an explicit join for a view. Joins are INNER by default — pass `'left'` as the third argument to keep entry rows without a match (fields read from a left-joined table must be optional). A join condition may reference the entry table and joins declared before it (chained joins). The target is a `@db.table`, a `@db.view` (since 0.1.141), or a `@db.alias` type — the way to join one table twice or to self-join the entry table: every scope name (entry + joins) must be unique.\n\n**Example:**\n```atscript\n@db.view.for Order\n@db.view.joins Customer, `Customer.id = Order.customerId`\n@db.view.joins Region, `Region.id = Customer.regionId`, 'left'\nexport interface OrderRegion { ... }\n```\n",
|
|
1943
|
+
description: "Declares an explicit join for a view. Joins are INNER by default — pass `'left'` as the third argument to keep entry rows without a match (fields read from a left-joined table must be optional). A join condition may reference the entry table and joins declared before it (chained joins). The target is a `@db.table`, a `@db.view` (since 0.1.141), or a `@db.alias` type — the way to join one table twice or to self-join the entry table: every scope name (entry + joins) must be unique.\n\n**Example:**\n```atscript\n@db.view.for Order\n@db.view.joins Customer, `Customer.id = Order.customerId`\n@db.view.joins Region, `Region.id = Customer.regionId`, 'left'\nexport interface OrderRegion { ... }\n```\n\n**First-row join** (since 0.1.147): a 4th argument — an ordering of the target's fields (`` `raisedAt, id` ``, `` `severity desc` ``) — joins only the FIRST matching target row by that ordering (the target's primary key is appended as the final tie-break), so every field read through the join comes from that one row. NULL sorts first in `asc`.\n```atscript\n@db.view.joins OldestOpenIssue, `OldestOpenIssue.ticketId = Ticket.id and OldestOpenIssue.status = 'open'`, 'left', `raisedAt`\n```\n",
|
|
1693
1944
|
nodeType: ["interface"],
|
|
1694
1945
|
passedWhenReferred: false,
|
|
1695
1946
|
multiple: true,
|
|
@@ -1713,6 +1964,13 @@ const dbViewAnnotations = { view: {
|
|
|
1713
1964
|
optional: true,
|
|
1714
1965
|
description: "`\"inner\"` (default) drops entry rows without a match; `\"left\"` keeps them with NULLs.",
|
|
1715
1966
|
values: ["inner", "left"]
|
|
1967
|
+
},
|
|
1968
|
+
{
|
|
1969
|
+
name: "order",
|
|
1970
|
+
type: "order",
|
|
1971
|
+
optional: true,
|
|
1972
|
+
description: "Makes this a first-row join: of the target rows matching the condition only the first by this ordering joins (`` `raisedAt, id` ``, `` `severity desc` ``). Keys are scalar fields of the join target; its primary key is appended as the final tie-break. NULL is the smallest value (first in `asc`, last in `desc`).",
|
|
1973
|
+
fieldScope: fieldScopes.joinOrder
|
|
1716
1974
|
}
|
|
1717
1975
|
],
|
|
1718
1976
|
validate(token, args, doc) {
|
|
@@ -1761,6 +2019,8 @@ const dbViewAnnotations = { view: {
|
|
|
1761
2019
|
}
|
|
1762
2020
|
const scope = join && viewJoinScope(owner, join, earlier);
|
|
1763
2021
|
if (args[1]?.queryNode && scope) errors.push(...require_validation_utils.validateQueryScope(args[1], scope, doc, "a join may reference the entry table and joins declared before it"));
|
|
2022
|
+
const order = join && args[3]?.orderNode ? joinOrderScope(join) : void 0;
|
|
2023
|
+
if (order) errors.push(...validateJoinOrder(args[3], order, doc));
|
|
1764
2024
|
return errors;
|
|
1765
2025
|
}
|
|
1766
2026
|
}),
|
|
@@ -1890,6 +2150,7 @@ const dbPlugin = (options) => ({
|
|
|
1890
2150
|
view: dbViewAnnotations.view,
|
|
1891
2151
|
alias: dbAliasAnnotations.alias,
|
|
1892
2152
|
agg: dbAggAnnotations.agg,
|
|
2153
|
+
compute: dbComputeAnnotations.compute,
|
|
1893
2154
|
search: dbSearchAnnotations.search,
|
|
1894
2155
|
amount: dbAmountAnnotations.amount,
|
|
1895
2156
|
unit: dbUnitAnnotations.unit
|