joist-core 2.2.0-next.4 → 2.2.0-next.40

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 (169) hide show
  1. package/build/Aliases.d.ts.map +1 -1
  2. package/build/Aliases.js.map +1 -1
  3. package/build/BaseEntity.d.ts +2 -0
  4. package/build/BaseEntity.d.ts.map +1 -1
  5. package/build/BaseEntity.js +1 -1
  6. package/build/BaseEntity.js.map +1 -1
  7. package/build/Entity.d.ts +1 -0
  8. package/build/Entity.d.ts.map +1 -1
  9. package/build/EntityFilter.d.ts +27 -8
  10. package/build/EntityFilter.d.ts.map +1 -1
  11. package/build/EntityFilter.js +18 -0
  12. package/build/EntityFilter.js.map +1 -1
  13. package/build/EntityManager.d.ts +14 -10
  14. package/build/EntityManager.d.ts.map +1 -1
  15. package/build/EntityManager.js +184 -90
  16. package/build/EntityManager.js.map +1 -1
  17. package/build/EntityMetadata.d.ts +20 -1
  18. package/build/EntityMetadata.d.ts.map +1 -1
  19. package/build/EntityMetadata.js +13 -0
  20. package/build/EntityMetadata.js.map +1 -1
  21. package/build/IndexManager.d.ts +5 -5
  22. package/build/IndexManager.d.ts.map +1 -1
  23. package/build/IndexManager.js +48 -32
  24. package/build/IndexManager.js.map +1 -1
  25. package/build/InstanceData.d.ts +16 -2
  26. package/build/InstanceData.d.ts.map +1 -1
  27. package/build/InstanceData.js +36 -3
  28. package/build/InstanceData.js.map +1 -1
  29. package/build/IsLoadedCache.d.ts.map +1 -1
  30. package/build/IsLoadedCache.js +31 -25
  31. package/build/IsLoadedCache.js.map +1 -1
  32. package/build/JoinRows.js +1 -1
  33. package/build/JoinRows.js.map +1 -1
  34. package/build/QueryParser.d.ts +6 -1
  35. package/build/QueryParser.d.ts.map +1 -1
  36. package/build/QueryParser.js +265 -190
  37. package/build/QueryParser.js.map +1 -1
  38. package/build/QueryParser.test.d.ts +2 -0
  39. package/build/QueryParser.test.d.ts.map +1 -0
  40. package/build/QueryParser.test.js +16 -0
  41. package/build/QueryParser.test.js.map +1 -0
  42. package/build/ReactionsManager.d.ts +2 -1
  43. package/build/ReactionsManager.d.ts.map +1 -1
  44. package/build/ReactionsManager.js +53 -49
  45. package/build/ReactionsManager.js.map +1 -1
  46. package/build/batchloaders/BatchLoader.d.ts.map +1 -1
  47. package/build/batchloaders/BatchLoader.js +6 -1
  48. package/build/batchloaders/BatchLoader.js.map +1 -1
  49. package/build/batchloaders/manyToManyBatchLoader.d.ts.map +1 -1
  50. package/build/batchloaders/manyToManyBatchLoader.js +1 -1
  51. package/build/batchloaders/manyToManyBatchLoader.js.map +1 -1
  52. package/build/batchloaders/populateBatchLoader.d.ts.map +1 -1
  53. package/build/batchloaders/populateBatchLoader.js +26 -8
  54. package/build/batchloaders/populateBatchLoader.js.map +1 -1
  55. package/build/batchloaders/recursiveChildrenBatchLoader.d.ts.map +1 -1
  56. package/build/batchloaders/recursiveChildrenBatchLoader.js +2 -0
  57. package/build/batchloaders/recursiveChildrenBatchLoader.js.map +1 -1
  58. package/build/batchloaders/recursiveM2mBatchLoader.d.ts.map +1 -1
  59. package/build/batchloaders/recursiveM2mBatchLoader.js +1 -3
  60. package/build/batchloaders/recursiveM2mBatchLoader.js.map +1 -1
  61. package/build/batchloaders/recursiveParentsBatchLoader.d.ts.map +1 -1
  62. package/build/batchloaders/recursiveParentsBatchLoader.js +2 -0
  63. package/build/batchloaders/recursiveParentsBatchLoader.js.map +1 -1
  64. package/build/configure.d.ts +5 -3
  65. package/build/configure.d.ts.map +1 -1
  66. package/build/configure.js +70 -0
  67. package/build/configure.js.map +1 -1
  68. package/build/dataloaders/fastWhereFilterHash.d.ts +15 -0
  69. package/build/dataloaders/fastWhereFilterHash.d.ts.map +1 -0
  70. package/build/dataloaders/fastWhereFilterHash.js +164 -0
  71. package/build/dataloaders/fastWhereFilterHash.js.map +1 -0
  72. package/build/dataloaders/fastWhereFilterHash.test.d.ts +2 -0
  73. package/build/dataloaders/fastWhereFilterHash.test.d.ts.map +1 -0
  74. package/build/dataloaders/fastWhereFilterHash.test.js +59 -0
  75. package/build/dataloaders/fastWhereFilterHash.test.js.map +1 -0
  76. package/build/dataloaders/findCountDataLoader.d.ts +12 -2
  77. package/build/dataloaders/findCountDataLoader.d.ts.map +1 -1
  78. package/build/dataloaders/findCountDataLoader.js +46 -4
  79. package/build/dataloaders/findCountDataLoader.js.map +1 -1
  80. package/build/dataloaders/findDataLoader.d.ts +14 -1
  81. package/build/dataloaders/findDataLoader.d.ts.map +1 -1
  82. package/build/dataloaders/findDataLoader.js +53 -44
  83. package/build/dataloaders/findDataLoader.js.map +1 -1
  84. package/build/dataloaders/findIdsDataLoader.d.ts.map +1 -1
  85. package/build/dataloaders/findIdsDataLoader.js +5 -3
  86. package/build/dataloaders/findIdsDataLoader.js.map +1 -1
  87. package/build/dataloaders/findOrCreateDataLoader.d.ts.map +1 -1
  88. package/build/dataloaders/findOrCreateDataLoader.js +3 -3
  89. package/build/dataloaders/findOrCreateDataLoader.js.map +1 -1
  90. package/build/dataloaders/findPaginatedDataLoader.d.ts.map +1 -1
  91. package/build/dataloaders/findPaginatedDataLoader.js +11 -5
  92. package/build/dataloaders/findPaginatedDataLoader.js.map +1 -1
  93. package/build/drivers/EntityWriter.js +13 -7
  94. package/build/drivers/EntityWriter.js.map +1 -1
  95. package/build/fields.d.ts.map +1 -1
  96. package/build/fields.js +16 -6
  97. package/build/fields.js.map +1 -1
  98. package/build/index.d.ts +2 -0
  99. package/build/index.d.ts.map +1 -1
  100. package/build/index.js +4 -1
  101. package/build/index.js.map +1 -1
  102. package/build/keys.d.ts.map +1 -1
  103. package/build/keys.js +8 -6
  104. package/build/keys.js.map +1 -1
  105. package/build/loadHints.d.ts +9 -0
  106. package/build/loadHints.d.ts.map +1 -1
  107. package/build/loadHints.js +63 -9
  108. package/build/loadHints.js.map +1 -1
  109. package/build/loadLens.d.ts +11 -0
  110. package/build/loadLens.d.ts.map +1 -1
  111. package/build/loadLens.js +43 -10
  112. package/build/loadLens.js.map +1 -1
  113. package/build/newEntity.d.ts.map +1 -1
  114. package/build/newEntity.js +16 -14
  115. package/build/newEntity.js.map +1 -1
  116. package/build/normalizeHints.d.ts +7 -0
  117. package/build/normalizeHints.d.ts.map +1 -1
  118. package/build/normalizeHints.js +40 -2
  119. package/build/normalizeHints.js.map +1 -1
  120. package/build/relations/ManyToManyCollection.d.ts.map +1 -1
  121. package/build/relations/ManyToManyCollection.js +2 -2
  122. package/build/relations/ManyToManyCollection.js.map +1 -1
  123. package/build/relations/ManyToManyLargeCollection.d.ts.map +1 -1
  124. package/build/relations/ManyToManyLargeCollection.js +2 -1
  125. package/build/relations/ManyToManyLargeCollection.js.map +1 -1
  126. package/build/relations/OneToManyCollection.d.ts.map +1 -1
  127. package/build/relations/OneToManyCollection.js +2 -2
  128. package/build/relations/OneToManyCollection.js.map +1 -1
  129. package/build/relations/OneToManyLargeCollection.d.ts.map +1 -1
  130. package/build/relations/OneToManyLargeCollection.js +2 -1
  131. package/build/relations/OneToManyLargeCollection.js.map +1 -1
  132. package/build/relations/ReactiveManyToMany.d.ts.map +1 -1
  133. package/build/relations/ReactiveManyToMany.js +1 -1
  134. package/build/relations/ReactiveManyToMany.js.map +1 -1
  135. package/build/relations/ReactiveManyToManyOtherSide.d.ts.map +1 -1
  136. package/build/relations/ReactiveManyToManyOtherSide.js +1 -1
  137. package/build/relations/ReactiveManyToManyOtherSide.js.map +1 -1
  138. package/build/relations/RecursiveCollection.d.ts +2 -6
  139. package/build/relations/RecursiveCollection.d.ts.map +1 -1
  140. package/build/relations/RecursiveCollection.js +9 -17
  141. package/build/relations/RecursiveCollection.js.map +1 -1
  142. package/build/relations/RecursiveCycleError.d.ts +13 -0
  143. package/build/relations/RecursiveCycleError.d.ts.map +1 -0
  144. package/build/relations/RecursiveCycleError.js +15 -0
  145. package/build/relations/RecursiveCycleError.js.map +1 -0
  146. package/build/relations/hasOneThrough.d.ts.map +1 -1
  147. package/build/relations/hasOneThrough.js +6 -4
  148. package/build/relations/hasOneThrough.js.map +1 -1
  149. package/build/relations/hasProperty.d.ts +2 -1
  150. package/build/relations/hasProperty.d.ts.map +1 -1
  151. package/build/relations/hasProperty.js +15 -5
  152. package/build/relations/hasProperty.js.map +1 -1
  153. package/build/scopes.d.ts +185 -0
  154. package/build/scopes.d.ts.map +1 -0
  155. package/build/scopes.js +302 -0
  156. package/build/scopes.js.map +1 -0
  157. package/build/temporal.d.ts +32 -2
  158. package/build/temporal.d.ts.map +1 -1
  159. package/build/temporal.js +18 -0
  160. package/build/temporal.js.map +1 -1
  161. package/build/utils.d.ts +2 -0
  162. package/build/utils.d.ts.map +1 -1
  163. package/build/utils.js +12 -0
  164. package/build/utils.js.map +1 -1
  165. package/package.json +3 -5
  166. package/build/caches.d.ts +0 -6
  167. package/build/caches.d.ts.map +0 -1
  168. package/build/caches.js +0 -42
  169. package/build/caches.js.map +0 -1
