joist-core 2.2.0-next.38 → 2.2.0-next.39

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 (39) hide show
  1. package/build/Aliases.d.ts.map +1 -1
  2. package/build/Aliases.js.map +1 -1
  3. package/build/EntityFilter.d.ts +27 -8
  4. package/build/EntityFilter.d.ts.map +1 -1
  5. package/build/EntityFilter.js +18 -0
  6. package/build/EntityFilter.js.map +1 -1
  7. package/build/EntityManager.d.ts +9 -9
  8. package/build/EntityManager.d.ts.map +1 -1
  9. package/build/EntityManager.js +9 -5
  10. package/build/EntityManager.js.map +1 -1
  11. package/build/QueryParser.d.ts +6 -1
  12. package/build/QueryParser.d.ts.map +1 -1
  13. package/build/QueryParser.js +247 -186
  14. package/build/QueryParser.js.map +1 -1
  15. package/build/configure.d.ts +2 -0
  16. package/build/configure.d.ts.map +1 -1
  17. package/build/configure.js +5 -0
  18. package/build/configure.js.map +1 -1
  19. package/build/dataloaders/findCountDataLoader.d.ts +12 -2
  20. package/build/dataloaders/findCountDataLoader.d.ts.map +1 -1
  21. package/build/dataloaders/findCountDataLoader.js +15 -7
  22. package/build/dataloaders/findCountDataLoader.js.map +1 -1
  23. package/build/dataloaders/findDataLoader.d.ts +11 -0
  24. package/build/dataloaders/findDataLoader.d.ts.map +1 -1
  25. package/build/dataloaders/findDataLoader.js +12 -1
  26. package/build/dataloaders/findDataLoader.js.map +1 -1
  27. package/build/dataloaders/findIdsDataLoader.js +1 -1
  28. package/build/dataloaders/findIdsDataLoader.js.map +1 -1
  29. package/build/dataloaders/findPaginatedDataLoader.js +1 -1
  30. package/build/dataloaders/findPaginatedDataLoader.js.map +1 -1
  31. package/build/index.d.ts +1 -0
  32. package/build/index.d.ts.map +1 -1
  33. package/build/index.js +1 -0
  34. package/build/index.js.map +1 -1
  35. package/build/scopes.d.ts +185 -0
  36. package/build/scopes.d.ts.map +1 -0
  37. package/build/scopes.js +302 -0
  38. package/build/scopes.js.map +1 -0
  39. package/package.json +2 -2
@@ -5,11 +5,13 @@ exports.parseFindQuery = parseFindQuery;
5
5
  exports.maybeAddIdNotNulls = maybeAddIdNotNulls;
6
6
  exports.parseAlias = parseAlias;
7
7
  exports.parseEntityFilter = parseEntityFilter;
8
+ exports.findFilterField = findFilterField;
8
9
  exports.parseValueFilter = parseValueFilter;
9
10
  exports.mapToDb = mapToDb;
10
11
  exports.maybeAddOrderBy = maybeAddOrderBy;
11
12
  exports.addTablePerClassJoinsAndClassTag = addTablePerClassJoinsAndClassTag;
12
13
  exports.maybeAddNotSoftDeleted = maybeAddNotSoftDeleted;
14
+ exports.filterSoftDeletes = filterSoftDeletes;
13
15
  exports.getTables = getTables;
14
16
  exports.makeLike = makeLike;
15
17
  const joist_utils_1 = require("joist-utils");
@@ -21,6 +23,7 @@ const QueryVisitor_1 = require("./QueryVisitor");
21
23
  const configure_1 = require("./configure");
22
24
  const index_1 = require("./index");
23
25
  const keywords_1 = require("./keywords");
26
+ const scopes_1 = require("./scopes");
24
27
  const utils_1 = require("./utils");
25
28
  /** A marker condition for alias methods to indicate they should be skipped/pruned. */
