@dsqlbase/core 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/dist/definition/base.d.ts +3 -30
- package/dist/definition/base.d.ts.map +1 -1
- package/dist/definition/base.js +3 -14
- package/dist/definition/base.js.map +1 -1
- package/dist/definition/column.d.ts +22 -14
- package/dist/definition/column.d.ts.map +1 -1
- package/dist/definition/column.js +11 -37
- package/dist/definition/column.js.map +1 -1
- package/dist/definition/constraint.d.ts +9 -9
- package/dist/definition/constraint.d.ts.map +1 -1
- package/dist/definition/constraint.js.map +1 -1
- package/dist/definition/domain.d.ts +2 -1
- package/dist/definition/domain.d.ts.map +1 -1
- package/dist/definition/domain.js +2 -1
- package/dist/definition/domain.js.map +1 -1
- package/dist/definition/index.d.ts +5 -6
- package/dist/definition/index.d.ts.map +1 -1
- package/dist/definition/index.js +3 -4
- package/dist/definition/index.js.map +1 -1
- package/dist/definition/indexes.d.ts +9 -20
- package/dist/definition/indexes.d.ts.map +1 -1
- package/dist/definition/indexes.js +3 -6
- package/dist/definition/indexes.js.map +1 -1
- package/dist/definition/namespace.d.ts +2 -2
- package/dist/definition/namespace.d.ts.map +1 -1
- package/dist/definition/namespace.js.map +1 -1
- package/dist/definition/policy.d.ts +35 -0
- package/dist/definition/policy.d.ts.map +1 -0
- package/dist/definition/policy.js +42 -0
- package/dist/definition/policy.js.map +1 -0
- package/dist/definition/relations.d.ts +28 -33
- package/dist/definition/relations.d.ts.map +1 -1
- package/dist/definition/relations.js +1 -4
- package/dist/definition/relations.js.map +1 -1
- package/dist/definition/table.d.ts +39 -32
- package/dist/definition/table.d.ts.map +1 -1
- package/dist/definition/table.js +57 -67
- package/dist/definition/table.js.map +1 -1
- package/dist/definition/union.d.ts +18 -61
- package/dist/definition/union.d.ts.map +1 -1
- package/dist/definition/union.js +8 -69
- package/dist/definition/union.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/resolvers/codec.d.ts +16 -0
- package/dist/resolvers/codec.d.ts.map +1 -0
- package/dist/resolvers/codec.js +9 -0
- package/dist/resolvers/codec.js.map +1 -0
- package/dist/resolvers/hooks.d.ts +39 -0
- package/dist/resolvers/hooks.d.ts.map +1 -0
- package/dist/resolvers/hooks.js +2 -0
- package/dist/resolvers/hooks.js.map +1 -0
- package/dist/resolvers/index.d.ts +5 -0
- package/dist/resolvers/index.d.ts.map +1 -0
- package/dist/resolvers/index.js +2 -0
- package/dist/resolvers/index.js.map +1 -0
- package/dist/resolvers/policy.d.ts +31 -0
- package/dist/resolvers/policy.d.ts.map +1 -0
- package/dist/resolvers/policy.js +2 -0
- package/dist/resolvers/policy.js.map +1 -0
- package/dist/resolvers/validator.d.ts +14 -0
- package/dist/resolvers/validator.d.ts.map +1 -0
- package/dist/resolvers/validator.js +2 -0
- package/dist/resolvers/validator.js.map +1 -0
- package/dist/runtime/base.d.ts +7 -4
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js.map +1 -1
- package/dist/runtime/build.d.ts +22 -0
- package/dist/runtime/build.d.ts.map +1 -0
- package/dist/runtime/build.js +184 -0
- package/dist/runtime/build.js.map +1 -0
- package/dist/runtime/column.d.ts +40 -23
- package/dist/runtime/column.d.ts.map +1 -1
- package/dist/runtime/column.js +120 -28
- package/dist/runtime/column.js.map +1 -1
- package/dist/runtime/context.d.ts +17 -26
- package/dist/runtime/context.d.ts.map +1 -1
- package/dist/runtime/context.js +4 -6
- package/dist/runtime/context.js.map +1 -1
- package/dist/runtime/errors.d.ts +31 -9
- package/dist/runtime/errors.d.ts.map +1 -1
- package/dist/runtime/errors.js +54 -11
- package/dist/runtime/errors.js.map +1 -1
- package/dist/runtime/executor.d.ts.map +1 -1
- package/dist/runtime/executor.js +10 -1
- package/dist/runtime/executor.js.map +1 -1
- package/dist/runtime/index.d.ts +10 -6
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +5 -3
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/operation.d.ts +62 -83
- package/dist/runtime/operation.d.ts.map +1 -1
- package/dist/runtime/operation.js +194 -260
- package/dist/runtime/operation.js.map +1 -1
- package/dist/runtime/policy.d.ts +35 -0
- package/dist/runtime/policy.d.ts.map +1 -0
- package/dist/runtime/policy.js +72 -0
- package/dist/runtime/policy.js.map +1 -0
- package/dist/runtime/reference.d.ts +87 -0
- package/dist/runtime/reference.d.ts.map +1 -0
- package/dist/runtime/reference.js +68 -0
- package/dist/runtime/reference.js.map +1 -0
- package/dist/runtime/registry.d.ts +26 -95
- package/dist/runtime/registry.d.ts.map +1 -1
- package/dist/runtime/registry.js +29 -408
- package/dist/runtime/registry.js.map +1 -1
- package/dist/runtime/relation.d.ts +45 -0
- package/dist/runtime/relation.d.ts.map +1 -0
- package/dist/runtime/relation.js +2 -0
- package/dist/runtime/relation.js.map +1 -0
- package/dist/runtime/table.d.ts +31 -35
- package/dist/runtime/table.d.ts.map +1 -1
- package/dist/runtime/table.js +78 -47
- package/dist/runtime/table.js.map +1 -1
- package/dist/runtime/union.d.ts +11 -4
- package/dist/runtime/union.d.ts.map +1 -1
- package/dist/runtime/union.js +18 -7
- package/dist/runtime/union.js.map +1 -1
- package/dist/sql/index.d.ts +2 -2
- package/dist/sql/index.d.ts.map +1 -1
- package/dist/sql/index.js +1 -1
- package/dist/sql/index.js.map +1 -1
- package/dist/sql/nodes.d.ts +22 -0
- package/dist/sql/nodes.d.ts.map +1 -1
- package/dist/sql/nodes.js +21 -0
- package/dist/sql/nodes.js.map +1 -1
- package/dist/sql/tag.d.ts +3 -15
- package/dist/sql/tag.d.ts.map +1 -1
- package/dist/sql/tag.js +5 -71
- package/dist/sql/tag.js.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/types.d.ts +2 -14
- package/dist/utils/types.d.ts.map +1 -1
- package/dist/utils/types.js.map +1 -1
- package/dist/validation/index.d.ts +2 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +2 -0
- package/dist/validation/index.js.map +1 -0
- package/dist/validation/references.d.ts +23 -0
- package/dist/validation/references.d.ts.map +1 -0
- package/dist/validation/references.js +240 -0
- package/dist/validation/references.js.map +1 -0
- package/dist/validation/validate.d.ts +40 -0
- package/dist/validation/validate.d.ts.map +1 -0
- package/dist/validation/validate.js +354 -0
- package/dist/validation/validate.js.map +1 -0
- package/package.json +13 -4
- package/dist/definition/embedded.d.ts +0 -131
- package/dist/definition/embedded.d.ts.map +0 -1
- package/dist/definition/embedded.js +0 -133
- package/dist/definition/embedded.js.map +0 -1
- package/dist/definition/tenant.d.ts +0 -50
- package/dist/definition/tenant.d.ts.map +0 -1
- package/dist/definition/tenant.js +0 -73
- package/dist/definition/tenant.js.map +0 -1
- package/dist/runtime/group.d.ts +0 -54
- package/dist/runtime/group.d.ts.map +0 -1
- package/dist/runtime/group.js +0 -96
- package/dist/runtime/group.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reference.js","sourceRoot":"","sources":["../../src/runtime/reference.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,QAAQ,EAAE,QAAQ,EAAgB,MAAM,iBAAiB,CAAC;AACxF,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAKpC;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,qBAAqB;IAChC,+EAA+E;IACtE,IAAI,CAAS;IAEb,KAAK,CAAW;IAEzB,yCAAyC;IAChC,IAAI,CAAuB;IAEpC,oFAAoF;IAC3E,OAAO,CAAiD;IAEjE,YACE,KAAe,EACf,IAA0B,EAC1B,OAAuD;QAEvD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,I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IAAI,CAAC,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC7D,CAAC;CACF;AAkDD,MAAM,UAAU,eAAe,CAAC,CAAkB,EAAE,CAAkB;IACpE,IAAI,CAAC,CAAC,IAAI,KAAK,OAAO,IAAI,CAAC,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QAC7C,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,CAAC;IAC/B,CAAC;IAED,IAAI,CAAC,CAAC,IAAI,KAAK,OAAO,IAAI,CAAC,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QAC7C,OAAO,CACL,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,KAAK;YACnB,CAAC,CAAC,aAAa,KAAK,CAAC,CAAC,aAAa;YACnC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,MAAM;YAC/D,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,MAAM,CAAC,CAClF,CAAC;IACJ,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import { SQLContext, SQLNode, SQLParam, SQLScope, SQLStatement } from \"../sql/nodes.js\";\nimport { sql } from \"../sql/tag.js\";\nimport type { AnyColumn } from \"./column.js\";\nimport type { RuntimeRelation } from \"./relation.js\";\nimport type { AnyUnion } from \"./union.js\";\n\n/**\n * Which member of a union a row's key points at, computed when the query runs: the first\n * member, in declaration order, holding a row with that key.\n *\n * ```sql\n * CASE WHEN EXISTS (SELECT 1 FROM \"photos\" AS \"__d0\" WHERE \"__d0\".\"id\" = \"__t0\".\"post_id\")\n * THEN 'photos'\n * WHEN EXISTS (SELECT 1 FROM \"videos\" AS \"__d1\" WHERE \"__d1\".\"id\" = \"__t0\".\"post_id\")\n * THEN 'videos' END\n * ```\n *\n * Stands in for a stored discriminator wherever a relation declares none: a belongs-to a union\n * without one, and the foreign key of a relation whose source is a union. It renders as an\n * expression, so it compares like a column (`= 'photos'` in a join's correlation or a filter),\n * and is projected under {@link name} so a column's `read` hook reads it from the row as it would a\n * stored discriminator. Each probe is one key lookup per member per row, and it reads the\n * members without their row policies — it only says which member holds a key the row\n * already carries.\n */\nexport class ComputedDiscriminator implements SQLNode {\n /** The name it is projected under: the first key column's, suffixed `$key`. */\n readonly name: string;\n\n readonly union: AnyUnion;\n\n /** The referencing row's key columns. */\n readonly keys: readonly AnyColumn[];\n\n /** Member alias → the member's columns, paired index by index with {@link keys}. */\n readonly members: Readonly<Record<string, readonly AnyColumn[]>>;\n\n constructor(\n union: AnyUnion,\n keys: readonly AnyColumn[],\n members: Readonly<Record<string, readonly AnyColumn[]>>\n ) {\n this.union = union;\n this.keys = keys;\n this.members = members;\n this.name = `${keys[0].name}$key`;\n }\n\n /** A member alias as a parameter to compare this with, as `Column.param` does for a column. */\n public param(value: unknown): SQLNode {\n return new SQLParam(value);\n }\n\n /** The expression aliased as {@link name}, for a select list. */\n public projection(): SQLNode {\n return sql`${this} AS ${sql.identifier(this.name)}`;\n }\n\n toSQL(ctx: SQLContext): SQLStatement {\n const branches = this.union.memberAliases.map((alias, index) => {\n const member = this.union.getMember(alias);\n // The member is aliased inside its probe only, so the key columns — rendered outside the\n // scope — keep their own alias even when the key's table is the member itself.\n const scope = new Map<unknown, string>([[member, `__d${index}`]]);\n const conditions = this.members[alias].map((column, position) =>\n sql.eq(new SQLScope(scope, column), this.keys[position])\n );\n\n return sql`WHEN EXISTS (SELECT 1 FROM ${member} AS ${sql.identifier(`__d${index}`)} WHERE ${sql.and(conditions)}) THEN ${sql.literal(alias)}`;\n });\n\n return sql`CASE ${sql.join(branches, \" \")} END`.toSQL(ctx);\n }\n}\n\n/** A foreign key pointing at one column of one table. */\nexport type TableReference = {\n readonly kind: \"table\";\n readonly column: AnyColumn;\n /** The relation that set the reference. */\n readonly via: RuntimeRelation;\n};\n\n/**\n * A foreign key pointing at a member of a union, chosen per row: the discriminator names the\n * member, and `members` holds the column the key points at on each one.\n */\nexport type UnionReference = {\n readonly kind: \"union\";\n readonly union: AnyUnion;\n /** Member alias → the member's column this key points at. */\n readonly members: Readonly<Record<string, AnyColumn>>;\n /**\n * What names the member per row: the referencing row's stored discriminator column, or a\n * {@link ComputedDiscriminator} when the relation declares none.\n */\n readonly discriminator: AnyColumn | ComputedDiscriminator;\n /** The relation that set the reference. */\n readonly via: RuntimeRelation;\n};\n\n/**\n * What a column points at, as a relation declares it. References always sit on the foreign key,\n * as in SQL: a belongs-to sets one on its `from` columns, a has-many / has-one on its `to`\n * columns — so a relation declared on one table may set a reference on another's column.\n */\nexport type ColumnReference = TableReference | UnionReference;\n\n/**\n * Where a chain of references stops: a column that references nothing further, or a union\n * reference, whose member is only known per row.\n */\nexport type ReferenceEnd =\n | { readonly kind: \"table\"; readonly column: AnyColumn }\n | { readonly kind: \"union\"; readonly reference: UnionReference };\n\nexport type ResolvedReferences = {\n /** Distinct chain ends, by identity. A column with no references ends at itself. */\n readonly ends: ReferenceEnd[];\n /** Set when a chain loops back onto a column already on it: the columns of the ring, in order. */\n readonly ring?: AnyColumn[];\n};\n\nexport function isSameReference(a: ColumnReference, b: ColumnReference): boolean {\n if (a.kind === \"table\" && b.kind === \"table\") {\n return a.column === b.column;\n }\n\n if (a.kind === \"union\" && b.kind === \"union\") {\n return (\n a.union === b.union &&\n a.discriminator === b.discriminator &&\n Object.keys(a.members).length === Object.keys(b.members).length &&\n Object.entries(a.members).every(([alias, column]) => b.members[alias] === column)\n );\n }\n\n return false;\n}\n"]}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { TypedObject, Prettify, WithMeta } from "../utils/index.js";
|
|
2
|
-
import {
|
|
2
|
+
import { DefinitionSchema, TableDefinition, UnionDefinition } from "../definition/index.js";
|
|
3
3
|
import { AnySchema, Schema, SchemaTableRelations } from "./base.js";
|
|
4
|
-
import {
|
|
4
|
+
import { RuntimeRelation } from "./relation.js";
|
|
5
5
|
import { AnyTable, Table } from "./table.js";
|
|
6
|
-
import { Union } from "./union.js";
|
|
6
|
+
import { AnyUnion, Union } from "./union.js";
|
|
7
7
|
/**
|
|
8
8
|
* The metadata a definition declared with `table().meta()`. Rebuilding a `Table` from the
|
|
9
9
|
* definition's three type parameters would otherwise drop it, taking `$$meta`'s declared
|
|
@@ -20,84 +20,23 @@ export type RuntimeTables<TSchema extends AnySchema> = {
|
|
|
20
20
|
export type TableByAlias<TSchema extends AnySchema, TAlias extends string> = TAlias extends keyof TSchema["tables"] ? TSchema["tables"][TAlias] extends TableDefinition<infer Name, infer Columns, infer Schema> ? WithMeta<Table<Name, Columns, Schema, SchemaTableRelations<TSchema, Name>>, DeclaredTableMeta<TSchema["tables"][TAlias]>> : never : never;
|
|
21
21
|
export type TableByName<TSchema extends AnySchema, TName extends string> = TSchema["tables"] extends Record<string, infer Def> ? Def extends TableDefinition<TName, infer Columns, infer Schema> ? Table<TName, Columns, Schema, SchemaTableRelations<TSchema, TName>> : never : never;
|
|
22
22
|
export type TableByNameOrAlias<TSchema extends AnySchema, TName extends string> = keyof TSchema["tables"] extends never ? AnyTable : TableByAlias<TSchema, TName> extends never ? TableByName<TSchema, TName> : TableByAlias<TSchema, TName>;
|
|
23
|
+
export type RuntimeUnions<TSchema extends AnySchema> = {
|
|
24
|
+
[K in keyof TSchema["unions"]]: K extends string ? TSchema["unions"][K] extends UnionDefinition<infer TName, infer TMembers, infer TColumns> ? Union<TName, TMembers, TColumns, SchemaTableRelations<TSchema, K>> : never : never;
|
|
25
|
+
};
|
|
26
|
+
export type UnionByAlias<TSchema extends AnySchema, TAlias extends string> = RuntimeUnions<TSchema> extends Record<TAlias, infer U> ? U : never;
|
|
27
|
+
/**
|
|
28
|
+
* The runtime form of a schema, for lookups.
|
|
29
|
+
*
|
|
30
|
+
* Built in two steps that know nothing of each other: `validateSchema` checks the definition and
|
|
31
|
+
* keeps nothing, then `buildSchema` makes the runtime objects — tables, unions, relations
|
|
32
|
+
* resolved to runtime columns, and the references they set. The registry only holds the result.
|
|
33
|
+
*/
|
|
23
34
|
export declare class SchemaRegistry<TDefinition extends DefinitionSchema = DefinitionSchema> implements TypedObject<Schema<TDefinition>> {
|
|
24
35
|
readonly __type: Schema<TDefinition>;
|
|
25
36
|
private readonly _tables;
|
|
26
|
-
private readonly _relations;
|
|
27
37
|
private readonly _unions;
|
|
28
|
-
|
|
29
|
-
* For every relation whose target is a union: source table name → field → member alias →
|
|
30
|
-
* the member's `to` columns. Resolved once here, whether `to` was written as shared fields or
|
|
31
|
-
* as one list per member, so nothing downstream has to know which.
|
|
32
|
-
*/
|
|
33
|
-
private readonly _unionPairs;
|
|
34
|
-
/** For every belongs-to a union: source table name → field → its discriminator column. */
|
|
35
|
-
private readonly _discriminators;
|
|
36
|
-
/**
|
|
37
|
-
* Every tenant claim declared anywhere in this schema, mapped to the data type it is
|
|
38
|
-
* declared with. Empty when no table is inside a tenant scope.
|
|
39
|
-
*
|
|
40
|
-
* This is what `$identityClaims` picks an identity object apart by: a claim is identified by
|
|
41
|
-
* its field name across the whole schema, so the same name must mean the same type
|
|
42
|
-
* everywhere — two tables disagreeing is caught here rather than at the first query.
|
|
43
|
-
*/
|
|
44
|
-
readonly claimKeys: Map<string, string>;
|
|
38
|
+
private readonly _relations;
|
|
45
39
|
constructor(definition: TDefinition);
|
|
46
|
-
/**
|
|
47
|
-
* Merges a second `relations()` declaration for the same table into the first.
|
|
48
|
-
*
|
|
49
|
-
* Merges into a copy, never into the definition: `relations()` objects belong to the schema
|
|
50
|
-
* module, so mutating one would mean a second registry over the same schema re-merges what
|
|
51
|
-
* the first already merged and reports every name as a duplicate. Two clients over one
|
|
52
|
-
* schema is an ordinary thing to want — an enforcing one and an unscoped one, for instance.
|
|
53
|
-
*/
|
|
54
|
-
private _mergeTableRelations;
|
|
55
|
-
private _validateAndTransformSchema;
|
|
56
|
-
private _buildTables;
|
|
57
|
-
/**
|
|
58
|
-
* Builds every union once the tables exist, checking what only the schema object can tell:
|
|
59
|
-
* that each member is keyed by the alias it is exported under. That is what makes a member
|
|
60
|
-
* key mean the same thing as `$$meta.key` and a node key, rather than a second name for the
|
|
61
|
-
* same table.
|
|
62
|
-
*/
|
|
63
|
-
private _buildUnions;
|
|
64
|
-
private _buildClaimKeys;
|
|
65
|
-
/**
|
|
66
|
-
* The field alias a column is declared under on `table`, or `undefined` when the column
|
|
67
|
-
* does not belong to it. Identity, not name: a column from a different table that happens
|
|
68
|
-
* to share a name is not a match, which is the copy-paste mistake this catches.
|
|
69
|
-
*/
|
|
70
|
-
/**
|
|
71
|
-
* The field a column is declared under on a table, if it is one of its plain columns. A
|
|
72
|
-
* relation key or a discriminator naming a column group's member throws: relations correlate
|
|
73
|
-
* plain columns only, as primary keys and tenant claims do.
|
|
74
|
-
*/
|
|
75
|
-
private _findColumnAlias;
|
|
76
|
-
/**
|
|
77
|
-
* Checks that a relation's column pairs line up: equal, non-zero length; each column
|
|
78
|
-
* declared on the side it is listed under; both columns of a pair of the same type.
|
|
79
|
-
*
|
|
80
|
-
* The query builder correlates over every pair (`packages/core/src/runtime/query.ts`), so
|
|
81
|
-
* a malformed relation would otherwise surface as a confusing SQL error at query time, or
|
|
82
|
-
* not at all.
|
|
83
|
-
*/
|
|
84
|
-
private _validateRelation;
|
|
85
|
-
/**
|
|
86
|
-
* The union half of {@link _validateRelation}: the union must be registered, `to` must name
|
|
87
|
-
* a column on every member — through a shared field or a per-member list — paired with
|
|
88
|
-
* `from` by type, and only a belongs-to carries a discriminator, which it must.
|
|
89
|
-
*/
|
|
90
|
-
private _validateUnionRelation;
|
|
91
|
-
/** `to` as one column-definition list per member, whichever form it was written in. */
|
|
92
|
-
private _getMemberToColumns;
|
|
93
|
-
/**
|
|
94
|
-
* A belongs-to a union stores which member it points at in a column of its own — the
|
|
95
|
-
* discriminator, holding a member alias. Nothing else can tell the members apart: their keys
|
|
96
|
-
* share one column. Has-many and has-one to a union need none, since the key lives on each
|
|
97
|
-
* member, so one there is a mistake.
|
|
98
|
-
*/
|
|
99
|
-
private _validateDiscriminator;
|
|
100
|
-
private _buildRelations;
|
|
101
40
|
getTable<TName extends string>(aliasOrName: TName): TableByNameOrAlias<this["__type"], TName>;
|
|
102
41
|
hasTable(aliasOrName: string): boolean;
|
|
103
42
|
getTables(): Prettify<RuntimeTables<this["__type"]>>;
|
|
@@ -112,26 +51,18 @@ export declare class SchemaRegistry<TDefinition extends DefinitionSchema = Defin
|
|
|
112
51
|
* Throws when no such table exists.
|
|
113
52
|
*/
|
|
114
53
|
getAlias(nameOrAlias: string): string;
|
|
115
|
-
|
|
116
|
-
getRelations(tableNameOrAlias: string): AnyTableRelations;
|
|
117
|
-
hasUnion(alias: string): boolean;
|
|
118
|
-
getUnion(alias: string): Union;
|
|
119
|
-
/** One entry per union, keyed by schema alias. */
|
|
120
|
-
getUnions(): [alias: string, union: Union][];
|
|
121
|
-
/**
|
|
122
|
-
* What a relation points at: a table, or a union when the relation targets one. Callers
|
|
123
|
-
* that only handle tables must narrow with `instanceof Union`.
|
|
124
|
-
*/
|
|
125
|
-
getRelationTarget(tableNameOrAlias: string, field: string): AnyTable | Union;
|
|
54
|
+
hasRelation(table: AnyTable | string, field: string): boolean;
|
|
126
55
|
/**
|
|
127
|
-
*
|
|
128
|
-
*
|
|
56
|
+
* A table's relation by field, resolved: runtime columns on both sides, the runtime target,
|
|
57
|
+
* and for a relation to a union its per-member columns and discriminator. `undefined` when
|
|
58
|
+
* the table has no such relation. Takes the table, its alias or its database name.
|
|
129
59
|
*/
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
60
|
+
getRelation(table: AnyTable | string, field: string): RuntimeRelation | undefined;
|
|
61
|
+
/** Every relation a table has, by field — empty for a table with none. */
|
|
62
|
+
getRelations(table: AnyTable | string): Readonly<Record<string, RuntimeRelation>>;
|
|
63
|
+
hasUnion(alias: string): boolean;
|
|
64
|
+
getUnion<TAlias extends string>(alias: TAlias): UnionByAlias<this["__type"], TAlias>;
|
|
65
|
+
/** One entry per union, keyed by schema alias. */
|
|
66
|
+
getUnions(): [alias: string, union: AnyUnion][];
|
|
136
67
|
}
|
|
137
68
|
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/runtime/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/runtime/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAE5F,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAEpE,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAE7C;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,CAAC,IAAI,IAAI,IAAI,SAAS;IAAE,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC,CAAA;KAAE,CAAA;CAAE,GAAG,CAAC,GAAG,OAAO,CAAC;AAE/F,MAAM,MAAM,aAAa,CAAC,OAAO,SAAS,SAAS,IAAI;KACpD,CAAC,IAAI,MAAM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AC,QAAQ,CAAC,EAAE,KAAK,CAAC;IAUrC,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO;IAItC,SAAS,IAAI,QAAQ,CAAC,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IAI3D;;;;OAIG;IACI,eAAe,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,EAAE;IAU5D;;;OAGG;IACI,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM;IAIrC,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO;IAIpE;;;;OAIG;IACI,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS;IAIxF,0EAA0E;IACnE,YAAY,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAMjF,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAIhC,QAAQ,CAAC,MAAM,SAAS,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAU3F,kDAAkD;IAC3C,SAAS,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,EAAE;CAGvD"}
|
package/dist/runtime/registry.js
CHANGED
|
@@ -1,362 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { validateSchema } from "../validation/index.js";
|
|
2
|
+
import { buildSchema } from "./build.js";
|
|
3
|
+
/**
|
|
4
|
+
* The runtime form of a schema, for lookups.
|
|
5
|
+
*
|
|
6
|
+
* Built in two steps that know nothing of each other: `validateSchema` checks the definition and
|
|
7
|
+
* keeps nothing, then `buildSchema` makes the runtime objects — tables, unions, relations
|
|
8
|
+
* resolved to runtime columns, and the references they set. The registry only holds the result.
|
|
9
|
+
*/
|
|
4
10
|
export class SchemaRegistry {
|
|
5
11
|
_tables;
|
|
6
|
-
_relations;
|
|
7
12
|
_unions;
|
|
8
|
-
|
|
9
|
-
* For every relation whose target is a union: source table name → field → member alias →
|
|
10
|
-
* the member's `to` columns. Resolved once here, whether `to` was written as shared fields or
|
|
11
|
-
* as one list per member, so nothing downstream has to know which.
|
|
12
|
-
*/
|
|
13
|
-
_unionPairs = new Map();
|
|
14
|
-
/** For every belongs-to a union: source table name → field → its discriminator column. */
|
|
15
|
-
_discriminators = new Map();
|
|
16
|
-
/**
|
|
17
|
-
* Every tenant claim declared anywhere in this schema, mapped to the data type it is
|
|
18
|
-
* declared with. Empty when no table is inside a tenant scope.
|
|
19
|
-
*
|
|
20
|
-
* This is what `$identityClaims` picks an identity object apart by: a claim is identified by
|
|
21
|
-
* its field name across the whole schema, so the same name must mean the same type
|
|
22
|
-
* everywhere — two tables disagreeing is caught here rather than at the first query.
|
|
23
|
-
*/
|
|
24
|
-
claimKeys;
|
|
13
|
+
_relations;
|
|
25
14
|
constructor(definition) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
-
this.
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Merges a second `relations()` declaration for the same table into the first.
|
|
34
|
-
*
|
|
35
|
-
* Merges into a copy, never into the definition: `relations()` objects belong to the schema
|
|
36
|
-
* module, so mutating one would mean a second registry over the same schema re-merges what
|
|
37
|
-
* the first already merged and reports every name as a duplicate. Two clients over one
|
|
38
|
-
* schema is an ordinary thing to want — an enforcing one and an unscoped one, for instance.
|
|
39
|
-
*/
|
|
40
|
-
_mergeTableRelations(existing, newRelations) {
|
|
41
|
-
for (const [name, relation] of Object.entries(newRelations)) {
|
|
42
|
-
if (existing[name]) {
|
|
43
|
-
throw new Error(`Duplicate relation name: ${name}`);
|
|
44
|
-
}
|
|
45
|
-
existing[name] = relation;
|
|
46
|
-
}
|
|
47
|
-
return existing;
|
|
48
|
-
}
|
|
49
|
-
_validateAndTransformSchema(schema) {
|
|
50
|
-
const relations = {};
|
|
51
|
-
const tables = {};
|
|
52
|
-
const unions = {};
|
|
53
|
-
for (const [name, node] of Object.entries(schema)) {
|
|
54
|
-
if (node instanceof RelationsDefinition) {
|
|
55
|
-
const tableName = node.table.name;
|
|
56
|
-
if (relations[tableName]) {
|
|
57
|
-
relations[tableName] = this._mergeTableRelations(relations[tableName], node.relations);
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
relations[tableName] = {
|
|
61
|
-
...node.relations,
|
|
62
|
-
};
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
if (node instanceof TableDefinition) {
|
|
66
|
-
tables[name] =
|
|
67
|
-
node;
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
if (node instanceof UnionDefinition) {
|
|
71
|
-
unions[name] = node;
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return { tables, relations, unions };
|
|
76
|
-
}
|
|
77
|
-
_buildTables(schema) {
|
|
78
|
-
const tables = new Map();
|
|
79
|
-
for (const [key, def] of Object.entries(schema.tables)) {
|
|
80
|
-
if (def instanceof TableDefinition) {
|
|
81
|
-
const relations = schema.relations[def.name];
|
|
82
|
-
const table = new Table(def, relations, key);
|
|
83
|
-
tables.set(def.name, table);
|
|
84
|
-
tables.set(key, table);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return tables;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Builds every union once the tables exist, checking what only the schema object can tell:
|
|
91
|
-
* that each member is keyed by the alias it is exported under. That is what makes a member
|
|
92
|
-
* key mean the same thing as `$$meta.key` and a node key, rather than a second name for the
|
|
93
|
-
* same table.
|
|
94
|
-
*/
|
|
95
|
-
_buildUnions(schema, definition) {
|
|
96
|
-
const unions = new Map();
|
|
97
|
-
for (const [alias, union] of Object.entries(schema.unions)) {
|
|
98
|
-
if (this._tables.has(alias)) {
|
|
99
|
-
throw new Error(`Union "${alias}" has the same name as a table in the schema. A union and a table ` +
|
|
100
|
-
`are both read through the client by that name, so it can only mean one of them.`);
|
|
101
|
-
}
|
|
102
|
-
const members = {};
|
|
103
|
-
for (const [key, member] of Object.entries(union.members)) {
|
|
104
|
-
if (definition[key] !== member) {
|
|
105
|
-
throw new Error(`Union "${alias}" lists table "${member.name}" under "${key}", but the schema does ` +
|
|
106
|
-
`not export that table as "${key}". Union members are keyed by their schema alias.`);
|
|
107
|
-
}
|
|
108
|
-
members[key] = this.getTable(key);
|
|
109
|
-
}
|
|
110
|
-
unions.set(alias, new Union(union, members, alias));
|
|
111
|
-
}
|
|
112
|
-
return unions;
|
|
113
|
-
}
|
|
114
|
-
_buildClaimKeys(schema) {
|
|
115
|
-
const claims = new Map();
|
|
116
|
-
const sources = new Map();
|
|
117
|
-
for (const [key, def] of Object.entries(schema.tables)) {
|
|
118
|
-
const table = this._tables.get(key);
|
|
119
|
-
if (!table) {
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
for (const [claim] of table.tenantKeys) {
|
|
123
|
-
const dataType = def.columns[claim]["_dataType"];
|
|
124
|
-
const declared = claims.get(claim);
|
|
125
|
-
if (declared !== undefined && declared !== dataType) {
|
|
126
|
-
throw new Error(`Claim "${claim}" is "${declared}" on table "${sources.get(claim)}" but "${dataType}" ` +
|
|
127
|
-
`on table "${def.name}". One claim name means one claim, so it must have the same ` +
|
|
128
|
-
`type on every table that declares it.`);
|
|
129
|
-
}
|
|
130
|
-
claims.set(claim, dataType);
|
|
131
|
-
sources.set(claim, def.name);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return claims;
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* The field alias a column is declared under on `table`, or `undefined` when the column
|
|
138
|
-
* does not belong to it. Identity, not name: a column from a different table that happens
|
|
139
|
-
* to share a name is not a match, which is the copy-paste mistake this catches.
|
|
140
|
-
*/
|
|
141
|
-
/**
|
|
142
|
-
* The field a column is declared under on a table, if it is one of its plain columns. A
|
|
143
|
-
* relation key or a discriminator naming a column group's member throws: relations correlate
|
|
144
|
-
* plain columns only, as primary keys and tenant claims do.
|
|
145
|
-
*/
|
|
146
|
-
_findColumnAlias(table, column) {
|
|
147
|
-
const alias = Object.entries(table.columns).find(([, candidate]) => candidate === column)?.[0];
|
|
148
|
-
if (alias === undefined) {
|
|
149
|
-
const member = columnEntries(table.columns).find(([, candidate]) => candidate === column);
|
|
150
|
-
if (member) {
|
|
151
|
-
throw new Error(`Column "${column.name}" is a member of the column group "${member[0][0]}" on table ` +
|
|
152
|
-
`"${table.name}". A relation key or a discriminator must be a plain column.`);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return alias;
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Checks that a relation's column pairs line up: equal, non-zero length; each column
|
|
159
|
-
* declared on the side it is listed under; both columns of a pair of the same type.
|
|
160
|
-
*
|
|
161
|
-
* The query builder correlates over every pair (`packages/core/src/runtime/query.ts`), so
|
|
162
|
-
* a malformed relation would otherwise surface as a confusing SQL error at query time, or
|
|
163
|
-
* not at all.
|
|
164
|
-
*/
|
|
165
|
-
_validateRelation(sourceName, field, relation, definitions) {
|
|
166
|
-
const label = `Relation "${field}" on table "${sourceName}"`;
|
|
167
|
-
const { from } = relation;
|
|
168
|
-
const source = definitions.get(sourceName);
|
|
169
|
-
if (!source) {
|
|
170
|
-
throw new Error(`${label} refers to a table that is not in the schema.`);
|
|
171
|
-
}
|
|
172
|
-
if (from.length === 0) {
|
|
173
|
-
throw new Error(`${label} must declare at least one column pair in "from" and "to".`);
|
|
174
|
-
}
|
|
175
|
-
if (relation.target instanceof UnionDefinition) {
|
|
176
|
-
this._validateUnionRelation(label, sourceName, field, relation, source);
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
const to = relation.to;
|
|
180
|
-
if (to.length === 0) {
|
|
181
|
-
throw new Error(`${label} must declare at least one column pair in "from" and "to".`);
|
|
182
|
-
}
|
|
183
|
-
if (from.length !== to.length) {
|
|
184
|
-
throw new Error(`${label} pairs ${from.length} "from" column(s) with ${to.length} "to" column(s); ` +
|
|
185
|
-
`the two sides must have the same length.`);
|
|
186
|
-
}
|
|
187
|
-
if (relation.discriminator) {
|
|
188
|
-
throw new Error(`${label} declares a discriminator, but its target is a single table. Only a ` +
|
|
189
|
-
`belongs-to a union needs one.`);
|
|
190
|
-
}
|
|
191
|
-
const target = definitions.get(relation.target.name);
|
|
192
|
-
if (!target) {
|
|
193
|
-
throw new Error(`${label} targets table "${relation.target.name}", which is not in the schema.`);
|
|
194
|
-
}
|
|
195
|
-
for (const [index, fromColumn] of from.entries()) {
|
|
196
|
-
const toColumn = to[index];
|
|
197
|
-
const fromAlias = this._findColumnAlias(source, fromColumn);
|
|
198
|
-
const toAlias = this._findColumnAlias(target, toColumn);
|
|
199
|
-
if (!fromAlias) {
|
|
200
|
-
throw new Error(`${label}: "from" column "${fromColumn.name}" is not declared on table "${sourceName}".`);
|
|
201
|
-
}
|
|
202
|
-
if (!toAlias) {
|
|
203
|
-
throw new Error(`${label}: "to" column "${toColumn.name}" is not declared on target table "${relation.target.name}".`);
|
|
204
|
-
}
|
|
205
|
-
const fromType = fromColumn["_dataType"];
|
|
206
|
-
const toType = toColumn["_dataType"];
|
|
207
|
-
if (fromType !== toType) {
|
|
208
|
-
throw new Error(`${label}: "${sourceName}"."${fromAlias}" is "${fromType}" but ` +
|
|
209
|
-
`"${relation.target.name}"."${toAlias}" is "${toType}"; ` +
|
|
210
|
-
`both columns of a pair must have the same type.`);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* The union half of {@link _validateRelation}: the union must be registered, `to` must name
|
|
216
|
-
* a column on every member — through a shared field or a per-member list — paired with
|
|
217
|
-
* `from` by type, and only a belongs-to carries a discriminator, which it must.
|
|
218
|
-
*/
|
|
219
|
-
_validateUnionRelation(label, sourceName, field, relation, source) {
|
|
220
|
-
const target = relation.target;
|
|
221
|
-
const union = [...this._unions.values()].find((candidate) => candidate.definition === target);
|
|
222
|
-
if (!union) {
|
|
223
|
-
throw new Error(`${label} targets ${target.name}, which is not in the schema.`);
|
|
224
|
-
}
|
|
225
|
-
const { from } = relation;
|
|
226
|
-
const lists = this._getMemberToColumns(label, union, relation.to);
|
|
227
|
-
const pairs = {};
|
|
228
|
-
for (const [alias, to] of Object.entries(lists)) {
|
|
229
|
-
const member = union.getMember(alias);
|
|
230
|
-
const memberDefinition = union.definition.members[alias];
|
|
231
|
-
if (to.length !== from.length) {
|
|
232
|
-
throw new Error(`${label} pairs ${from.length} "from" column(s) with ${to.length} "to" column(s) on ` +
|
|
233
|
-
`member "${alias}"; the two sides must have the same length.`);
|
|
234
|
-
}
|
|
235
|
-
pairs[alias] = to.map((toColumn, index) => {
|
|
236
|
-
const fromColumn = from[index];
|
|
237
|
-
const fromAlias = this._findColumnAlias(source, fromColumn);
|
|
238
|
-
const toAlias = this._findColumnAlias(memberDefinition, toColumn);
|
|
239
|
-
if (!fromAlias) {
|
|
240
|
-
throw new Error(`${label}: "from" column "${fromColumn.name}" is not declared on table "${sourceName}".`);
|
|
241
|
-
}
|
|
242
|
-
if (!toAlias) {
|
|
243
|
-
throw new Error(`${label}: "to" column "${toColumn.name}" is not declared on member "${alias}" of ` +
|
|
244
|
-
`union "${union.alias}".`);
|
|
245
|
-
}
|
|
246
|
-
if (fromColumn["_dataType"] !== toColumn["_dataType"]) {
|
|
247
|
-
throw new Error(`${label}: "${sourceName}"."${fromAlias}" is "${fromColumn["_dataType"]}" but ` +
|
|
248
|
-
`"${alias}"."${toAlias}" is "${toColumn["_dataType"]}"; both columns of a pair ` +
|
|
249
|
-
`must have the same type.`);
|
|
250
|
-
}
|
|
251
|
-
return member.getColumn(toAlias);
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
this._validateDiscriminator(label, relation, source, union);
|
|
255
|
-
if (relation.discriminator) {
|
|
256
|
-
const byField = this._discriminators.get(sourceName) ?? new Map();
|
|
257
|
-
const alias = this._findColumnAlias(source, relation.discriminator);
|
|
258
|
-
byField.set(field, this.getTable(sourceName).getColumn(alias));
|
|
259
|
-
this._discriminators.set(sourceName, byField);
|
|
260
|
-
}
|
|
261
|
-
const byField = this._unionPairs.get(sourceName) ?? new Map();
|
|
262
|
-
byField.set(field, pairs);
|
|
263
|
-
this._unionPairs.set(sourceName, byField);
|
|
264
|
-
}
|
|
265
|
-
/** `to` as one column-definition list per member, whichever form it was written in. */
|
|
266
|
-
_getMemberToColumns(label, union, to) {
|
|
267
|
-
if (Array.isArray(to)) {
|
|
268
|
-
if (to.length === 0) {
|
|
269
|
-
throw new Error(`${label} must declare at least one column pair in "from" and "to".`);
|
|
270
|
-
}
|
|
271
|
-
return Object.fromEntries(union.memberAliases.map((alias) => [
|
|
272
|
-
alias,
|
|
273
|
-
to.map((column) => {
|
|
274
|
-
if (!(column instanceof UnionColumnDefinition) ||
|
|
275
|
-
union.definition.columns[column.name] !== column) {
|
|
276
|
-
throw new Error(`${label}: a "to" column given as a list must be a shared field of union ` +
|
|
277
|
-
`"${union.alias}" (\`${union.alias}.columns.<field>\`). To name a different ` +
|
|
278
|
-
`column per member, pass one list per member instead.`);
|
|
279
|
-
}
|
|
280
|
-
return column.members[alias];
|
|
281
|
-
}),
|
|
282
|
-
]));
|
|
283
|
-
}
|
|
284
|
-
const lists = to;
|
|
285
|
-
const given = Object.keys(lists);
|
|
286
|
-
const missing = union.memberAliases.filter((alias) => !given.includes(alias));
|
|
287
|
-
const unknown = given.filter((alias) => !union.hasMember(alias));
|
|
288
|
-
if (missing.length > 0 || unknown.length > 0) {
|
|
289
|
-
throw new Error(`${label} must name one "to" column list per member of union "${union.alias}" ` +
|
|
290
|
-
`(${union.memberAliases.join(", ")})` +
|
|
291
|
-
(missing.length > 0 ? `; missing: ${missing.join(", ")}` : "") +
|
|
292
|
-
(unknown.length > 0 ? `; not members: ${unknown.join(", ")}` : "") +
|
|
293
|
-
`.`);
|
|
294
|
-
}
|
|
295
|
-
return lists;
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* A belongs-to a union stores which member it points at in a column of its own — the
|
|
299
|
-
* discriminator, holding a member alias. Nothing else can tell the members apart: their keys
|
|
300
|
-
* share one column. Has-many and has-one to a union need none, since the key lives on each
|
|
301
|
-
* member, so one there is a mistake.
|
|
302
|
-
*/
|
|
303
|
-
_validateDiscriminator(label, relation, source, union) {
|
|
304
|
-
const { discriminator } = relation;
|
|
305
|
-
if (relation.type !== Relation.BELONGS_TO) {
|
|
306
|
-
if (discriminator) {
|
|
307
|
-
throw new Error(`${label} declares a discriminator, but only a belongs-to a union needs one: the ` +
|
|
308
|
-
`members of "${union.alias}" hold the key.`);
|
|
309
|
-
}
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
if (!discriminator) {
|
|
313
|
-
throw new Error(`${label} belongs to union "${union.alias}" and must declare a discriminator: the ` +
|
|
314
|
-
`source column holding which member each row points at.`);
|
|
315
|
-
}
|
|
316
|
-
const alias = this._findColumnAlias(source, discriminator);
|
|
317
|
-
if (!alias) {
|
|
318
|
-
throw new Error(`${label}: discriminator "${discriminator.name}" is not declared on table "${source.name}".`);
|
|
319
|
-
}
|
|
320
|
-
const dataType = discriminator["_dataType"];
|
|
321
|
-
const textLike = discriminator["_domain"] !== undefined ||
|
|
322
|
-
dataType === "text" ||
|
|
323
|
-
/^(varchar|char)\(\d+\)$/.test(dataType);
|
|
324
|
-
if (!textLike) {
|
|
325
|
-
throw new Error(`${label}: discriminator "${source.name}"."${alias}" is "${dataType}"; it holds a ` +
|
|
326
|
-
`member alias, so it must be text, varchar, char, or a text domain.`);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
_buildRelations(schema) {
|
|
330
|
-
const map = new Map();
|
|
331
|
-
const definitions = new Map();
|
|
332
|
-
for (const definition of Object.values(schema.tables)) {
|
|
333
|
-
if (definition instanceof TableDefinition) {
|
|
334
|
-
definitions.set(definition.name, definition);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
for (const [tableName, relations] of Object.entries(schema.relations)) {
|
|
338
|
-
const table = this._tables.get(tableName);
|
|
339
|
-
if (!table) {
|
|
340
|
-
throw new Error(`Table not found for relations: ${tableName}`);
|
|
341
|
-
}
|
|
342
|
-
for (const [field, relation] of Object.entries(relations)) {
|
|
343
|
-
// Columns and relations share one namespace: the client addresses both as fields of
|
|
344
|
-
// the same model (`select`, `join`, and the keys of a result row), so a name can
|
|
345
|
-
// only mean one of them.
|
|
346
|
-
if (table.hasColumn(field)) {
|
|
347
|
-
throw new Error(`Relation "${field}" on table "${tableName}" collides with a column of the same ` +
|
|
348
|
-
`name. Columns and relations share one field namespace on a table.`);
|
|
349
|
-
}
|
|
350
|
-
// That one namespace also excludes the names the runtime writes onto result rows.
|
|
351
|
-
if (RESERVED_FIELD_NAMES.includes(field)) {
|
|
352
|
-
throw new Error(`Table "${tableName}" declares a relation named "${field}", which is reserved. ` +
|
|
353
|
-
`The runtime writes ${RESERVED_FIELD_NAMES.join(" and ")} onto every result row.`);
|
|
354
|
-
}
|
|
355
|
-
this._validateRelation(tableName, field, relation, definitions);
|
|
356
|
-
}
|
|
357
|
-
map.set(tableName, relations);
|
|
358
|
-
}
|
|
359
|
-
return map;
|
|
15
|
+
validateSchema(definition);
|
|
16
|
+
const built = buildSchema(definition);
|
|
17
|
+
this._tables = built.tables;
|
|
18
|
+
this._unions = built.unions;
|
|
19
|
+
this._relations = built.relations;
|
|
360
20
|
}
|
|
361
21
|
getTable(aliasOrName) {
|
|
362
22
|
const table = this._tables.get(aliasOrName);
|
|
@@ -390,20 +50,21 @@ export class SchemaRegistry {
|
|
|
390
50
|
getAlias(nameOrAlias) {
|
|
391
51
|
return this.getTable(nameOrAlias).alias;
|
|
392
52
|
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
return false;
|
|
396
|
-
}
|
|
397
|
-
const table = this.getTable(tableNameOrAlias);
|
|
398
|
-
return this._relations.has(table.name);
|
|
53
|
+
hasRelation(table, field) {
|
|
54
|
+
return Object.hasOwn(this.getRelations(table), field);
|
|
399
55
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
return
|
|
56
|
+
/**
|
|
57
|
+
* A table's relation by field, resolved: runtime columns on both sides, the runtime target,
|
|
58
|
+
* and for a relation to a union its per-member columns and discriminator. `undefined` when
|
|
59
|
+
* the table has no such relation. Takes the table, its alias or its database name.
|
|
60
|
+
*/
|
|
61
|
+
getRelation(table, field) {
|
|
62
|
+
return this.getRelations(table)[field];
|
|
63
|
+
}
|
|
64
|
+
/** Every relation a table has, by field — empty for a table with none. */
|
|
65
|
+
getRelations(table) {
|
|
66
|
+
const resolved = typeof table === "string" ? this.getTable(table) : table;
|
|
67
|
+
return this._relations.get(resolved) ?? {};
|
|
407
68
|
}
|
|
408
69
|
hasUnion(alias) {
|
|
409
70
|
return this._unions.has(alias);
|
|
@@ -419,45 +80,5 @@ export class SchemaRegistry {
|
|
|
419
80
|
getUnions() {
|
|
420
81
|
return [...this._unions.entries()];
|
|
421
82
|
}
|
|
422
|
-
/**
|
|
423
|
-
* What a relation points at: a table, or a union when the relation targets one. Callers
|
|
424
|
-
* that only handle tables must narrow with `instanceof Union`.
|
|
425
|
-
*/
|
|
426
|
-
getRelationTarget(tableNameOrAlias, field) {
|
|
427
|
-
const sourceTable = this.getTable(tableNameOrAlias);
|
|
428
|
-
const relations = this._relations.get(sourceTable.name);
|
|
429
|
-
if (!relations?.[field]) {
|
|
430
|
-
throw new Error(`Relation not found for field: ${field} on table: ${sourceTable.name}`);
|
|
431
|
-
}
|
|
432
|
-
const target = relations[field].target;
|
|
433
|
-
if (target instanceof UnionDefinition) {
|
|
434
|
-
const union = [...this._unions.values()].find((candidate) => candidate.definition === target);
|
|
435
|
-
if (!union) {
|
|
436
|
-
throw new Error(`Target union not found for relation: ${field} on table: ${sourceTable.name}`);
|
|
437
|
-
}
|
|
438
|
-
return union;
|
|
439
|
-
}
|
|
440
|
-
return this.getTable(target.name);
|
|
441
|
-
}
|
|
442
|
-
/**
|
|
443
|
-
* The `to` columns of a relation to a union, per member alias — each member's own columns,
|
|
444
|
-
* paired index by index with the relation's `from`.
|
|
445
|
-
*/
|
|
446
|
-
/**
|
|
447
|
-
* The discriminator column of a belongs-to a union — the source column naming which member
|
|
448
|
-
* each row points at — or `undefined` for any other relation.
|
|
449
|
-
*/
|
|
450
|
-
getRelationDiscriminator(tableNameOrAlias, field) {
|
|
451
|
-
const sourceTable = this.getTable(tableNameOrAlias);
|
|
452
|
-
return this._discriminators.get(sourceTable.name)?.get(field);
|
|
453
|
-
}
|
|
454
|
-
getUnionRelationColumns(tableNameOrAlias, field) {
|
|
455
|
-
const sourceTable = this.getTable(tableNameOrAlias);
|
|
456
|
-
const pairs = this._unionPairs.get(sourceTable.name)?.get(field);
|
|
457
|
-
if (!pairs) {
|
|
458
|
-
throw new Error(`Relation "${field}" on table "${sourceTable.name}" does not target a union.`);
|
|
459
|
-
}
|
|
460
|
-
return pairs;
|
|
461
|
-
}
|
|
462
83
|
}
|
|
463
84
|
//# sourceMappingURL=registry.js.map
|