@@ -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,202 +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 = meta.allFields[key] ??
129
- meta.polyComponentFields?.[key] ??
130
- (0, utils_1.fail)(`Field '${key}' not found on ${meta.tableName}`);
131
- const fa = `${alias}${field.aliasSuffix}`;
132
- if (field.kind === "primitive" || field.kind === "primaryKey" || field.kind === "enum") {
133
- const column = field.serde.columns[0];
134
- parseValueFilter(ef.subFilter[key]).forEach((filter) => {
135
- cb.addValueFilter(fa, column, filter);
136
- });
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);
137
154
  }
138
- else if (field.kind === "m2o") {
139
- const column = field.serde.columns[0];
140
- const sub = ef.subFilter[key];
141
- const joinKind = field.required && join !== "outer" ? "inner" : "outer";
142
- if ((0, Aliases_1.isAlias)(sub)) {
143
- const a = getAlias(field.otherMetadata().tableName);
144
- addTable(field.otherMetadata(), a, joinKind, (0, keywords_1.kqDot)(fa, column.columnName), (0, keywords_1.kqDot)(a, "id"), sub);
145
- }
146
- const f = parseEntityFilter(field.otherMetadata(), sub);
147
- // Probe the filter and see if it's just an id (...and not soft deleted), if so we can avoid the join
148
- if (!f) {
149
- // skip
150
- }
151
- else if (f.kind === "join" || filterSoftDeletes(field.otherMetadata(), softDeletes)) {
152
- const a = getAlias(field.otherMetadata().tableName);
153
- addTable(field.otherMetadata(), a, joinKind, (0, keywords_1.kqDot)(fa, column.columnName), (0, keywords_1.kqDot)(a, "id"), sub);
154
- }
155
- else {
156
- cb.addValueFilter(fa, column, f);
157
- }
155
+ else {
156
+ const conditions = Array.isArray(result) ? result : [result];
157
+ if (conditions.length > 0)
158
+ targetCb.maybeAddExpression({ and: conditions });
158
159
  }
159
- else if (field.kind === "poly") {
160
- const f = parseEntityFilter(meta, ef.subFilter[key]);
161
- if (!f) {
162
- // skip
163
- }
164
- else if (f.kind === "join") {
165
- 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);
166
226
  }
167
- else {
168
- // We're left with basically a ValueFilter against the ids
169
- // For now only support eq/ne/in/is-null
170
- if (f.kind === "eq" || f.kind === "ne") {
171
- if (isNilIdValue(f.value))
172
- return;
173
- const comp = field.components.find((p) => {
174
- const otherMeta = p.otherMetadata();
175
- const cstr = (0, index_1.getConstructorFromTaggedId)(f.value);
176
- // tagged ids from subclasses always map to the base class, so we should compare to the base class if we don't directly match
177
- return otherMeta.cstr === cstr || otherMeta.baseType === cstr.name;
178
- });
179
- if (!comp)
180
- (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) => {
181
230
  const column = field.serde.columns.find((c) => c.columnName === comp.columnName);
182
- cb.addValueFilter(fa, column, f);
183
- }
184
- else if (f.kind === "is-null") {
185
- // Add a condition for every component--these can be AND-d with the rest of the simple/inline conditions
186
- field.components.forEach((comp) => {
187
- const column = field.serde.columns.find((c) => c.columnName === comp.columnName);
188
- cb.addSimpleCondition({
189
- kind: "column",
190
- alias: fa,
191
- column: comp.columnName,
192
- dbType: column.dbType,
193
- cond: f,
194
- });
231
+ targetCb.addSimpleCondition({
232
+ kind: "column",
233
+ alias: fa,
234
+ column: comp.columnName,
235
+ dbType: column.dbType,
236
+ cond: f,
195
237
  });
196
- }
197
- else if (f.kind === "not-null") {
198
- const conditions = field.components.map((comp) => {
199
- const column = field.serde.columns.find((c) => c.columnName === comp.columnName);
200
- return {
201
- kind: "column",
202
- alias: fa,
203
- column: comp.columnName,
204
- dbType: column.dbType,
205
- cond: { kind: "not-null" },
206
- };
207
- });
208
- cb.addParsedExpression({ kind: "exp", op: "or", conditions });
209
- }
210
- else if (f.kind === "in") {
211
- // Split up the ids by constructor
212
- const idsByConstructor = (0, joist_utils_1.groupBy)(f.value, (id) => (0, index_1.getConstructorFromTaggedId)(id).name);
213
- // Or together `parent_book_id in (1,2,3) OR parent_author_id IN (4,5,6)`
214
- // ...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)`...
215
- const conditions = Object.entries(idsByConstructor).map(([cstrName, ids]) => {
216
- const column = field.serde.columns.find(
217
- // tagged ids from subclasses always map to the base class, so we should compare to the base class if we don't directly match
218
- (c) => c.otherMetadata().cstr.name === cstrName || c.otherMetadata().baseType === cstrName) ?? (0, utils_1.fail)(`Invalid tagged ids passed to ${meta.type}.${key}: ${ids}`);
219
- return {
220
- kind: "column",
221
- alias: fa,
222
- column: column.columnName,
223
- dbType: column.dbType,
224
- cond: mapToDb(column, { kind: "in", value: ids }),
225
- };
226
- });
227
- if (conditions.length > 0) {
228
- cb.addParsedExpression({ kind: "exp", op: "or", conditions });
229
- }
230
- }
231
- else {
232
- 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 });
233
272
  }
234
273
  }
274
+ else {
275
+ throw new Error(`Filters on polys for ${f.kind} are not supported`);
276
+ }
235
277
  }
236
- else if (field.kind === "o2o") {
237
- // We have to always join into o2os, i.e. we can't probe the filter like we do for m2os
238
- const otherMeta = field.otherMetadata();
239
- const a = getAlias(otherMeta.tableName);
240
- const otherField = otherMeta.allFields[field.otherFieldName];
241
- const otherColumn =
242
- // if our other is a poly, we need to find a matching column rather than just picking the first
243
- otherField.kind === "poly"
244
- ? otherField.components.find((c) => c.otherMetadata() === meta || c.otherMetadata() === (0, EntityMetadata_1.getBaseMeta)(meta)).columnName
245
- : otherField.serde.columns[0].columnName;
246
- 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;
247
300
  }
248
- else if (field.kind === "o2m") {
249
- const otherMeta = field.otherMetadata();
250
- const otherField = otherMeta.allFields[field.otherFieldName];
251
- let otherColumn = otherField.serde.columns[0].columnName;
252
- if (otherField.kind === "poly") {
253
- const otherComponent = otherField.components.find((c) => c.otherMetadata() === meta) ??
254
- (0, utils_1.fail)(`No poly component found for ${otherField.fieldName}`);
255
- 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" };
256
308
  }
257
- const isCtiBaseFk = otherMeta.inheritanceType === "cti" && field.otherFieldName && !(field.otherFieldName in otherMeta.fields);
258
- const a = getAlias(otherMeta.tableName);
259
- addTable(otherMeta, a, "outer", (0, keywords_1.kqDot)(alias, "id"), (0, keywords_1.kqDot)(a, otherColumn), ef.subFilter[key], field.otherFieldName);
260
- if (!isCtiBaseFk) {
261
- const table = tables.find((t) => t.alias === a);
262
- if (table && (table.join === "inner" || table.join === "outer")) {
263
- table.collection = { parentAlias: alias, rootAlias: a, kind: "o2m" };
264
- }
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" };
265
331
  }
266
332
  }
267
- else if (field.kind === "m2m") {
268
- const sub = ef.subFilter[key];
269
- const f = parseEntityFilter(field.otherMetadata(), sub);
270
- if (!f && !(0, Aliases_1.isAlias)(sub))
271
- return;
272
- const ja = getAlias(field.joinTableName);
273
- tables.push({
274
- 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",
275
346
  alias: ja,
276
- table: field.joinTableName,
277
- col1: (0, keywords_1.kqDot)(alias, "id"),
278
- col2: (0, keywords_1.kqDot)(ja, field.columnNames[0]),
279
- collection: { parentAlias: alias, rootAlias: ja, kind: "m2m" },
347
+ column: field.columnNames[1],
348
+ dbType: otherMeta.idDbType,
349
+ cond: mapToDb(column, f),
280
350
  });
281
- if ((0, Aliases_1.isAlias)(sub) || f?.kind === "join" || filterSoftDeletes(field.otherMetadata(), softDeletes)) {
282
- const a = getAlias(field.otherMetadata().tableName);
283
- addTable(field.otherMetadata(), a, "outer", (0, keywords_1.kqDot)(ja, field.columnNames[1]), (0, keywords_1.kqDot)(a, "id"), sub);
284
- const table = tables.find((t) => t.alias === a);
285
- if (table && (table.join === "inner" || table.join === "outer")) {
286
- table.collection = { parentAlias: ja, rootAlias: ja, kind: "m2m" };
287
- }
288
- }
289
- else if (f) {
290
- const otherMeta = field.otherMetadata();
291
- const column = {
292
- columnName: field.columnNames[1],
293
- dbType: otherMeta.idDbType,
294
- mapToDb(value) {
295
- return value === null || isNilIdValue(value)
296
- ? value
297
- : (0, index_1.keyToNumber)(otherMeta, (0, index_1.maybeResolveReferenceToId)(value));
298
- },
299
- };
300
- cb.addSimpleCondition({
301
- kind: "column",
302
- alias: ja,
303
- column: field.columnNames[1],
304
- dbType: otherMeta.idDbType,
305
- cond: mapToDb(column, f),
306
- });
307
- }
308
- }
309
- else {
310
- throw new Error(`Unsupported field ${key}`);
311
351
  }
312
- });
313
- }
314
- else if (ef) {
315
- const column = meta.fields["id"].serde.columns[0];
316
- cb.addValueFilter(alias, column, ef);
317
- }
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 });
318
368
  }
