@dsqlbase/core 0.1.6 → 0.2.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 +195 -0
- package/LICENSE +21 -0
- package/README.md +4 -0
- package/dist/definition/base.d.ts +50 -3
- package/dist/definition/base.d.ts.map +1 -1
- package/dist/definition/base.js +34 -1
- package/dist/definition/base.js.map +1 -1
- package/dist/definition/column.d.ts +87 -14
- package/dist/definition/column.d.ts.map +1 -1
- package/dist/definition/column.js +99 -5
- package/dist/definition/column.js.map +1 -1
- package/dist/definition/constraint.d.ts +16 -15
- package/dist/definition/constraint.d.ts.map +1 -1
- package/dist/definition/constraint.js +4 -1
- package/dist/definition/constraint.js.map +1 -1
- package/dist/definition/domain.d.ts +12 -8
- package/dist/definition/domain.d.ts.map +1 -1
- package/dist/definition/domain.js +10 -2
- package/dist/definition/domain.js.map +1 -1
- package/dist/definition/embedded.d.ts +131 -0
- package/dist/definition/embedded.d.ts.map +1 -0
- package/dist/definition/embedded.js +133 -0
- package/dist/definition/embedded.js.map +1 -0
- package/dist/definition/index.d.ts +7 -4
- package/dist/definition/index.d.ts.map +1 -1
- package/dist/definition/index.js +4 -1
- package/dist/definition/index.js.map +1 -1
- package/dist/definition/indexes.d.ts +49 -19
- package/dist/definition/indexes.d.ts.map +1 -1
- package/dist/definition/indexes.js +38 -9
- 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/relations.d.ts +47 -15
- package/dist/definition/relations.d.ts.map +1 -1
- package/dist/definition/relations.js +17 -12
- package/dist/definition/relations.js.map +1 -1
- package/dist/definition/sequence.d.ts +4 -4
- package/dist/definition/sequence.d.ts.map +1 -1
- package/dist/definition/sequence.js.map +1 -1
- package/dist/definition/table.d.ts +118 -13
- package/dist/definition/table.d.ts.map +1 -1
- package/dist/definition/table.js +137 -4
- package/dist/definition/table.js.map +1 -1
- package/dist/definition/tenant.d.ts +50 -0
- package/dist/definition/tenant.d.ts.map +1 -0
- package/dist/definition/tenant.js +73 -0
- package/dist/definition/tenant.js.map +1 -0
- package/dist/definition/union.d.ts +84 -0
- package/dist/definition/union.d.ts.map +1 -0
- package/dist/definition/union.js +89 -0
- package/dist/definition/union.js.map +1 -0
- package/dist/definition/view.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/runtime/base.d.ts +22 -4
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js.map +1 -1
- package/dist/runtime/column.d.ts +54 -3
- package/dist/runtime/column.d.ts.map +1 -1
- package/dist/runtime/column.js +75 -7
- package/dist/runtime/column.js.map +1 -1
- package/dist/runtime/context.d.ts +30 -2
- package/dist/runtime/context.d.ts.map +1 -1
- package/dist/runtime/context.js +6 -0
- package/dist/runtime/context.js.map +1 -1
- package/dist/runtime/errors.d.ts +14 -0
- package/dist/runtime/errors.d.ts.map +1 -0
- package/dist/runtime/errors.js +19 -0
- package/dist/runtime/errors.js.map +1 -0
- package/dist/runtime/executor.d.ts +29 -1
- package/dist/runtime/executor.d.ts.map +1 -1
- package/dist/runtime/executor.js +23 -0
- package/dist/runtime/executor.js.map +1 -1
- package/dist/runtime/group.d.ts +54 -0
- package/dist/runtime/group.d.ts.map +1 -0
- package/dist/runtime/group.js +96 -0
- package/dist/runtime/group.js.map +1 -0
- package/dist/runtime/index.d.ts +9 -6
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +6 -3
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/operation.d.ts +227 -29
- package/dist/runtime/operation.d.ts.map +1 -1
- package/dist/runtime/operation.js +525 -49
- package/dist/runtime/operation.js.map +1 -1
- package/dist/runtime/query.d.ts +102 -34
- package/dist/runtime/query.d.ts.map +1 -1
- package/dist/runtime/query.js +140 -31
- package/dist/runtime/query.js.map +1 -1
- package/dist/runtime/registry.d.ts +114 -4
- package/dist/runtime/registry.d.ts.map +1 -1
- package/dist/runtime/registry.js +366 -9
- package/dist/runtime/registry.js.map +1 -1
- package/dist/runtime/session.d.ts +10 -4
- package/dist/runtime/session.d.ts.map +1 -1
- package/dist/runtime/session.js.map +1 -1
- package/dist/runtime/table.d.ts +87 -8
- package/dist/runtime/table.d.ts.map +1 -1
- package/dist/runtime/table.js +142 -5
- package/dist/runtime/table.js.map +1 -1
- package/dist/runtime/union.d.ts +33 -0
- package/dist/runtime/union.d.ts.map +1 -0
- package/dist/runtime/union.js +69 -0
- package/dist/runtime/union.js.map +1 -0
- package/dist/sql/index.d.ts +1 -1
- package/dist/sql/index.d.ts.map +1 -1
- package/dist/sql/index.js.map +1 -1
- package/dist/sql/nodes.d.ts +33 -6
- package/dist/sql/nodes.d.ts.map +1 -1
- package/dist/sql/nodes.js +25 -0
- package/dist/sql/nodes.js.map +1 -1
- package/dist/sql/tag.d.ts +17 -0
- package/dist/sql/tag.d.ts.map +1 -1
- package/dist/sql/tag.js +95 -0
- package/dist/sql/tag.js.map +1 -1
- package/dist/sql/utils.d.ts +2 -2
- package/dist/sql/utils.d.ts.map +1 -1
- package/dist/sql/utils.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/thenable.js.map +1 -1
- package/dist/utils/types.d.ts +50 -2
- package/dist/utils/types.d.ts.map +1 -1
- package/dist/utils/types.js.map +1 -1
- package/package.json +27 -22
- package/dist/sql/expressions.d.ts +0 -25
- package/dist/sql/expressions.d.ts.map +0 -1
- package/dist/sql/expressions.js +0 -79
- package/dist/sql/expressions.js.map +0 -1
package/dist/runtime/table.d.ts
CHANGED
|
@@ -1,35 +1,114 @@
|
|
|
1
1
|
import { TypedObject } from "../utils/index.js";
|
|
2
|
-
import {
|
|
2
|
+
import { AnyTableRelations, ColumnConfig, ColumnDefinition, AnyNamespaceDefinition, TableConfig, TableDefinition, TableColumnDefinitions, NamespaceDefinition } from "../definition/index.js";
|
|
3
3
|
import { SQLContext, SQLNode, SQLStatement } from "../sql/index.js";
|
|
4
|
-
import { Column } from "./column.js";
|
|
4
|
+
import { AnyColumn, Column } from "./column.js";
|
|
5
|
+
import { AnyColumnGroup, AnyField, ColumnGroup } from "./group.js";
|
|
5
6
|
export type AnyTable = Table<any, any, any, any>;
|
|
6
|
-
|
|
7
|
+
/**
|
|
8
|
+
* What a row reports about the table it came from, written to every result record as
|
|
9
|
+
* `$$meta` (`packages/core/src/runtime/operation.ts`).
|
|
10
|
+
*
|
|
11
|
+
* `key` is the schema alias — the name the client addresses the table by, and the intended
|
|
12
|
+
* discriminant when a row could have come from one of several tables. `table` is the
|
|
13
|
+
* database name, which may differ.
|
|
14
|
+
*/
|
|
15
|
+
export type RecordMeta = {
|
|
16
|
+
readonly key: string;
|
|
17
|
+
readonly table: string;
|
|
18
|
+
readonly schema?: string;
|
|
19
|
+
readonly [field: string]: unknown;
|
|
20
|
+
};
|
|
21
|
+
export type WithRelations<TColumns extends TableColumnDefinitions, TNamespace extends AnyNamespaceDefinition, TRelations extends AnyTableRelations | undefined> = TableConfig<TColumns, TNamespace> & {
|
|
7
22
|
relations: TRelations extends AnyTableRelations ? TRelations : never;
|
|
8
23
|
};
|
|
9
24
|
export type TableSchemaName<T extends AnyTable> = T["__type"] extends {
|
|
10
25
|
schema: infer S;
|
|
11
26
|
} ? S extends NamespaceDefinition<infer SN> ? SN : never : undefined;
|
|
27
|
+
/** A table's fields, without its deprecated columns: the client never sees those. */
|
|
12
28
|
export type TableColumnName<T extends AnyTable> = T["__type"] extends {
|
|
13
29
|
columns: infer C;
|
|
14
|
-
} ? C extends
|
|
30
|
+
} ? C extends TableColumnDefinitions ? {
|
|
31
|
+
[K in keyof C]: C[K] extends {
|
|
32
|
+
__type: {
|
|
33
|
+
deprecated: true;
|
|
34
|
+
};
|
|
35
|
+
} ? never : K;
|
|
36
|
+
}[keyof C] : never : never;
|
|
15
37
|
export type TableColumns<T extends AnyTable> = {
|
|
16
38
|
readonly [K in TableColumnName<T>]: T["__type"] extends {
|
|
17
39
|
columns: infer C;
|
|
18
|
-
} ? C extends Record<K, infer CD> ? CD extends
|
|
40
|
+
} ? C extends Record<K, infer CD> ? CD extends {
|
|
41
|
+
readonly kind: "COLUMN_GROUP";
|
|
42
|
+
readonly columns: infer GC;
|
|
43
|
+
} ? GC extends TableColumnDefinitions ? ColumnGroup<string, GC, T> : never : CD extends ColumnDefinition<infer CName, infer CConfig> ? Column<CName, CConfig, T> : never : never : never;
|
|
19
44
|
};
|
|
20
|
-
export declare class Table<TName extends string, TColumns extends
|
|
45
|
+
export declare class Table<TName extends string, TColumns extends TableColumnDefinitions, TNamespace extends AnyNamespaceDefinition, TRelations extends AnyTableRelations> implements SQLNode, TypedObject<TableConfig<TColumns, TNamespace>> {
|
|
21
46
|
readonly __type: WithRelations<TColumns, TNamespace, TRelations>;
|
|
22
47
|
readonly name: TName;
|
|
48
|
+
/**
|
|
49
|
+
* The key this table is exported under in the schema object — the name the client
|
|
50
|
+
* addresses it by (`dsql.members`), which may differ from the database table name
|
|
51
|
+
* (`team_members`). Set by `SchemaRegistry`; defaults to `name` when a `Table` is
|
|
52
|
+
* constructed directly.
|
|
53
|
+
*/
|
|
54
|
+
readonly alias: string;
|
|
23
55
|
readonly schema: TableSchemaName<this>;
|
|
24
56
|
readonly columns: TableColumns<this>;
|
|
25
57
|
readonly relations: TRelations;
|
|
26
|
-
|
|
58
|
+
/**
|
|
59
|
+
* The columns of the table's primary key, in key order. Empty when the table declares none.
|
|
60
|
+
*
|
|
61
|
+
* A table has at most one primary key; a composite key is that one key spanning several
|
|
62
|
+
* columns. Declaring more than one — two flagged columns, a flagged column alongside a
|
|
63
|
+
* table-level constraint, or two table-level constraints — throws when the table is built.
|
|
64
|
+
*/
|
|
65
|
+
readonly primaryKey: AnyColumn[];
|
|
66
|
+
/** True when the primary key spans more than one column. */
|
|
67
|
+
readonly isCompositeKey: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* The table's tenant claim columns, paired with the claim they are filled from, in
|
|
70
|
+
* declaration order. Empty for a table outside any tenant scope.
|
|
71
|
+
*
|
|
72
|
+
* The claim is the *field* name, not the database column name: it is the key the runtime
|
|
73
|
+
* looks up in `ExecutionContext.identity`, and the name `tenantScope()` declared it under.
|
|
74
|
+
*/
|
|
75
|
+
readonly tenantKeys: [claim: string, column: AnyColumn][];
|
|
76
|
+
/**
|
|
77
|
+
* What every row of this table reports about itself, surfaced as `$$meta` on each result
|
|
78
|
+
* record — the built-in `key` / `table` / `schema` plus whatever `table().meta()` declared.
|
|
79
|
+
*
|
|
80
|
+
* Frozen and built once: the same object is shared by reference across every row of a
|
|
81
|
+
* level, because nothing in it varies per row.
|
|
82
|
+
*/
|
|
83
|
+
readonly meta: RecordMeta;
|
|
84
|
+
private readonly _ref;
|
|
85
|
+
constructor(definition: TableDefinition<TName, TColumns, TNamespace>, relations?: TRelations, alias?: string);
|
|
27
86
|
private _buildColumns;
|
|
87
|
+
private _buildPrimaryKey;
|
|
88
|
+
private _buildMeta;
|
|
28
89
|
hasColumn(name: string): boolean;
|
|
29
|
-
|
|
90
|
+
/**
|
|
91
|
+
* The field `name` — a column or a column group — or the plain column whose database name it
|
|
92
|
+
* is. A group's member is reached through the group: `getColumn("netValue")`, then the
|
|
93
|
+
* group's `getColumn("amount")`.
|
|
94
|
+
*/
|
|
95
|
+
getColumn(name: string): AnyField | undefined;
|
|
96
|
+
/** The table's plain columns, by field. A column group's members are not among them. */
|
|
30
97
|
getColumnEntries(): [string, Column<string, ColumnConfig, this>][];
|
|
98
|
+
/** The table's column groups, by field. */
|
|
99
|
+
getGroupEntries(): [string, AnyColumnGroup][];
|
|
100
|
+
/**
|
|
101
|
+
* Every real column of the table with its field path — `["netValue", "amount"]` for a group's
|
|
102
|
+
* member — groups walked depth first, in declaration order.
|
|
103
|
+
*/
|
|
104
|
+
getLeafEntries(): [path: string[], column: AnyColumn][];
|
|
31
105
|
hasRelation(fieldName: string): boolean;
|
|
32
106
|
getRelation(fieldName: string): import("../definition/relations.js").AnyFieldRelation | undefined;
|
|
107
|
+
/**
|
|
108
|
+
* The node that names this table when qualifying a column: the alias bound in the
|
|
109
|
+
* rendering context, else the table name. Cached — it holds no per-render state.
|
|
110
|
+
*/
|
|
111
|
+
ref(): SQLNode;
|
|
33
112
|
toSQL(ctx: SQLContext): SQLStatement;
|
|
34
113
|
}
|
|
35
114
|
//# sourceMappingURL=table.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/runtime/table.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/runtime/table.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACL,iBAAiB,EACjB,YAAY,EACZ,gBAAgB,EAChB,sBAAsB,EACtB,WAAW,EACX,eAAe,EACf,sBAAsB,EACtB,mBAAmB,EAGpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAO,UAAU,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACzE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGnE,MAAM,MAAM,QAAQ,GAAG,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAEjD;;;;;;;GAOG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EA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EAAE,UAAU,CAAC;IAE1B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA+B;gBAGlD,UAAU,EAAE,eAAe,CAAC,KAAK,EAAE,QAAQ,EAAE,UAAU,CAAC,EACxD,SAAS,CAAC,EAAE,UAAU,EACtB,KAAK,CAAC,EAAE,MAAM;IAahB,OAAO,CAAC,aAAa;IAgBrB,OAAO,CAAC,gBAAgB;IAuCxB,OAAO,CAAC,UAAU;IAoBX,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIvC;;;;OAIG;IACI,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS;IAQpD,wFAAwF;IACjF,gBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,CAAC,CAAC,EAAE;IAMzE,2CAA2C;IACpC,eAAe,IAAI,CAAC,MAAM,EAAE,cAAc,CAAC,EAAE;IAMpD;;;OAGG;IACI,cAAc,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE;IAWvD,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAIvC,WAAW,CAAC,SAAS,EAAE,MAAM;IAQpC;;;OAGG;IACI,GAAG,IAAI,OAAO;IAId,KAAK,CAAC,GAAG,EAAE,UAAU,GAAG,YAAY;CAO5C"}
|
package/dist/runtime/table.js
CHANGED
|
@@ -1,34 +1,164 @@
|
|
|
1
|
+
import { ColumnDefinition, ColumnGroupDefinition, PrimaryKeyConstraintDefinition, } from "../definition/index.js";
|
|
1
2
|
import { sql } from "../sql/index.js";
|
|
2
3
|
import { Column } from "./column.js";
|
|
4
|
+
import { ColumnGroup } from "./group.js";
|
|
5
|
+
/** Meta keys the runtime owns; `table().meta()` may not redeclare them. */
|
|
6
|
+
const BUILT_IN_META_KEYS = Object.freeze(["key", "table", "schema"]);
|
|
7
|
+
/**
|
|
8
|
+
* How a table is named when it *qualifies a column* — the alias bound for it in the
|
|
9
|
+
* rendering context, or its bare name when none is.
|
|
10
|
+
*
|
|
11
|
+
* Counterpart to `Table.toSQL`, which renders the table as a *source*
|
|
12
|
+
* (`"billing"."invoices"`, for `FROM` / `UPDATE` / `INSERT INTO`). A source says *which
|
|
13
|
+
* table*, so it carries the schema; a reference says *which correlation in this query*,
|
|
14
|
+
* where the schema is not part of the name.
|
|
15
|
+
*
|
|
16
|
+
* Deliberately **not** schema-qualified when unaliased, for two reasons Postgres enforces:
|
|
17
|
+
* once a source is aliased, `"billing"."invoices"."id"` is rejected outright with
|
|
18
|
+
* `invalid reference to FROM-clause entry`; and schema-qualifying would not rescue an
|
|
19
|
+
* unaliased shadowed subquery anyway, because that needs *both* sides qualified and a table
|
|
20
|
+
* declared without a namespace has no schema to qualify with. Aliasing is the fix; this
|
|
21
|
+
* fallback only ever runs where exactly one table is in scope (DML, `$query`).
|
|
22
|
+
*/
|
|
23
|
+
class TableRef {
|
|
24
|
+
_table;
|
|
25
|
+
constructor(table) {
|
|
26
|
+
this._table = table;
|
|
27
|
+
}
|
|
28
|
+
toSQL(ctx) {
|
|
29
|
+
return sql.identifier(ctx.aliases?.get(this._table) ?? this._table.name).toSQL(ctx);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
3
32
|
export class Table {
|
|
4
33
|
name;
|
|
34
|
+
/**
|
|
35
|
+
* The key this table is exported under in the schema object — the name the client
|
|
36
|
+
* addresses it by (`dsql.members`), which may differ from the database table name
|
|
37
|
+
* (`team_members`). Set by `SchemaRegistry`; defaults to `name` when a `Table` is
|
|
38
|
+
* constructed directly.
|
|
39
|
+
*/
|
|
40
|
+
alias;
|
|
5
41
|
schema;
|
|
6
42
|
columns;
|
|
7
43
|
relations;
|
|
8
|
-
|
|
44
|
+
/**
|
|
45
|
+
* The columns of the table's primary key, in key order. Empty when the table declares none.
|
|
46
|
+
*
|
|
47
|
+
* A table has at most one primary key; a composite key is that one key spanning several
|
|
48
|
+
* columns. Declaring more than one — two flagged columns, a flagged column alongside a
|
|
49
|
+
* table-level constraint, or two table-level constraints — throws when the table is built.
|
|
50
|
+
*/
|
|
51
|
+
primaryKey;
|
|
52
|
+
/** True when the primary key spans more than one column. */
|
|
53
|
+
isCompositeKey;
|
|
54
|
+
/**
|
|
55
|
+
* The table's tenant claim columns, paired with the claim they are filled from, in
|
|
56
|
+
* declaration order. Empty for a table outside any tenant scope.
|
|
57
|
+
*
|
|
58
|
+
* The claim is the *field* name, not the database column name: it is the key the runtime
|
|
59
|
+
* looks up in `ExecutionContext.identity`, and the name `tenantScope()` declared it under.
|
|
60
|
+
*/
|
|
61
|
+
tenantKeys;
|
|
62
|
+
/**
|
|
63
|
+
* What every row of this table reports about itself, surfaced as `$$meta` on each result
|
|
64
|
+
* record — the built-in `key` / `table` / `schema` plus whatever `table().meta()` declared.
|
|
65
|
+
*
|
|
66
|
+
* Frozen and built once: the same object is shared by reference across every row of a
|
|
67
|
+
* level, because nothing in it varies per row.
|
|
68
|
+
*/
|
|
69
|
+
meta;
|
|
70
|
+
_ref = new TableRef(this);
|
|
71
|
+
constructor(definition, relations, alias) {
|
|
9
72
|
this.schema = definition["_namespace"]?.name;
|
|
10
73
|
this.name = definition.name;
|
|
74
|
+
this.alias = alias ?? definition.name;
|
|
11
75
|
this.columns = this._buildColumns(definition);
|
|
76
|
+
this.primaryKey = this._buildPrimaryKey(definition);
|
|
77
|
+
this.isCompositeKey = this.primaryKey.length > 1;
|
|
78
|
+
this.tenantKeys = this.getColumnEntries().filter(([, column]) => column.tenantKey);
|
|
79
|
+
this.meta = this._buildMeta(definition);
|
|
12
80
|
this.relations = relations;
|
|
13
81
|
}
|
|
14
82
|
_buildColumns(definition) {
|
|
15
83
|
const columns = {};
|
|
16
84
|
for (const [name, def] of Object.entries(definition.columns)) {
|
|
17
|
-
|
|
85
|
+
// Deprecated: still in the database until a later release drops it, but not the client's.
|
|
86
|
+
if (def instanceof ColumnDefinition && def["_deprecated"])
|
|
87
|
+
continue;
|
|
88
|
+
columns[name] =
|
|
89
|
+
def instanceof ColumnGroupDefinition ? new ColumnGroup(this, def) : new Column(this, def);
|
|
18
90
|
}
|
|
19
91
|
return columns;
|
|
20
92
|
}
|
|
93
|
+
_buildPrimaryKey(definition) {
|
|
94
|
+
const flagged = this.getColumnEntries().filter(([, column]) => column.primaryKey);
|
|
95
|
+
const constraints = definition["_constraints"].filter((constraint) => constraint instanceof PrimaryKeyConstraintDefinition);
|
|
96
|
+
const declarations = [
|
|
97
|
+
...flagged.map(([field]) => `column "${field}"`),
|
|
98
|
+
...constraints.map((constraint) => `constraint "${constraint.name}"`),
|
|
99
|
+
];
|
|
100
|
+
if (declarations.length > 1) {
|
|
101
|
+
throw new Error(`Table "${this.name}" declares more than one primary key (${declarations.join(", ")}). ` +
|
|
102
|
+
`A table has at most one primary key; use table.primaryKey((c) => [...]) for a composite key.`);
|
|
103
|
+
}
|
|
104
|
+
const constraint = constraints[0];
|
|
105
|
+
if (!constraint) {
|
|
106
|
+
return flagged.map(([, column]) => column);
|
|
107
|
+
}
|
|
108
|
+
return constraint["_columns"].map((ref) => {
|
|
109
|
+
const column = this.getColumn(ref.name);
|
|
110
|
+
if (!(column instanceof Column)) {
|
|
111
|
+
throw new Error(`Primary key constraint "${constraint.name}" on table "${this.name}" references unknown column "${ref.name}"`);
|
|
112
|
+
}
|
|
113
|
+
return column;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
_buildMeta(definition) {
|
|
117
|
+
const declared = definition["_meta"] ?? {};
|
|
118
|
+
for (const key of Object.keys(declared)) {
|
|
119
|
+
if (BUILT_IN_META_KEYS.includes(key)) {
|
|
120
|
+
throw new Error(`Table "${this.name}" declares meta key "${key}", which is built in. ` +
|
|
121
|
+
`${BUILT_IN_META_KEYS.join(", ")} are set from the schema and cannot be overridden.`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return Object.freeze({
|
|
125
|
+
key: this.alias,
|
|
126
|
+
table: this.name,
|
|
127
|
+
...(this.schema ? { schema: this.schema } : {}),
|
|
128
|
+
...declared,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
21
131
|
hasColumn(name) {
|
|
22
132
|
return Object.hasOwn(this.columns, name);
|
|
23
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* The field `name` — a column or a column group — or the plain column whose database name it
|
|
136
|
+
* is. A group's member is reached through the group: `getColumn("netValue")`, then the
|
|
137
|
+
* group's `getColumn("amount")`.
|
|
138
|
+
*/
|
|
24
139
|
getColumn(name) {
|
|
25
|
-
if (this.
|
|
140
|
+
if (this.hasColumn(name)) {
|
|
26
141
|
return this.columns[name];
|
|
27
142
|
}
|
|
28
|
-
return
|
|
143
|
+
return this.getColumnEntries().find(([, column]) => column.name === name)?.[1];
|
|
29
144
|
}
|
|
145
|
+
/** The table's plain columns, by field. A column group's members are not among them. */
|
|
30
146
|
getColumnEntries() {
|
|
31
|
-
return Object.entries(this.columns);
|
|
147
|
+
return Object.entries(this.columns).filter((entry) => entry[1] instanceof Column);
|
|
148
|
+
}
|
|
149
|
+
/** The table's column groups, by field. */
|
|
150
|
+
getGroupEntries() {
|
|
151
|
+
return Object.entries(this.columns).filter((entry) => entry[1] instanceof ColumnGroup);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Every real column of the table with its field path — `["netValue", "amount"]` for a group's
|
|
155
|
+
* member — groups walked depth first, in declaration order.
|
|
156
|
+
*/
|
|
157
|
+
getLeafEntries() {
|
|
158
|
+
const walk = (fields, path) => Object.entries(fields).flatMap(([field, member]) => member instanceof ColumnGroup
|
|
159
|
+
? walk(member.columns, [...path, field])
|
|
160
|
+
: [[[...path, field], member]]);
|
|
161
|
+
return walk(this.columns, []);
|
|
32
162
|
}
|
|
33
163
|
hasRelation(fieldName) {
|
|
34
164
|
return this.relations ? Object.hasOwn(this.relations, fieldName) : false;
|
|
@@ -39,6 +169,13 @@ export class Table {
|
|
|
39
169
|
}
|
|
40
170
|
return this.relations[fieldName];
|
|
41
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* The node that names this table when qualifying a column: the alias bound in the
|
|
174
|
+
* rendering context, else the table name. Cached — it holds no per-render state.
|
|
175
|
+
*/
|
|
176
|
+
ref() {
|
|
177
|
+
return this._ref;
|
|
178
|
+
}
|
|
42
179
|
toSQL(ctx) {
|
|
43
180
|
if (this.schema) {
|
|
44
181
|
return sql.join([sql.identifier(this.schema), sql.identifier(this.name)], ".").toSQL(ctx);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"table.js","sourceRoot":"","sources":["../../src/runtime/table.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,GAAG,EAAqC,MAAM,iBAAiB,CAAC;AACzE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAmCrC,MAAM,OAAO,KAAK;IAUP,IAAI,CAAQ;IACZ,MAAM,CAAwB;IAC9B,OAAO,CAAqB;IAC5B,SAAS,CAAa;IAE/B,YAAY,UAAwD,EAAE,SAAsB;QAC1F,IAAI,CAAC,MAAM,GAAG,UAAU,CAAC,YAAY,CAAC,EAAE,IAA6B,CAAC;QACtE,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC;QAC5B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;QAC9C,IAAI,CAAC,SAAS,GAAG,SAAuB,CAAC;IAC3C,CAAC;IAEO,aAAa,CACnB,UAAwD;QAExD,MAAM,OAAO,GAAG,EAAwD,CAAC;QAEzE,KAAK,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;YAC7D,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QACxC,CAAC;QAED,OAAO,OAA6B,CAAC;IACvC,CAAC;IAEM,SAAS,CAAC,IAAY;QAC3B,OAAO,MAAM,CAAC,MA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,OAAO,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;IACnC,CAAC;IAED;;;OAGG;IACI,GAAG;QACR,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAEM,KAAK,CAAC,GAAe;QAC1B,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC5F,CAAC;QAED,OAAO,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9C,CAAC;CACF","sourcesContent":["import { TypedObject } from \"../utils/index.js\";\nimport {\n AnyTableRelations,\n ColumnConfig,\n ColumnDefinition,\n AnyNamespaceDefinition,\n TableConfig,\n TableDefinition,\n TableColumnDefinitions,\n NamespaceDefinition,\n ColumnGroupDefinition,\n PrimaryKeyConstraintDefinition,\n} from \"../definition/index.js\";\nimport { sql, SQLContext, SQLNode, SQLStatement } from \"../sql/index.js\";\nimport { AnyColumn, Column } from \"./column.js\";\nimport { AnyColumnGroup, AnyField, ColumnGroup } from \"./group.js\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyTable = Table<any, any, any, any>;\n\n/**\n * What a row reports about the table it came from, written to every result record as\n * `$$meta` (`packages/core/src/runtime/operation.ts`).\n *\n * `key` is the schema alias — the name the client addresses the table by, and the intended\n * discriminant when a row could have come from one of several tables. `table` is the\n * database name, which may differ.\n */\nexport type RecordMeta = {\n readonly key: string;\n readonly table: string;\n readonly schema?: string;\n readonly [field: string]: unknown;\n};\n\n/** Meta keys the runtime owns; `table().meta()` may not redeclare them. */\nconst BUILT_IN_META_KEYS: readonly string[] = Object.freeze([\"key\", \"table\", \"schema\"]);\n\n/**\n * How a table is named when it *qualifies a column* — the alias bound for it in the\n * rendering context, or its bare name when none is.\n *\n * Counterpart to `Table.toSQL`, which renders the table as a *source*\n * (`\"billing\".\"invoices\"`, for `FROM` / `UPDATE` / `INSERT INTO`). A source says *which\n * table*, so it carries the schema; a reference says *which correlation in this query*,\n * where the schema is not part of the name.\n *\n * Deliberately **not** schema-qualified when unaliased, for two reasons Postgres enforces:\n * once a source is aliased, `\"billing\".\"invoices\".\"id\"` is rejected outright with\n * `invalid reference to FROM-clause entry`; and schema-qualifying would not rescue an\n * unaliased shadowed subquery anyway, because that needs *both* sides qualified and a table\n * declared without a namespace has no schema to qualify with. Aliasing is the fix; this\n * fallback only ever runs where exactly one table is in scope (DML, `$query`).\n */\nclass TableRef implements SQLNode {\n private readonly _table: AnyTable;\n\n constructor(table: AnyTable) {\n this._table = table;\n }\n\n toSQL(ctx: SQLContext): SQLStatement {\n return sql.identifier(ctx.aliases?.get(this._table) ?? this._table.name).toSQL(ctx);\n }\n}\n\nexport type WithRelations<\n TColumns extends TableColumnDefinitions,\n TNamespace extends AnyNamespaceDefinition,\n TRelations extends AnyTableRelations | undefined,\n> = TableConfig<TColumns, TNamespace> & {\n relations: TRelations extends AnyTableRelations ? TRelations : never;\n};\n\nexport type TableSchemaName<T extends AnyTable> = T[\"__type\"] extends { schema: infer S }\n ? S extends NamespaceDefinition<infer SN>\n ? SN\n : never\n : undefined;\n\n/** A table's fields, without its deprecated columns: the client never sees those. */\nexport type TableColumnName<T extends AnyTable> = T[\"__type\"] extends { columns: infer C }\n ? C extends TableColumnDefinitions\n ? {\n [K in keyof C]: C[K] extends { __type: { deprecated: true } } ? never : K;\n }[keyof C]\n : never\n : never;\n\nexport type TableColumns<T extends AnyTable> = {\n readonly [K in TableColumnName<T>]: T[\"__type\"] extends { columns: infer C }\n ? C extends Record<K, infer CD>\n ? CD extends { readonly kind: \"COLUMN_GROUP\"; readonly columns: infer GC }\n ? GC extends TableColumnDefinitions\n ? ColumnGroup<string, GC, T>\n : never\n : CD extends ColumnDefinition<infer CName, infer CConfig>\n ? Column<CName, CConfig, T>\n : never\n : never\n : never;\n};\n\nexport class Table<\n TName extends string,\n TColumns extends TableColumnDefinitions,\n TNamespace extends AnyNamespaceDefinition,\n TRelations extends AnyTableRelations,\n>\n implements SQLNode, TypedObject<TableConfig<TColumns, TNamespace>>\n{\n declare readonly __type: WithRelations<TColumns, TNamespace, TRelations>;\n\n readonly name: TName;\n\n /**\n * The key this table is exported under in the schema object — the name the client\n * addresses it by (`dsql.members`), which may differ from the database table name\n * (`team_members`). Set by `SchemaRegistry`; defaults to `name` when a `Table` is\n * constructed directly.\n */\n readonly alias: string;\n\n readonly schema: TableSchemaName<this>;\n readonly columns: TableColumns<this>;\n readonly relations: TRelations;\n\n /**\n * The columns of the table's primary key, in key order. Empty when the table declares none.\n *\n * A table has at most one primary key; a composite key is that one key spanning several\n * columns. Declaring more than one — two flagged columns, a flagged column alongside a\n * table-level constraint, or two table-level constraints — throws when the table is built.\n */\n readonly primaryKey: AnyColumn[];\n\n /** True when the primary key spans more than one column. */\n readonly isCompositeKey: boolean;\n\n /**\n * The table's tenant claim columns, paired with the claim they are filled from, in\n * declaration order. Empty for a table outside any tenant scope.\n *\n * The claim is the *field* name, not the database column name: it is the key the runtime\n * looks up in `ExecutionContext.identity`, and the name `tenantScope()` declared it under.\n */\n readonly tenantKeys: [claim: string, column: AnyColumn][];\n\n /**\n * What every row of this table reports about itself, surfaced as `$$meta` on each result\n * record — the built-in `key` / `table` / `schema` plus whatever `table().meta()` declared.\n *\n * Frozen and built once: the same object is shared by reference across every row of a\n * level, because nothing in it varies per row.\n */\n readonly meta: RecordMeta;\n\n private readonly _ref: SQLNode = new TableRef(this);\n\n constructor(\n definition: TableDefinition<TName, TColumns, TNamespace>,\n relations?: TRelations,\n alias?: string\n ) {\n this.schema = definition[\"_namespace\"]?.name as TableSchemaName<this>;\n this.name = definition.name;\n this.alias = alias ?? definition.name;\n this.columns = this._buildColumns(definition);\n this.primaryKey = this._buildPrimaryKey(definition);\n this.isCompositeKey = this.primaryKey.length > 1;\n this.tenantKeys = this.getColumnEntries().filter(([, column]) => column.tenantKey);\n this.meta = this._buildMeta(definition);\n this.relations = relations as TRelations;\n }\n\n private _buildColumns(\n definition: TableDefinition<TName, TColumns, TNamespace>\n ): TableColumns<this> {\n const columns = {} as Record<string, AnyField>;\n\n for (const [name, def] of Object.entries(definition.columns)) {\n // Deprecated: still in the database until a later release drops it, but not the client's.\n if (def instanceof ColumnDefinition && def[\"_deprecated\"]) continue;\n\n columns[name] =\n def instanceof ColumnGroupDefinition ? new ColumnGroup(this, def) : new Column(this, def);\n }\n\n return columns as TableColumns<this>;\n }\n\n private _buildPrimaryKey(\n definition: TableDefinition<TName, TColumns, TNamespace>\n ): AnyColumn[] {\n const flagged = this.getColumnEntries().filter(([, column]) => column.primaryKey);\n const constraints = definition[\"_constraints\"].filter(\n (constraint) => constraint instanceof PrimaryKeyConstraintDefinition\n );\n\n const declarations = [\n ...flagged.map(([field]) => `column \"${field}\"`),\n ...constraints.map((constraint) => `constraint \"${constraint.name}\"`),\n ];\n\n if (declarations.length > 1) {\n throw new Error(\n `Table \"${this.name}\" declares more than one primary key (${declarations.join(\", \")}). ` +\n `A table has at most one primary key; use table.primaryKey((c) => [...]) for a composite key.`\n );\n }\n\n const constraint = constraints[0];\n\n if (!constraint) {\n return flagged.map(([, column]) => column);\n }\n\n return constraint[\"_columns\"].map((ref) => {\n const column = this.getColumn(ref.name);\n\n if (!(column instanceof Column)) {\n throw new Error(\n `Primary key constraint \"${constraint.name}\" on table \"${this.name}\" references unknown column \"${ref.name}\"`\n );\n }\n\n return column;\n });\n }\n\n private _buildMeta(definition: TableDefinition<TName, TColumns, TNamespace>): RecordMeta {\n const declared = definition[\"_meta\"] ?? {};\n\n for (const key of Object.keys(declared)) {\n if (BUILT_IN_META_KEYS.includes(key)) {\n throw new Error(\n `Table \"${this.name}\" declares meta key \"${key}\", which is built in. ` +\n `${BUILT_IN_META_KEYS.join(\", \")} are set from the schema and cannot be overridden.`\n );\n }\n }\n\n return Object.freeze({\n key: this.alias,\n table: this.name,\n ...(this.schema ? { schema: this.schema as string } : {}),\n ...declared,\n });\n }\n\n public hasColumn(name: string): boolean {\n return Object.hasOwn(this.columns, name);\n }\n\n /**\n * The field `name` — a column or a column group — or the plain column whose database name it\n * is. A group's member is reached through the group: `getColumn(\"netValue\")`, then the\n * group's `getColumn(\"amount\")`.\n */\n public getColumn(name: string): AnyField | undefined {\n if (this.hasColumn(name)) {\n return this.columns[name as TableColumnName<this>] as AnyField;\n }\n\n return this.getColumnEntries().find(([, column]) => column.name === name)?.[1];\n }\n\n /** The table's plain columns, by field. A column group's members are not among them. */\n public getColumnEntries(): [string, Column<string, ColumnConfig, this>][] {\n return Object.entries<AnyField>(this.columns).filter(\n (entry): entry is [string, Column<string, ColumnConfig, this>] => entry[1] instanceof Column\n );\n }\n\n /** The table's column groups, by field. */\n public getGroupEntries(): [string, AnyColumnGroup][] {\n return Object.entries<AnyField>(this.columns).filter(\n (entry): entry is [string, AnyColumnGroup] => entry[1] instanceof ColumnGroup\n );\n }\n\n /**\n * Every real column of the table with its field path — `[\"netValue\", \"amount\"]` for a group's\n * member — groups walked depth first, in declaration order.\n */\n public getLeafEntries(): [path: string[], column: AnyColumn][] {\n const walk = (fields: Readonly<Record<string, AnyField>>, path: string[]) =>\n Object.entries(fields).flatMap(([field, member]): [string[], AnyColumn][] =>\n member instanceof ColumnGroup\n ? walk(member.columns, [...path, field])\n : [[[...path, field], member]]\n );\n\n return walk(this.columns as Record<string, AnyField>, []);\n }\n\n public hasRelation(fieldName: string): boolean {\n return this.relations ? Object.hasOwn(this.relations, fieldName) : false;\n }\n\n public getRelation(fieldName: string) {\n if (!this.relations || !this.relations[fieldName]) {\n return undefined;\n }\n\n return this.relations[fieldName];\n }\n\n /**\n * The node that names this table when qualifying a column: the alias bound in the\n * rendering context, else the table name. Cached — it holds no per-render state.\n */\n public ref(): SQLNode {\n return this._ref;\n }\n\n public toSQL(ctx: SQLContext): SQLStatement {\n if (this.schema) {\n return sql.join([sql.identifier(this.schema), sql.identifier(this.name)], \".\").toSQL(ctx);\n }\n\n return sql.identifier(this.name).toSQL(ctx);\n }\n}\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AnyUnionDefinition } from "../definition/index.js";
|
|
2
|
+
import { AnyColumn } from "./column.js";
|
|
3
|
+
import { AnyTable } from "./table.js";
|
|
4
|
+
/**
|
|
5
|
+
* The runtime form of a `union()`: its members as built tables, keyed by schema alias, and
|
|
6
|
+
* each shared field resolved to the real column on every member.
|
|
7
|
+
*
|
|
8
|
+
* Built by the registry once every table exists, since a member is only a `Table` from then.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Union {
|
|
11
|
+
/** The schema key the union was exported under. */
|
|
12
|
+
readonly alias: string;
|
|
13
|
+
readonly definition: AnyUnionDefinition;
|
|
14
|
+
readonly members: Readonly<Record<string, AnyTable>>;
|
|
15
|
+
/** Shared field → member alias → that member's column for the field. */
|
|
16
|
+
readonly sharedColumns: Readonly<Record<string, Readonly<Record<string, AnyColumn>>>>;
|
|
17
|
+
constructor(definition: AnyUnionDefinition, members: Record<string, AnyTable>, alias: string);
|
|
18
|
+
/** The member aliases, in declaration order. */
|
|
19
|
+
get memberAliases(): string[];
|
|
20
|
+
hasMember(alias: string): boolean;
|
|
21
|
+
getMember(alias: string): AnyTable;
|
|
22
|
+
/**
|
|
23
|
+
* How many primary-key columns can break ties across the union: all of them when every
|
|
24
|
+
* member's key has the same arity and the same types position by position, none otherwise —
|
|
25
|
+
* a `UNION ALL` column must have one type across branches.
|
|
26
|
+
*/
|
|
27
|
+
get tiebreakers(): number;
|
|
28
|
+
isShared(field: string): boolean;
|
|
29
|
+
/** The column a shared field resolves to on one member. */
|
|
30
|
+
getMemberColumn(alias: string, field: string): AnyColumn;
|
|
31
|
+
}
|
|
32
|
+
export type AnyUnion = Union;
|
|
33
|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
+
* The runtime form of a `union()`: its members as built tables, keyed by schema alias, and
|
|
3
|
+
* each shared field resolved to the real column on every member.
|
|
4
|
+
*
|
|
5
|
+
* Built by the registry once every table exists, since a member is only a `Table` from then.
|
|
6
|
+
*/
|
|
7
|
+
export class Union {
|
|
8
|
+
/** The schema key the union was exported under. */
|
|
9
|
+
alias;
|
|
10
|
+
definition;
|
|
11
|
+
members;
|
|
12
|
+
/** Shared field → member alias → that member's column for the field. */
|
|
13
|
+
sharedColumns;
|
|
14
|
+
constructor(definition, members, alias) {
|
|
15
|
+
this.alias = alias;
|
|
16
|
+
this.definition = definition;
|
|
17
|
+
this.members = Object.freeze({ ...members });
|
|
18
|
+
const shared = {};
|
|
19
|
+
for (const field of Object.keys(definition.columns)) {
|
|
20
|
+
shared[field] = Object.freeze(Object.fromEntries(Object.entries(this.members).map(([key, member]) => [
|
|
21
|
+
key,
|
|
22
|
+
member.getColumn(field),
|
|
23
|
+
])));
|
|
24
|
+
}
|
|
25
|
+
this.sharedColumns = Object.freeze(shared);
|
|
26
|
+
}
|
|
27
|
+
/** The member aliases, in declaration order. */
|
|
28
|
+
get memberAliases() {
|
|
29
|
+
return Object.keys(this.members);
|
|
30
|
+
}
|
|
31
|
+
hasMember(alias) {
|
|
32
|
+
return Object.hasOwn(this.members, alias);
|
|
33
|
+
}
|
|
34
|
+
getMember(alias) {
|
|
35
|
+
const member = this.members[alias];
|
|
36
|
+
if (!member) {
|
|
37
|
+
throw new Error(`Union "${this.alias}" has no member "${alias}".`);
|
|
38
|
+
}
|
|
39
|
+
return member;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* How many primary-key columns can break ties across the union: all of them when every
|
|
43
|
+
* member's key has the same arity and the same types position by position, none otherwise —
|
|
44
|
+
* a `UNION ALL` column must have one type across branches.
|
|
45
|
+
*/
|
|
46
|
+
get tiebreakers() {
|
|
47
|
+
const keys = Object.values(this.members).map((member) => member.primaryKey);
|
|
48
|
+
const [first] = keys;
|
|
49
|
+
if (!first || first.length === 0) {
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
const aligned = keys.every((key) => key.length === first.length &&
|
|
53
|
+
key.every((column, index) => column.dataType === first[index].dataType));
|
|
54
|
+
return aligned ? first.length : 0;
|
|
55
|
+
}
|
|
56
|
+
isShared(field) {
|
|
57
|
+
return Object.hasOwn(this.sharedColumns, field);
|
|
58
|
+
}
|
|
59
|
+
/** The column a shared field resolves to on one member. */
|
|
60
|
+
getMemberColumn(alias, field) {
|
|
61
|
+
const column = this.sharedColumns[field]?.[alias];
|
|
62
|
+
if (!column) {
|
|
63
|
+
throw new Error(`Field "${field}" is not shared by every member of union "${this.alias}", so it cannot ` +
|
|
64
|
+
`be used across the union.`);
|
|
65
|
+
}
|
|
66
|
+
return column;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=union.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"union.js","sourceRoot":"","sources":["../../src/runtime/union.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,MAAM,OAAO,KAAK;IAChB,mDAAmD;IAC1C,KAAK,CAAS;IAEd,UAAU,CAAqB;IAE/B,OAAO,CAAqC;IAErD,wEAAwE;IAC/D,aAAa,CAAgE;IAEtF,YAAY,UAA8B,EAAE,OAAiC,EAAE,KAAa;QAC1F,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;QAE7C,MAAM,MAAM,GAA8C,EAAE,CAAC;QAE7D,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;YACpD,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,MAAM,CAC3B,MAAM,CAAC,WAAW,CAChB,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CAAC,MAAM;YAC3B,GAAG,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,KAAK,KAAK,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,CAC1E,CAAC;QAEF,OAAO,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IACpC,CAAC;IAEM,QAAQ,CAAC,KAAa;QAC3B,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC;IAED,2DAA2D;IACpD,eAAe,CAAC,KAAa,EAAE,KAAa;QACjD,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;QAElD,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CACb,UAAU,KAAK,6CAA6C,IAAI,CAAC,KAAK,kBAAkB;gBACtF,2BAA2B,CAC9B,CAAC;QACJ,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["import { AnyUnionDefinition } from \"../definition/index.js\";\nimport { AnyColumn } from \"./column.js\";\nimport { AnyTable } from \"./table.js\";\n\n/**\n * The runtime form of a `union()`: its members as built tables, keyed by schema alias, and\n * each shared field resolved to the real column on every member.\n *\n * Built by the registry once every table exists, since a member is only a `Table` from then.\n */\nexport class Union {\n /** The schema key the union was exported under. */\n readonly alias: string;\n\n readonly definition: AnyUnionDefinition;\n\n readonly members: Readonly<Record<string, AnyTable>>;\n\n /** Shared field → member alias → that member's column for the field. */\n readonly sharedColumns: Readonly<Record<string, Readonly<Record<string, AnyColumn>>>>;\n\n constructor(definition: AnyUnionDefinition, members: Record<string, AnyTable>, alias: string) {\n this.alias = alias;\n this.definition = definition;\n this.members = Object.freeze({ ...members });\n\n const shared: Record<string, Record<string, AnyColumn>> = {};\n\n for (const field of Object.keys(definition.columns)) {\n shared[field] = Object.freeze(\n Object.fromEntries(\n Object.entries(this.members).map(([key, member]) => [\n key,\n member.getColumn(field) as AnyColumn,\n ])\n )\n );\n }\n\n this.sharedColumns = Object.freeze(shared);\n }\n\n /** The member aliases, in declaration order. */\n public get memberAliases(): string[] {\n return Object.keys(this.members);\n }\n\n public hasMember(alias: string): boolean {\n return Object.hasOwn(this.members, alias);\n }\n\n public getMember(alias: string): AnyTable {\n const member = this.members[alias];\n\n if (!member) {\n throw new Error(`Union \"${this.alias}\" has no member \"${alias}\".`);\n }\n\n return member;\n }\n\n /**\n * How many primary-key columns can break ties across the union: all of them when every\n * member's key has the same arity and the same types position by position, none otherwise —\n * a `UNION ALL` column must have one type across branches.\n */\n public get tiebreakers(): number {\n const keys = Object.values(this.members).map((member) => member.primaryKey);\n const [first] = keys;\n\n if (!first || first.length === 0) {\n return 0;\n }\n\n const aligned = keys.every(\n (key) =>\n key.length === first.length &&\n key.every((column, index) => column.dataType === first[index].dataType)\n );\n\n return aligned ? first.length : 0;\n }\n\n public isShared(field: string): boolean {\n return Object.hasOwn(this.sharedColumns, field);\n }\n\n /** The column a shared field resolves to on one member. */\n public getMemberColumn(alias: string, field: string): AnyColumn {\n const column = this.sharedColumns[field]?.[alias];\n\n if (!column) {\n throw new Error(\n `Field \"${field}\" is not shared by every member of union \"${this.alias}\", so it cannot ` +\n `be used across the union.`\n );\n }\n\n return column;\n }\n}\n\nexport type AnyUnion = Union;\n"]}
|
package/dist/sql/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { type SQLNode, type SQLStatement, type SQLContext, SQLRaw, SQLParam, SQLIdentifier, SQLQuery, SQLWrapper, isSQLNode, type SQLValue, type ValueSerializer, } from "./nodes.js";
|
|
2
|
-
export { sql } from "./tag.js";
|
|
2
|
+
export { sql, type KeysetKey, type KeysetBound } from "./tag.js";
|
|
3
3
|
export { counter, escapeValue, escapeIdentifier, type ParamIndexCounter } from "./utils.js";
|
|
4
4
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/sql/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sql/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EACZ,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,MAAM,EACN,QAAQ,EACR,aAAa,EACb,QAAQ,EACR,UAAU,EACV,SAAS,EACT,KAAK,QAAQ,EACb,KAAK,eAAe,GACrB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sql/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EACZ,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,MAAM,EACN,QAAQ,EACR,aAAa,EACb,QAAQ,EACR,UAAU,EACV,SAAS,EACT,KAAK,QAAQ,EACb,KAAK,eAAe,GACrB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,GAAG,EAAE,KAAK,SAAS,EAAE,KAAK,WAAW,EAAE,MAAM,UAAU,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,KAAK,iBAAiB,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/sql/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/sql/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,MAAM,EACN,QAAQ,EACR,aAAa,EACb,QAAQ,EACR,UAAU,EACV,SAAS,GAGV,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,GAAG,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/sql/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,MAAM,EACN,QAAQ,EACR,aAAa,EACb,QAAQ,EACR,UAAU,EACV,SAAS,GAGV,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,GAAG,EAAoC,MAAM,UAAU,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAA0B,MAAM,YAAY,CAAC","sourcesContent":["export {\n type SQLNode,\n type SQLStatement,\n type SQLContext,\n SQLRaw,\n SQLParam,\n SQLIdentifier,\n SQLQuery,\n SQLWrapper,\n isSQLNode,\n type SQLValue,\n type ValueSerializer,\n} from \"./nodes.js\";\nexport { sql, type KeysetKey, type KeysetBound } from \"./tag.js\";\nexport { counter, escapeValue, escapeIdentifier, type ParamIndexCounter } from \"./utils.js\";\n"]}
|
package/dist/sql/nodes.d.ts
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
import { type ParamIndexCounter } from "./utils.js";
|
|
2
|
-
export
|
|
2
|
+
export type SQLStatement = {
|
|
3
3
|
text: string;
|
|
4
4
|
params: unknown[];
|
|
5
|
-
}
|
|
6
|
-
export
|
|
5
|
+
};
|
|
6
|
+
export type SQLContext = {
|
|
7
7
|
inlineParams: boolean;
|
|
8
8
|
paramCounter: ParamIndexCounter;
|
|
9
9
|
escapeValue(str: string): string;
|
|
10
10
|
escapeIdentifier(str: string): string;
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Alias bindings in effect for the subtree being rendered, set by {@link SQLScope}.
|
|
13
|
+
*
|
|
14
|
+
* Keys are opaque to this module — a node that wants to be referenced by an alias looks
|
|
15
|
+
* itself up here and falls back to its own name when absent. Nothing in `sql/` knows what
|
|
16
|
+
* the keys are, which keeps this layer independent of the runtime.
|
|
17
|
+
*/
|
|
18
|
+
aliases?: ReadonlyMap<unknown, string>;
|
|
19
|
+
};
|
|
12
20
|
export type SQLValue = string | number | boolean | bigint | null | object;
|
|
13
21
|
export type ValueSerializer<T> = (value: T) => unknown;
|
|
14
|
-
export
|
|
22
|
+
export type SQLNode = {
|
|
15
23
|
toSQL(ctx: SQLContext): SQLStatement;
|
|
16
|
-
}
|
|
24
|
+
};
|
|
17
25
|
export declare const isSQLNode: (value: unknown) => value is SQLNode;
|
|
18
26
|
export declare class SQLRaw implements SQLNode {
|
|
19
27
|
private readonly _text;
|
|
@@ -32,6 +40,25 @@ export declare class SQLWrapper implements SQLNode {
|
|
|
32
40
|
constructor(node: SQLNode);
|
|
33
41
|
toSQL(ctx: SQLContext): SQLStatement;
|
|
34
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Renders a subtree with extra alias bindings in scope.
|
|
45
|
+
*
|
|
46
|
+
* Query-builder machinery, deliberately absent from the `sql` tag and the `./sql` barrel:
|
|
47
|
+
* the bindings are keyed by the very node objects the builder threads through a select
|
|
48
|
+
* tree, which is not knowledge a caller can be expected to have. A user-facing way to
|
|
49
|
+
* alias a table in hand-written SQL is a separate thing to design.
|
|
50
|
+
*
|
|
51
|
+
* Scopes nest by rendering with a derived context rather than by mutating a stack, so the
|
|
52
|
+
* innermost binding for a key wins. That is what lets one correlated predicate reference the
|
|
53
|
+
* same table under two different aliases — wrap the outer side in its own scope and it keeps
|
|
54
|
+
* the outer alias even though it is rendered inside the inner query.
|
|
55
|
+
*/
|
|
56
|
+
export declare class SQLScope implements SQLNode {
|
|
57
|
+
private readonly _bindings;
|
|
58
|
+
private readonly _node;
|
|
59
|
+
constructor(bindings: ReadonlyMap<unknown, string>, node: SQLNode);
|
|
60
|
+
toSQL(ctx: SQLContext): SQLStatement;
|
|
61
|
+
}
|
|
35
62
|
export declare class SQLIdentifier implements SQLNode {
|
|
36
63
|
readonly name: string;
|
|
37
64
|
constructor(name: string);
|
package/dist/sql/nodes.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nodes.d.ts","sourceRoot":"","sources":["../../src/sql/nodes.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0C,KAAK,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE5F,MAAM,
|
|
1
|
+
{"version":3,"file":"nodes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
|
+
*
|
|
69
|
+
* Query-builder machinery, deliberately absent from the `sql` tag and the `./sql` barrel:
|
|
70
|
+
* the bindings are keyed by the very node objects the builder threads through a select
|
|
71
|
+
* tree, which is not knowledge a caller can be expected to have. A user-facing way to
|
|
72
|
+
* alias a table in hand-written SQL is a separate thing to design.
|
|
73
|
+
*
|
|
74
|
+
* Scopes nest by rendering with a derived context rather than by mutating a stack, so the
|
|
75
|
+
* innermost binding for a key wins. That is what lets one correlated predicate reference the
|
|
76
|
+
* same table under two different aliases — wrap the outer side in its own scope and it keeps
|
|
77
|
+
* the outer alias even though it is rendered inside the inner query.
|
|
78
|
+
*/
|
|
79
|
+
export class SQLScope {
|
|
80
|
+
_bindings;
|
|
81
|
+
_node;
|
|
82
|
+
constructor(bindings, node) {
|
|
83
|
+
this._bindings = bindings;
|
|
84
|
+
this._node = node;
|
|
85
|
+
}
|
|
86
|
+
toSQL(ctx) {
|
|
87
|
+
const aliases = ctx.aliases ? new Map([...ctx.aliases, ...this._bindings]) : this._bindings;
|
|
88
|
+
return this._node.toSQL({ ...ctx, aliases });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
66
91
|
export class SQLIdentifier {
|
|
67
92
|
name;
|
|
68
93
|
constructor(name) {
|