joist-core 2.3.0-next.99 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/build/EntityManager.cjs +1 -1
  2. package/build/EntityManager.cjs.map +1 -1
  3. package/build/EntityManager.d.cts +9 -8
  4. package/build/EntityManager.d.cts.map +1 -1
  5. package/build/EntityManager.d.mts +9 -8
  6. package/build/EntityManager.d.mts.map +1 -1
  7. package/build/EntityManager.js +1 -1
  8. package/build/EntityManager.js.map +1 -1
  9. package/build/dataloaders/findCountDataLoader.cjs +2 -0
  10. package/build/dataloaders/findCountDataLoader.cjs.map +1 -1
  11. package/build/dataloaders/findCountDataLoader.d.cts.map +1 -1
  12. package/build/dataloaders/findCountDataLoader.d.mts.map +1 -1
  13. package/build/dataloaders/findCountDataLoader.js +2 -0
  14. package/build/dataloaders/findCountDataLoader.js.map +1 -1
  15. package/build/dataloaders/findDataLoader.cjs +1 -0
  16. package/build/dataloaders/findDataLoader.cjs.map +1 -1
  17. package/build/dataloaders/findDataLoader.d.cts.map +1 -1
  18. package/build/dataloaders/findDataLoader.d.mts.map +1 -1
  19. package/build/dataloaders/findDataLoader.js +2 -1
  20. package/build/dataloaders/findDataLoader.js.map +1 -1
  21. package/build/dataloaders/findIdsDataLoader.cjs +2 -0
  22. package/build/dataloaders/findIdsDataLoader.cjs.map +1 -1
  23. package/build/dataloaders/findIdsDataLoader.d.cts.map +1 -1
  24. package/build/dataloaders/findIdsDataLoader.d.mts.map +1 -1
  25. package/build/dataloaders/findIdsDataLoader.js +2 -0
  26. package/build/dataloaders/findIdsDataLoader.js.map +1 -1
  27. package/build/dataloaders/findPaginatedDataLoader.cjs +2 -0
  28. package/build/dataloaders/findPaginatedDataLoader.cjs.map +1 -1
  29. package/build/dataloaders/findPaginatedDataLoader.d.cts.map +1 -1
  30. package/build/dataloaders/findPaginatedDataLoader.d.mts.map +1 -1
  31. package/build/dataloaders/findPaginatedDataLoader.js +2 -0
  32. package/build/dataloaders/findPaginatedDataLoader.js.map +1 -1
  33. package/build/index.cjs +6 -3
  34. package/build/index.cjs.map +1 -1
  35. package/build/index.d.cts +6 -4
  36. package/build/index.d.cts.map +1 -1
  37. package/build/index.d.mts +6 -4
  38. package/build/index.d.mts.map +1 -1
  39. package/build/index.js +6 -5
  40. package/build/index.js.map +1 -1
  41. package/build/keys.cjs +6 -1
  42. package/build/keys.cjs.map +1 -1
  43. package/build/keys.d.cts +3 -1
  44. package/build/keys.d.cts.map +1 -1
  45. package/build/keys.d.mts +3 -1
  46. package/build/keys.d.mts.map +1 -1
  47. package/build/keys.js +6 -2
  48. package/build/keys.js.map +1 -1
  49. package/build/queries/find/QueryParser.cjs +1 -2
  50. package/build/queries/find/QueryParser.cjs.map +1 -1
  51. package/build/queries/find/QueryParser.d.cts.map +1 -1
  52. package/build/queries/find/QueryParser.d.mts.map +1 -1
  53. package/build/queries/find/QueryParser.js +2 -3
  54. package/build/queries/find/QueryParser.js.map +1 -1
  55. package/build/queries/find/QueryVisitor.cjs +17 -0
  56. package/build/queries/find/QueryVisitor.cjs.map +1 -1
  57. package/build/queries/find/QueryVisitor.d.cts +3 -1
  58. package/build/queries/find/QueryVisitor.d.cts.map +1 -1
  59. package/build/queries/find/QueryVisitor.d.mts +3 -1
  60. package/build/queries/find/QueryVisitor.d.mts.map +1 -1
  61. package/build/queries/find/QueryVisitor.js +17 -1
  62. package/build/queries/find/QueryVisitor.js.map +1 -1
  63. package/build/queries/sql/Expr.cjs +18 -1
  64. package/build/queries/sql/Expr.cjs.map +1 -1
  65. package/build/queries/sql/Expr.d.cts +13 -3
  66. package/build/queries/sql/Expr.d.cts.map +1 -1
  67. package/build/queries/sql/Expr.d.mts +13 -3
  68. package/build/queries/sql/Expr.d.mts.map +1 -1
  69. package/build/queries/sql/Expr.js +18 -2
  70. package/build/queries/sql/Expr.js.map +1 -1
  71. package/build/queries/sql/JoinTree.cjs +72 -0
  72. package/build/queries/sql/JoinTree.cjs.map +1 -0
  73. package/build/queries/sql/JoinTree.d.cts +107 -0
  74. package/build/queries/sql/JoinTree.d.cts.map +1 -0
  75. package/build/queries/sql/JoinTree.d.mts +107 -0
  76. package/build/queries/sql/JoinTree.d.mts.map +1 -0
  77. package/build/queries/sql/JoinTree.js +71 -0
  78. package/build/queries/sql/JoinTree.js.map +1 -0
  79. package/build/queries/sql/Tables.cjs +103 -6
  80. package/build/queries/sql/Tables.cjs.map +1 -1
  81. package/build/queries/sql/Tables.d.cts +72 -13
  82. package/build/queries/sql/Tables.d.cts.map +1 -1
  83. package/build/queries/sql/Tables.d.mts +72 -13
  84. package/build/queries/sql/Tables.d.mts.map +1 -1
  85. package/build/queries/sql/Tables.js +101 -10
  86. package/build/queries/sql/Tables.js.map +1 -1
  87. package/build/queries/sql/custom.cjs +52 -0
  88. package/build/queries/sql/custom.cjs.map +1 -0
  89. package/build/queries/sql/custom.d.cts +85 -0
  90. package/build/queries/sql/custom.d.cts.map +1 -0
  91. package/build/queries/sql/custom.d.mts +85 -0
  92. package/build/queries/sql/custom.d.mts.map +1 -0
  93. package/build/queries/sql/custom.js +49 -0
  94. package/build/queries/sql/custom.js.map +1 -0
  95. package/build/queries/sql/execute.cjs +68 -28
  96. package/build/queries/sql/execute.cjs.map +1 -1
  97. package/build/queries/sql/execute.d.cts +110 -11
  98. package/build/queries/sql/execute.d.cts.map +1 -1
  99. package/build/queries/sql/execute.d.mts +110 -11
  100. package/build/queries/sql/execute.d.mts.map +1 -1
  101. package/build/queries/sql/execute.js +69 -29
  102. package/build/queries/sql/execute.js.map +1 -1
  103. package/build/queries/sql/expressions/types.d.cts +5 -5
  104. package/build/queries/sql/expressions/types.d.cts.map +1 -1
  105. package/build/queries/sql/expressions/types.d.mts +5 -5
  106. package/build/queries/sql/expressions/types.d.mts.map +1 -1
  107. package/build/queries/sql/keywords.cjs +1 -1
  108. package/build/queries/sql/keywords.cjs.map +1 -1
  109. package/build/queries/sql/keywords.d.cts +1 -1
  110. package/build/queries/sql/keywords.d.mts +1 -1
  111. package/build/queries/sql/keywords.js +1 -1
  112. package/build/queries/sql/keywords.js.map +1 -1
  113. package/build/queries/sql/query.cjs +100 -38
  114. package/build/queries/sql/query.cjs.map +1 -1
  115. package/build/queries/sql/query.d.cts +96 -46
  116. package/build/queries/sql/query.d.cts.map +1 -1
  117. package/build/queries/sql/query.d.mts +96 -46
  118. package/build/queries/sql/query.d.mts.map +1 -1
  119. package/build/queries/sql/query.js +102 -40
  120. package/build/queries/sql/query.js.map +1 -1
  121. package/build/queries/valueFilters.cjs +28 -36
  122. package/build/queries/valueFilters.cjs.map +1 -1
  123. package/build/queries/valueFilters.d.cts +2 -4
  124. package/build/queries/valueFilters.d.cts.map +1 -1
  125. package/build/queries/valueFilters.d.mts +2 -4
  126. package/build/queries/valueFilters.d.mts.map +1 -1
  127. package/build/queries/valueFilters.js +29 -36
  128. package/build/queries/valueFilters.js.map +1 -1
  129. package/build/serde/serde.cjs.map +1 -1
  130. package/build/serde/serde.js.map +1 -1
  131. package/package.json +2 -2