319
369
  function addOrderBy(meta, alias, orderBy) {
320
370
  const entries = Object.entries(orderBy);
@@ -446,6 +496,15 @@ function parseEntityFilter(meta, filter) {
446
496
  // We're just binding an alias to this position in the join tree
447
497
  return undefined;
448
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
+ }
449
508
  else if (filter === null) {
450
509
  return { kind: "is-null" };
451
510
  }
@@ -553,6 +612,13 @@ function nilIdValue(meta) {
553
612
  function isNilIdValue(value) {
554
613
  return value === -1 || value === "00000000-0000-0000-0000-000000000000";
555
614
  }
615
+ /** Finds a filter field by fieldName or generated fieldIdName. */
616
+ function findFilterField(meta, key) {
617
+ return (meta.allFields[key] ??
618
+ meta.polyComponentFields?.[key] ??
619
+ Object.values(meta.allFields).find((field) => field.fieldIdName === key) ??
620
+ Object.values(meta.polyComponentFields ?? {}).find((field) => field.fieldIdName === key));
621
+ }
556
622
  /**
557
623
  * Parses the many/hodgepodge (ergonomic!) patterns of value filters into a `ParsedValueFilter[]`.
558
624
  *
@@ -579,7 +645,10 @@ function parseValueFilter(filter) {
579
645
  else if (keys.length === 2 && "op" in filter && "value" in filter) {
580
646
  // Probe for `findGql` op & value
581
647
  const { op, value } = filter;
582
- if (value === null) {
648
+ if (shouldPruneValueFilter(op, value)) {
649
+ return [];
650
+ }
651
+ else if (value === null) {
583
652
  return [{ kind: "is-null" }];
584
653
  }
585
654
  else {
@@ -594,7 +663,7 @@ function parseValueFilter(filter) {
594
663
  return Object.entries(filter)
595
664
  .map(([key, value]) => {
596
665
  // Always do condition pruning on the value
597
- if (value === undefined) {
666
+ if (shouldPruneValueFilter(key, value)) {
598
667
  return undefined;
599
668
  }
600
669
  switch (key) {
@@ -859,4 +928,10 @@ function addStiSubtypeFilter(cb, subtypeMeta, alias) {
859
928
  function makeLike(search) {
860
929
  return search ? `%${search.replace(/\s+/g, "%")}%` : undefined;
861
930
  }
931
+ /** Returns true for values that should be treated like an omitted filter. */
932
+ function shouldPruneValueFilter(key, value) {
933
+ if (value === undefined)
934
+ return true;
935
+ return value === false && key !== "eq" && key !== "ne";
936
+ }
862
937
  //# sourceMappingURL=QueryParser.js.map