@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.
Files changed (134) hide show
  1. package/CHANGELOG.md +195 -0
  2. package/LICENSE +21 -0
  3. package/README.md +4 -0
  4. package/dist/definition/base.d.ts +50 -3
  5. package/dist/definition/base.d.ts.map +1 -1
  6. package/dist/definition/base.js +34 -1
  7. package/dist/definition/base.js.map +1 -1
  8. package/dist/definition/column.d.ts +87 -14
  9. package/dist/definition/column.d.ts.map +1 -1
  10. package/dist/definition/column.js +99 -5
  11. package/dist/definition/column.js.map +1 -1
  12. package/dist/definition/constraint.d.ts +16 -15
  13. package/dist/definition/constraint.d.ts.map +1 -1
  14. package/dist/definition/constraint.js +4 -1
  15. package/dist/definition/constraint.js.map +1 -1
  16. package/dist/definition/domain.d.ts +12 -8
  17. package/dist/definition/domain.d.ts.map +1 -1
  18. package/dist/definition/domain.js +10 -2
  19. package/dist/definition/domain.js.map +1 -1
  20. package/dist/definition/embedded.d.ts +131 -0
  21. package/dist/definition/embedded.d.ts.map +1 -0
  22. package/dist/definition/embedded.js +133 -0
  23. package/dist/definition/embedded.js.map +1 -0
  24. package/dist/definition/index.d.ts +7 -4
  25. package/dist/definition/index.d.ts.map +1 -1
  26. package/dist/definition/index.js +4 -1
  27. package/dist/definition/index.js.map +1 -1
  28. package/dist/definition/indexes.d.ts +49 -19
  29. package/dist/definition/indexes.d.ts.map +1 -1
  30. package/dist/definition/indexes.js +38 -9
  31. package/dist/definition/indexes.js.map +1 -1
  32. package/dist/definition/namespace.d.ts +2 -2
  33. package/dist/definition/namespace.d.ts.map +1 -1
  34. package/dist/definition/namespace.js.map +1 -1
  35. package/dist/definition/relations.d.ts +47 -15
  36. package/dist/definition/relations.d.ts.map +1 -1
  37. package/dist/definition/relations.js +17 -12
  38. package/dist/definition/relations.js.map +1 -1
  39. package/dist/definition/sequence.d.ts +4 -4
  40. package/dist/definition/sequence.d.ts.map +1 -1
  41. package/dist/definition/sequence.js.map +1 -1
  42. package/dist/definition/table.d.ts +118 -13
  43. package/dist/definition/table.d.ts.map +1 -1
  44. package/dist/definition/table.js +137 -4
  45. package/dist/definition/table.js.map +1 -1
  46. package/dist/definition/tenant.d.ts +50 -0
  47. package/dist/definition/tenant.d.ts.map +1 -0
  48. package/dist/definition/tenant.js +73 -0
  49. package/dist/definition/tenant.js.map +1 -0
  50. package/dist/definition/union.d.ts +84 -0
  51. package/dist/definition/union.d.ts.map +1 -0
  52. package/dist/definition/union.js +89 -0
  53. package/dist/definition/union.js.map +1 -0
  54. package/dist/definition/view.js.map +1 -1
  55. package/dist/index.d.ts +3 -3
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +2 -2
  58. package/dist/index.js.map +1 -1
  59. package/dist/runtime/base.d.ts +22 -4
  60. package/dist/runtime/base.d.ts.map +1 -1
  61. package/dist/runtime/base.js.map +1 -1
  62. package/dist/runtime/column.d.ts +54 -3
  63. package/dist/runtime/column.d.ts.map +1 -1
  64. package/dist/runtime/column.js +75 -7
  65. package/dist/runtime/column.js.map +1 -1
  66. package/dist/runtime/context.d.ts +30 -2
  67. package/dist/runtime/context.d.ts.map +1 -1
  68. package/dist/runtime/context.js +6 -0
  69. package/dist/runtime/context.js.map +1 -1
  70. package/dist/runtime/errors.d.ts +14 -0
  71. package/dist/runtime/errors.d.ts.map +1 -0
  72. package/dist/runtime/errors.js +19 -0
  73. package/dist/runtime/errors.js.map +1 -0
  74. package/dist/runtime/executor.d.ts +29 -1
  75. package/dist/runtime/executor.d.ts.map +1 -1
  76. package/dist/runtime/executor.js +23 -0
  77. package/dist/runtime/executor.js.map +1 -1
  78. package/dist/runtime/group.d.ts +54 -0
  79. package/dist/runtime/group.d.ts.map +1 -0
  80. package/dist/runtime/group.js +96 -0
  81. package/dist/runtime/group.js.map +1 -0
  82. package/dist/runtime/index.d.ts +9 -6
  83. package/dist/runtime/index.d.ts.map +1 -1
  84. package/dist/runtime/index.js +6 -3
  85. package/dist/runtime/index.js.map +1 -1
  86. package/dist/runtime/operation.d.ts +227 -29
  87. package/dist/runtime/operation.d.ts.map +1 -1
  88. package/dist/runtime/operation.js +525 -49
  89. package/dist/runtime/operation.js.map +1 -1
  90. package/dist/runtime/query.d.ts +102 -34
  91. package/dist/runtime/query.d.ts.map +1 -1
  92. package/dist/runtime/query.js +140 -31
  93. package/dist/runtime/query.js.map +1 -1
  94. package/dist/runtime/registry.d.ts +114 -4
  95. package/dist/runtime/registry.d.ts.map +1 -1
  96. package/dist/runtime/registry.js +366 -9
  97. package/dist/runtime/registry.js.map +1 -1
  98. package/dist/runtime/session.d.ts +10 -4
  99. package/dist/runtime/session.d.ts.map +1 -1
  100. package/dist/runtime/session.js.map +1 -1
  101. package/dist/runtime/table.d.ts +87 -8
  102. package/dist/runtime/table.d.ts.map +1 -1
  103. package/dist/runtime/table.js +142 -5
  104. package/dist/runtime/table.js.map +1 -1
  105. package/dist/runtime/union.d.ts +33 -0
  106. package/dist/runtime/union.d.ts.map +1 -0
  107. package/dist/runtime/union.js +69 -0
  108. package/dist/runtime/union.js.map +1 -0
  109. package/dist/sql/index.d.ts +1 -1
  110. package/dist/sql/index.d.ts.map +1 -1
  111. package/dist/sql/index.js.map +1 -1
  112. package/dist/sql/nodes.d.ts +33 -6
  113. package/dist/sql/nodes.d.ts.map +1 -1
  114. package/dist/sql/nodes.js +25 -0
  115. package/dist/sql/nodes.js.map +1 -1
  116. package/dist/sql/tag.d.ts +17 -0
  117. package/dist/sql/tag.d.ts.map +1 -1
  118. package/dist/sql/tag.js +95 -0
  119. package/dist/sql/tag.js.map +1 -1
  120. package/dist/sql/utils.d.ts +2 -2
  121. package/dist/sql/utils.d.ts.map +1 -1
  122. package/dist/sql/utils.js.map +1 -1
  123. package/dist/utils/index.d.ts +1 -1
  124. package/dist/utils/index.d.ts.map +1 -1
  125. package/dist/utils/index.js.map +1 -1
  126. package/dist/utils/thenable.js.map +1 -1
  127. package/dist/utils/types.d.ts +50 -2
  128. package/dist/utils/types.d.ts.map +1 -1
  129. package/dist/utils/types.js.map +1 -1
  130. package/package.json +27 -22
  131. package/dist/sql/expressions.d.ts +0 -25
  132. package/dist/sql/expressions.d.ts.map +0 -1
  133. package/dist/sql/expressions.js +0 -79
  134. package/dist/sql/expressions.js.map +0 -1