26
29
  exports.skipCondition = {
@@ -50,10 +53,10 @@ function parseFindQuery(meta, filter, opts = {}) {
50
53
  aliases[abbrev] = i + 1;
51
54
  return i === 0 ? abbrev : `${abbrev}${i}`;
52
55
  }
53
- function addSoftDeleteCondition(meta, alias) {
56
+ function addSoftDeleteCondition(meta, alias, targetCb) {
54
57
  if (filterSoftDeletes(meta, softDeletes)) {
55
58
  const column = meta.allFields[(0, EntityMetadata_1.getBaseMeta)(meta).timestampFields.deletedAt].serde?.columns[0];
56
- cb.addSimpleCondition({
59
+ targetCb.addSimpleCondition({
57
60
  kind: "column",
58
61
  alias,
59
62
  column: column.columnName,
@@ -63,10 +66,11 @@ function parseFindQuery(meta, filter, opts = {}) {
63
66
  });
64
67
  }
65
68
  }
66
- function addTable(meta, alias, join, col1, col2, filter, fieldName) {
69
+ function addTable(meta, alias, join, col1, col2, filter, fieldName, targetCb = cb) {
67
70
  // look at filter, is it `{ book: "b2" }` or `{ book: { ... } }`
68
- const ef = parseEntityFilter(meta, filter);
69
- if (!ef && join !== "primary" && !(0, Aliases_1.isAlias)(filter)) {
71
+ const scopeFilter = (0, scopes_1.isScope)(filter);
72
+ const ef = scopeFilter ? undefined : parseEntityFilter(meta, filter);
73
+ if (!scopeFilter && !ef && join !== "primary" && !(0, Aliases_1.isAlias)(filter)) {
70
74
  return;
71
75
  }
72
76
  if (join === "primary") {
@@ -106,9 +110,9 @@ function parseFindQuery(meta, filter, opts = {}) {
106
110
  addTablePerClassJoinsAndClassTag({ selects, tables, orderBys }, meta, alias, false);
107
111
  }
108
112
  if (needsStiDiscriminator(meta)) {
109
- addStiSubtypeFilter(cb, meta, alias);
113
+ addStiSubtypeFilter(targetCb, meta, alias);
110
114
  }
111
- addSoftDeleteCondition(meta, alias);
115
+ addSoftDeleteCondition(meta, alias, targetCb);
112
116
  // The user's locally declared aliases, i.e. `const [a, b] = aliases(Author, Book)`,
113
117
  // aren't guaranteed to line up with the aliases we've assigned internally, like `a`
114
118
  // might actually be `a1` if there are two `authors` tables in the query, so push the
@@ -119,200 +123,248 @@ function parseFindQuery(meta, filter, opts = {}) {
119
123
  else if ((0, Aliases_1.isAlias)(filter)) {
120
124
  (0, Aliases_1.getAliasMgmt)(filter).setAlias(meta, alias);
121
125
  }
122
- if (ef && ef.kind === "join") {
123
- // subFilter really means we're matching against the entity columns/further joins
124
- Object.keys(ef.subFilter).forEach((key) => {
125
- // Skip the `{ as: ... }` alias binding
126
- if (key === "as")
127
- return;
128
- const field = findFilterField(meta, key) ?? (0, utils_1.fail)(`Field '${key}' not found on ${meta.tableName}`);
129
- const fa = `${alias}${field.aliasSuffix}`;
130
- if (field.kind === "primitive" || field.kind === "primaryKey" || field.kind === "enum") {
131
- const column = field.serde.columns[0];
132
- parseValueFilter(ef.subFilter[key]).forEach((filter) => {
133
- cb.addValueFilter(fa, column, filter);
134
- });
126
+ addFilterAt(meta, alias, filter, targetCb, ef, join);
127
+ }
128
+ function addFilterAt(meta, tableAlias, filter, targetCb, parsed = parseEntityFilter(meta, filter), parentJoin = "primary") {
129
+ if ((0, scopes_1.isScope)(filter)) {
130
+ addScopeFilterAt(meta, tableAlias, filter, targetCb, parentJoin);
131
+ }
132
+ else if (parsed && parsed.kind === "join") {
133
+ addSubFilter(meta, tableAlias, parsed.subFilter, targetCb, parentJoin);
134
+ }
135
+ else if (parsed) {
136
+ const column = meta.fields["id"].serde.columns[0];
137
+ targetCb.addValueFilter(tableAlias, column, parsed);
138
+ }
139
+ }
140
+ function addScopeFilterAt(meta, tableAlias, scope, targetCb, parentJoin) {
141
+ for (const fragment of (0, scopes_1.resolveScope)(scope).fragments) {
142
+ if (fragment.kind === "filter") {
143
+ addFilterAt(meta, tableAlias, fragment.filter, targetCb, undefined, parentJoin);
144
+ }
145
+ else {
146
+ const a = (0, Aliases_1.alias)(meta.cstr);
147
+ const result = fragment.fn(a);
148
+ (0, Aliases_1.getAliasMgmt)(a).setAlias(meta, tableAlias);
149
+ if ((0, scopes_1.isScopeJoinFilter)(result)) {
150
+ // Parse the join tree first so its `as:` bindings re-root the aliases that
151
+ // `conditions` reference, then add the conditions against the now-bound aliases.
152
+ addFilterAt(meta, tableAlias, result.where, targetCb, undefined, parentJoin);
153
+ targetCb.maybeAddExpression(result.conditions);
135
154
  }
136
- else if (field.kind === "m2o") {
137
- const column = field.serde.columns[0];
138
- const sub = ef.subFilter[key];
139
- const joinKind = field.required && join !== "outer" ? "inner" : "outer";
140
- if ((0, Aliases_1.isAlias)(sub)) {
141
- const a = getAlias(field.otherMetadata().tableName);
142
- addTable(field.otherMetadata(), a, joinKind, (0, keywords_1.kqDot)(fa, column.columnName), (0, keywords_1.kqDot)(a, "id"), sub);
143
- }
144
- const f = parseEntityFilter(field.otherMetadata(), sub);
145
- // Probe the filter and see if it's just an id (...and not soft deleted), if so we can avoid the join
146
- if (!f) {
147
- // skip
148
- }
149
- else if (f.kind === "join" || filterSoftDeletes(field.otherMetadata(), softDeletes)) {
150
- const a = getAlias(field.otherMetadata().tableName);
151
- addTable(field.otherMetadata(), a, joinKind, (0, keywords_1.kqDot)(fa, column.columnName), (0, keywords_1.kqDot)(a, "id"), sub);
152
- }
153
- else {
154
- cb.addValueFilter(fa, column, f);
155
- }
155
+ else {
156
+ const conditions = Array.isArray(result) ? result : [result];
157
+ if (conditions.length > 0)
158
+ targetCb.maybeAddExpression({ and: conditions });
156
159
  }
157
- else if (field.kind === "poly") {
158
- const f = parseEntityFilter(meta, ef.subFilter[key]);
159
- if (!f) {
160
- // skip
161
- }
162
- else if (f.kind === "join") {
163
- throw new Error("Joins through polys are not supported");
160
+ }
161
+ }
162
+ }
163
+ function addSubFilter(meta, tableAlias, subFilter, targetCb, parentJoin) {
164
+ // subFilter really means we're matching against the entity columns/further joins
165
+ Object.keys(subFilter).forEach((key) => {
166
+ // Skip the `{ as: ... }` alias binding
167
+ if (key === "as")
168
+ return;
169
+ if (key === "and" || key === "or") {
170
+ addLogicalFilter(meta, tableAlias, key, subFilter[key], targetCb, parentJoin);
171
+ return;
172
+ }
173
+ const field = findFilterField(meta, key) ?? (0, utils_1.fail)(`Field '${key}' not found on ${meta.tableName}`);
174
+ const fa = `${tableAlias}${field.aliasSuffix}`;
175
+ if (field.kind === "primitive" || field.kind === "primaryKey" || field.kind === "enum") {
176
+ const column = field.serde.columns[0];
177
+ parseValueFilter(subFilter[key]).forEach((filter) => {
178
+ targetCb.addValueFilter(fa, column, filter);
179
+ });
180
+ }
181
+ else if (field.kind === "m2o") {
182
+ const column = field.serde.columns[0];
183
+ const sub = subFilter[key];
184
+ const joinKind = field.required && parentJoin !== "outer" ? "inner" : "outer";
185
+ if ((0, Aliases_1.isAlias)(sub)) {
186
+ const a = getAlias(field.otherMetadata().tableName);
187
+ addTable(field.otherMetadata(), a, joinKind, (0, keywords_1.kqDot)(fa, column.columnName), (0, keywords_1.kqDot)(a, "id"), sub, undefined, targetCb);
188
+ }
189
+ const f = parseEntityFilter(field.otherMetadata(), sub);
190
+ // Probe the filter and see if it's just an id (...and not soft deleted), if so we can avoid the join
191
+ if (!f) {
192
+ // skip
193
+ }
194
+ else if (f.kind === "join" || filterSoftDeletes(field.otherMetadata(), softDeletes)) {
195
+ const a = getAlias(field.otherMetadata().tableName);
196
+ addTable(field.otherMetadata(), a, joinKind, (0, keywords_1.kqDot)(fa, column.columnName), (0, keywords_1.kqDot)(a, "id"), sub, undefined, targetCb);
197
+ }
198
+ else {
199
+ targetCb.addValueFilter(fa, column, f);
200
+ }
201
+ }
202
+ else if (field.kind === "poly") {
203
+ const f = parseEntityFilter(meta, subFilter[key]);
204
+ if (!f) {
205
+ // skip
206
+ }
207
+ else if (f.kind === "join") {
208
+ throw new Error("Joins through polys are not supported");
209
+ }
210
+ else {
211
+ // We're left with basically a ValueFilter against the ids
212
+ // For now only support eq/ne/in/is-null
213
+ if (f.kind === "eq" || f.kind === "ne") {
214
+ if (isNilIdValue(f.value))
215
+ return;
216
+ const comp = field.components.find((p) => {
217
+ const otherMeta = p.otherMetadata();
218
+ const cstr = (0, index_1.getConstructorFromTaggedId)(f.value);
219
+ // tagged ids from subclasses always map to the base class, so we should compare to the base class if we don't directly match
220
+ return otherMeta.cstr === cstr || otherMeta.baseType === cstr.name;
221
+ });
222
+ if (!comp)
223
+ (0, utils_1.fail)(`Invalid tagged id passed to ${meta.type}.${key}: ${f.value}`);
224
+ const column = field.serde.columns.find((c) => c.columnName === comp.columnName);
225
+ targetCb.addValueFilter(fa, column, f);
164
226
  }
165
- else {
166
- // We're left with basically a ValueFilter against the ids
167
- // For now only support eq/ne/in/is-null
168
- if (f.kind === "eq" || f.kind === "ne") {
169
- if (isNilIdValue(f.value))
170
- return;
171
- const comp = field.components.find((p) => {
172
- const otherMeta = p.otherMetadata();
173
- const cstr = (0, index_1.getConstructorFromTaggedId)(f.value);
174
- // tagged ids from subclasses always map to the base class, so we should compare to the base class if we don't directly match
175
- return otherMeta.cstr === cstr || otherMeta.baseType === cstr.name;
176
- });
177
- if (!comp)
178
- (0, utils_1.fail)(`Invalid tagged id passed to ${meta.type}.${key}: ${f.value}`);
227
+ else if (f.kind === "is-null") {
228
+ // Add a condition for every component--these can be AND-d with the rest of the simple/inline conditions
229
+ field.components.forEach((comp) => {
179
230
  const column = field.serde.columns.find((c) => c.columnName === comp.columnName);
180
- cb.addValueFilter(fa, column, f);
181
- }
182
- else if (f.kind === "is-null") {
183
- // Add a condition for every component--these can be AND-d with the rest of the simple/inline conditions
184
- field.components.forEach((comp) => {
185
- const column = field.serde.columns.find((c) => c.columnName === comp.columnName);
186
- cb.addSimpleCondition({
187
- kind: "column",
188
- alias: fa,
189
- column: comp.columnName,
190
- dbType: column.dbType,
191
- cond: f,
192
- });
193
- });
194
- }
195
- else if (f.kind === "not-null") {
196
- const conditions = field.components.map((comp) => {
197
- const column = field.serde.columns.find((c) => c.columnName === comp.columnName);
198
- return {
199
- kind: "column",
200
- alias: fa,
201
- column: comp.columnName,
202
- dbType: column.dbType,
203
- cond: { kind: "not-null" },
204
- };
205
- });
206
- cb.addParsedExpression({ kind: "exp", op: "or", conditions });
207
- }
208
- else if (f.kind === "in") {
209
- // Split up the ids by constructor
210
- const idsByConstructor = (0, joist_utils_1.groupBy)(f.value, (id) => (0, index_1.getConstructorFromTaggedId)(id).name);
211
- // Or together `parent_book_id in (1,2,3) OR parent_author_id IN (4,5,6)`
212
- // ...if there is a `parent IN [b:1, b:2, a:1, null]` we'd need to pull the `null` out and do an `OR (all columns are null)`...
213
- const conditions = Object.entries(idsByConstructor).map(([cstrName, ids]) => {
214
- const column = field.serde.columns.find(
215
- // tagged ids from subclasses always map to the base class, so we should compare to the base class if we don't directly match
216
- (c) => c.otherMetadata().cstr.name === cstrName || c.otherMetadata().baseType === cstrName) ?? (0, utils_1.fail)(`Invalid tagged ids passed to ${meta.type}.${key}: ${ids}`);
217
- return {
218
- kind: "column",
219
- alias: fa,
220
- column: column.columnName,
221
- dbType: column.dbType,
222
- cond: mapToDb(column, { kind: "in", value: ids }),
223
- };
231
+ targetCb.addSimpleCondition({
232
+ kind: "column",
233
+ alias: fa,
234
+ column: comp.columnName,
235
+ dbType: column.dbType,
236
+ cond: f,
224
237
  });
225
- if (conditions.length > 0) {
226
- cb.addParsedExpression({ kind: "exp", op: "or", conditions });
227
- }
228
- }
229
- else {
230
- throw new Error(`Filters on polys for ${f.kind} are not supported`);
238
+ });
239
+ }
240
+ else if (f.kind === "not-null") {
241
+ const conditions = field.components.map((comp) => {
242
+ const column = field.serde.columns.find((c) => c.columnName === comp.columnName);
243
+ return {
244
+ kind: "column",
245
+ alias: fa,
246
+ column: comp.columnName,
247
+ dbType: column.dbType,
248
+ cond: { kind: "not-null" },
249
+ };
250
+ });
251
+ targetCb.addParsedExpression({ kind: "exp", op: "or", conditions });
252
+ }
253
+ else if (f.kind === "in") {
254
+ // Split up the ids by constructor
255
+ const idsByConstructor = (0, joist_utils_1.groupBy)(f.value, (id) => (0, index_1.getConstructorFromTaggedId)(id).name);
256
+ // Or together `parent_book_id in (1,2,3) OR parent_author_id IN (4,5,6)`
257
+ // ...if there is a `parent IN [b:1, b:2, a:1, null]` we'd need to pull the `null` out and do an `OR (all columns are null)`...
258
+ const conditions = Object.entries(idsByConstructor).map(([cstrName, ids]) => {
259
+ const column = field.serde.columns.find(
260
+ // tagged ids from subclasses always map to the base class, so we should compare to the base class if we don't directly match
261
+ (c) => c.otherMetadata().cstr.name === cstrName || c.otherMetadata().baseType === cstrName) ?? (0, utils_1.fail)(`Invalid tagged ids passed to ${meta.type}.${key}: ${ids}`);
262
+ return {
263
+ kind: "column",
264
+ alias: fa,
265
+ column: column.columnName,
266
+ dbType: column.dbType,
267
+ cond: mapToDb(column, { kind: "in", value: ids }),
268
+ };
269
+ });
270
+ if (conditions.length > 0) {
271
+ targetCb.addParsedExpression({ kind: "exp", op: "or", conditions });
231
272
  }
232
273
  }
274
+ else {
275
+ throw new Error(`Filters on polys for ${f.kind} are not supported`);
276
+ }
233
277
  }
234
- else if (field.kind === "o2o") {
235
- // We have to always join into o2os, i.e. we can't probe the filter like we do for m2os
236
- const otherMeta = field.otherMetadata();
237
- const a = getAlias(otherMeta.tableName);
238
- const otherField = otherMeta.allFields[field.otherFieldName];
239
- const otherColumn =
240
- // if our other is a poly, we need to find a matching column rather than just picking the first
241
- otherField.kind === "poly"
242
- ? otherField.components.find((c) => c.otherMetadata() === meta || c.otherMetadata() === (0, EntityMetadata_1.getBaseMeta)(meta)).columnName
243
- : otherField.serde.columns[0].columnName;
244
- addTable(field.otherMetadata(), a, "outer", (0, keywords_1.kqDot)(alias, "id"), (0, keywords_1.kqDot)(a, otherColumn), ef.subFilter[key], field.otherFieldName);
278
+ }
279
+ else if (field.kind === "o2o") {
280
+ // We have to always join into o2os, i.e. we can't probe the filter like we do for m2os
281
+ const otherMeta = field.otherMetadata();
282
+ const a = getAlias(otherMeta.tableName);
283
+ const otherField = otherMeta.allFields[field.otherFieldName];
284
+ const otherColumn =
285
+ // if our other is a poly, we need to find a matching column rather than just picking the first
286
+ otherField.kind === "poly"
287
+ ? otherField.components.find((c) => c.otherMetadata() === meta || c.otherMetadata() === (0, EntityMetadata_1.getBaseMeta)(meta))
288
+ .columnName
289
+ : otherField.serde.columns[0].columnName;
290
+ addTable(field.otherMetadata(), a, "outer", (0, keywords_1.kqDot)(tableAlias, "id"), (0, keywords_1.kqDot)(a, otherColumn), subFilter[key], field.otherFieldName, targetCb);
291
+ }
292
+ else if (field.kind === "o2m") {
293
+ const otherMeta = field.otherMetadata();
294
+ const otherField = otherMeta.allFields[field.otherFieldName];
295
+ let otherColumn = otherField.serde.columns[0].columnName;
296
+ if (otherField.kind === "poly") {
297
+ const otherComponent = otherField.components.find((c) => c.otherMetadata() === meta) ??
298
+ (0, utils_1.fail)(`No poly component found for ${otherField.fieldName}`);
299
+ otherColumn = otherComponent.columnName;
245
300
  }
246
- else if (field.kind === "o2m") {
247
- const otherMeta = field.otherMetadata();
248
- const otherField = otherMeta.allFields[field.otherFieldName];
249
- let otherColumn = otherField.serde.columns[0].columnName;
250
- if (otherField.kind === "poly") {
251
- const otherComponent = otherField.components.find((c) => c.otherMetadata() === meta) ??
252
- (0, utils_1.fail)(`No poly component found for ${otherField.fieldName}`);
253
- otherColumn = otherComponent.columnName;
301
+ const isCtiBaseFk = otherMeta.inheritanceType === "cti" && field.otherFieldName && !(field.otherFieldName in otherMeta.fields);
302
+ const a = getAlias(otherMeta.tableName);
303
+ addTable(otherMeta, a, "outer", (0, keywords_1.kqDot)(tableAlias, "id"), (0, keywords_1.kqDot)(a, otherColumn), subFilter[key], field.otherFieldName, targetCb);
304
+ if (!isCtiBaseFk) {
305
+ const table = tables.find((t) => t.alias === a);
306
+ if (table && (table.join === "inner" || table.join === "outer")) {
307
+ table.collection = { parentAlias: tableAlias, rootAlias: a, kind: "o2m" };
254
308
  }
255
- const isCtiBaseFk = otherMeta.inheritanceType === "cti" && field.otherFieldName && !(field.otherFieldName in otherMeta.fields);
256
- const a = getAlias(otherMeta.tableName);
257
- addTable(otherMeta, a, "outer", (0, keywords_1.kqDot)(alias, "id"), (0, keywords_1.kqDot)(a, otherColumn), ef.subFilter[key], field.otherFieldName);
258
- if (!isCtiBaseFk) {
259
- const table = tables.find((t) => t.alias === a);
260
- if (table && (table.join === "inner" || table.join === "outer")) {
261
- table.collection = { parentAlias: alias, rootAlias: a, kind: "o2m" };
262
- }
309
+ }
310
+ }
311
+ else if (field.kind === "m2m") {
312
+ const sub = subFilter[key];
313
+ const f = parseEntityFilter(field.otherMetadata(), sub);
314
+ if (!f && !(0, Aliases_1.isAlias)(sub))
315
+ return;
316
+ const ja = getAlias(field.joinTableName);
317
+ tables.push({
318
+ join: "outer",
319
+ alias: ja,
320
+ table: field.joinTableName,
321
+ col1: (0, keywords_1.kqDot)(tableAlias, "id"),
322
+ col2: (0, keywords_1.kqDot)(ja, field.columnNames[0]),
323
+ collection: { parentAlias: tableAlias, rootAlias: ja, kind: "m2m" },
324
+ });
325
+ if ((0, Aliases_1.isAlias)(sub) || f?.kind === "join" || filterSoftDeletes(field.otherMetadata(), softDeletes)) {
326
+ const a = getAlias(field.otherMetadata().tableName);
327
+ addTable(field.otherMetadata(), a, "outer", (0, keywords_1.kqDot)(ja, field.columnNames[1]), (0, keywords_1.kqDot)(a, "id"), sub, undefined, targetCb);
328
+ const table = tables.find((t) => t.alias === a);
329
+ if (table && (table.join === "inner" || table.join === "outer")) {
330
+ table.collection = { parentAlias: ja, rootAlias: ja, kind: "m2m" };
263
331
  }
264
332
  }
265
- else if (field.kind === "m2m") {
266
- const sub = ef.subFilter[key];
267
- const f = parseEntityFilter(field.otherMetadata(), sub);
268
- if (!f && !(0, Aliases_1.isAlias)(sub))
269
- return;
270
- const ja = getAlias(field.joinTableName);
271
- tables.push({
272
- join: "outer",
333
+ else if (f) {
334
+ const otherMeta = field.otherMetadata();
335
+ const column = {
336
+ columnName: field.columnNames[1],
337
+ dbType: otherMeta.idDbType,
338
+ mapToDb(value) {
339
+ return value === null || isNilIdValue(value)
340
+ ? value
341
+ : (0, index_1.keyToNumber)(otherMeta, (0, index_1.maybeResolveReferenceToId)(value));
342
+ },
343
+ };
344
+ targetCb.addSimpleCondition({
345
+ kind: "column",
273
346
  alias: ja,
274
- table: field.joinTableName,
275
- col1: (0, keywords_1.kqDot)(alias, "id"),
276
- col2: (0, keywords_1.kqDot)(ja, field.columnNames[0]),
277
- collection: { parentAlias: alias, rootAlias: ja, kind: "m2m" },
347
+ column: field.columnNames[1],
348
+ dbType: otherMeta.idDbType,
349
+ cond: mapToDb(column, f),
278
350
  });
279
- if ((0, Aliases_1.isAlias)(sub) || f?.kind === "join" || filterSoftDeletes(field.otherMetadata(), softDeletes)) {
280
- const a = getAlias(field.otherMetadata().tableName);
281
- addTable(field.otherMetadata(), a, "outer", (0, keywords_1.kqDot)(ja, field.columnNames[1]), (0, keywords_1.kqDot)(a, "id"), sub);
282
- const table = tables.find((t) => t.alias === a);
283
- if (table && (table.join === "inner" || table.join === "outer")) {
284
- table.collection = { parentAlias: ja, rootAlias: ja, kind: "m2m" };
285
- }
286
- }
287
- else if (f) {
288
- const otherMeta = field.otherMetadata();
289
- const column = {
290
- columnName: field.columnNames[1],
291
- dbType: otherMeta.idDbType,
292
- mapToDb(value) {
293
- return value === null || isNilIdValue(value)
294
- ? value
295
- : (0, index_1.keyToNumber)(otherMeta, (0, index_1.maybeResolveReferenceToId)(value));
296
- },
297
- };
298
- cb.addSimpleCondition({
299
- kind: "column",
300
- alias: ja,
301
- column: field.columnNames[1],
302
- dbType: otherMeta.idDbType,
303
- cond: mapToDb(column, f),
304
- });
305
- }
306
351
  }
307
- else {
308
- throw new Error(`Unsupported field ${key}`);
309
- }
310
- });
311
- }
312
- else if (ef) {
313
- const column = meta.fields["id"].serde.columns[0];
314
- cb.addValueFilter(alias, column, ef);
315
- }
352
+ }
353
+ else {
354
+ throw new Error(`Unsupported field ${key}`);
355
+ }
356
+ });
357
+ }
358
+ function addLogicalFilter(meta, tableAlias, op, value, targetCb, parentJoin) {
359
+ const filters = Array.isArray(value) ? value : [value];
360
+ const conditions = filters.flatMap((filter) => {
361
+ const branchCb = new index_1.ConditionBuilder();
362
+ addFilterAt(meta, tableAlias, filter, branchCb, undefined, parentJoin);
363
+ const condition = branchCb.toExpressionFilter();
364
+ return condition ? [condition] : [];
365
+ });
366
+ if (conditions.length > 0)
367
+ targetCb.addParsedExpression({ kind: "exp", op, conditions });
316
368
  }
317
369
  function addOrderBy(meta, alias, orderBy) {
318
370
  const entries = Object.entries(orderBy);
@@ -444,6 +496,15 @@ function parseEntityFilter(meta, filter) {
444
496
  // We're just binding an alias to this position in the join tree
445
497
  return undefined;
446
498
  }
499
+ else if ((0, scopes_1.isScope)(filter)) {
500
+ // A scope on a relation (e.g. `{ author: Author.adult }`) always implies conditions on the
501
+ // related entity, so signal "needs a join" via the existing `join` discriminant rather than
502
+ // adding a dedicated `kind: "scope"` that every relation branch would have to learn. The empty
503
+ // `subFilter` is intentionally never read: the m2o/m2m branches only check `kind === "join"` to
504
+ // decide whether to join, then re-dispatch the original scope value back through `addTable`,
505
+ // where `isScope(...)` routes it to `addScopeFilterAt` to apply the scope's actual fragments.
506
+ return { kind: "join", subFilter: {} };
507
+ }
447
508
  else if (filter === null) {
448
509
  return { kind: "is-null" };
449
510
  }