@@ -1,12 +1,16 @@
1
1
  import { ColumnsOf, FieldsOf, FilterOf, TypeMap, TypeNameOf } from "../../typeMap.cjs";
2
2
  import { SqlCondition } from "../conditions.cjs";
3
3
  import { EntityMetadata } from "../../EntityMetadata.cjs";
4
- import { Expr, ExprLike, InnerJoin, LeftJoin } from "./Expr.cjs";
4
+ import { CustomColumnInputs, CustomTable, CustomTableDefinition, CustomTableFor, CustomTableMgmt } from "./custom.cjs";
5
+ import { QueryArg, QueryJoinInput, QuerySource } from "./query.cjs";
6
+ import { BaseExpr, Expr, ExprLike, InnerJoin, LeftJoin, selectKeyBrand } from "./Expr.cjs";
5
7
  import { IdOf, MaybeAbstractEntityConstructor, TaggedId } from "../../EntityManager.cjs";
6
8
  import { Entity } from "../../Entity.cjs";
7
9
  //#region src/queries/sql/Tables.d.ts
8
10
  /** Creates physical column expressions and relationship joins for `T`. */
9
11
  declare function table<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Table<T>;
12
+ /** Creates a typed physical handle for a table declared with `declareTable`. */
13
+ declare function table<TableName extends string, C extends CustomColumnInputs>(definition: CustomTableDefinition<TableName, C>): CustomTable<C, TableName>;
10
14
  /**
11
15
  * Creates a table with an explicit type-level name, i.e. `table(Author, "m")` for a self-join.
12
16
  *
@@ -14,6 +18,8 @@ declare function table<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>
14
18
  * so a left-joined mentor would also mark the mentee's columns nullable; a named alias has its own key.
15
19
  */
16
20
  declare function table<T extends Entity, Name extends string>(cstr: MaybeAbstractEntityConstructor<T>, name: Name): Table<T, Name>;
21
+ /** Creates a named custom-table handle for joins and self-joins. */
22
+ declare function table<C extends CustomColumnInputs, TableName extends string, Name extends string>(definition: CustomTableDefinition<TableName, C>, name: Name): CustomTable<C, Name>;
17
23
  /** Creates multiple physical table handles. */
18
24
  declare function tables<T extends readonly MaybeAbstractEntityConstructor<any>[]>(...type: T): { [P in keyof T]: T[P] extends MaybeAbstractEntityConstructor<infer E extends Entity> ? Table<E> : never; };
19
25
  /**
@@ -22,10 +28,13 @@ declare function tables<T extends readonly MaybeAbstractEntityConstructor<any>[]
22
28
  * `__entity` lets `QueryRow` recover `T` for entity mode (you cannot `infer T` back out of a mapped
23
29
  * type), and `__name` is the table's source key (see `Expr`).
24
30
  */
25
- interface TableBrand<T, Name extends string> extends TableMgmt {
26
- readonly __entity: T;
31
+ interface TableSourceBrand<Name extends string> extends TableSourceMgmt {
27
32
  readonly __name: Name;
28
33
  }
34
+ /** A modeled table source that can hydrate its generated entity type. */
35
+ interface TableBrand<T, Name extends string> extends TableSourceBrand<Name>, TableMgmt {
36
+ readonly __entity: T;
37
+ }
29
38
  /**
30
39
  * A physical table for `T`: one expression per SQL column, with relationship names as join factories.
31
40
  * Selecting the table itself retains entity hydration; selecting columns returns decoded SQL values.
@@ -54,6 +63,18 @@ interface TableBrand<T, Name extends string> extends TableMgmt {
54
63
  type Table<T extends Entity, Name extends string = TableNameOf<T>> = TableShape<T, Name> & ("where" extends keyof TableShape<T, Name> ? {} : {
55
64
  /** Builds an AND condition from local domain fields, without implicit joins. */
56
65
  where(filter: TableFilter<T>): SqlCondition;
66
+ }) & ("column" extends keyof TableShape<T, Name> ? {} : {
67
+ /**
68
+ * References a column Joist does not model, i.e. `a.column<string>("ts_search")`.
69
+ *
70
+ * The expression keeps this table's source key, so it is scope-checked and nullified by a
71
+ * LEFT join like any modeled column. `R` is unchecked: it asserts what the database returns,
72
+ * and the value is not decoded through a codec.
73
+ *
74
+ * A schema that has its own `column` column or relation keeps that meaning, and then has no
75
+ * escape hatch for unmodeled columns.
76
+ */
77
+ column<R = unknown>(column: string): Expr<R, Name>;
57
78
  });