@@ -1,13 +1,42 @@
1
- import { RelationsDefinition, TableDefinition, } from "../definition/index.js";
1
+ import { columnEntries, RESERVED_FIELD_NAMES, Relation, RelationsDefinition, TableDefinition, UnionColumnDefinition, UnionDefinition, } from "../definition/index.js";
2
2
  import { Table } from "./table.js";
3
+ import { Union } from "./union.js";
3
4
  export class SchemaRegistry {
4
5
  _tables;
5
6
  _relations;
7
+ _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;
6
25
  constructor(definition) {
7
26
  const schema = this._validateAndTransformSchema(definition);
8
27
  this._tables = this._buildTables(schema);
28
+ this._unions = this._buildUnions(schema, definition);
9
29
  this._relations = this._buildRelations(schema);
30
+ this.claimKeys = this._buildClaimKeys(schema);
10
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
+ */
11
40
  _mergeTableRelations(existing, newRelations) {
12
41
  for (const [name, relation] of Object.entries(newRelations)) {
13
42
  if (existing[name]) {
@@ -20,6 +49,7 @@ export class SchemaRegistry {
20
49
  _validateAndTransformSchema(schema) {
21
50
  const relations = {};
22
51
  const tables = {};
52
+ const unions = {};
23
53
  for (const [name, node] of Object.entries(schema)) {
24
54
  if (node instanceof RelationsDefinition) {
25
55
  const tableName = node.table.name;
@@ -27,7 +57,9 @@ export class SchemaRegistry {
27
57
  relations[tableName] = this._mergeTableRelations(relations[tableName], node.relations);
28
58
  continue;
29
59
  }
30
- relations[tableName] = node.relations;
60
+ relations[tableName] = {
61
+ ...node.relations,
62
+ };
31
63
  continue;
32
64
  }
33
65
  if (node instanceof TableDefinition) {
@@ -35,28 +67,293 @@ export class SchemaRegistry {
35
67
  node;
36
68
  continue;
37
69
  }
70
+ if (node instanceof UnionDefinition) {
71
+ unions[name] = node;
72
+ continue;
73
+ }
38
74
  }
39
- return { tables, relations };
75
+ return { tables, relations, unions };
40
76
  }
41
77
  _buildTables(schema) {
42
78
  const tables = new Map();
43
79
  for (const [key, def] of Object.entries(schema.tables)) {
44
80
  if (def instanceof TableDefinition) {
45
81
  const relations = schema.relations[def.name];
46
- const table = new Table(def, relations);
82
+ const table = new Table(def, relations, key);
47
83
  tables.set(def.name, table);
48
84
  tables.set(key, table);
49
85
  }
50
86
  }
51
87
  return tables;
52
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
+ }
53
329
  _buildRelations(schema) {
54
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
+ }
55
337
  for (const [tableName, relations] of Object.entries(schema.relations)) {
56
338
  const table = this._tables.get(tableName);
57
339
  if (!table) {
58
340
  throw new Error(`Table not found for relations: ${tableName}`);
59
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
+ }
60
357
  map.set(tableName, relations);
61
358
  }
62
359
  return map;
@@ -74,6 +371,25 @@ export class SchemaRegistry {
74
371
  getTables() {
75
372
  return Object.fromEntries(this._tables.entries());
76
373
  }
374
+ /**
375
+ * One entry per table, keyed by schema alias. Unlike `getTables()`, which holds every
376
+ * table under both its alias and its database name, this never yields the same table
377
+ * twice — use it whenever you iterate tables to build something per table.
378
+ */
379
+ getTableEntries() {
380
+ const entries = new Map();
381
+ for (const table of this._tables.values()) {
382
+ entries.set(table.alias, table);
383
+ }
384
+ return [...entries.entries()];
385
+ }
386
+ /**
387
+ * The schema alias for a table, given either its alias or its database name.
388
+ * Throws when no such table exists.
389
+ */
390
+ getAlias(nameOrAlias) {
391
+ return this.getTable(nameOrAlias).alias;
392
+ }
77
393
  hasRelations(tableNameOrAlias) {
78
394
  if (!this.hasTable(tableNameOrAlias)) {
79
395
  return false;
@@ -89,18 +405,59 @@ export class SchemaRegistry {
89
405
  }
90
406
  return relations;
91
407
  }
408
+ hasUnion(alias) {
409
+ return this._unions.has(alias);
410
+ }
411
+ getUnion(alias) {
412
+ const union = this._unions.get(alias);
413
+ if (!union) {
414
+ throw new Error(`Union not found: ${alias}`);
415
+ }
416
+ return union;
417
+ }
418
+ /** One entry per union, keyed by schema alias. */
419
+ getUnions() {
420
+ return [...this._unions.entries()];
421
+ }
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
+ */
92
426
  getRelationTarget(tableNameOrAlias, field) {
93
427
  const sourceTable = this.getTable(tableNameOrAlias);
94
428
  const relations = this._relations.get(sourceTable.name);
95
429
  if (!relations?.[field]) {
96
430
  throw new Error(`Relation not found for field: ${field} on table: ${sourceTable.name}`);
97
431
  }
98
- const targetTableName = relations[field].target.name;
99
- const targetTable = this.getTable(targetTableName);
100
- if (!targetTable) {
101
- throw new Error(`Target table not found for relation: ${field} on table: ${sourceTable.name}`);
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.`);
102
459
  }
103
- return targetTable;
460
+ return pairs;
104
461
  }
105
462
  }
106
463
  //# sourceMappingURL=registry.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/runtime/registry.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,mBAAmB,EACnB,eAAe,GAChB,MAAM,wBAAwB,CAAC;AAUhC,OAAO,EAAY,KAAK,EAAE,MAAM,YAAY,CAAC;AAqC7C,MAAM,OAAO,cAAc;IAKR,OAAO,CAAwB;IAC/B,UAAU,CAAiC;IAE5D,YAAY,UAAuB;QACjC,MAAM,MAAM,GAAG,IAAI,CAAC,2BAA2B,CAAC,UAAU,CAAC,CAAC;QAE5D,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;QACzC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;IACjD,CAAC;IAEO,oBAA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EAAE,CAC9E,CAAC;YACJ,CAAC;YAED,OAAO,KAAK,CAAC;QACf,CAAC;QAED,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IAED;;;OAGG;IACH;;;OAGG;IACI,wBAAwB,CAAC,gBAAwB,EAAE,KAAa;QACrE,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC;QAEpD,OAAO,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;IAChE,CAAC;IAEM,uBAAuB,CAC5B,gBAAwB,EACxB,KAAa;QAEb,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC;QACpD,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;QAEjE,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CACb,aAAa,KAAK,eAAe,WAAW,CAAC,IAAI,4BAA4B,CAC9E,CAAC;QACJ,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["import { TypedObject, Prettify, WithMeta } from \"../utils/index.js\";\nimport {\n AnyColumnDefinition,\n AnyFieldRelation,\n AnyRelationDefinition,\n AnyTableDefinition,\n AnyTableRelations,\n AnyUnionDefinition,\n columnEntries,\n DefinitionSchema,\n RESERVED_FIELD_NAMES,\n Relation,\n RelationsDefinition,\n TableDefinition,\n UnionColumnDefinition,\n UnionDefinition,\n} from \"../definition/index.js\";\nimport {\n AnySchema,\n DefinitionRelationsTableName,\n DefinitionTableName,\n Schema,\n SchemaRelationDefinitions,\n SchemaTableDefinitions,\n SchemaTableRelations,\n SchemaUnionDefinitions,\n} from \"./base.js\";\nimport { AnyColumn } from \"./column.js\";\nimport { AnyTable, Table } from \"./table.js\";\nimport { Union } from \"./union.js\";\n\n/**\n * The metadata a definition declared with `table().meta()`. Rebuilding a `Table` from the\n * definition's three type parameters would otherwise drop it, taking `$$meta`'s declared\n * half with it.\n */\nexport type DeclaredTableMeta<TDef> = TDef extends { __type: { meta: infer M } } ? M : unknown;\n\nexport type RuntimeTables<TSchema extends AnySchema> = {\n [K in keyof TSchema[\"tables\"]]: TSchema[\"tables\"][K] extends TableDefinition<\n infer Name,\n infer Columns,\n infer Schema\n >\n ? WithMeta<\n Table<Name, Columns, Schema, SchemaTableRelations<TSchema, Name>>,\n DeclaredTableMeta<TSchema[\"tables\"][K]>\n >\n : never;\n};\n\nexport type TableByAlias<\n TSchema extends AnySchema,\n TAlias extends string,\n> = TAlias extends keyof TSchema[\"tables\"]\n ? TSchema[\"tables\"][TAlias] extends TableDefinition<infer Name, infer Columns, infer Schema>\n ? WithMeta<\n Table<Name, Columns, Schema, SchemaTableRelations<TSchema, Name>>,\n DeclaredTableMeta<TSchema[\"tables\"][TAlias]>\n >\n : never\n : never;\n\nexport type TableByName<TSchema extends AnySchema, TName extends string> =\n TSchema[\"tables\"] extends Record<string, infer Def>\n ? Def extends TableDefinition<TName, infer Columns, infer Schema>\n ? Table<TName, Columns, Schema, SchemaTableRelations<TSchema, TName>>\n : never\n : never;\n\nexport type TableByNameOrAlias<\n TSchema extends AnySchema,\n TName extends string,\n> = keyof TSchema[\"tables\"] extends never\n ? AnyTable\n : TableByAlias<TSchema, TName> extends never\n ? TableByName<TSchema, TName>\n : TableByAlias<TSchema, TName>;\n\nexport class SchemaRegistry<\n TDefinition extends DefinitionSchema = DefinitionSchema,\n> implements TypedObject<Schema<TDefinition>> {\n declare readonly __type: Schema<TDefinition>;\n\n private readonly _tables: Map<string, AnyTable>;\n private readonly _relations: Map<string, AnyTableRelations>;\n private readonly _unions: Map<string, Union>;\n\n /**\n * For every relation whose target is a union: source table name → field → member alias →\n * the member's `to` columns. Resolved once here, whether `to` was written as shared fields or\n * as one list per member, so nothing downstream has to know which.\n */\n private readonly _unionPairs = new Map<string, Map<string, Record<string, AnyColumn[]>>>();\n\n /** For every belongs-to a union: source table name → field → its discriminator column. */\n private readonly _discriminators = new Map<string, Map<string, AnyColumn>>();\n\n /**\n * Every tenant claim declared anywhere in this schema, mapped to the data type it is\n * declared with. Empty when no table is inside a tenant scope.\n *\n * This is what `$identityClaims` picks an identity object apart by: a claim is identified by\n * its field name across the whole schema, so the same name must mean the same type\n * everywhere — two tables disagreeing is caught here rather than at the first query.\n */\n readonly claimKeys: Map<string, string>;\n\n constructor(definition: TDefinition) {\n const schema = this._validateAndTransformSchema(definition);\n\n this._tables = this._buildTables(schema);\n this._unions = this._buildUnions(schema, definition);\n this._relations = this._buildRelations(schema);\n this.claimKeys = this._buildClaimKeys(schema);\n }\n\n /**\n * Merges a second `relations()` declaration for the same table into the first.\n *\n * Merges into a copy, never into the definition: `relations()` objects belong to the schema\n * module, so mutating one would mean a second registry over the same schema re-merges what\n * the first already merged and reports every name as a duplicate. Two clients over one\n * schema is an ordinary thing to want — an enforcing one and an unscoped one, for instance.\n */\n private _mergeTableRelations(\n existing: AnyRelationDefinition[\"__type\"][\"relations\"],\n newRelations: AnyRelationDefinition[\"__type\"][\"relations\"]\n ) {\n for (const [name, relation] of Object.entries(newRelations)) {\n if (existing[name]) {\n throw new Error(`Duplicate relation name: ${name}`);\n }\n\n existing[name] = relation;\n }\n\n return existing;\n }\n\n private _validateAndTransformSchema(schema: TDefinition): Schema<TDefinition> {\n const relations = {} as SchemaRelationDefinitions<TDefinition>;\n const tables = {} as SchemaTableDefinitions<TDefinition>;\n const unions = {} as SchemaUnionDefinitions<TDefinition>;\n\n for (const [name, node] of Object.entries(schema)) {\n if (node instanceof RelationsDefinition) {\n const tableName = node.table.name as DefinitionRelationsTableName<TDefinition>;\n\n if (relations[tableName]) {\n relations[tableName] = this._mergeTableRelations(\n relations[tableName],\n node.relations\n ) as SchemaRelationDefinitions<TDefinition>[DefinitionRelationsTableName<TDefinition>];\n\n continue;\n }\n\n relations[tableName] = {\n ...node.relations,\n } as SchemaRelationDefinitions<TDefinition>[DefinitionRelationsTableName<TDefinition>];\n\n continue;\n }\n\n if (node instanceof TableDefinition) {\n tables[name as DefinitionTableName<TDefinition>] =\n node as SchemaTableDefinitions<TDefinition>[DefinitionTableName<TDefinition>];\n continue;\n }\n\n if (node instanceof UnionDefinition) {\n (unions as Record<string, AnyUnionDefinition>)[name] = node;\n continue;\n }\n }\n\n return { tables, relations, unions };\n }\n\n private _buildTables(schema: Schema<TDefinition>) {\n const tables = new Map<string, AnyTable>();\n\n for (const [key, def] of Object.entries(schema.tables)) {\n if (def instanceof TableDefinition) {\n const relations = schema.relations[def.name as DefinitionRelationsTableName<TDefinition>];\n const table = new Table(def, relations, key);\n\n tables.set(def.name, table);\n tables.set(key, table);\n }\n }\n\n return tables;\n }\n\n /**\n * Builds every union once the tables exist, checking what only the schema object can tell:\n * that each member is keyed by the alias it is exported under. That is what makes a member\n * key mean the same thing as `$$meta.key` and a node key, rather than a second name for the\n * same table.\n */\n private _buildUnions(schema: Schema<TDefinition>, definition: TDefinition) {\n const unions = new Map<string, Union>();\n\n for (const [alias, union] of Object.entries<AnyUnionDefinition>(schema.unions)) {\n if (this._tables.has(alias)) {\n throw new Error(\n `Union \"${alias}\" has the same name as a table in the schema. A union and a table ` +\n `are both read through the client by that name, so it can only mean one of them.`\n );\n }\n\n const members: Record<string, AnyTable> = {};\n\n for (const [key, member] of Object.entries<AnyTableDefinition>(union.members)) {\n if (definition[key] !== member) {\n throw new Error(\n `Union \"${alias}\" lists table \"${member.name}\" under \"${key}\", but the schema does ` +\n `not export that table as \"${key}\". Union members are keyed by their schema alias.`\n );\n }\n\n members[key] = this.getTable(key);\n }\n\n unions.set(alias, new Union(union, members, alias));\n }\n\n return unions;\n }\n\n private _buildClaimKeys(schema: Schema<TDefinition>): Map<string, string> {\n const claims = new Map<string, string>();\n const sources = new Map<string, string>();\n\n for (const [key, def] of Object.entries<AnyTableDefinition>(schema.tables)) {\n const table = this._tables.get(key);\n\n if (!table) {\n continue;\n }\n\n for (const [claim] of table.tenantKeys) {\n const dataType = def.columns[claim][\"_dataType\"] as string;\n const declared = claims.get(claim);\n\n if (declared !== undefined && declared !== dataType) {\n throw new Error(\n `Claim \"${claim}\" is \"${declared}\" on table \"${sources.get(claim)}\" but \"${dataType}\" ` +\n `on table \"${def.name}\". One claim name means one claim, so it must have the same ` +\n `type on every table that declares it.`\n );\n }\n\n claims.set(claim, dataType);\n sources.set(claim, def.name);\n }\n }\n\n return claims;\n }\n\n /**\n * The field alias a column is declared under on `table`, or `undefined` when the column\n * does not belong to it. Identity, not name: a column from a different table that happens\n * to share a name is not a match, which is the copy-paste mistake this catches.\n */\n /**\n * The field a column is declared under on a table, if it is one of its plain columns. A\n * relation key or a discriminator naming a column group's member throws: relations correlate\n * plain columns only, as primary keys and tenant claims do.\n */\n private _findColumnAlias(\n table: AnyTableDefinition,\n column: AnyColumnDefinition\n ): string | undefined {\n const alias = Object.entries(table.columns).find(([, candidate]) => candidate === column)?.[0];\n\n if (alias === undefined) {\n const member = columnEntries(table.columns).find(([, candidate]) => candidate === column);\n\n if (member) {\n throw new Error(\n `Column \"${column.name}\" is a member of the column group \"${member[0][0]}\" on table ` +\n `\"${table.name}\". A relation key or a discriminator must be a plain column.`\n );\n }\n }\n\n return alias;\n }\n\n /**\n * Checks that a relation's column pairs line up: equal, non-zero length; each column\n * declared on the side it is listed under; both columns of a pair of the same type.\n *\n * The query builder correlates over every pair (`packages/core/src/runtime/query.ts`), so\n * a malformed relation would otherwise surface as a confusing SQL error at query time, or\n * not at all.\n */\n private _validateRelation(\n sourceName: string,\n field: string,\n relation: AnyFieldRelation,\n definitions: Map<string, AnyTableDefinition>\n ): void {\n const label = `Relation \"${field}\" on table \"${sourceName}\"`;\n const { from } = relation;\n const source = definitions.get(sourceName);\n\n if (!source) {\n throw new Error(`${label} refers to a table that is not in the schema.`);\n }\n\n if (from.length === 0) {\n throw new Error(`${label} must declare at least one column pair in \"from\" and \"to\".`);\n }\n\n if (relation.target instanceof UnionDefinition) {\n this._validateUnionRelation(label, sourceName, field, relation, source);\n return;\n }\n\n const to = relation.to as AnyColumnDefinition[];\n\n if (to.length === 0) {\n throw new Error(`${label} must declare at least one column pair in \"from\" and \"to\".`);\n }\n\n if (from.length !== to.length) {\n throw new Error(\n `${label} pairs ${from.length} \"from\" column(s) with ${to.length} \"to\" column(s); ` +\n `the two sides must have the same length.`\n );\n }\n\n if (relation.discriminator) {\n throw new Error(\n `${label} declares a discriminator, but its target is a single table. Only a ` +\n `belongs-to a union needs one.`\n );\n }\n\n const target = definitions.get(relation.target.name);\n\n if (!target) {\n throw new Error(\n `${label} targets table \"${relation.target.name}\", which is not in the schema.`\n );\n }\n\n for (const [index, fromColumn] of from.entries()) {\n const toColumn = to[index];\n\n const fromAlias = this._findColumnAlias(source, fromColumn);\n const toAlias = this._findColumnAlias(target, toColumn);\n\n if (!fromAlias) {\n throw new Error(\n `${label}: \"from\" column \"${fromColumn.name}\" is not declared on table \"${sourceName}\".`\n );\n }\n\n if (!toAlias) {\n throw new Error(\n `${label}: \"to\" column \"${toColumn.name}\" is not declared on target table \"${relation.target.name}\".`\n );\n }\n\n const fromType = fromColumn[\"_dataType\"];\n const toType = toColumn[\"_dataType\"];\n\n if (fromType !== toType) {\n throw new Error(\n `${label}: \"${sourceName}\".\"${fromAlias}\" is \"${fromType}\" but ` +\n `\"${relation.target.name}\".\"${toAlias}\" is \"${toType}\"; ` +\n `both columns of a pair must have the same type.`\n );\n }\n }\n }\n\n /**\n * The union half of {@link _validateRelation}: the union must be registered, `to` must name\n * a column on every member — through a shared field or a per-member list — paired with\n * `from` by type, and only a belongs-to carries a discriminator, which it must.\n */\n private _validateUnionRelation(\n label: string,\n sourceName: string,\n field: string,\n relation: AnyFieldRelation,\n source: AnyTableDefinition\n ): void {\n const target = relation.target as AnyUnionDefinition;\n const union = [...this._unions.values()].find((candidate) => candidate.definition === target);\n\n if (!union) {\n throw new Error(`${label} targets ${target.name}, which is not in the schema.`);\n }\n\n const { from } = relation;\n const lists = this._getMemberToColumns(label, union, relation.to);\n const pairs: Record<string, AnyColumn[]> = {};\n\n for (const [alias, to] of Object.entries(lists)) {\n const member = union.getMember(alias);\n const memberDefinition = union.definition.members[alias] as AnyTableDefinition;\n\n if (to.length !== from.length) {\n throw new Error(\n `${label} pairs ${from.length} \"from\" column(s) with ${to.length} \"to\" column(s) on ` +\n `member \"${alias}\"; the two sides must have the same length.`\n );\n }\n\n pairs[alias] = to.map((toColumn, index) => {\n const fromColumn = from[index];\n const fromAlias = this._findColumnAlias(source, fromColumn);\n const toAlias = this._findColumnAlias(memberDefinition, toColumn);\n\n if (!fromAlias) {\n throw new Error(\n `${label}: \"from\" column \"${fromColumn.name}\" is not declared on table \"${sourceName}\".`\n );\n }\n\n if (!toAlias) {\n throw new Error(\n `${label}: \"to\" column \"${toColumn.name}\" is not declared on member \"${alias}\" of ` +\n `union \"${union.alias}\".`\n );\n }\n\n if (fromColumn[\"_dataType\"] !== toColumn[\"_dataType\"]) {\n throw new Error(\n `${label}: \"${sourceName}\".\"${fromAlias}\" is \"${fromColumn[\"_dataType\"]}\" but ` +\n `\"${alias}\".\"${toAlias}\" is \"${toColumn[\"_dataType\"]}\"; both columns of a pair ` +\n `must have the same type.`\n );\n }\n\n return member.getColumn(toAlias) as AnyColumn;\n });\n }\n\n this._validateDiscriminator(label, relation, source, union);\n\n if (relation.discriminator) {\n const byField = this._discriminators.get(sourceName) ?? new Map<string, AnyColumn>();\n const alias = this._findColumnAlias(source, relation.discriminator) as string;\n\n byField.set(field, this.getTable(sourceName).getColumn(alias) as AnyColumn);\n this._discriminators.set(sourceName, byField);\n }\n\n const byField =\n this._unionPairs.get(sourceName) ?? new Map<string, Record<string, AnyColumn[]>>();\n byField.set(field, pairs);\n this._unionPairs.set(sourceName, byField);\n }\n\n /** `to` as one column-definition list per member, whichever form it was written in. */\n private _getMemberToColumns(\n label: string,\n union: Union,\n to: AnyFieldRelation[\"to\"]\n ): Record<string, AnyColumnDefinition[]> {\n if (Array.isArray(to)) {\n if (to.length === 0) {\n throw new Error(`${label} must declare at least one column pair in \"from\" and \"to\".`);\n }\n\n return Object.fromEntries(\n union.memberAliases.map((alias) => [\n alias,\n (to as unknown[]).map((column) => {\n if (\n !(column instanceof UnionColumnDefinition) ||\n union.definition.columns[column.name] !== column\n ) {\n throw new Error(\n `${label}: a \"to\" column given as a list must be a shared field of union ` +\n `\"${union.alias}\" (\\`${union.alias}.columns.<field>\\`). To name a different ` +\n `column per member, pass one list per member instead.`\n );\n }\n\n return column.members[alias];\n }),\n ])\n );\n }\n\n const lists = to as Record<string, AnyColumnDefinition[]>;\n const given = Object.keys(lists);\n const missing = union.memberAliases.filter((alias) => !given.includes(alias));\n const unknown = given.filter((alias) => !union.hasMember(alias));\n\n if (missing.length > 0 || unknown.length > 0) {\n throw new Error(\n `${label} must name one \"to\" column list per member of union \"${union.alias}\" ` +\n `(${union.memberAliases.join(\", \")})` +\n (missing.length > 0 ? `; missing: ${missing.join(\", \")}` : \"\") +\n (unknown.length > 0 ? `; not members: ${unknown.join(\", \")}` : \"\") +\n `.`\n );\n }\n\n return lists;\n }\n\n /**\n * A belongs-to a union stores which member it points at in a column of its own — the\n * discriminator, holding a member alias. Nothing else can tell the members apart: their keys\n * share one column. Has-many and has-one to a union need none, since the key lives on each\n * member, so one there is a mistake.\n */\n private _validateDiscriminator(\n label: string,\n relation: AnyFieldRelation,\n source: AnyTableDefinition,\n union: Union\n ): void {\n const { discriminator } = relation;\n\n if (relation.type !== Relation.BELONGS_TO) {\n if (discriminator) {\n throw new Error(\n `${label} declares a discriminator, but only a belongs-to a union needs one: the ` +\n `members of \"${union.alias}\" hold the key.`\n );\n }\n\n return;\n }\n\n if (!discriminator) {\n throw new Error(\n `${label} belongs to union \"${union.alias}\" and must declare a discriminator: the ` +\n `source column holding which member each row points at.`\n );\n }\n\n const alias = this._findColumnAlias(source, discriminator);\n\n if (!alias) {\n throw new Error(\n `${label}: discriminator \"${discriminator.name}\" is not declared on table \"${source.name}\".`\n );\n }\n\n const dataType = discriminator[\"_dataType\"] as string;\n const textLike =\n discriminator[\"_domain\"] !== undefined ||\n dataType === \"text\" ||\n /^(varchar|char)\\(\\d+\\)$/.test(dataType);\n\n if (!textLike) {\n throw new Error(\n `${label}: discriminator \"${source.name}\".\"${alias}\" is \"${dataType}\"; it holds a ` +\n `member alias, so it must be text, varchar, char, or a text domain.`\n );\n }\n }\n\n private _buildRelations(schema: Schema<TDefinition>) {\n const map = new Map<string, AnyTableRelations>();\n\n const definitions = new Map<string, AnyTableDefinition>();\n\n for (const definition of Object.values<AnyTableDefinition>(schema.tables)) {\n if (definition instanceof TableDefinition) {\n definitions.set(definition.name, definition);\n }\n }\n\n for (const [tableName, relations] of Object.entries(\n schema.relations as Record<string, AnyTableRelations>\n )) {\n const table = this._tables.get(tableName);\n\n if (!table) {\n throw new Error(`Table not found for relations: ${tableName}`);\n }\n\n for (const [field, relation] of Object.entries(relations)) {\n // Columns and relations share one namespace: the client addresses both as fields of\n // the same model (`select`, `join`, and the keys of a result row), so a name can\n // only mean one of them.\n if (table.hasColumn(field)) {\n throw new Error(\n `Relation \"${field}\" on table \"${tableName}\" collides with a column of the same ` +\n `name. Columns and relations share one field namespace on a table.`\n );\n }\n\n // That one namespace also excludes the names the runtime writes onto result rows.\n if (RESERVED_FIELD_NAMES.includes(field)) {\n throw new Error(\n `Table \"${tableName}\" declares a relation named \"${field}\", which is reserved. ` +\n `The runtime writes ${RESERVED_FIELD_NAMES.join(\" and \")} onto every result row.`\n );\n }\n\n this._validateRelation(tableName, field, relation, definitions);\n }\n\n map.set(tableName, relations);\n }\n\n return map;\n }\n\n public getTable<TName extends string>(\n aliasOrName: TName\n ): TableByNameOrAlias<this[\"__type\"], TName> {\n const table = this._tables.get(aliasOrName);\n\n if (!table) {\n throw new Error(`Table not found: ${aliasOrName}`);\n }\n\n return table as TableByNameOrAlias<this[\"__type\"], TName>;\n }\n\n public hasTable(aliasOrName: string): boolean {\n return this._tables.has(aliasOrName);\n }\n\n public getTables(): Prettify<RuntimeTables<this[\"__type\"]>> {\n return Object.fromEntries(this._tables.entries()) as RuntimeTables<this[\"__type\"]>;\n }\n\n /**\n * One entry per table, keyed by schema alias. Unlike `getTables()`, which holds every\n * table under both its alias and its database name, this never yields the same table\n * twice — use it whenever you iterate tables to build something per table.\n */\n public getTableEntries(): [alias: string, table: AnyTable][] {\n const entries = new Map<string, AnyTable>();\n\n for (const table of this._tables.values()) {\n entries.set(table.alias, table);\n }\n\n return [...entries.entries()];\n }\n\n /**\n * The schema alias for a table, given either its alias or its database name.\n * Throws when no such table exists.\n */\n public getAlias(nameOrAlias: string): string {\n return this.getTable(nameOrAlias).alias;\n }\n\n public hasRelations(tableNameOrAlias: string): boolean {\n if (!this.hasTable(tableNameOrAlias)) {\n return false;\n }\n\n const table = this.getTable(tableNameOrAlias);\n return this._relations.has(table.name);\n }\n\n public getRelations(tableNameOrAlias: string) {\n const table = this.getTable(tableNameOrAlias);\n const relations = this._relations.get(table.name);\n\n if (!relations) {\n throw new Error(`Relations not found for table: ${tableNameOrAlias}`);\n }\n\n return relations;\n }\n\n public hasUnion(alias: string): boolean {\n return this._unions.has(alias);\n }\n\n public getUnion(alias: string): Union {\n const union = this._unions.get(alias);\n\n if (!union) {\n throw new Error(`Union not found: ${alias}`);\n }\n\n return union;\n }\n\n /** One entry per union, keyed by schema alias. */\n public getUnions(): [alias: string, union: Union][] {\n return [...this._unions.entries()];\n }\n\n /**\n * What a relation points at: a table, or a union when the relation targets one. Callers\n * that only handle tables must narrow with `instanceof Union`.\n */\n public getRelationTarget(tableNameOrAlias: string, field: string): AnyTable | Union {\n const sourceTable = this.getTable(tableNameOrAlias);\n const relations = this._relations.get(sourceTable.name);\n\n if (!relations?.[field]) {\n throw new Error(`Relation not found for field: ${field} on table: ${sourceTable.name}`);\n }\n\n const target = relations[field].target;\n\n if (target instanceof UnionDefinition) {\n const union = [...this._unions.values()].find((candidate) => candidate.definition === target);\n\n if (!union) {\n throw new Error(\n `Target union not found for relation: ${field} on table: ${sourceTable.name}`\n );\n }\n\n return union;\n }\n\n return this.getTable(target.name);\n }\n\n /**\n * The `to` columns of a relation to a union, per member alias — each member's own columns,\n * paired index by index with the relation's `from`.\n */\n /**\n * The discriminator column of a belongs-to a union — the source column naming which member\n * each row points at — or `undefined` for any other relation.\n */\n public getRelationDiscriminator(tableNameOrAlias: string, field: string): AnyColumn | undefined {\n const sourceTable = this.getTable(tableNameOrAlias);\n\n return this._discriminators.get(sourceTable.name)?.get(field);\n }\n\n public getUnionRelationColumns(\n tableNameOrAlias: string,\n field: string\n ): Record<string, AnyColumn[]> {\n const sourceTable = this.getTable(tableNameOrAlias);\n const pairs = this._unionPairs.get(sourceTable.name)?.get(field);\n\n if (!pairs) {\n throw new Error(\n `Relation \"${field}\" on table \"${sourceTable.name}\" does not target a union.`\n );\n }\n\n return pairs;\n }\n}\n"]}
@@ -5,19 +5,25 @@ import { SQLStatement } from "../sql/nodes.js";
5
5
  * This interface abstracts the underlying driver connection and provides methods
6
6
  * for executing queries and handling transactions.
7
7
  */
8
- export interface Session {
8
+ export type Session = {
9
9
  execute<T = unknown>(query: SQLStatement): Promise<T[]>;
10
10
  beginTransaction?(): Promise<TransactionSession>;
11
- }
11
+ };
12
12
  /**
13
13
  * Defines the TransactionSession interface, which extends the Session interface with
14
14
  * additional methods for committing and rolling back transactions.
15
15
  *
16
16
  * This interface is used when a transaction is active, allowing the caller to
17
17
  * manage the transaction lifecycle explicitly.
18
+ *
19
+ * `commit` and `rollback` each end the transaction and free whatever it holds (a pooled
20
+ * connection) whether they succeed or not; after either, the other is a no-op. `commit` throws
21
+ * what the database answered, a `40001` included. `rollback` should not throw: it runs after
22
+ * another error, which is the one the caller needs, and `$transaction` ignores a rollback
23
+ * failure.
18
24
  */
19
- export interface TransactionSession extends Session {
25
+ export type TransactionSession = {
20
26
  commit(): Promise<void>;
21
27
  rollback(): Promise<void>;
22
- }
28
+ } & Session;
23
29
  //# sourceMappingURL=session.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/runtime/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C;;;;;GAKG;AAEH,MAAM,WAAW,OAAO;IACtB,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC;IACxD,gBAAgB,CAAC,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAAC;CAClD;AAED;;;;;;GAMG;AAEH,MAAM,WAAW,kBAAmB,SAAQ,OAAO;IACjD,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACxB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B"}
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/runtime/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C;;;;;GAKG;AAEH,MAAM,MAAM,OAAO,GAAG;IACpB,OAAO,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC;IACxD,gBAAgB,CAAC,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAAC;CAClD,CAAC;AAEF;;;;;;;;;;;;GAYG;AAEH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACxB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B,GAAG,OAAO,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/runtime/session.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/runtime/session.ts"],"names":[],"mappings":"","sourcesContent":["import { SQLStatement } from \"../sql/nodes.js\";\n\n/**\n * Defines the Session interface for executing SQL queries and managing transactions.\n *\n * This interface abstracts the underlying driver connection and provides methods\n * for executing queries and handling transactions.\n */\n\nexport type Session = {\n execute<T = unknown>(query: SQLStatement): Promise<T[]>;\n beginTransaction?(): Promise<TransactionSession>;\n};\n\n/**\n * Defines the TransactionSession interface, which extends the Session interface with\n * additional methods for committing and rolling back transactions.\n *\n * This interface is used when a transaction is active, allowing the caller to\n * manage the transaction lifecycle explicitly.\n *\n * `commit` and `rollback` each end the transaction and free whatever it holds (a pooled\n * connection) whether they succeed or not; after either, the other is a no-op. `commit` throws\n * what the database answered, a `40001` included. `rollback` should not throw: it runs after\n * another error, which is the one the caller needs, and `$transaction` ignores a rollback\n * failure.\n */\n\nexport type TransactionSession = {\n commit(): Promise<void>;\n rollback(): Promise<void>;\n} & Session;\n"]}