@mikro-orm/core 7.0.10 → 7.0.11-dev.1

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 (206) hide show
  1. package/EntityManager.d.ts +584 -885
  2. package/EntityManager.js +1900 -1927
  3. package/MikroORM.d.ts +74 -103
  4. package/MikroORM.js +178 -177
  5. package/README.md +1 -1
  6. package/cache/CacheAdapter.d.ts +36 -36
  7. package/cache/FileCacheAdapter.d.ts +24 -30
  8. package/cache/FileCacheAdapter.js +78 -80
  9. package/cache/GeneratedCacheAdapter.d.ts +20 -18
  10. package/cache/GeneratedCacheAdapter.js +30 -30
  11. package/cache/MemoryCacheAdapter.d.ts +20 -18
  12. package/cache/MemoryCacheAdapter.js +36 -35
  13. package/cache/NullCacheAdapter.d.ts +16 -16
  14. package/cache/NullCacheAdapter.js +24 -24
  15. package/connections/Connection.d.ts +84 -95
  16. package/connections/Connection.js +168 -165
  17. package/drivers/DatabaseDriver.d.ts +80 -186
  18. package/drivers/DatabaseDriver.js +443 -450
  19. package/drivers/IDatabaseDriver.d.ts +301 -440
  20. package/entity/BaseEntity.d.ts +83 -120
  21. package/entity/BaseEntity.js +43 -43
  22. package/entity/Collection.d.ts +181 -215
  23. package/entity/Collection.js +724 -730
  24. package/entity/EntityAssigner.d.ts +77 -88
  25. package/entity/EntityAssigner.js +230 -231
  26. package/entity/EntityFactory.d.ts +55 -67
  27. package/entity/EntityFactory.js +416 -457
  28. package/entity/EntityHelper.d.ts +23 -35
  29. package/entity/EntityHelper.js +290 -302
  30. package/entity/EntityIdentifier.d.ts +4 -4
  31. package/entity/EntityIdentifier.js +10 -10
  32. package/entity/EntityLoader.d.ts +72 -98
  33. package/entity/EntityLoader.js +776 -805
  34. package/entity/EntityRepository.d.ts +201 -316
  35. package/entity/EntityRepository.js +213 -213
  36. package/entity/PolymorphicRef.d.ts +5 -5
  37. package/entity/PolymorphicRef.js +10 -10
  38. package/entity/Reference.d.ts +83 -127
  39. package/entity/Reference.js +277 -281
  40. package/entity/WrappedEntity.d.ts +72 -115
  41. package/entity/WrappedEntity.js +166 -168
  42. package/entity/defineEntity.d.ts +654 -1359
  43. package/entity/defineEntity.js +518 -527
  44. package/entity/utils.d.ts +3 -13
  45. package/entity/utils.js +73 -71
  46. package/entity/validators.js +43 -43
  47. package/entity/wrap.js +8 -8
  48. package/enums.d.ts +253 -258
  49. package/enums.js +252 -251
  50. package/errors.d.ts +72 -114
  51. package/errors.js +253 -350
  52. package/events/EventManager.d.ts +14 -26
  53. package/events/EventManager.js +77 -79
  54. package/events/EventSubscriber.d.ts +29 -29
  55. package/events/TransactionEventBroadcaster.d.ts +8 -15
  56. package/events/TransactionEventBroadcaster.js +14 -14
  57. package/exceptions.d.ts +40 -23
  58. package/exceptions.js +52 -35
  59. package/hydration/Hydrator.d.ts +17 -42
  60. package/hydration/Hydrator.js +43 -43
  61. package/hydration/ObjectHydrator.d.ts +17 -50
  62. package/hydration/ObjectHydrator.js +418 -483
  63. package/index.d.ts +2 -116
  64. package/index.js +1 -10
  65. package/logging/DefaultLogger.d.ts +32 -34
  66. package/logging/DefaultLogger.js +86 -86
  67. package/logging/Logger.d.ts +41 -41
  68. package/logging/SimpleLogger.d.ts +11 -13
  69. package/logging/SimpleLogger.js +22 -22
  70. package/logging/colors.d.ts +6 -6
  71. package/logging/colors.js +10 -11
  72. package/logging/inspect.js +7 -7
  73. package/metadata/EntitySchema.d.ts +130 -214
  74. package/metadata/EntitySchema.js +412 -411
  75. package/metadata/MetadataDiscovery.d.ts +114 -114
  76. package/metadata/MetadataDiscovery.js +1884 -1962
  77. package/metadata/MetadataProvider.d.ts +26 -29
  78. package/metadata/MetadataProvider.js +97 -95
  79. package/metadata/MetadataStorage.d.ts +32 -38
  80. package/metadata/MetadataStorage.js +118 -118
  81. package/metadata/MetadataValidator.d.ts +39 -39
  82. package/metadata/MetadataValidator.js +338 -381
  83. package/metadata/discover-entities.d.ts +2 -5
  84. package/metadata/discover-entities.js +37 -35
  85. package/metadata/types.d.ts +531 -615
  86. package/naming-strategy/AbstractNamingStrategy.d.ts +39 -54
  87. package/naming-strategy/AbstractNamingStrategy.js +85 -90
  88. package/naming-strategy/EntityCaseNamingStrategy.d.ts +6 -6
  89. package/naming-strategy/EntityCaseNamingStrategy.js +22 -22
  90. package/naming-strategy/MongoNamingStrategy.d.ts +6 -6
  91. package/naming-strategy/MongoNamingStrategy.js +18 -18
  92. package/naming-strategy/NamingStrategy.d.ts +99 -109
  93. package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
  94. package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
  95. package/not-supported.js +4 -7
  96. package/package.json +1 -1
  97. package/platforms/ExceptionConverter.d.ts +1 -1
  98. package/platforms/ExceptionConverter.js +4 -4
  99. package/platforms/Platform.d.ts +303 -312
  100. package/platforms/Platform.js +675 -695
  101. package/serialization/EntitySerializer.d.ts +26 -49
  102. package/serialization/EntitySerializer.js +218 -224
  103. package/serialization/EntityTransformer.d.ts +6 -10
  104. package/serialization/EntityTransformer.js +217 -219
  105. package/serialization/SerializationContext.d.ts +23 -27
  106. package/serialization/SerializationContext.js +105 -105
  107. package/types/ArrayType.d.ts +8 -8
  108. package/types/ArrayType.js +33 -33
  109. package/types/BigIntType.d.ts +10 -17
  110. package/types/BigIntType.js +37 -37
  111. package/types/BlobType.d.ts +3 -3
  112. package/types/BlobType.js +13 -13
  113. package/types/BooleanType.d.ts +4 -4
  114. package/types/BooleanType.js +12 -12
  115. package/types/CharacterType.d.ts +2 -2
  116. package/types/CharacterType.js +6 -6
  117. package/types/DateTimeType.d.ts +5 -5
  118. package/types/DateTimeType.js +15 -15
  119. package/types/DateType.d.ts +5 -5
  120. package/types/DateType.js +15 -15
  121. package/types/DecimalType.d.ts +7 -7
  122. package/types/DecimalType.js +26 -26
  123. package/types/DoubleType.d.ts +3 -3
  124. package/types/DoubleType.js +12 -12
  125. package/types/EnumArrayType.d.ts +5 -5
  126. package/types/EnumArrayType.js +24 -24
  127. package/types/EnumType.d.ts +3 -3
  128. package/types/EnumType.js +11 -11
  129. package/types/FloatType.d.ts +3 -3
  130. package/types/FloatType.js +9 -9
  131. package/types/IntegerType.d.ts +3 -3
  132. package/types/IntegerType.js +9 -9
  133. package/types/IntervalType.d.ts +4 -4
  134. package/types/IntervalType.js +12 -12
  135. package/types/JsonType.d.ts +8 -8
  136. package/types/JsonType.js +32 -32
  137. package/types/MediumIntType.d.ts +1 -1
  138. package/types/MediumIntType.js +3 -3
  139. package/types/SmallIntType.d.ts +3 -3
  140. package/types/SmallIntType.js +9 -9
  141. package/types/StringType.d.ts +4 -4
  142. package/types/StringType.js +12 -12
  143. package/types/TextType.d.ts +3 -3
  144. package/types/TextType.js +9 -9
  145. package/types/TimeType.d.ts +5 -5
  146. package/types/TimeType.js +17 -17
  147. package/types/TinyIntType.d.ts +3 -3
  148. package/types/TinyIntType.js +10 -10
  149. package/types/Type.d.ts +79 -83
  150. package/types/Type.js +82 -82
  151. package/types/Uint8ArrayType.d.ts +4 -4
  152. package/types/Uint8ArrayType.js +21 -21
  153. package/types/UnknownType.d.ts +4 -4
  154. package/types/UnknownType.js +12 -12
  155. package/types/UuidType.d.ts +5 -5
  156. package/types/UuidType.js +19 -19
  157. package/types/index.d.ts +49 -75
  158. package/types/index.js +26 -52
  159. package/typings.d.ts +741 -1254
  160. package/typings.js +233 -244
  161. package/unit-of-work/ChangeSet.d.ts +26 -26
  162. package/unit-of-work/ChangeSet.js +56 -56
  163. package/unit-of-work/ChangeSetComputer.d.ts +12 -12
  164. package/unit-of-work/ChangeSetComputer.js +179 -187
  165. package/unit-of-work/ChangeSetPersister.d.ts +50 -69
  166. package/unit-of-work/ChangeSetPersister.js +442 -465
  167. package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
  168. package/unit-of-work/CommitOrderCalculator.js +88 -89
  169. package/unit-of-work/IdentityMap.d.ts +31 -31
  170. package/unit-of-work/IdentityMap.js +105 -105
  171. package/unit-of-work/UnitOfWork.d.ts +141 -181
  172. package/unit-of-work/UnitOfWork.js +1223 -1237
  173. package/utils/AbstractMigrator.d.ts +91 -111
  174. package/utils/AbstractMigrator.js +275 -275
  175. package/utils/AbstractSchemaGenerator.d.ts +34 -43
  176. package/utils/AbstractSchemaGenerator.js +122 -121
  177. package/utils/AsyncContext.d.ts +3 -3
  178. package/utils/AsyncContext.js +35 -34
  179. package/utils/Configuration.d.ts +808 -852
  180. package/utils/Configuration.js +344 -359
  181. package/utils/Cursor.d.ts +22 -40
  182. package/utils/Cursor.js +127 -135
  183. package/utils/DataloaderUtils.d.ts +43 -58
  184. package/utils/DataloaderUtils.js +198 -203
  185. package/utils/EntityComparator.d.ts +82 -99
  186. package/utils/EntityComparator.js +737 -829
  187. package/utils/NullHighlighter.d.ts +1 -1
  188. package/utils/NullHighlighter.js +3 -3
  189. package/utils/QueryHelper.d.ts +51 -79
  190. package/utils/QueryHelper.js +364 -375
  191. package/utils/RawQueryFragment.d.ts +34 -50
  192. package/utils/RawQueryFragment.js +121 -122
  193. package/utils/RequestContext.d.ts +32 -32
  194. package/utils/RequestContext.js +53 -52
  195. package/utils/TransactionContext.d.ts +16 -16
  196. package/utils/TransactionContext.js +27 -27
  197. package/utils/TransactionManager.d.ts +58 -58
  198. package/utils/TransactionManager.js +200 -202
  199. package/utils/Utils.d.ts +145 -204
  200. package/utils/Utils.js +826 -826
  201. package/utils/clone.js +114 -105
  202. package/utils/env-vars.js +88 -90
  203. package/utils/fs-utils.d.ts +15 -15
  204. package/utils/fs-utils.js +182 -181
  205. package/utils/upsert-utils.d.ts +5 -20
  206. package/utils/upsert-utils.js +116 -114