58
79
  /** Domain filters for fields stored on this physical table, without relationship traversal. */
59
80
  type TableFilter<T extends Entity> = { [K in LocalFieldNames<T> & keyof FieldsOf<T>]?: FieldsOf<T>[K] extends {
@@ -69,15 +90,16 @@ type LocalFieldNames<T> = ColumnsOf<T>[keyof ColumnsOf<T>] extends {
69
90
  type TableReferenceFilter<T extends Entity, N> = T | IdOf<T> | readonly (T | IdOf<T>)[] | boolean | N | undefined | {
70
91
  ne: T | IdOf<T> | N | undefined;
71
92
  };
93
+ /** Maps an entity's columns and relationships to expressions and joins for one typed table handle. */
72
94
  type TableShape<T extends Entity, Name extends string> = {
73
95
  readonly [tableMgmt]: TableBrand<T, Name>;
74
- } & { [P in keyof ColumnsOf<T>]: P extends "id" ? EntityColumn<T, never, Name> : ColumnsOf<T>[P] extends {
96
+ } & { [P in keyof ColumnsOf<T> & string]: P extends "id" ? EntityColumn<T, never, Name, P> : ColumnsOf<T>[P] extends {
75
97
  entity: infer U extends Entity;
76
98
  nullable: infer N;
77
- } ? ReferenceColumn<U, N extends true ? null : never, Name> : ColumnsOf<T>[P] extends {
99
+ } ? ReferenceColumn<U, N extends true ? null : never, Name, P> : ColumnsOf<T>[P] extends {
78
100
  type: infer V;
79
101
  nullable: infer N;
80
- } ? PrimitiveColumn<V, N extends true ? null : never, Name> : never; } & { [P in keyof FieldsOf<T> as P extends keyof ColumnsOf<T> ? never : FieldsOf<T>[P] extends {
102
+ } ? PrimitiveColumn<V, N extends true ? null : never, Name, P> : never; } & { [P in keyof FieldsOf<T> as P extends keyof ColumnsOf<T> ? never : FieldsOf<T>[P] extends {
81
103
  kind: "m2o" | "poly" | "o2m" | "lo2m" | "m2m" | "o2o";
82
104
  } ? P : never]: FieldsOf<T>[P] extends {
83
105
  kind: "m2o";
@@ -150,7 +172,7 @@ interface SubtypeJoin<U extends Entity> extends CollectionJoin<U> {
150
172
  * (`b.authorId.as(a)` joins). The default join kind follows physical column nullability:
151
173
  * a NOT NULL FK is INNER unless its source was LEFT joined, a nullable one is LEFT, and the row type reflects it.
152
174
  */
153
- interface ReferenceColumn<U extends Entity, N extends null | never, Src extends string> extends EntityColumn<U, N, Src>, ReferenceJoin<U, N, Src> {}
175
+ interface ReferenceColumn<U extends Entity, N extends null | never, Src extends string, Key extends string = string> extends EntityColumn<U, N, Src, Key>, ReferenceJoin<U, N, Src> {}
154
176
  /**
155
177
  * A polymorphic reference: condition methods (each resolving the component column from the value), plus
156
178
  * a join factory that picks the component from the argument's entity, i.e. `c.parent.as(a)` joins
@@ -163,7 +185,8 @@ interface PolyReference<U extends Entity, N extends null | never, Src extends st
163
185
  /** Applies an IN condition unless the list is empty or undefined. */
164
186
  inNonEmpty(values: readonly (U | TaggedId)[] | undefined): SqlCondition;
165
187
  }
166
- interface PrimitiveColumn<V, N extends null | never, Src extends string = string> extends Expr<V | N, Src> {
188
+ interface PrimitiveColumn<V, N extends null | never, Src extends string = string, Key extends string = string> extends Expr<V | N, Src> {
189
+ readonly [selectKeyBrand]: Key;
167
190
  eq(value: V | ExprLike<V | N> | N | undefined): SqlCondition;
168
191
  ne(value: V | ExprLike<V | N> | N | undefined): SqlCondition;
169
192
  in(values: readonly (V | null)[] | ExprLike<V | null> | undefined): SqlCondition;
@@ -210,9 +233,13 @@ interface PrimitiveColumn<V, N extends null | never, Src extends string = string
210
233
  */
211
234
  raw(exp: string, bindings: readonly any[] | undefined): SqlCondition;
212
235
  }
213
- interface EntityColumn<T, N extends null | never = never, Src extends string = string> extends Expr<IdOf<T> | N, Src> {
236
+ interface EntityColumn<T, N extends null | never = never, Src extends string = string, Key extends string = string> extends Expr<IdOf<T> | N, Src> {
237
+ readonly [selectKeyBrand]: Key;
238
+ /** Produces the tagged ID as SQL text, preserving NULL and the column's source. */
239
+ taggedId(): Expr<string | N, Src>;
214
240
  eq(value: T | IdOf<T> | ExprLike<IdOf<T> | null> | null | undefined): SqlCondition;
215
241
  ne(value: T | IdOf<T> | ExprLike<IdOf<T> | null> | null | undefined): SqlCondition;
242
+ in<F extends QuerySource, const S extends ExprLike<IdOf<T> | null>, J extends QueryJoinInput = [], Name extends string = "?">(value: QueryArg<F, S, J, Name>): SqlCondition;
216
243
  in(value: readonly (T | IdOf<T> | null)[] | ExprLike<IdOf<T> | null> | null | undefined): SqlCondition;
217
244
  /** Applies an IN condition unless the list is empty, null, or undefined. */
218
245
  inNonEmpty(value: readonly (T | IdOf<T> | null)[] | null | undefined): SqlCondition;
@@ -224,10 +251,26 @@ interface EntityColumn<T, N extends null | never = never, Src extends string = s
224
251
  raw(exp: string, bindings: readonly any[] | undefined): SqlCondition;
225
252
  }
226
253
  declare const tableMgmt: unique symbol;
227
- declare function getTableMgmt(table: TableFor<unknown>): TableMgmt;
254
+ declare const inQueryCondition: unique symbol;
255
+ /** Defers a direct IN query literal to query.ts, which owns query pruning and SQL generation. */
256
+ interface InQueryCondition {
257
+ readonly [inQueryCondition]: {
258
+ column: BaseExpr;
259
+ query: object;
260
+ };
261
+ }
262
+ declare function getTableMgmt<T extends Entity>(table: TableFor<T>): TableMgmt;
263
+ declare function getTableMgmt(table: CustomTableFor): CustomTableMgmt;
264
+ declare function getTableMgmt(table: TableFor<Entity> | CustomTableFor): TableSourceMgmt;
228
265
  /** The identity SQL queries bind a table by, plus its original entity metadata. */
229
- interface TableMgmt {
266
+ interface TableSourceMgmt {
230
267
  tableName: string;
268
+ schema?: string;
269
+ }
270
+ /** Qualifies custom tables without changing SQL for tables on the default search path. */
271
+ declare function tableSqlName(table: TableSourceMgmt): string;
272
+ /** The identity and generated metadata of a modeled entity table. */
273
+ interface TableMgmt extends TableSourceMgmt {
231
274
  /**
232
275
  * The metadata this table was created with, i.e. `table(TaskNew)` keeps `taskNewMeta`.
233
276
  *
@@ -240,7 +283,11 @@ interface TableMgmt {
240
283
  /** Returns the metadata for the entity that `table` is bound to. */
241
284
  declare function getTableMetadata<T extends Entity>(table: TableFor<T>): EntityMetadata<T>;
242
285
  declare function newTableProxy<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Table<T>;
243
- declare function isTable(obj: unknown): obj is Table<any, any>;
286
+ declare function isTable(obj: unknown): obj is Table<any, any> | CustomTableFor;
287
+ /** Whether a physical table has generated entity metadata. */
288
+ declare function isEntityTable(obj: unknown): obj is Table<any, any>;
289
+ /** Whether a physical table has columns declared through `declareTable`. */
290
+ declare function isCustomTable(obj: unknown): obj is CustomTableFor;
244
291
  /**
245
292
  * Marks a sugar join entry whose target is a *collection* (o2m/m2m), which filter soft-deletes like
246
293
  * em.find's "collections filter out soft-deletes, but m2o/o2o references still return them"; see
@@ -258,6 +305,18 @@ declare class JoinTableHandle {
258
305
  readonly joinTableName: string;
259
306
  constructor(joinTableName: string);
260
307
  }
308
+ /**
309
+ * Maps local domain fields to table-bound conditions, encoding each leaf once.
310
+ * I.e. Author.firstName uses the first_name column but resolves its alias in each query scope.
311
+ * Collections, polymorphic references, and inherited nonlocal fields require explicit expressions.
312
+ */
313
+ declare function tableWhere(mgmt: TableMgmt, filter: object): SqlCondition;
314
+ /**
315
+ * Converts relationship filters into conditions on the joined table's id column.
316
+ * I.e. Author's `join: { books: "b:1" }` filters Book.id to 1; `{ books: book }` uses book.id.
317
+ * Lists match any supplied ID, `true` requires a Book, and `null` or `false` requires no Book.
318
+ */
319
+ declare function tableEntityWhere(mgmt: TableMgmt, value: unknown): SqlCondition;
261
320
  //#endregion
262
- export { CollectionJoin, DefaultReferenceJoin, EntityColumn, JoinTableHandle, M2mJoinTable, PolyReference, PrimitiveColumn, ReferenceColumn, ReferenceJoin, Table, TableBrand, TableFilter, TableFor, TableMgmt, collectionJoin, getTableMetadata, getTableMgmt, isTable, m2mJoinTable, newTableProxy, referenceJoinSource, table, tableMgmt, tables };
321
+ export { CollectionJoin, DefaultReferenceJoin, EntityColumn, InQueryCondition, JoinTableHandle, M2mJoinTable, PolyReference, PrimitiveColumn, ReferenceColumn, ReferenceJoin, Table, TableBrand, TableFilter, TableFor, TableMgmt, TableSourceBrand, TableSourceMgmt, collectionJoin, getTableMetadata, getTableMgmt, inQueryCondition, isCustomTable, isEntityTable, isTable, m2mJoinTable, newTableProxy, referenceJoinSource, table, tableEntityWhere, tableMgmt, tableSqlName, tableWhere, tables };
263
322
  //# sourceMappingURL=Tables.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Tables.d.cts","names":[],"sources":["../../../src/queries/sql/Tables.ts"],"mappings":";;;;;;;;iBA6CgB,MAAM,UAAU,QAAQ,MAAM,+BAA+B,KAAK,MAAM;;;;;;;iBAOxE,MAAM,UAAU,QAAQ,qBACtC,MAAM,+BAA+B,IACrC,MAAM,OACL,MAAM,GAAG;;iBAOI,OAAO,mBAAmB,0CACrC,MAAM,OACL,WAAW,IAAI,EAAE,WAAW,qCAAqC,UAAU,UAAU,MAAM;;;;;;;UAUhF,WAAW,GAAG,6BAA6B;WACjD,UAAU;WACV,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;KA4BP,MAAM,UAAU,QAAQ,sBAAsB,YAAY,MAAM,WAAW,GAAG,+BACjE,WAAW,GAAG;;EAI/B,MAAM,QAAQ,YAAY,KAAK;;;KAI3B,YAAY,UAAU,aAC/B,KAAK,gBAAgB,WAAW,SAAS,MAAM,SAAS,GAAG;EAC1D;EACA,YAAY,UAAU;EACtB,gBAAgB;IA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 -1,12 +1,16 @@
1
1
  import { ColumnsOf, FieldsOf, FilterOf, TypeMap, TypeNameOf } from "../../typeMap.mjs";
2
2
  import { SqlCondition } from "../conditions.mjs";
3
3
  import { EntityMetadata } from "../../EntityMetadata.mjs";
4
- import { Expr, ExprLike, InnerJoin, LeftJoin } from "./Expr.mjs";
4
+ import { CustomColumnInputs, CustomTable, CustomTableDefinition, CustomTableFor, CustomTableMgmt } from "./custom.mjs";
5
+ import { QueryArg, QueryJoinInput, QuerySource } from "./query.mjs";
6
+ import { BaseExpr, Expr, ExprLike, InnerJoin, LeftJoin, selectKeyBrand } from "./Expr.mjs";
5
7
  import { IdOf, MaybeAbstractEntityConstructor, TaggedId } from "../../EntityManager.mjs";
6
8
  import { Entity } from "../../Entity.mjs";
7
9
  //#region src/queries/sql/Tables.d.ts
8
10
  /** Creates physical column expressions and relationship joins for `T`. */
9
11
  declare function table<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Table<T>;
12
+ /** Creates a typed physical handle for a table declared with `declareTable`. */
13
+ declare function table<TableName extends string, C extends CustomColumnInputs>(definition: CustomTableDefinition<TableName, C>): CustomTable<C, TableName>;
10
14
  /**
11
15
  * Creates a table with an explicit type-level name, i.e. `table(Author, "m")` for a self-join.
12
16
  *
@@ -14,6 +18,8 @@ declare function table<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>
14
18
  * so a left-joined mentor would also mark the mentee's columns nullable; a named alias has its own key.
15
19
  */
16
20
  declare function table<T extends Entity, Name extends string>(cstr: MaybeAbstractEntityConstructor<T>, name: Name): Table<T, Name>;
21
+ /** Creates a named custom-table handle for joins and self-joins. */
22
+ declare function table<C extends CustomColumnInputs, TableName extends string, Name extends string>(definition: CustomTableDefinition<TableName, C>, name: Name): CustomTable<C, Name>;
17
23
  /** Creates multiple physical table handles. */
18
24
  declare function tables<T extends readonly MaybeAbstractEntityConstructor<any>[]>(...type: T): { [P in keyof T]: T[P] extends MaybeAbstractEntityConstructor<infer E extends Entity> ? Table<E> : never; };
19
25
  /**
@@ -22,10 +28,13 @@ declare function tables<T extends readonly MaybeAbstractEntityConstructor<any>[]
22
28
  * `__entity` lets `QueryRow` recover `T` for entity mode (you cannot `infer T` back out of a mapped
23
29
  * type), and `__name` is the table's source key (see `Expr`).
24
30
  */
25
- interface TableBrand<T, Name extends string> extends TableMgmt {
26
- readonly __entity: T;
31
+ interface TableSourceBrand<Name extends string> extends TableSourceMgmt {
27
32
  readonly __name: Name;
28
33
  }
34
+ /** A modeled table source that can hydrate its generated entity type. */
35
+ interface TableBrand<T, Name extends string> extends TableSourceBrand<Name>, TableMgmt {
36
+ readonly __entity: T;
37
+ }
29
38
  /**
30
39
  * A physical table for `T`: one expression per SQL column, with relationship names as join factories.
31
40
  * Selecting the table itself retains entity hydration; selecting columns returns decoded SQL values.
@@ -54,6 +63,18 @@ interface TableBrand<T, Name extends string> extends TableMgmt {
54
63
  type Table<T extends Entity, Name extends string = TableNameOf<T>> = TableShape<T, Name> & ("where" extends keyof TableShape<T, Name> ? {} : {
55
64
  /** Builds an AND condition from local domain fields, without implicit joins. */
56
65
  where(filter: TableFilter<T>): SqlCondition;
66
+ }) & ("column" extends keyof TableShape<T, Name> ? {} : {
67
+ /**
68
+ * References a column Joist does not model, i.e. `a.column<string>("ts_search")`.
69
+ *
70
+ * The expression keeps this table's source key, so it is scope-checked and nullified by a
71
+ * LEFT join like any modeled column. `R` is unchecked: it asserts what the database returns,
72
+ * and the value is not decoded through a codec.
73
+ *
74
+ * A schema that has its own `column` column or relation keeps that meaning, and then has no
75
+ * escape hatch for unmodeled columns.
76
+ */
77
+ column<R = unknown>(column: string): Expr<R, Name>;
57
78
  });
58
79
  /** Domain filters for fields stored on this physical table, without relationship traversal. */
59
80
  type TableFilter<T extends Entity> = { [K in LocalFieldNames<T> & keyof FieldsOf<T>]?: FieldsOf<T>[K] extends {
@@ -69,15 +90,16 @@ type LocalFieldNames<T> = ColumnsOf<T>[keyof ColumnsOf<T>] extends {
69
90
  type TableReferenceFilter<T extends Entity, N> = T | IdOf<T> | readonly (T | IdOf<T>)[] | boolean | N | undefined | {
70
91
  ne: T | IdOf<T> | N | undefined;
71
92
  };
93
+ /** Maps an entity's columns and relationships to expressions and joins for one typed table handle. */
72
94
  type TableShape<T extends Entity, Name extends string> = {
73
95
  readonly [tableMgmt]: TableBrand<T, Name>;
74
- } & { [P in keyof ColumnsOf<T>]: P extends "id" ? EntityColumn<T, never, Name> : ColumnsOf<T>[P] extends {
96
+ } & { [P in keyof ColumnsOf<T> & string]: P extends "id" ? EntityColumn<T, never, Name, P> : ColumnsOf<T>[P] extends {
75
97
  entity: infer U extends Entity;
76
98
  nullable: infer N;
77
- } ? ReferenceColumn<U, N extends true ? null : never, Name> : ColumnsOf<T>[P] extends {
99
+ } ? ReferenceColumn<U, N extends true ? null : never, Name, P> : ColumnsOf<T>[P] extends {
78
100
  type: infer V;
79
101
  nullable: infer N;
80
- } ? PrimitiveColumn<V, N extends true ? null : never, Name> : never; } & { [P in keyof FieldsOf<T> as P extends keyof ColumnsOf<T> ? never : FieldsOf<T>[P] extends {
102
+ } ? PrimitiveColumn<V, N extends true ? null : never, Name, P> : never; } & { [P in keyof FieldsOf<T> as P extends keyof ColumnsOf<T> ? never : FieldsOf<T>[P] extends {
81
103
  kind: "m2o" | "poly" | "o2m" | "lo2m" | "m2m" | "o2o";
82
104
  } ? P : never]: FieldsOf<T>[P] extends {
83
105
  kind: "m2o";
@@ -150,7 +172,7 @@ interface SubtypeJoin<U extends Entity> extends CollectionJoin<U> {
150
172
  * (`b.authorId.as(a)` joins). The default join kind follows physical column nullability:
151
173
  * a NOT NULL FK is INNER unless its source was LEFT joined, a nullable one is LEFT, and the row type reflects it.
152
174
  */
153
- interface ReferenceColumn<U extends Entity, N extends null | never, Src extends string> extends EntityColumn<U, N, Src>, ReferenceJoin<U, N, Src> {}
175
+ interface ReferenceColumn<U extends Entity, N extends null | never, Src extends string, Key extends string = string> extends EntityColumn<U, N, Src, Key>, ReferenceJoin<U, N, Src> {}
154
176
  /**
155
177
  * A polymorphic reference: condition methods (each resolving the component column from the value), plus
156
178
  * a join factory that picks the component from the argument's entity, i.e. `c.parent.as(a)` joins
@@ -163,7 +185,8 @@ interface PolyReference<U extends Entity, N extends null | never, Src extends st
163
185
  /** Applies an IN condition unless the list is empty or undefined. */
164
186
  inNonEmpty(values: readonly (U | TaggedId)[] | undefined): SqlCondition;
165
187
  }
166
- interface PrimitiveColumn<V, N extends null | never, Src extends string = string> extends Expr<V | N, Src> {
188
+ interface PrimitiveColumn<V, N extends null | never, Src extends string = string, Key extends string = string> extends Expr<V | N, Src> {
189
+ readonly [selectKeyBrand]: Key;
167
190
  eq(value: V | ExprLike<V | N> | N | undefined): SqlCondition;
168
191
  ne(value: V | ExprLike<V | N> | N | undefined): SqlCondition;
169
192
  in(values: readonly (V | null)[] | ExprLike<V | null> | undefined): SqlCondition;
@@ -210,9 +233,13 @@ interface PrimitiveColumn<V, N extends null | never, Src extends string = string
210
233
  */
211
234
  raw(exp: string, bindings: readonly any[] | undefined): SqlCondition;
212
235
  }
213
- interface EntityColumn<T, N extends null | never = never, Src extends string = string> extends Expr<IdOf<T> | N, Src> {
236
+ interface EntityColumn<T, N extends null | never = never, Src extends string = string, Key extends string = string> extends Expr<IdOf<T> | N, Src> {
237
+ readonly [selectKeyBrand]: Key;
238
+ /** Produces the tagged ID as SQL text, preserving NULL and the column's source. */
239
+ taggedId(): Expr<string | N, Src>;
214
240
  eq(value: T | IdOf<T> | ExprLike<IdOf<T> | null> | null | undefined): SqlCondition;
215
241
  ne(value: T | IdOf<T> | ExprLike<IdOf<T> | null> | null | undefined): SqlCondition;
242
+ in<F extends QuerySource, const S extends ExprLike<IdOf<T> | null>, J extends QueryJoinInput = [], Name extends string = "?">(value: QueryArg<F, S, J, Name>): SqlCondition;
216
243
  in(value: readonly (T | IdOf<T> | null)[] | ExprLike<IdOf<T> | null> | null | undefined): SqlCondition;
217
244
  /** Applies an IN condition unless the list is empty, null, or undefined. */
218
245
  inNonEmpty(value: readonly (T | IdOf<T> | null)[] | null | undefined): SqlCondition;
@@ -224,10 +251,26 @@ interface EntityColumn<T, N extends null | never = never, Src extends string = s
224
251
  raw(exp: string, bindings: readonly any[] | undefined): SqlCondition;
225
252
  }
226
253
  declare const tableMgmt: unique symbol;
227
- declare function getTableMgmt(table: TableFor<unknown>): TableMgmt;
254
+ declare const inQueryCondition: unique symbol;
255
+ /** Defers a direct IN query literal to query.ts, which owns query pruning and SQL generation. */
256
+ interface InQueryCondition {
257
+ readonly [inQueryCondition]: {
258
+ column: BaseExpr;
259
+ query: object;
260
+ };
261
+ }
262
+ declare function getTableMgmt<T extends Entity>(table: TableFor<T>): TableMgmt;
263
+ declare function getTableMgmt(table: CustomTableFor): CustomTableMgmt;
264
+ declare function getTableMgmt(table: TableFor<Entity> | CustomTableFor): TableSourceMgmt;
228
265
  /** The identity SQL queries bind a table by, plus its original entity metadata. */
229
- interface TableMgmt {
266
+ interface TableSourceMgmt {
230
267
  tableName: string;
268
+ schema?: string;
269
+ }
270
+ /** Qualifies custom tables without changing SQL for tables on the default search path. */
271
+ declare function tableSqlName(table: TableSourceMgmt): string;
272
+ /** The identity and generated metadata of a modeled entity table. */
273
+ interface TableMgmt extends TableSourceMgmt {
231
274
  /**
232
275
  * The metadata this table was created with, i.e. `table(TaskNew)` keeps `taskNewMeta`.
233
276
  *
@@ -240,7 +283,11 @@ interface TableMgmt {
240
283
  /** Returns the metadata for the entity that `table` is bound to. */
241
284
  declare function getTableMetadata<T extends Entity>(table: TableFor<T>): EntityMetadata<T>;
242
285
  declare function newTableProxy<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Table<T>;
243
- declare function isTable(obj: unknown): obj is Table<any, any>;
286
+ declare function isTable(obj: unknown): obj is Table<any, any> | CustomTableFor;
287
+ /** Whether a physical table has generated entity metadata. */
288
+ declare function isEntityTable(obj: unknown): obj is Table<any, any>;
289
+ /** Whether a physical table has columns declared through `declareTable`. */
290
+ declare function isCustomTable(obj: unknown): obj is CustomTableFor;
244
291
  /**
245
292
  * Marks a sugar join entry whose target is a *collection* (o2m/m2m), which filter soft-deletes like
246
293
  * em.find's "collections filter out soft-deletes, but m2o/o2o references still return them"; see
@@ -258,6 +305,18 @@ declare class JoinTableHandle {
258
305
  readonly joinTableName: string;
259
306
  constructor(joinTableName: string);
260
307
  }
308
+ /**
309
+ * Maps local domain fields to table-bound conditions, encoding each leaf once.
310
+ * I.e. Author.firstName uses the first_name column but resolves its alias in each query scope.
311
+ * Collections, polymorphic references, and inherited nonlocal fields require explicit expressions.
312
+ */
313
+ declare function tableWhere(mgmt: TableMgmt, filter: object): SqlCondition;
314
+ /**
315
+ * Converts relationship filters into conditions on the joined table's id column.
316
+ * I.e. Author's `join: { books: "b:1" }` filters Book.id to 1; `{ books: book }` uses book.id.
317
+ * Lists match any supplied ID, `true` requires a Book, and `null` or `false` requires no Book.
318
+ */
319
+ declare function tableEntityWhere(mgmt: TableMgmt, value: unknown): SqlCondition;
261
320
  //#endregion
262
- export { CollectionJoin, DefaultReferenceJoin, EntityColumn, JoinTableHandle, M2mJoinTable, PolyReference, PrimitiveColumn, ReferenceColumn, ReferenceJoin, Table, TableBrand, TableFilter, TableFor, TableMgmt, collectionJoin, getTableMetadata, getTableMgmt, isTable, m2mJoinTable, newTableProxy, referenceJoinSource, table, tableMgmt, tables };
321
+ export { CollectionJoin, DefaultReferenceJoin, EntityColumn, InQueryCondition, JoinTableHandle, M2mJoinTable, PolyReference, PrimitiveColumn, ReferenceColumn, ReferenceJoin, Table, TableBrand, TableFilter, TableFor, TableMgmt, TableSourceBrand, TableSourceMgmt, collectionJoin, getTableMetadata, getTableMgmt, inQueryCondition, isCustomTable, isEntityTable, isTable, m2mJoinTable, newTableProxy, referenceJoinSource, table, tableEntityWhere, tableMgmt, tableSqlName, tableWhere, tables };
263
322
  //# sourceMappingURL=Tables.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Tables.d.mts","names":[],"sources":["../../../src/queries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QAAQ;IAAU;;;iBAGnC,aAAa,UAAU,QAAQ,OAAO,SAAS,KAAK;iBACpD,aAAa,OAAO,iBAAiB;iBACrC,aAAa,OAAO,SAAS,UAAU,iBAAiB;;UAMvD;EACf;EACA;;;iBAIc,aAAa,OAAO;;UAKnB,kBAAkB;;;;;;;;EAQjC,MAAM;;;iBAIQ,iBAAiB,UAAU,QAAQ,OAAO,SAAS,KAAK,eAAe;iBAKvE,cAAc,UAAU,QAAQ,MAAM,+BAA+B,KAAK,MAAM;iBA0FhF,QAAQ,eAAe,OAAO,kBAAkB;;iBAKhD,cAAc,eAAe,OAAO;;iBAKpC,cAAc,eAAe,OAAO;;;;;;cA+bvC;;cAGA;UAEI;EACf,QAAQ;EACR,IAAI;;;cAIO;WACU;EAArB,YAAqB;;;;;;;iBA8LP,WAAW,MAAM,WAAW,iBAAiB;;;;;;iBAwD7C,iBAAiB,MAAM,WAAW,iBAAiB"}
@@ -1,17 +1,18 @@
1
- import { maybeResolveReferenceToId, toIdOf } from "../../keys.js";
2
- import { kqDot } from "./keywords.js";
1
+ import { maybeResolveReferenceToId, taggedIdPrefix, toIdOf } from "../../keys.js";
2
+ import { kq, kqDot, safeKq } from "./keywords.js";
3
3
  import { skipCondition } from "../skipCondition.js";
4
4
  import { makeLike, mapToDb, parseEntityFilter, parseValueFilter } from "../valueFilters.js";
5
- import { BaseExpr, asNode, deferredCondition, isExpr } from "./Expr.js";
5
+ import { BaseExpr, RefExpr, asNode, deferredCondition, isExpr, selectKeyBrand } from "./Expr.js";
6
6
  import { fail } from "../../utils.js";
7
7
  import { getConstructorFromTaggedId } from "../../configure.js";
8
8
  import { getBaseAndSelfMetas, getBaseMeta, getBaseSelfAndSubMetas, getMetadata } from "../../EntityMetadata.js";
9
9
  import { buildValueCondition } from "../renderConditions.js";
10
+ import { getCustomTableDefinition, isCustomTableDefinition } from "./custom.js";
10
11
  import { isEntity } from "../../Entity.js";
11
12
  import { groupBy } from "joist-utils";
12
13
  //#region src/queries/sql/Tables.ts
13
14
  function table(cstr, _name) {
14
- return newTableProxy(cstr);
15
+ return isCustomTableDefinition(cstr) ? newCustomTableProxy(cstr) : newTableProxy(cstr);
15
16
  }
16
17
  /** Creates multiple physical table handles. */
17
18
  function tables(...type) {
@@ -20,9 +21,14 @@ function tables(...type) {
20
21
  /** The source of a default reference join, used to inherit optional paths within each query. */
21
22
  const referenceJoinSource = Symbol("joist.referenceJoinSource");
22
23
  const tableMgmt = Symbol("tableMgmt");
24
+ const inQueryCondition = Symbol("joist.inQueryCondition");
23
25
  function getTableMgmt(table) {
24
26
  return table[tableMgmt];
25
27
  }
28
+ /** Qualifies custom tables without changing SQL for tables on the default search path. */
29
+ function tableSqlName(table) {
30
+ return table.schema ? `${safeKq(table.schema)}.${kq(table.tableName)}` : kq(table.tableName);
31
+ }
26
32
  /** Returns the metadata for the entity that `table` is bound to. */
27
33
  function getTableMetadata(table) {
28
34
  return table[tableMgmt].meta;
@@ -40,7 +46,7 @@ function newTableProxy(cstr) {
40
46
  if (key === tableMgmt) return mgmt;
41
47
  if (typeof key !== "string") return void 0;
42
48
  const descriptor = Object.hasOwn(meta.columns, key) ? meta.columns[key] : void 0;
43
- if (descriptor) return descriptor.idMetadata && key !== "id" ? new EntityColumnImpl(meta, descriptor, mgmt) : new PrimitiveColumnImpl(meta, descriptor, mgmt);
49
+ if (descriptor) return descriptor.idMetadata ? new EntityColumnImpl(meta, descriptor, mgmt, key) : new PrimitiveColumnImpl(meta, descriptor, mgmt, key);
44
50
  const subtype = subtypes.get(key);
45
51
  if (subtype) {
46
52
  const join = new SubtypeJoinImpl(proxy, subtype);
@@ -52,6 +58,7 @@ function newTableProxy(cstr) {
52
58
  }
53
59
  const relation = physicalRelation(meta, key);
54
60
  if (key === "where" && !isRelation(meta.allFields[key])) return (filter) => tableWhere(mgmt, filter);
61
+ if (key === "column" && !isRelation(meta.allFields[key])) return (column) => new RefExpr(mgmt, column);
55
62
  const field = relation ?? fail(`No physical field ${key} on ${cstr.name}; join its base table explicitly`);
56
63
  switch (field.kind) {
57
64
  case "m2o": return new ReferenceJoinImpl(new EntityColumnImpl(meta, field.serde.columns[0].column, mgmt));
@@ -64,13 +71,43 @@ function newTableProxy(cstr) {
64
71
  }
65
72
  },
66
73
  has(_, key) {
67
- return key === tableMgmt || typeof key === "string" && (key === "where" || Object.hasOwn(meta.columns, key) || !!physicalRelation(meta, key) || subtypes.has(key));
74
+ return key === tableMgmt || typeof key === "string" && (key === "where" || key === "column" || Object.hasOwn(meta.columns, key) || !!physicalRelation(meta, key) || subtypes.has(key));
68
75
  }
69
76
  });
70
77
  return proxy;
71
78
  }
79
+ /** Creates a typed handle for primitive columns declared outside Joist codegen. */
80
+ function newCustomTableProxy(definition) {
81
+ const { tableName, schema, columns } = getCustomTableDefinition(definition);
82
+ const mgmt = {
83
+ tableName,
84
+ schema,
85
+ columns
86
+ };
87
+ return new Proxy({}, {
88
+ get(_, key) {
89
+ if (key === tableMgmt) return mgmt;
90
+ if (typeof key !== "string") return void 0;
91
+ const descriptor = Object.hasOwn(columns, key) ? columns[key] : void 0;
92
+ if (descriptor) return new PrimitiveColumnImpl(void 0, descriptor, mgmt, key);
93
+ if (key === "column") return (column) => new RefExpr(mgmt, column);
94
+ return fail(`Custom table ${tableName} has no declared column ${key}; use .column("${key}")`);
95
+ },
96
+ has(_, key) {
97
+ return key === tableMgmt || key === "column" || typeof key === "string" && Object.hasOwn(columns, key);
98
+ }
99
+ });
100
+ }
72
101
  function isTable(obj) {
73
- return typeof obj === "function" && tableMgmt in obj;
102
+ return (typeof obj === "function" || typeof obj === "object" && obj !== null) && tableMgmt in obj;
103
+ }
104
+ /** Whether a physical table has generated entity metadata. */
105
+ function isEntityTable(obj) {
106
+ return isTable(obj) && "meta" in obj[tableMgmt];
107
+ }
108
+ /** Whether a physical table has columns declared through `declareTable`. */
109
+ function isCustomTable(obj) {
110
+ return isTable(obj) && "columns" in obj[tableMgmt];
74
111
  }
75
112
  /**
76
113
  * A physical table column implements `Expr`: it renders as `alias."column"`, decodes result
@@ -80,11 +117,16 @@ var TableColumn = class extends BaseExpr {
80
117
  meta;
81
118
  column;
82
119
  mgmt;
83
- constructor(meta, column, mgmt) {
120
+ selectKey;
121
+ constructor(meta, column, mgmt, selectKey = column.columnName) {
84
122
  super();
85
123
  this.meta = meta;
86
124
  this.column = column;
87
125
  this.mgmt = mgmt;
126
+ this.selectKey = selectKey;
127
+ }
128
+ get [selectKeyBrand]() {
129
+ return this.selectKey;
88
130
  }
89
131
  /** Author.id, Book.authorId, and Comment.parentAuthorId use the same Author ID domain. */
90
132
  get outputType() {
@@ -282,10 +324,18 @@ var PrimitiveColumnImpl = class extends TableColumn {
282
324
  }
283
325
  };
284
326
  var EntityColumnImpl = class extends TableColumn {
327
+ /** Uses the ID target's tag, including for foreign keys and polymorphic components. */
328
+ taggedId() {
329
+ return new TaggedIdExpr(this);
330
+ }
285
331
  in(values) {
286
332
  if (values === void 0) return skipCondition;
287
333
  else if (values === null) throw new Error("Unsupported");
288
334
  else if (isExpr(values)) return this.inList("IN", values);
335
+ else if (!Array.isArray(values)) return { [inQueryCondition]: {
336
+ column: this,
337
+ query: values
338
+ } };
289
339
  else if (values.includes(null)) return { or: [this.addCondition({ kind: "is-null" }), this.addCondition({
290
340
  kind: "in",
291
341
  value: values.filter((v) => v !== null)
@@ -353,6 +403,36 @@ var EntityColumnImpl = class extends TableColumn {
353
403
  });
354
404
  }
355
405
  };
406
+ /** Converts a physical ID to SQL text so nested SQL expressions also receive the tag. */
407
+ var TaggedIdExpr = class extends BaseExpr {
408
+ column;
409
+ constructor(column) {
410
+ super();
411
+ this.column = column;
412
+ }
413
+ toSql(ctx) {
414
+ const fragment = this.column.toSql(ctx);
415
+ const meta = this.column.idMetadata ?? fail("Tagged IDs require an ID column");
416
+ return {
417
+ ...fragment,
418
+ sql: `(? || ${fragment.sql}::text)`,
419
+ bindings: [taggedIdPrefix(meta), ...fragment.bindings]
420
+ };
421
+ }
422
+ get outputType() {
423
+ return {
424
+ dbType: "text",
425
+ domain: String,
426
+ arrayElementSafe: true
427
+ };
428
+ }
429
+ get sqlNullable() {
430
+ return this.column.sqlNullable;
431
+ }
432
+ get sqlSource() {
433
+ return this.column.sqlSource;
434
+ }
435
+ };
356
436
  var PolyReferenceImpl = class {
357
437
  meta;
358
438
  mgmt;
@@ -581,7 +661,7 @@ var ManyToManyJoinImpl = class extends CollectionJoinImpl {
581
661
  };
582
662
  /** Fails fast when a join factory is passed something other than a table. */
583
663
  function requireTable(other) {
584
- if (!isTable(other)) return fail(`Expected a table to join, got ${other}`);
664
+ if (!isEntityTable(other)) return fail(`Expected an entity table to join, got ${other}`);
585
665
  return other;
586
666
  }
587
667
  /** The id column of a joined table, for building sugar ON conditions. */
@@ -675,6 +755,17 @@ function tableWhere(mgmt, filter) {
675
755
  }
676
756
  return conditions.length === 0 ? skipCondition : { and: conditions };
677
757
  }
758
+ /**
759
+ * Converts relationship filters into conditions on the joined table's id column.
760
+ * I.e. Author's `join: { books: "b:1" }` filters Book.id to 1; `{ books: book }` uses book.id.
761
+ * Lists match any supplied ID, `true` requires a Book, and `null` or `false` requires no Book.
762
+ */
763
+ function tableEntityWhere(mgmt, value) {
764
+ const parsed = parseEntityFilter(mgmt.meta, value);
765
+ if (!parsed) return skipCondition;
766
+ if (parsed.kind === "join") fail(`Expected an entity or ID filter for ${mgmt.meta.type}`);
767
+ return new TableColumn(mgmt.meta, mgmt.meta.fields.id.serde.columns[0].column, mgmt).addCondition(parsed);
768
+ }
678
769
  /** Rejects nested relations, scopes, and aliases before parsing owning-reference values. */
679
770
  function isTableReferenceFilter(value) {
680
771
  if (value == null || typeof value === "boolean" || isTableReferenceValue(value)) return true;
@@ -690,6 +781,6 @@ function isTableReferenceValue(value) {
690
781
  return typeof value === "string" || typeof value === "number" || isEntity(value);
691
782
  }
692
783
  //#endregion
693
- export { JoinTableHandle, collectionJoin, getTableMetadata, getTableMgmt, isTable, m2mJoinTable, newTableProxy, referenceJoinSource, table, tableMgmt, tables };
784
+ export { JoinTableHandle, collectionJoin, getTableMetadata, getTableMgmt, inQueryCondition, isCustomTable, isEntityTable, isTable, m2mJoinTable, newTableProxy, referenceJoinSource, table, tableEntityWhere, tableMgmt, tableSqlName, tableWhere, tables };
694
785
 
695
786
  //# sourceMappingURL=Tables.js.map