@@ -6,395 +6,384 @@ import { helper } from '../entity/wrap.js';
6
6
  import { isRaw, Raw } from './RawQueryFragment.js';
7
7
  /** @internal */
8
8
  export class QueryHelper {
9
- static SUPPORTED_OPERATORS = ['>', '<', '<=', '>=', '!', '!='];
10
- /**
11
- * Finds the discriminator value (key) for a given entity class in a discriminator map.
12
- */
13
- static findDiscriminatorValue(discriminatorMap, targetClass) {
14
- return Object.entries(discriminatorMap).find(([, cls]) => cls === targetClass)?.[0];
15
- }
16
- static processParams(params) {
17
- if (Reference.isReference(params)) {
18
- params = params.unwrap();
19
- }
20
- if (Utils.isEntity(params)) {
21
- if (helper(params).__meta.compositePK) {
22
- return helper(params).__primaryKeys;
23
- }
24
- return helper(params).getPrimaryKey();
25
- }
26
- if (params === undefined) {
27
- return null;
28
- }
29
- if (Array.isArray(params)) {
30
- return params.map(item => QueryHelper.processParams(item));
31
- }
32
- if (Utils.isPlainObject(params)) {
33
- QueryHelper.processObjectParams(params);
9
+ static SUPPORTED_OPERATORS = ['>', '<', '<=', '>=', '!', '!='];
10
+ /**
11
+ * Finds the discriminator value (key) for a given entity class in a discriminator map.
12
+ */
13
+ static findDiscriminatorValue(discriminatorMap, targetClass) {
14
+ return Object.entries(discriminatorMap).find(([, cls]) => cls === targetClass)?.[0];
15
+ }
16
+ static processParams(params) {
17
+ if (Reference.isReference(params)) {
18
+ params = params.unwrap();
19
+ }
20
+ if (Utils.isEntity(params)) {
21
+ if (helper(params).__meta.compositePK) {
22
+ return helper(params).__primaryKeys;
23
+ }
24
+ return helper(params).getPrimaryKey();
25
+ }
26
+ if (params === undefined) {
27
+ return null;
28
+ }
29
+ if (Array.isArray(params)) {
30
+ return params.map(item => QueryHelper.processParams(item));
31
+ }
32
+ if (Utils.isPlainObject(params)) {
33
+ QueryHelper.processObjectParams(params);
34
+ }
35
+ return params;
34
36
  }
35
- return params;
36
- }
37
- static processObjectParams(params = {}) {
38
- Utils.getObjectQueryKeys(params).forEach(k => {
39
- params[k] = QueryHelper.processParams(params[k]);
40
- });
41
- return params;
42
- }
43
- /**
44
- * converts `{ account: { $or: [ [Object], [Object] ] } }`
45
- * to `{ $or: [ { account: [Object] }, { account: [Object] } ] }`
46
- */
47
- static liftGroupOperators(where, meta, metadata, key) {
48
- if (!Utils.isPlainObject(where)) {
49
- return undefined;
37
+ static processObjectParams(params = {}) {
38
+ Utils.getObjectQueryKeys(params).forEach(k => {
39
+ params[k] = QueryHelper.processParams(params[k]);
40
+ });
41
+ return params;
42
+ }
43
+ /**
44
+ * converts `{ account: { $or: [ [Object], [Object] ] } }`
45
+ * to `{ $or: [ { account: [Object] }, { account: [Object] } ] }`
46
+ */
47
+ static liftGroupOperators(where, meta, metadata, key) {
48
+ if (!Utils.isPlainObject(where)) {
49
+ return undefined;
50
+ }
51
+ const keys = Object.keys(where);
52
+ const groupOperator = keys.find(k => {
53
+ return (k in GroupOperator &&
54
+ Array.isArray(where[k]) &&
55
+ where[k].every(cond => {
56
+ return (Utils.isPlainObject(cond) &&
57
+ Object.keys(cond).every(k2 => {
58
+ if (Utils.isOperator(k2, false)) {
59
+ if (k2 === '$not') {
60
+ return Object.keys(cond[k2]).every(k3 => meta.primaryKeys.includes(k3));
61
+ }
62
+ /* v8 ignore next */
63
+ return true;
64
+ }
65
+ return meta.primaryKeys.includes(k2);
66
+ }));
67
+ }));
68
+ });
69
+ if (groupOperator) {
70
+ return groupOperator;
71
+ }
72
+ for (const k of keys) {
73
+ const value = where[k];
74
+ const prop = meta.properties[k];
75
+ // Polymorphic relations use multiple columns (discriminator + FK), so they cannot
76
+ // participate in the standard single-column FK expansion. Query by discriminator
77
+ // column directly instead, e.g. { likeableType: 'post', likeableId: 1 }.
78
+ if (!prop || ![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) || prop.polymorphic) {
79
+ continue;
80
+ }
81
+ const op = this.liftGroupOperators(value, prop.targetMeta, metadata, k);
82
+ if (op) {
83
+ delete where[k];
84
+ where[op] = value[op].map((v) => {
85
+ return { [k]: v };
86
+ });
87
+ }
88
+ }
89
+ return undefined;
50
90
  }
51
- const keys = Object.keys(where);
52
- const groupOperator = keys.find(k => {
53
- return (
54
- k in GroupOperator &&
55
- Array.isArray(where[k]) &&
56
- where[k].every(cond => {
57
- return (
58
- Utils.isPlainObject(cond) &&
59
- Object.keys(cond).every(k2 => {
60
- if (Utils.isOperator(k2, false)) {
61
- if (k2 === '$not') {
62
- return Object.keys(cond[k2]).every(k3 => meta.primaryKeys.includes(k3));
91
+ static inlinePrimaryKeyObjects(where, meta, metadata, key) {
92
+ if (Array.isArray(where)) {
93
+ where.forEach((item, i) => {
94
+ if (this.inlinePrimaryKeyObjects(item, meta, metadata, key)) {
95
+ where[i] = Utils.getPrimaryKeyValues(item, meta, false);
63
96
  }
64
- /* v8 ignore next */
65
- return true;
66
- }
67
- return meta.primaryKeys.includes(k2);
68
- })
69
- );
70
- })
71
- );
72
- });
73
- if (groupOperator) {
74
- return groupOperator;
75
- }
76
- for (const k of keys) {
77
- const value = where[k];
78
- const prop = meta.properties[k];
79
- // Polymorphic relations use multiple columns (discriminator + FK), so they cannot
80
- // participate in the standard single-column FK expansion. Query by discriminator
81
- // column directly instead, e.g. { likeableType: 'post', likeableId: 1 }.
82
- if (!prop || ![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) || prop.polymorphic) {
83
- continue;
84
- }
85
- const op = this.liftGroupOperators(value, prop.targetMeta, metadata, k);
86
- if (op) {
87
- delete where[k];
88
- where[op] = value[op].map(v => {
89
- return { [k]: v };
97
+ });
98
+ }
99
+ if (!Utils.isPlainObject(where) || (key && meta.properties[key]?.customType instanceof JsonType)) {
100
+ return false;
101
+ }
102
+ if (meta.primaryKeys.every(pk => pk in where) && Utils.getObjectKeysSize(where) === meta.primaryKeys.length) {
103
+ return (!!key &&
104
+ !GroupOperator[key] &&
105
+ key !== '$not' &&
106
+ Object.keys(where).every(k => !Utils.isPlainObject(where[k]) ||
107
+ Object.keys(where[k]).every(v => {
108
+ if (Utils.isOperator(v, false)) {
109
+ // multi-value operators (e.g. `$in`/`$nin`) cannot be inlined into a composite
110
+ // PK tuple — `getPrimaryKeyValues` would flatten the operator's array alongside
111
+ // the sibling PK values, producing a malformed `(col1, col2) IN ((a, b))` clause
112
+ return !meta.compositePK || !Array.isArray(where[k][v]);
113
+ }
114
+ if (meta.properties[k].primary &&
115
+ [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(meta.properties[k].kind)) {
116
+ return this.inlinePrimaryKeyObjects(where[k], meta.properties[k].targetMeta, metadata, v);
117
+ }
118
+ /* v8 ignore next */
119
+ return true;
120
+ })));
121
+ }
122
+ Object.keys(where).forEach(k => {
123
+ const meta2 = metadata.find(meta.properties[k]?.targetMeta?.class) || meta;
124
+ if (this.inlinePrimaryKeyObjects(where[k], meta2, metadata, k)) {
125
+ where[k] = Utils.getPrimaryKeyValues(where[k], meta2, true);
126
+ }
90
127
  });
91
- }
92
- }
93
- return undefined;
94
- }
95
- static inlinePrimaryKeyObjects(where, meta, metadata, key) {
96
- if (Array.isArray(where)) {
97
- where.forEach((item, i) => {
98
- if (this.inlinePrimaryKeyObjects(item, meta, metadata, key)) {
99
- where[i] = Utils.getPrimaryKeyValues(item, meta, false);
100
- }
101
- });
102
- }
103
- if (!Utils.isPlainObject(where) || (key && meta.properties[key]?.customType instanceof JsonType)) {
104
- return false;
105
- }
106
- if (meta.primaryKeys.every(pk => pk in where) && Utils.getObjectKeysSize(where) === meta.primaryKeys.length) {
107
- return (
108
- !!key &&
109
- !GroupOperator[key] &&
110
- key !== '$not' &&
111
- Object.keys(where).every(
112
- k =>
113
- !Utils.isPlainObject(where[k]) ||
114
- Object.keys(where[k]).every(v => {
115
- if (Utils.isOperator(v, false)) {
116
- // multi-value operators (e.g. `$in`/`$nin`) cannot be inlined into a composite
117
- // PK tuple `getPrimaryKeyValues` would flatten the operator's array alongside
118
- // the sibling PK values, producing a malformed `(col1, col2) IN ((a, b))` clause
119
- return !meta.compositePK || !Array.isArray(where[k][v]);
120
- }
121
- if (
122
- meta.properties[k].primary &&
123
- [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(meta.properties[k].kind)
124
- ) {
125
- return this.inlinePrimaryKeyObjects(where[k], meta.properties[k].targetMeta, metadata, v);
126
- }
127
- /* v8 ignore next */
128
- return true;
129
- }),
130
- )
131
- );
132
- }
133
- Object.keys(where).forEach(k => {
134
- const meta2 = metadata.find(meta.properties[k]?.targetMeta?.class) || meta;
135
- if (this.inlinePrimaryKeyObjects(where[k], meta2, metadata, k)) {
136
- where[k] = Utils.getPrimaryKeyValues(where[k], meta2, true);
137
- }
138
- });
139
- return false;
140
- }
141
- static processWhere(options) {
142
- // eslint-disable-next-line prefer-const
143
- let { where, entityName, metadata, platform, aliased = true, convertCustomTypes = true, root = true } = options;
144
- const meta = metadata.find(entityName);
145
- // inline PK-only objects in M:N queries, so we don't join the target entity when not needed
146
- if (meta && root) {
147
- QueryHelper.liftGroupOperators(where, meta, metadata);
148
- QueryHelper.inlinePrimaryKeyObjects(where, meta, metadata);
149
- }
150
- if (meta && root) {
151
- QueryHelper.convertCompositeEntityRefs(where, meta);
152
- }
153
- if (platform.getConfig().get('ignoreUndefinedInQuery') && where && typeof where === 'object') {
154
- Utils.dropUndefinedProperties(where);
155
- }
156
- where = QueryHelper.processParams(where) ?? {};
157
- /* v8 ignore next */
158
- if (!root && Utils.isPrimaryKey(where)) {
159
- return where;
160
- }
161
- if (meta && Utils.isPrimaryKey(where, meta.compositePK)) {
162
- where = { [Utils.getPrimaryKeyHash(meta.primaryKeys)]: where };
163
- }
164
- if (Array.isArray(where) && root) {
165
- const rootPrimaryKey = meta ? Utils.getPrimaryKeyHash(meta.primaryKeys) : Utils.className(entityName);
166
- let cond = { [rootPrimaryKey]: { $in: where } };
167
- // @ts-ignore
168
- // detect tuple comparison, use `$or` in case the number of constituents don't match
169
- if (
170
- meta &&
171
- !where.every(
172
- c =>
173
- Utils.isPrimaryKey(c) ||
174
- (Array.isArray(c) && c.length === meta.primaryKeys.length && c.every(i => Utils.isPrimaryKey(i))),
175
- )
176
- ) {
177
- cond = { $or: where };
178
- }
179
- return QueryHelper.processWhere({ ...options, where: cond, root: false });
180
- }
181
- if (!Utils.isPlainObject(where)) {
182
- return where;
183
- }
184
- return Utils.getObjectQueryKeys(where).reduce((o, key) => {
185
- let value = where[key];
186
- const customExpression = Raw.isKnownFragmentSymbol(key);
187
- if (Array.isArray(value) && value.length === 0 && customExpression) {
188
- o[key] = value;
189
- return o;
190
- }
191
- if (key in GroupOperator) {
192
- o[key] = value.map(sub => QueryHelper.processWhere({ ...options, where: sub, root }));
193
- return o;
194
- }
195
- // wrap top level operators (except platform allowed operators) with PK
196
- if (Utils.isOperator(key) && root && meta && !platform.isAllowedTopLevelOperator(key)) {
197
- const rootPrimaryKey = Utils.getPrimaryKeyHash(meta.primaryKeys);
198
- o[rootPrimaryKey] = { [key]: QueryHelper.processWhere({ ...options, where: value, root: false }) };
199
- return o;
200
- }
201
- const prop = customExpression ? null : this.findProperty(key, options);
202
- const keys = prop?.joinColumns?.length ?? 0;
203
- const composite = keys > 1;
204
- if (prop?.customType && convertCustomTypes && !isRaw(value)) {
205
- value = QueryHelper.processCustomType(prop, value, platform, undefined, true);
206
- }
207
- // oxfmt-ignore
208
- const isJsonProperty = prop?.customType instanceof JsonType && !isRaw(value) && (Utils.isPlainObject(value) ? !['$eq', '$elemMatch'].includes(Object.keys(value)[0]) : !Array.isArray(value));
209
- if (isJsonProperty && prop?.kind !== ReferenceKind.EMBEDDED) {
210
- return this.processJsonCondition(o, value, [prop.fieldNames[0]], platform, aliased);
211
- }
212
- // oxfmt-ignore
213
- if (Array.isArray(value) && !Utils.isOperator(key) && !QueryHelper.isSupportedOperator(key) && !(customExpression && Raw.getKnownFragment(key).params.length > 0) && options.type !== 'orderBy') {
128
+ return false;
129
+ }
130
+ static processWhere(options) {
131
+ // eslint-disable-next-line prefer-const
132
+ let { where, entityName, metadata, platform, aliased = true, convertCustomTypes = true, root = true } = options;
133
+ const meta = metadata.find(entityName);
134
+ // inline PK-only objects in M:N queries, so we don't join the target entity when not needed
135
+ if (meta && root) {
136
+ QueryHelper.liftGroupOperators(where, meta, metadata);
137
+ QueryHelper.inlinePrimaryKeyObjects(where, meta, metadata);
138
+ }
139
+ if (meta && root) {
140
+ QueryHelper.convertCompositeEntityRefs(where, meta);
141
+ }
142
+ if (platform.getConfig().get('ignoreUndefinedInQuery') && where && typeof where === 'object') {
143
+ Utils.dropUndefinedProperties(where);
144
+ }
145
+ where = QueryHelper.processParams(where) ?? {};
146
+ /* v8 ignore next */
147
+ if (!root && Utils.isPrimaryKey(where)) {
148
+ return where;
149
+ }
150
+ if (meta && Utils.isPrimaryKey(where, meta.compositePK)) {
151
+ where = { [Utils.getPrimaryKeyHash(meta.primaryKeys)]: where };
152
+ }
153
+ if (Array.isArray(where) && root) {
154
+ const rootPrimaryKey = meta ? Utils.getPrimaryKeyHash(meta.primaryKeys) : Utils.className(entityName);
155
+ let cond = { [rootPrimaryKey]: { $in: where } };
156
+ // @ts-ignore
157
+ // detect tuple comparison, use `$or` in case the number of constituents don't match
158
+ if (meta &&
159
+ !where.every(c => Utils.isPrimaryKey(c) ||
160
+ (Array.isArray(c) && c.length === meta.primaryKeys.length && c.every(i => Utils.isPrimaryKey(i))))) {
161
+ cond = { $or: where };
162
+ }
163
+ return QueryHelper.processWhere({ ...options, where: cond, root: false });
164
+ }
165
+ if (!Utils.isPlainObject(where)) {
166
+ return where;
167
+ }
168
+ return Utils.getObjectQueryKeys(where).reduce((o, key) => {
169
+ let value = where[key];
170
+ const customExpression = Raw.isKnownFragmentSymbol(key);
171
+ if (Array.isArray(value) && value.length === 0 && customExpression) {
172
+ o[key] = value;
173
+ return o;
174
+ }
175
+ if (key in GroupOperator) {
176
+ o[key] = value.map((sub) => QueryHelper.processWhere({ ...options, where: sub, root }));
177
+ return o;
178
+ }
179
+ // wrap top level operators (except platform allowed operators) with PK
180
+ if (Utils.isOperator(key) && root && meta && !platform.isAllowedTopLevelOperator(key)) {
181
+ const rootPrimaryKey = Utils.getPrimaryKeyHash(meta.primaryKeys);
182
+ o[rootPrimaryKey] = { [key]: QueryHelper.processWhere({ ...options, where: value, root: false }) };
183
+ return o;
184
+ }
185
+ const prop = customExpression ? null : this.findProperty(key, options);
186
+ const keys = prop?.joinColumns?.length ?? 0;
187
+ const composite = keys > 1;
188
+ if (prop?.customType && convertCustomTypes && !isRaw(value)) {
189
+ value = QueryHelper.processCustomType(prop, value, platform, undefined, true);
190
+ }
191
+ // oxfmt-ignore
192
+ const isJsonProperty = prop?.customType instanceof JsonType && !isRaw(value) && (Utils.isPlainObject(value) ? !['$eq', '$elemMatch'].includes(Object.keys(value)[0]) : !Array.isArray(value));
193
+ if (isJsonProperty && prop?.kind !== ReferenceKind.EMBEDDED) {
194
+ return this.processJsonCondition(o, value, [prop.fieldNames[0]], platform, aliased);
195
+ }
196
+ // oxfmt-ignore
197
+ if (Array.isArray(value) && !Utils.isOperator(key) && !QueryHelper.isSupportedOperator(key) && !(customExpression && Raw.getKnownFragment(key).params.length > 0) && options.type !== 'orderBy') {
214
198
  // comparing single composite key - use $eq instead of $in
215
199
  const op = composite && !value.every(v => Array.isArray(v)) ? '$eq' : '$in';
216
200
  o[key] = { [op]: value };
217
201
  return o;
218
202
  }
219
- if (Utils.isPlainObject(value)) {
220
- o[key] = QueryHelper.processWhere({
221
- ...options,
222
- where: value,
223
- entityName: prop?.targetMeta?.class ?? entityName,
224
- root: false,
225
- });
226
- } else {
227
- o[key] = value;
228
- }
229
- return o;
230
- }, {});
231
- }
232
- static getActiveFilters(meta, options, filters) {
233
- if (options === false) {
234
- return [];
235
- }
236
- const opts = {};
237
- if (Array.isArray(options)) {
238
- options.forEach(filter => (opts[filter] = true));
239
- } else if (Utils.isPlainObject(options)) {
240
- Object.keys(options).forEach(filter => (opts[filter] = options[filter]));
241
- }
242
- return Object.keys(filters)
243
- .filter(f => QueryHelper.isFilterActive(meta, f, filters[f], opts))
244
- .map(f => {
245
- filters[f].name = f;
246
- return filters[f];
247
- });
248
- }
249
- static mergePropertyFilters(propFilters, options) {
250
- if (!options || !propFilters || options === true || propFilters === true) {
251
- return options ?? propFilters;
252
- }
253
- if (Array.isArray(propFilters)) {
254
- propFilters = propFilters.reduce((o, item) => {
255
- o[item] = true;
256
- return o;
257
- }, {});
258
- }
259
- if (Array.isArray(options)) {
260
- options = options.reduce((o, item) => {
261
- o[item] = true;
262
- return o;
263
- }, {});
264
- }
265
- return Utils.mergeConfig({}, propFilters, options);
266
- }
267
- static isFilterActive(meta, filterName, filter, options) {
268
- if (filter.entity && !filter.entity.includes(meta.className)) {
269
- return false;
270
- }
271
- if (options[filterName] === false) {
272
- return false;
273
- }
274
- return filter.default || filterName in options;
275
- }
276
- static processCustomType(prop, cond, platform, key, fromQuery) {
277
- if (Utils.isPlainObject(cond)) {
278
- return Utils.getObjectQueryKeys(cond).reduce((o, k) => {
279
- if (!Raw.isKnownFragmentSymbol(k) && (Utils.isOperator(k, true) || prop.referencedPKs?.includes(k))) {
280
- o[k] = QueryHelper.processCustomType(prop, cond[k], platform, k, fromQuery);
281
- } else {
282
- o[k] = cond[k];
283
- }
284
- return o;
285
- }, {});
286
- }
287
- if (key && JSON_KEY_OPERATORS.includes(key)) {
288
- return Array.isArray(cond) ? platform.marshallArray(cond) : cond;
203
+ if (Utils.isPlainObject(value)) {
204
+ o[key] = QueryHelper.processWhere({
205
+ ...options,
206
+ where: value,
207
+ entityName: prop?.targetMeta?.class ?? entityName,
208
+ root: false,
209
+ });
210
+ }
211
+ else {
212
+ o[key] = value;
213
+ }
214
+ return o;
215
+ }, {});
289
216
  }
290
- if (Array.isArray(cond) && !(key && ARRAY_OPERATORS.includes(key))) {
291
- return cond.map(v => QueryHelper.processCustomType(prop, v, platform, key, fromQuery));
217
+ static getActiveFilters(meta, options, filters) {
218
+ if (options === false) {
219
+ return [];
220
+ }
221
+ const opts = {};
222
+ if (Array.isArray(options)) {
223
+ options.forEach(filter => (opts[filter] = true));
224
+ }
225
+ else if (Utils.isPlainObject(options)) {
226
+ Object.keys(options).forEach(filter => (opts[filter] = options[filter]));
227
+ }
228
+ return Object.keys(filters)
229
+ .filter(f => QueryHelper.isFilterActive(meta, f, filters[f], opts))
230
+ .map(f => {
231
+ filters[f].name = f;
232
+ return filters[f];
233
+ });
292
234
  }
293
- if (isRaw(cond)) {
294
- return cond;
235
+ static mergePropertyFilters(propFilters, options) {
236
+ if (!options || !propFilters || options === true || propFilters === true) {
237
+ return options ?? propFilters;
238
+ }
239
+ if (Array.isArray(propFilters)) {
240
+ propFilters = propFilters.reduce((o, item) => {
241
+ o[item] = true;
242
+ return o;
243
+ }, {});
244
+ }
245
+ if (Array.isArray(options)) {
246
+ options = options.reduce((o, item) => {
247
+ o[item] = true;
248
+ return o;
249
+ }, {});
250
+ }
251
+ return Utils.mergeConfig({}, propFilters, options);
295
252
  }
296
- return prop.customType.convertToDatabaseValue(cond, platform, { fromQuery, key, mode: 'query' });
297
- }
298
- static isSupportedOperator(key) {
299
- return !!QueryHelper.SUPPORTED_OPERATORS.find(op => key === op);
300
- }
301
- static processJsonCondition(o, value, path, platform, alias) {
302
- return platform.processJsonCondition(o, value, path, alias);
303
- }
304
- static findProperty(fieldName, options) {
305
- const parts = fieldName.split('.');
306
- const propName = parts.pop();
307
- const alias = parts.length > 0 ? parts.join('.') : undefined;
308
- const entityName = alias ? options.aliasMap?.[alias] : options.entityName;
309
- const meta = entityName ? options.metadata.find(entityName) : undefined;
310
- return meta?.properties[propName];
311
- }
312
- /**
313
- * Converts entity references for composite FK properties into flat arrays
314
- * of correctly-ordered join column values, before processParams flattens them
315
- * incorrectly due to shared FK columns.
316
- */
317
- static convertCompositeEntityRefs(where, meta) {
318
- if (!Utils.isPlainObject(where)) {
319
- return;
253
+ static isFilterActive(meta, filterName, filter, options) {
254
+ if (filter.entity && !filter.entity.includes(meta.className)) {
255
+ return false;
256
+ }
257
+ if (options[filterName] === false) {
258
+ return false;
259
+ }
260
+ return filter.default || filterName in options;
320
261
  }
321
- for (const k of Object.keys(where)) {
322
- if (k in GroupOperator) {
323
- if (Array.isArray(where[k])) {
324
- where[k].forEach(sub => this.convertCompositeEntityRefs(sub, meta));
325
- }
326
- continue;
327
- }
328
- if (k === '$not') {
329
- this.convertCompositeEntityRefs(where[k], meta);
330
- continue;
331
- }
332
- const prop = meta.properties[k];
333
- if (!prop?.joinColumns || prop.joinColumns.length <= 1) {
334
- continue;
335
- }
336
- const w = where[k];
337
- if (Utils.isEntity(w)) {
338
- where[k] = this.extractJoinColumnValues(w, prop);
339
- } else if (Utils.isPlainObject(w)) {
340
- for (const op of Object.keys(w)) {
341
- if (Utils.isOperator(op, false) && Array.isArray(w[op])) {
342
- w[op] = w[op].map(item => (Utils.isEntity(item) ? this.extractJoinColumnValues(item, prop) : item));
343
- }
344
- }
345
- }
262
+ static processCustomType(prop, cond, platform, key, fromQuery) {
263
+ if (Utils.isPlainObject(cond)) {
264
+ return Utils.getObjectQueryKeys(cond).reduce((o, k) => {
265
+ if (!Raw.isKnownFragmentSymbol(k) && (Utils.isOperator(k, true) || prop.referencedPKs?.includes(k))) {
266
+ o[k] = QueryHelper.processCustomType(prop, cond[k], platform, k, fromQuery);
267
+ }
268
+ else {
269
+ o[k] = cond[k];
270
+ }
271
+ return o;
272
+ }, {});
273
+ }
274
+ if (key && JSON_KEY_OPERATORS.includes(key)) {
275
+ return Array.isArray(cond) ? platform.marshallArray(cond) : cond;
276
+ }
277
+ if (Array.isArray(cond) && !(key && ARRAY_OPERATORS.includes(key))) {
278
+ return cond.map(v => QueryHelper.processCustomType(prop, v, platform, key, fromQuery));
279
+ }
280
+ if (isRaw(cond)) {
281
+ return cond;
282
+ }
283
+ return prop.customType.convertToDatabaseValue(cond, platform, { fromQuery, key, mode: 'query' });
284
+ }
285
+ static isSupportedOperator(key) {
286
+ return !!QueryHelper.SUPPORTED_OPERATORS.find(op => key === op);
287
+ }
288
+ static processJsonCondition(o, value, path, platform, alias) {
289
+ return platform.processJsonCondition(o, value, path, alias);
290
+ }
291
+ static findProperty(fieldName, options) {
292
+ const parts = fieldName.split('.');
293
+ const propName = parts.pop();
294
+ const alias = parts.length > 0 ? parts.join('.') : undefined;
295
+ const entityName = alias ? options.aliasMap?.[alias] : options.entityName;
296
+ const meta = entityName ? options.metadata.find(entityName) : undefined;
297
+ return meta?.properties[propName];
298
+ }
299
+ /**
300
+ * Converts entity references for composite FK properties into flat arrays
301
+ * of correctly-ordered join column values, before processParams flattens them
302
+ * incorrectly due to shared FK columns.
303
+ */
304
+ static convertCompositeEntityRefs(where, meta) {
305
+ if (!Utils.isPlainObject(where)) {
306
+ return;
307
+ }
308
+ for (const k of Object.keys(where)) {
309
+ if (k in GroupOperator) {
310
+ if (Array.isArray(where[k])) {
311
+ where[k].forEach((sub) => this.convertCompositeEntityRefs(sub, meta));
312
+ }
313
+ continue;
314
+ }
315
+ if (k === '$not') {
316
+ this.convertCompositeEntityRefs(where[k], meta);
317
+ continue;
318
+ }
319
+ const prop = meta.properties[k];
320
+ if (!prop?.joinColumns || prop.joinColumns.length <= 1) {
321
+ continue;
322
+ }
323
+ const w = where[k];
324
+ if (Utils.isEntity(w)) {
325
+ where[k] = this.extractJoinColumnValues(w, prop);
326
+ }
327
+ else if (Utils.isPlainObject(w)) {
328
+ for (const op of Object.keys(w)) {
329
+ if (Utils.isOperator(op, false) && Array.isArray(w[op])) {
330
+ w[op] = w[op].map((item) => Utils.isEntity(item) ? this.extractJoinColumnValues(item, prop) : item);
331
+ }
332
+ }
333
+ }
334
+ }
346
335
  }
347
- }
348
- /**
349
- * Extracts values for a FK's join columns from an entity by traversing the FK chain.
350
- * Handles shared FK columns (e.g., tenant_id referenced by multiple FKs) correctly.
351
- */
352
- static extractJoinColumnValues(entity, prop) {
353
- return prop.referencedColumnNames.map(refCol => {
354
- return this.extractColumnValue(entity, prop.targetMeta, refCol);
355
- });
356
- }
357
- /**
358
- * Extracts the value for a specific column from an entity by finding which PK property
359
- * owns that column and recursively traversing FK references.
360
- */
361
- static extractColumnValue(entity, meta, columnName) {
362
- for (const pk of meta.primaryKeys) {
363
- const pkProp = meta.properties[pk];
364
- const colIdx = pkProp.fieldNames.indexOf(columnName);
365
- if (colIdx !== -1) {
366
- const value = entity[pk];
367
- if (pkProp.targetMeta && Utils.isEntity(value, true)) {
368
- const refCol = pkProp.referencedColumnNames[colIdx];
369
- return this.extractColumnValue(value, pkProp.targetMeta, refCol);
370
- }
371
- return value;
372
- }
336
+ /**
337
+ * Extracts values for a FK's join columns from an entity by traversing the FK chain.
338
+ * Handles shared FK columns (e.g., tenant_id referenced by multiple FKs) correctly.
339
+ */
340
+ static extractJoinColumnValues(entity, prop) {
341
+ return prop.referencedColumnNames.map(refCol => {
342
+ return this.extractColumnValue(entity, prop.targetMeta, refCol);
343
+ });
373
344
  }
374
- return null;
375
- }
376
- /**
377
- * Merges multiple orderBy sources with key-level deduplication (first-seen key wins).
378
- * RawQueryFragment symbol keys are never deduped (each is unique).
379
- */
380
- static mergeOrderBy(...sources) {
381
- const result = [];
382
- const seenKeys = new Set();
383
- for (const source of sources) {
384
- if (source == null) {
385
- continue;
386
- }
387
- for (const item of Utils.asArray(source)) {
388
- for (const key of Utils.getObjectQueryKeys(item)) {
389
- if (typeof key === 'symbol') {
390
- result.push({ [key]: item[key] });
391
- } else if (!seenKeys.has(key)) {
392
- seenKeys.add(key);
393
- result.push({ [key]: item[key] });
394
- }
395
- }
396
- }
345
+ /**
346
+ * Extracts the value for a specific column from an entity by finding which PK property
347
+ * owns that column and recursively traversing FK references.
348
+ */
349
+ static extractColumnValue(entity, meta, columnName) {
350
+ for (const pk of meta.primaryKeys) {
351
+ const pkProp = meta.properties[pk];
352
+ const colIdx = pkProp.fieldNames.indexOf(columnName);
353
+ if (colIdx !== -1) {
354
+ const value = entity[pk];
355
+ if (pkProp.targetMeta && Utils.isEntity(value, true)) {
356
+ const refCol = pkProp.referencedColumnNames[colIdx];
357
+ return this.extractColumnValue(value, pkProp.targetMeta, refCol);
358
+ }
359
+ return value;
360
+ }
361
+ }
362
+ return null;
363
+ }
364
+ /**
365
+ * Merges multiple orderBy sources with key-level deduplication (first-seen key wins).
366
+ * RawQueryFragment symbol keys are never deduped (each is unique).
367
+ */
368
+ static mergeOrderBy(...sources) {
369
+ const result = [];
370
+ const seenKeys = new Set();
371
+ for (const source of sources) {
372
+ if (source == null) {
373
+ continue;
374
+ }
375
+ for (const item of Utils.asArray(source)) {
376
+ for (const key of Utils.getObjectQueryKeys(item)) {
377
+ if (typeof key === 'symbol') {
378
+ result.push({ [key]: item[key] });
379
+ }
380
+ else if (!seenKeys.has(key)) {
381
+ seenKeys.add(key);
382
+ result.push({ [key]: item[key] });
383
+ }
384
+ }
385
+ }
386
+ }
387
+ return result;
397
388
  }
398
- return result;
399
- }
400
389
  }