@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
@@ -1,840 +1,811 @@
1
1
  import { QueryHelper } from '../utils/QueryHelper.js';
2
2
  import { Utils } from '../utils/Utils.js';
3
3
  import { ValidationError } from '../errors.js';
4
- import { LoadStrategy, PopulatePath, ReferenceKind } from '../enums.js';
4
+ import { LoadStrategy, PopulatePath, ReferenceKind, } from '../enums.js';
5
5
  import { Reference } from './Reference.js';
6
6
  import { helper } from './wrap.js';
7
7
  import { expandDotPaths } from './utils.js';
8
8
  import { Raw } from '../utils/RawQueryFragment.js';
9
9
  /** Responsible for batch-loading entity relations using either select-in or joined loading strategies. */
10
10
  export class EntityLoader {
11
- #metadata;
12
- #driver;
13
- #em;
14
- constructor(em) {
15
- this.#em = em;
16
- this.#metadata = this.#em.getMetadata();
17
- this.#driver = this.#em.getDriver();
18
- }
19
- /**
20
- * Loads specified relations in batch.
21
- * This will execute one query for each relation, that will populate it on all the specified entities.
22
- */
23
- async populate(entityName, entities, populate, options) {
24
- if (entities.length === 0 || Utils.isEmpty(populate)) {
25
- return this.setSerializationContext(entities, populate, options);
26
- }
27
- const meta = this.#metadata.find(entityName);
28
- if (entities.some(e => !e.__helper)) {
29
- const entity = entities.find(e => !Utils.isEntity(e));
30
- throw ValidationError.notDiscoveredEntity(entity, meta, 'populate');
31
- }
32
- const references = entities.filter(e => !helper(e).isInitialized());
33
- const visited = (options.visited ??= new Set());
34
- options.where ??= {};
35
- options.orderBy ??= {};
36
- options.lookup ??= true;
37
- options.validate ??= true;
38
- options.refresh ??= false;
39
- options.convertCustomTypes ??= true;
40
- if (references.length > 0) {
41
- await this.populateScalar(meta, references, { ...options, populateWhere: undefined });
42
- }
43
- populate = this.normalizePopulate(entityName, populate, options.strategy, options.lookup, options.exclude);
44
- const invalid = populate.find(({ field }) => !this.#em.canPopulate(entityName, field));
45
- /* v8 ignore next */
46
- if (options.validate && invalid) {
47
- throw ValidationError.invalidPropertyName(entityName, invalid.field);
48
- }
49
- this.setSerializationContext(entities, populate, options);
50
- for (const entity of entities) {
51
- visited.add(entity);
52
- }
53
- for (const pop of populate) {
54
- await this.populateField(entityName, entities, pop, options);
11
+ #metadata;
12
+ #driver;
13
+ #em;
14
+ constructor(em) {
15
+ this.#em = em;
16
+ this.#metadata = this.#em.getMetadata();
17
+ this.#driver = this.#em.getDriver();
18
+ }
19
+ /**
20
+ * Loads specified relations in batch.
21
+ * This will execute one query for each relation, that will populate it on all the specified entities.
22
+ */
23
+ async populate(entityName, entities, populate, options) {
24
+ if (entities.length === 0 || Utils.isEmpty(populate)) {
25
+ return this.setSerializationContext(entities, populate, options);
26
+ }
27
+ const meta = this.#metadata.find(entityName);
28
+ if (entities.some(e => !e.__helper)) {
29
+ const entity = entities.find(e => !Utils.isEntity(e));
30
+ throw ValidationError.notDiscoveredEntity(entity, meta, 'populate');
31
+ }
32
+ const references = entities.filter(e => !helper(e).isInitialized());
33
+ const visited = (options.visited ??= new Set());
34
+ options.where ??= {};
35
+ options.orderBy ??= {};
36
+ options.lookup ??= true;
37
+ options.validate ??= true;
38
+ options.refresh ??= false;
39
+ options.convertCustomTypes ??= true;
40
+ if (references.length > 0) {
41
+ await this.populateScalar(meta, references, { ...options, populateWhere: undefined });
42
+ }
43
+ populate = this.normalizePopulate(entityName, populate, options.strategy, options.lookup, options.exclude);
44
+ const invalid = populate.find(({ field }) => !this.#em.canPopulate(entityName, field));
45
+ /* v8 ignore next */
46
+ if (options.validate && invalid) {
47
+ throw ValidationError.invalidPropertyName(entityName, invalid.field);
48
+ }
49
+ this.setSerializationContext(entities, populate, options);
50
+ for (const entity of entities) {
51
+ visited.add(entity);
52
+ }
53
+ for (const pop of populate) {
54
+ await this.populateField(entityName, entities, pop, options);
55
+ }
56
+ // Child-specific relations exist only on child metadata, so the parent-scoped populate loop above skips them (GH #7453).
57
+ if (Array.isArray(populate) &&
58
+ populate.some(p => p.all) &&
59
+ meta.inheritanceType === 'tpt' &&
60
+ meta.tptChildren?.length) {
61
+ const byType = new Map();
62
+ for (const entity of entities) {
63
+ const entityMeta = helper(entity).__meta;
64
+ if (entityMeta !== meta) {
65
+ const group = byType.get(entityMeta);
66
+ if (group) {
67
+ group.push(entity);
68
+ }
69
+ else {
70
+ byType.set(entityMeta, [entity]);
71
+ }
72
+ }
73
+ }
74
+ for (const [childMeta, group] of byType) {
75
+ await this.populate(childMeta.class, group, true, {
76
+ ...options,
77
+ lookup: false,
78
+ validate: false,
79
+ });
80
+ }
81
+ }
82
+ for (const entity of entities) {
83
+ visited.delete(entity);
84
+ }
55
85
  }
56
- // Child-specific relations exist only on child metadata, so the parent-scoped populate loop above skips them (GH #7453).
57
- if (
58
- Array.isArray(populate) &&
59
- populate.some(p => p.all) &&
60
- meta.inheritanceType === 'tpt' &&
61
- meta.tptChildren?.length
62
- ) {
63
- const byType = new Map();
64
- for (const entity of entities) {
65
- const entityMeta = helper(entity).__meta;
66
- if (entityMeta !== meta) {
67
- const group = byType.get(entityMeta);
68
- if (group) {
69
- group.push(entity);
70
- } else {
71
- byType.set(entityMeta, [entity]);
72
- }
73
- }
74
- }
75
- for (const [childMeta, group] of byType) {
76
- await this.populate(childMeta.class, group, true, {
77
- ...options,
78
- lookup: false,
79
- validate: false,
86
+ /** Normalizes populate hints into a structured array of PopulateOptions, expanding dot paths and eager relations. */
87
+ normalizePopulate(entityName, populate, strategy, lookup = true, exclude) {
88
+ const meta = this.#metadata.find(entityName);
89
+ let normalized = Utils.asArray(populate).map(field => {
90
+ // oxfmt-ignore
91
+ return typeof field === 'boolean' || field.field === PopulatePath.ALL ? { all: !!field, field: meta.primaryKeys[0] } : field;
80
92
  });
81
- }
82
- }
83
- for (const entity of entities) {
84
- visited.delete(entity);
85
- }
86
- }
87
- /** Normalizes populate hints into a structured array of PopulateOptions, expanding dot paths and eager relations. */
88
- normalizePopulate(entityName, populate, strategy, lookup = true, exclude) {
89
- const meta = this.#metadata.find(entityName);
90
- let normalized = Utils.asArray(populate).map(field => {
91
- // oxfmt-ignore
92
- return typeof field === 'boolean' || field.field === PopulatePath.ALL ? { all: !!field, field: meta.primaryKeys[0] } : field;
93
- });
94
- if (normalized.some(p => p.all)) {
95
- normalized = this.lookupAllRelationships(entityName);
96
- }
97
- // convert nested `field` with dot syntax to PopulateOptions with `children` array
98
- expandDotPaths(meta, normalized, true);
99
- if (lookup && populate !== false) {
100
- normalized = this.lookupEagerLoadedRelationships(entityName, normalized, strategy, '', [], exclude);
101
- // convert nested `field` with dot syntax produced by eager relations
102
- expandDotPaths(meta, normalized, true);
103
- }
104
- // merge same fields
105
- return this.mergeNestedPopulate(normalized);
106
- }
107
- setSerializationContext(entities, populate, options) {
108
- for (const entity of entities) {
109
- helper(entity).setSerializationContext({
110
- populate,
111
- fields: options.fields,
112
- exclude: options.exclude,
113
- });
114
- }
115
- }
116
- /**
117
- * Merge multiple populates for the same entity with different children. Also skips `*` fields, those can come from
118
- * partial loading hints (`fields`) that are used to infer the `populate` hint if missing.
119
- */
120
- mergeNestedPopulate(populate) {
121
- const tmp = populate.reduce((ret, item) => {
122
- /* v8 ignore next */
123
- if (item.field === PopulatePath.ALL) {
124
- return ret;
125
- }
126
- if (!ret[item.field]) {
127
- ret[item.field] = item;
128
- return ret;
129
- }
130
- if (!ret[item.field].children && item.children) {
131
- ret[item.field].children = item.children;
132
- } else if (ret[item.field].children && item.children) {
133
- ret[item.field].children.push(...item.children);
134
- }
135
- return ret;
136
- }, {});
137
- return Object.values(tmp).map(item => {
138
- if (item.children) {
139
- item.children = this.mergeNestedPopulate(item.children);
140
- }
141
- return item;
142
- });
143
- }
144
- /**
145
- * preload everything in one call (this will update already existing references in IM)
146
- */
147
- async populateMany(entityName, entities, populate, options) {
148
- const [field, ref] = populate.field.split(':', 2);
149
- const meta = this.#metadata.find(entityName);
150
- const prop = meta.properties[field];
151
- if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner && !this.#driver.getPlatform().usesPivotTable()) {
152
- const filtered = entities.filter(e => !e[prop.name]?.isInitialized());
153
- if (filtered.length > 0) {
154
- await this.populateScalar(meta, filtered, { ...options, fields: [prop.name] });
155
- }
156
- }
157
- if (prop.kind === ReferenceKind.SCALAR && prop.lazy) {
158
- const filtered = entities.filter(
159
- e => options.refresh || (prop.ref ? !e[prop.name]?.isInitialized() : e[prop.name] === undefined),
160
- );
161
- if (options.ignoreLazyScalarProperties || filtered.length === 0) {
162
- return entities;
163
- }
164
- await this.populateScalar(meta, filtered, { ...options, fields: [prop.name] });
165
- return entities;
166
- }
167
- if (prop.kind === ReferenceKind.EMBEDDED) {
168
- return [];
169
- }
170
- const filtered = this.filterCollections(entities, field, options, ref);
171
- const innerOrderBy = Utils.asArray(options.orderBy)
172
- .filter(
173
- orderBy =>
174
- (Array.isArray(orderBy[prop.name]) && orderBy[prop.name].length > 0) || Utils.isObject(orderBy[prop.name]),
175
- )
176
- .flatMap(orderBy => orderBy[prop.name]);
177
- const where = await this.extractChildCondition(options, prop);
178
- if (prop.kind === ReferenceKind.MANY_TO_MANY && this.#driver.getPlatform().usesPivotTable()) {
179
- const pivotOrderBy = QueryHelper.mergeOrderBy(innerOrderBy, prop.orderBy, prop.targetMeta?.orderBy);
180
- const res = await this.findChildrenFromPivotTable(filtered, prop, options, pivotOrderBy, populate, !!ref);
181
- return Utils.flatten(res);
182
- }
183
- if (prop.polymorphic && prop.polymorphTargets) {
184
- return this.populatePolymorphic(entities, prop, options, !!ref);
185
- }
186
- const { items, partial } = await this.findChildren(
187
- options.filtered ?? entities,
188
- prop,
189
- populate,
190
- {
191
- ...options,
192
- where,
193
- orderBy: innerOrderBy,
194
- },
195
- !!(ref || prop.mapToPk),
196
- );
197
- const customOrder = innerOrderBy.length > 0 || !!prop.orderBy || !!prop.targetMeta?.orderBy;
198
- this.initializeCollections(filtered, prop, field, items, customOrder, partial);
199
- return items;
200
- }
201
- async populateScalar(meta, filtered, options) {
202
- const pk = Utils.getPrimaryKeyHash(meta.primaryKeys);
203
- const ids = Utils.unique(filtered.map(e => Utils.getPrimaryKeyValues(e, meta, true)));
204
- const where = this.mergePrimaryCondition(ids, pk, options, meta, this.#metadata, this.#driver.getPlatform());
205
- const { filters, convertCustomTypes, lockMode, strategy, populateWhere, connectionType, logging, fields } = options;
206
- await this.#em.find(meta.class, where, {
207
- filters,
208
- convertCustomTypes,
209
- lockMode,
210
- strategy,
211
- populateWhere,
212
- connectionType,
213
- logging,
214
- fields: fields,
215
- populate: [],
216
- });
217
- }
218
- async populatePolymorphic(entities, prop, options, ref) {
219
- const ownerMeta = this.#metadata.get(entities[0].constructor);
220
- // Separate entities: those with loaded refs vs those needing FK load
221
- const toPopulate = [];
222
- const needsFkLoad = [];
223
- for (const entity of entities) {
224
- const refValue = entity[prop.name];
225
- if (refValue && helper(refValue).hasPrimaryKey()) {
226
- if (
227
- (ref && !options.refresh) || // :ref hint - already have reference
228
- (!ref && helper(refValue).__initialized && !options.refresh) // already loaded
229
- ) {
230
- continue;
231
- }
232
- toPopulate.push(entity);
233
- } else if (refValue == null && !helper(entity).__loadedProperties.has(prop.name)) {
234
- // FK columns weren't loaded (partial loading) — need to re-fetch them.
235
- // If the property IS in __loadedProperties, the FK was loaded and is genuinely null.
236
- needsFkLoad.push(entity);
237
- }
238
- }
239
- // Load FK columns using populateScalar pattern
240
- if (needsFkLoad.length > 0) {
241
- await this.populateScalar(ownerMeta, needsFkLoad, {
242
- ...options,
243
- fields: [...ownerMeta.primaryKeys, prop.name],
244
- });
245
- // After loading FKs, add to toPopulate if not using :ref hint
246
- if (!ref) {
247
- for (const entity of needsFkLoad) {
248
- const refValue = entity[prop.name];
249
- if (refValue && helper(refValue).hasPrimaryKey()) {
250
- toPopulate.push(entity);
251
- }
252
- }
253
- }
254
- }
255
- if (toPopulate.length === 0) {
256
- return [];
257
- }
258
- // Group references by target class for batch loading
259
- const groups = new Map();
260
- for (const entity of toPopulate) {
261
- const refValue = Reference.unwrapReference(entity[prop.name]);
262
- const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, helper(refValue).__meta.class);
263
- const group = groups.get(discriminator) ?? [];
264
- group.push(refValue);
265
- groups.set(discriminator, group);
266
- }
267
- // Load each group concurrently - identity map handles merging with existing references
268
- const allItems = [];
269
- await Promise.all(
270
- [...groups].map(async ([discriminator, children]) => {
271
- const targetMeta = this.#metadata.find(prop.discriminatorMap[discriminator]);
272
- await this.populateScalar(targetMeta, children, options);
273
- allItems.push(...children);
274
- }),
275
- );
276
- return allItems;
277
- }
278
- initializeCollections(filtered, prop, field, children, customOrder, partial) {
279
- if (prop.kind === ReferenceKind.ONE_TO_MANY) {
280
- this.initializeOneToMany(filtered, children, prop, field, partial);
281
- }
282
- if (prop.kind === ReferenceKind.MANY_TO_MANY && !this.#driver.getPlatform().usesPivotTable()) {
283
- this.initializeManyToMany(filtered, children, prop, field, customOrder, partial);
284
- }
285
- }
286
- initializeOneToMany(filtered, children, prop, field, partial) {
287
- const mapToPk = prop.targetMeta.properties[prop.mappedBy].mapToPk;
288
- const map = {};
289
- for (const entity of filtered) {
290
- const key = helper(entity).getSerializedPrimaryKey();
291
- map[key] = [];
292
- }
293
- for (const child of children) {
294
- const pk = child.__helper.__data[prop.mappedBy] ?? child[prop.mappedBy];
295
- if (pk) {
296
- const key = helper(mapToPk ? this.#em.getReference(prop.targetMeta.class, pk) : pk).getSerializedPrimaryKey();
297
- map[key]?.push(child);
298
- }
299
- }
300
- for (const entity of filtered) {
301
- const key = helper(entity).getSerializedPrimaryKey();
302
- entity[field].hydrate(map[key], undefined, partial);
303
- }
304
- }
305
- initializeManyToMany(filtered, children, prop, field, customOrder, partial) {
306
- if (prop.mappedBy) {
307
- for (const entity of filtered) {
308
- const items = children.filter(child => child[prop.mappedBy].contains(entity, false));
309
- entity[field].hydrate(items, true, partial);
310
- }
311
- } else {
312
- // owning side of M:N without pivot table needs to be reordered
313
- for (const entity of filtered) {
314
- const order = !customOrder ? [...entity[prop.name].getItems(false)] : []; // copy order of references
315
- const items = children.filter(child => entity[prop.name].contains(child, false));
316
- if (!customOrder) {
317
- items.sort((a, b) => order.indexOf(a) - order.indexOf(b));
318
- }
319
- entity[field].hydrate(items, true, partial);
320
- }
321
- }
322
- }
323
- async findChildren(entities, prop, populate, options, ref) {
324
- const children = Utils.unique(this.getChildReferences(entities, prop, options, ref));
325
- const meta = prop.targetMeta;
326
- // When targetKey is set, use it for FK lookup instead of the PK
327
- let fk = prop.targetKey ?? Utils.getPrimaryKeyHash(meta.primaryKeys);
328
- let schema = options.schema;
329
- const partial = !Utils.isEmpty(prop.where) || !Utils.isEmpty(options.where);
330
- let polymorphicOwnerProp;
331
- if (prop.kind === ReferenceKind.ONE_TO_MANY || (prop.kind === ReferenceKind.MANY_TO_MANY && !prop.owner)) {
332
- const ownerProp = meta.properties[prop.mappedBy];
333
- if (ownerProp.polymorphic && ownerProp.fieldNames.length >= 2) {
334
- const idColumns = ownerProp.fieldNames.slice(1);
335
- fk = idColumns.length === 1 ? idColumns[0] : idColumns;
336
- polymorphicOwnerProp = ownerProp;
337
- } else {
338
- fk = ownerProp.name;
339
- }
340
- }
341
- if (prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner && !ref) {
342
- children.length = 0;
343
- fk = meta.properties[prop.mappedBy].name;
344
- children.push(...this.filterByReferences(entities, prop.name, options.refresh));
345
- }
346
- if (children.length === 0) {
347
- return { items: [], partial };
348
- }
349
- if (!schema && [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
350
- schema = children.find(e => e.__helper.__schema)?.__helper.__schema;
351
- }
352
- const ids = Utils.unique(children.map(e => (prop.targetKey ? e[prop.targetKey] : e.__helper.getPrimaryKey())));
353
- let where;
354
- if (polymorphicOwnerProp && Array.isArray(fk)) {
355
- const conditions = ids.map(id => {
356
- const pkValues = Object.values(id);
357
- return Object.fromEntries(fk.map((col, idx) => [col, pkValues[idx]]));
358
- });
359
- where = conditions.length === 1 ? conditions[0] : { $or: conditions };
360
- } else {
361
- where = this.mergePrimaryCondition(ids, fk, options, meta, this.#metadata, this.#driver.getPlatform());
362
- }
363
- if (polymorphicOwnerProp) {
364
- const parentMeta = this.#metadata.find(entities[0].constructor);
365
- const discriminatorValue =
366
- QueryHelper.findDiscriminatorValue(polymorphicOwnerProp.discriminatorMap, parentMeta.class) ??
367
- parentMeta.tableName;
368
- const discriminatorColumn = polymorphicOwnerProp.fieldNames[0];
369
- where = { $and: [where, { [discriminatorColumn]: discriminatorValue }] };
370
- }
371
- const fields = this.buildFields(options.fields, prop, ref);
372
- /* eslint-disable prefer-const */
373
- let {
374
- refresh,
375
- filters,
376
- convertCustomTypes,
377
- lockMode,
378
- strategy,
379
- populateWhere = 'infer',
380
- connectionType,
381
- logging,
382
- } = options;
383
- /* eslint-enable prefer-const */
384
- if (typeof populateWhere === 'object') {
385
- populateWhere = await this.extractChildCondition({ where: populateWhere }, prop);
386
- }
387
- if (!Utils.isEmpty(prop.where) || Raw.hasObjectFragments(prop.where)) {
388
- where = { $and: [where, prop.where] };
389
- }
390
- const orderBy = QueryHelper.mergeOrderBy(options.orderBy, prop.orderBy);
391
- const items = await this.#em.find(meta.class, where, {
392
- filters,
393
- convertCustomTypes,
394
- lockMode,
395
- populateWhere,
396
- logging,
397
- orderBy,
398
- populate: populate.children ?? populate.all ?? [],
399
- exclude: Array.isArray(options.exclude)
400
- ? Utils.extractChildElements(options.exclude, prop.name)
401
- : options.exclude,
402
- strategy,
403
- fields,
404
- schema,
405
- connectionType,
406
- // @ts-ignore not a public option, will be propagated to the populate call
407
- refresh: refresh && !children.every(item => options.visited.has(item)),
408
- // @ts-ignore not a public option, will be propagated to the populate call
409
- visited: options.visited,
410
- });
411
- // For targetKey relations, wire up loaded entities to parent references
412
- // This is needed because the references were created under alternate key,
413
- // but loaded entities are stored under PK, so they don't automatically merge
414
- if (prop.targetKey && [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
415
- const itemsByKey = new Map();
416
- for (const item of items) {
417
- itemsByKey.set('' + item[prop.targetKey], item);
418
- }
419
- for (const entity of entities) {
420
- const ref = entity[prop.name];
421
- /* v8 ignore next */
422
- if (!ref) {
423
- continue;
93
+ if (normalized.some(p => p.all)) {
94
+ normalized = this.lookupAllRelationships(entityName);
424
95
  }
425
- // oxfmt-ignore
426
- const keyValue = '' + (Reference.isReference(ref) ? ref.unwrap()[prop.targetKey] : ref[prop.targetKey]);
427
- const loadedItem = itemsByKey.get(keyValue);
428
- if (loadedItem) {
429
- entity[prop.name] = Reference.isReference(ref) ? Reference.create(loadedItem) : loadedItem;
96
+ // convert nested `field` with dot syntax to PopulateOptions with `children` array
97
+ expandDotPaths(meta, normalized, true);
98
+ if (lookup && populate !== false) {
99
+ normalized = this.lookupEagerLoadedRelationships(entityName, normalized, strategy, '', [], exclude);
100
+ // convert nested `field` with dot syntax produced by eager relations
101
+ expandDotPaths(meta, normalized, true);
102
+ }
103
+ // merge same fields
104
+ return this.mergeNestedPopulate(normalized);
105
+ }
106
+ setSerializationContext(entities, populate, options) {
107
+ for (const entity of entities) {
108
+ helper(entity).setSerializationContext({
109
+ populate,
110
+ fields: options.fields,
111
+ exclude: options.exclude,
112
+ });
430
113
  }
431
- }
432
114
  }
433
- if ([ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) && items.length !== children.length) {
434
- const nullVal = this.#em.config.get('forceUndefined') ? undefined : null;
435
- const itemsMap = new Set();
436
- const childrenMap = new Set();
437
- // Use targetKey value if set, otherwise use serialized PK
438
- const getKey = e => (prop.targetKey ? '' + e[prop.targetKey] : helper(e).getSerializedPrimaryKey());
439
- for (const item of items) {
440
- /* v8 ignore next */
441
- itemsMap.add(getKey(item));
442
- }
443
- for (const child of children) {
444
- childrenMap.add(getKey(child));
445
- }
446
- const isInverseOneToOne = prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner;
447
- for (const entity of entities) {
448
- const ref = entity[prop.name];
449
- if (ref == null) {
450
- continue;
451
- }
452
- const refKey = getKey(ref);
453
- // For 1:1 inverse, `children` contains parent entities, so `childrenMap`
454
- // has parent PKs — match against the entity's own PK, not the referenced entity's PK.
455
- const childKey = isInverseOneToOne ? getKey(entity) : refKey;
456
- if (childrenMap.has(childKey) && !itemsMap.has(refKey)) {
457
- entity[prop.name] = nullVal;
458
- helper(entity).__originalEntityData[prop.name] = null;
459
- }
460
- }
115
+ /**
116
+ * Merge multiple populates for the same entity with different children. Also skips `*` fields, those can come from
117
+ * partial loading hints (`fields`) that are used to infer the `populate` hint if missing.
118
+ */
119
+ mergeNestedPopulate(populate) {
120
+ const tmp = populate.reduce((ret, item) => {
121
+ /* v8 ignore next */
122
+ if (item.field === PopulatePath.ALL) {
123
+ return ret;
124
+ }
125
+ if (!ret[item.field]) {
126
+ ret[item.field] = item;
127
+ return ret;
128
+ }
129
+ if (!ret[item.field].children && item.children) {
130
+ ret[item.field].children = item.children;
131
+ }
132
+ else if (ret[item.field].children && item.children) {
133
+ ret[item.field].children.push(...item.children);
134
+ }
135
+ return ret;
136
+ }, {});
137
+ return Object.values(tmp).map(item => {
138
+ if (item.children) {
139
+ item.children = this.mergeNestedPopulate(item.children);
140
+ }
141
+ return item;
142
+ });
461
143
  }
462
- for (const item of items) {
463
- if (ref && !helper(item).__onLoadFired) {
464
- helper(item).__initialized = false;
465
- this.#em.getUnitOfWork().unmarkAsLoaded(item);
466
- }
144
+ /**
145
+ * preload everything in one call (this will update already existing references in IM)
146
+ */
147
+ async populateMany(entityName, entities, populate, options) {
148
+ const [field, ref] = populate.field.split(':', 2);
149
+ const meta = this.#metadata.find(entityName);
150
+ const prop = meta.properties[field];
151
+ if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner && !this.#driver.getPlatform().usesPivotTable()) {
152
+ const filtered = entities.filter(e => !e[prop.name]?.isInitialized());
153
+ if (filtered.length > 0) {
154
+ await this.populateScalar(meta, filtered, { ...options, fields: [prop.name] });
155
+ }
156
+ }
157
+ if (prop.kind === ReferenceKind.SCALAR && prop.lazy) {
158
+ const filtered = entities.filter(e => options.refresh ||
159
+ (prop.ref ? !e[prop.name]?.isInitialized() : e[prop.name] === undefined));
160
+ if (options.ignoreLazyScalarProperties || filtered.length === 0) {
161
+ return entities;
162
+ }
163
+ await this.populateScalar(meta, filtered, { ...options, fields: [prop.name] });
164
+ return entities;
165
+ }
166
+ if (prop.kind === ReferenceKind.EMBEDDED) {
167
+ return [];
168
+ }
169
+ const filtered = this.filterCollections(entities, field, options, ref);
170
+ const innerOrderBy = Utils.asArray(options.orderBy)
171
+ .filter(orderBy => (Array.isArray(orderBy[prop.name]) && orderBy[prop.name].length > 0) ||
172
+ Utils.isObject(orderBy[prop.name]))
173
+ .flatMap(orderBy => orderBy[prop.name]);
174
+ const where = await this.extractChildCondition(options, prop);
175
+ if (prop.kind === ReferenceKind.MANY_TO_MANY && this.#driver.getPlatform().usesPivotTable()) {
176
+ const pivotOrderBy = QueryHelper.mergeOrderBy(innerOrderBy, prop.orderBy, prop.targetMeta?.orderBy);
177
+ const res = await this.findChildrenFromPivotTable(filtered, prop, options, pivotOrderBy, populate, !!ref);
178
+ return Utils.flatten(res);
179
+ }
180
+ if (prop.polymorphic && prop.polymorphTargets) {
181
+ return this.populatePolymorphic(entities, prop, options, !!ref);
182
+ }
183
+ const { items, partial } = await this.findChildren(options.filtered ?? entities, prop, populate, {
184
+ ...options,
185
+ where,
186
+ orderBy: innerOrderBy,
187
+ }, !!(ref || prop.mapToPk));
188
+ const customOrder = innerOrderBy.length > 0 || !!prop.orderBy || !!prop.targetMeta?.orderBy;
189
+ this.initializeCollections(filtered, prop, field, items, customOrder, partial);
190
+ return items;
191
+ }
192
+ async populateScalar(meta, filtered, options) {
193
+ const pk = Utils.getPrimaryKeyHash(meta.primaryKeys);
194
+ const ids = Utils.unique(filtered.map(e => Utils.getPrimaryKeyValues(e, meta, true)));
195
+ const where = this.mergePrimaryCondition(ids, pk, options, meta, this.#metadata, this.#driver.getPlatform());
196
+ const { filters, convertCustomTypes, lockMode, strategy, populateWhere, connectionType, logging, fields } = options;
197
+ await this.#em.find(meta.class, where, {
198
+ filters,
199
+ convertCustomTypes,
200
+ lockMode,
201
+ strategy,
202
+ populateWhere,
203
+ connectionType,
204
+ logging,
205
+ fields: fields,
206
+ populate: [],
207
+ });
467
208
  }
468
- return { items, partial };
469
- }
470
- mergePrimaryCondition(ids, pk, options, meta, metadata, platform) {
471
- const cond1 = QueryHelper.processWhere({
472
- where: { [pk]: { $in: ids } },
473
- entityName: meta.class,
474
- metadata,
475
- platform,
476
- convertCustomTypes: !options.convertCustomTypes,
477
- });
478
- const where = { ...options.where };
479
- Utils.dropUndefinedProperties(where);
480
- return where[pk] ? { $and: [cond1, where] } : { ...cond1, ...where };
481
- }
482
- async populateField(entityName, entities, populate, options) {
483
- const field = populate.field.split(':')[0];
484
- const prop = this.#metadata.find(entityName).properties[field];
485
- if (prop.kind === ReferenceKind.SCALAR && !prop.lazy) {
486
- return;
209
+ async populatePolymorphic(entities, prop, options, ref) {
210
+ const ownerMeta = this.#metadata.get(entities[0].constructor);
211
+ // Separate entities: those with loaded refs vs those needing FK load
212
+ const toPopulate = [];
213
+ const needsFkLoad = [];
214
+ for (const entity of entities) {
215
+ const refValue = entity[prop.name];
216
+ if (refValue && helper(refValue).hasPrimaryKey()) {
217
+ if ((ref && !options.refresh) || // :ref hint - already have reference
218
+ (!ref && helper(refValue).__initialized && !options.refresh) // already loaded
219
+ ) {
220
+ continue;
221
+ }
222
+ toPopulate.push(entity);
223
+ }
224
+ else if (refValue == null && !helper(entity).__loadedProperties.has(prop.name)) {
225
+ // FK columns weren't loaded (partial loading) — need to re-fetch them.
226
+ // If the property IS in __loadedProperties, the FK was loaded and is genuinely null.
227
+ needsFkLoad.push(entity);
228
+ }
229
+ }
230
+ // Load FK columns using populateScalar pattern
231
+ if (needsFkLoad.length > 0) {
232
+ await this.populateScalar(ownerMeta, needsFkLoad, {
233
+ ...options,
234
+ fields: [...ownerMeta.primaryKeys, prop.name],
235
+ });
236
+ // After loading FKs, add to toPopulate if not using :ref hint
237
+ if (!ref) {
238
+ for (const entity of needsFkLoad) {
239
+ const refValue = entity[prop.name];
240
+ if (refValue && helper(refValue).hasPrimaryKey()) {
241
+ toPopulate.push(entity);
242
+ }
243
+ }
244
+ }
245
+ }
246
+ if (toPopulate.length === 0) {
247
+ return [];
248
+ }
249
+ // Group references by target class for batch loading
250
+ const groups = new Map();
251
+ for (const entity of toPopulate) {
252
+ const refValue = Reference.unwrapReference(entity[prop.name]);
253
+ const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, helper(refValue).__meta.class);
254
+ const group = groups.get(discriminator) ?? [];
255
+ group.push(refValue);
256
+ groups.set(discriminator, group);
257
+ }
258
+ // Load each group concurrently - identity map handles merging with existing references
259
+ const allItems = [];
260
+ await Promise.all([...groups].map(async ([discriminator, children]) => {
261
+ const targetMeta = this.#metadata.find(prop.discriminatorMap[discriminator]);
262
+ await this.populateScalar(targetMeta, children, options);
263
+ allItems.push(...children);
264
+ }));
265
+ return allItems;
266
+ }
267
+ initializeCollections(filtered, prop, field, children, customOrder, partial) {
268
+ if (prop.kind === ReferenceKind.ONE_TO_MANY) {
269
+ this.initializeOneToMany(filtered, children, prop, field, partial);
270
+ }
271
+ if (prop.kind === ReferenceKind.MANY_TO_MANY && !this.#driver.getPlatform().usesPivotTable()) {
272
+ this.initializeManyToMany(filtered, children, prop, field, customOrder, partial);
273
+ }
487
274
  }
488
- options = { ...options, filters: QueryHelper.mergePropertyFilters(prop.filters, options.filters) };
489
- const populated = await this.populateMany(entityName, entities, populate, options);
490
- if (!populate.children && !populate.all) {
491
- return;
275
+ initializeOneToMany(filtered, children, prop, field, partial) {
276
+ const mapToPk = prop.targetMeta.properties[prop.mappedBy].mapToPk;
277
+ const map = {};
278
+ for (const entity of filtered) {
279
+ const key = helper(entity).getSerializedPrimaryKey();
280
+ map[key] = [];
281
+ }
282
+ for (const child of children) {
283
+ const pk = child.__helper.__data[prop.mappedBy] ?? child[prop.mappedBy];
284
+ if (pk) {
285
+ const key = helper(mapToPk ? this.#em.getReference(prop.targetMeta.class, pk) : pk).getSerializedPrimaryKey();
286
+ map[key]?.push(child);
287
+ }
288
+ }
289
+ for (const entity of filtered) {
290
+ const key = helper(entity).getSerializedPrimaryKey();
291
+ entity[field].hydrate(map[key], undefined, partial);
292
+ }
492
293
  }
493
- const children = [];
494
- for (const entity of entities) {
495
- const ref = entity[field];
496
- if (Utils.isEntity(ref)) {
497
- children.push(ref);
498
- } else if (Reference.isReference(ref)) {
499
- children.push(ref.unwrap());
500
- } else if (Utils.isCollection(ref)) {
501
- children.push(...ref.getItems());
502
- } else if (ref && prop.kind === ReferenceKind.EMBEDDED) {
503
- children.push(...Utils.asArray(ref));
504
- }
294
+ initializeManyToMany(filtered, children, prop, field, customOrder, partial) {
295
+ if (prop.mappedBy) {
296
+ for (const entity of filtered) {
297
+ const items = children.filter(child => child[prop.mappedBy].contains(entity, false));
298
+ entity[field].hydrate(items, true, partial);
299
+ }
300
+ }
301
+ else {
302
+ // owning side of M:N without pivot table needs to be reordered
303
+ for (const entity of filtered) {
304
+ const order = !customOrder ? [...entity[prop.name].getItems(false)] : []; // copy order of references
305
+ const items = children.filter(child => entity[prop.name].contains(child, false));
306
+ if (!customOrder) {
307
+ items.sort((a, b) => order.indexOf(a) - order.indexOf(b));
308
+ }
309
+ entity[field].hydrate(items, true, partial);
310
+ }
311
+ }
505
312
  }
506
- if (populated.length === 0 && !populate.children) {
507
- // Populate child-specific relations for TPT entities already in the identity map (GH #7529).
508
- // Pass a sentinel hint so the existing TPT child handling in `populate()` does the real work.
509
- if (populate.all && prop.targetMeta?.inheritanceType === 'tpt') {
313
+ async findChildren(entities, prop, populate, options, ref) {
314
+ const children = Utils.unique(this.getChildReferences(entities, prop, options, ref));
315
+ const meta = prop.targetMeta;
316
+ // When targetKey is set, use it for FK lookup instead of the PK
317
+ let fk = prop.targetKey ?? Utils.getPrimaryKeyHash(meta.primaryKeys);
318
+ let schema = options.schema;
319
+ const partial = !Utils.isEmpty(prop.where) || !Utils.isEmpty(options.where);
320
+ let polymorphicOwnerProp;
321
+ if (prop.kind === ReferenceKind.ONE_TO_MANY || (prop.kind === ReferenceKind.MANY_TO_MANY && !prop.owner)) {
322
+ const ownerProp = meta.properties[prop.mappedBy];
323
+ if (ownerProp.polymorphic && ownerProp.fieldNames.length >= 2) {
324
+ const idColumns = ownerProp.fieldNames.slice(1);
325
+ fk = idColumns.length === 1 ? idColumns[0] : idColumns;
326
+ polymorphicOwnerProp = ownerProp;
327
+ }
328
+ else {
329
+ fk = ownerProp.name;
330
+ }
331
+ }
332
+ if (prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner && !ref) {
333
+ children.length = 0;
334
+ fk = meta.properties[prop.mappedBy].name;
335
+ children.push(...this.filterByReferences(entities, prop.name, options.refresh));
336
+ }
337
+ if (children.length === 0) {
338
+ return { items: [], partial };
339
+ }
340
+ if (!schema && [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
341
+ schema = children.find(e => e.__helper.__schema)?.__helper.__schema;
342
+ }
343
+ const ids = Utils.unique(children.map(e => (prop.targetKey ? e[prop.targetKey] : e.__helper.getPrimaryKey())));
344
+ let where;
345
+ if (polymorphicOwnerProp && Array.isArray(fk)) {
346
+ const conditions = ids.map(id => {
347
+ const pkValues = Object.values(id);
348
+ return Object.fromEntries(fk.map((col, idx) => [col, pkValues[idx]]));
349
+ });
350
+ where = (conditions.length === 1 ? conditions[0] : { $or: conditions });
351
+ }
352
+ else {
353
+ where = this.mergePrimaryCondition(ids, fk, options, meta, this.#metadata, this.#driver.getPlatform());
354
+ }
355
+ if (polymorphicOwnerProp) {
356
+ const parentMeta = this.#metadata.find(entities[0].constructor);
357
+ const discriminatorValue = QueryHelper.findDiscriminatorValue(polymorphicOwnerProp.discriminatorMap, parentMeta.class) ??
358
+ parentMeta.tableName;
359
+ const discriminatorColumn = polymorphicOwnerProp.fieldNames[0];
360
+ where = { $and: [where, { [discriminatorColumn]: discriminatorValue }] };
361
+ }
362
+ const fields = this.buildFields(options.fields, prop, ref);
363
+ /* eslint-disable prefer-const */
364
+ let { refresh, filters, convertCustomTypes, lockMode, strategy, populateWhere = 'infer', connectionType, logging, } = options;
365
+ /* eslint-enable prefer-const */
366
+ if (typeof populateWhere === 'object') {
367
+ populateWhere = await this.extractChildCondition({ where: populateWhere }, prop);
368
+ }
369
+ if (!Utils.isEmpty(prop.where) || Raw.hasObjectFragments(prop.where)) {
370
+ where = { $and: [where, prop.where] };
371
+ }
372
+ const orderBy = QueryHelper.mergeOrderBy(options.orderBy, prop.orderBy);
373
+ const items = await this.#em.find(meta.class, where, {
374
+ filters,
375
+ convertCustomTypes,
376
+ lockMode,
377
+ populateWhere,
378
+ logging,
379
+ orderBy,
380
+ populate: populate.children ?? populate.all ?? [],
381
+ exclude: Array.isArray(options.exclude)
382
+ ? Utils.extractChildElements(options.exclude, prop.name)
383
+ : options.exclude,
384
+ strategy,
385
+ fields,
386
+ schema,
387
+ connectionType,
388
+ // @ts-ignore not a public option, will be propagated to the populate call
389
+ refresh: refresh && !children.every(item => options.visited.has(item)),
390
+ // @ts-ignore not a public option, will be propagated to the populate call
391
+ visited: options.visited,
392
+ });
393
+ // For targetKey relations, wire up loaded entities to parent references
394
+ // This is needed because the references were created under alternate key,
395
+ // but loaded entities are stored under PK, so they don't automatically merge
396
+ if (prop.targetKey && [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
397
+ const itemsByKey = new Map();
398
+ for (const item of items) {
399
+ itemsByKey.set('' + item[prop.targetKey], item);
400
+ }
401
+ for (const entity of entities) {
402
+ const ref = entity[prop.name];
403
+ /* v8 ignore next */
404
+ if (!ref) {
405
+ continue;
406
+ }
407
+ // oxfmt-ignore
408
+ const keyValue = '' + (Reference.isReference(ref) ? ref.unwrap()[prop.targetKey] : ref[prop.targetKey]);
409
+ const loadedItem = itemsByKey.get(keyValue);
410
+ if (loadedItem) {
411
+ entity[prop.name] = (Reference.isReference(ref) ? Reference.create(loadedItem) : loadedItem);
412
+ }
413
+ }
414
+ }
415
+ if ([ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) && items.length !== children.length) {
416
+ const nullVal = this.#em.config.get('forceUndefined') ? undefined : null;
417
+ const itemsMap = new Set();
418
+ const childrenMap = new Set();
419
+ // Use targetKey value if set, otherwise use serialized PK
420
+ const getKey = (e) => (prop.targetKey ? '' + e[prop.targetKey] : helper(e).getSerializedPrimaryKey());
421
+ for (const item of items) {
422
+ /* v8 ignore next */
423
+ itemsMap.add(getKey(item));
424
+ }
425
+ for (const child of children) {
426
+ childrenMap.add(getKey(child));
427
+ }
428
+ const isInverseOneToOne = prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner;
429
+ for (const entity of entities) {
430
+ const ref = entity[prop.name];
431
+ if (ref == null) {
432
+ continue;
433
+ }
434
+ const refKey = getKey(ref);
435
+ // For 1:1 inverse, `children` contains parent entities, so `childrenMap`
436
+ // has parent PKs — match against the entity's own PK, not the referenced entity's PK.
437
+ const childKey = isInverseOneToOne ? getKey(entity) : refKey;
438
+ if (childrenMap.has(childKey) && !itemsMap.has(refKey)) {
439
+ entity[prop.name] = nullVal;
440
+ helper(entity).__originalEntityData[prop.name] = null;
441
+ }
442
+ }
443
+ }
444
+ for (const item of items) {
445
+ if (ref && !helper(item).__onLoadFired) {
446
+ helper(item).__initialized = false;
447
+ this.#em.getUnitOfWork().unmarkAsLoaded(item);
448
+ }
449
+ }
450
+ return { items, partial };
451
+ }
452
+ mergePrimaryCondition(ids, pk, options, meta, metadata, platform) {
453
+ const cond1 = QueryHelper.processWhere({
454
+ where: { [pk]: { $in: ids } },
455
+ entityName: meta.class,
456
+ metadata,
457
+ platform,
458
+ convertCustomTypes: !options.convertCustomTypes,
459
+ });
460
+ const where = { ...options.where };
461
+ Utils.dropUndefinedProperties(where);
462
+ return where[pk] ? { $and: [cond1, where] } : { ...cond1, ...where };
463
+ }
464
+ async populateField(entityName, entities, populate, options) {
465
+ const field = populate.field.split(':')[0];
466
+ const prop = this.#metadata.find(entityName).properties[field];
467
+ if (prop.kind === ReferenceKind.SCALAR && !prop.lazy) {
468
+ return;
469
+ }
470
+ options = { ...options, filters: QueryHelper.mergePropertyFilters(prop.filters, options.filters) };
471
+ const populated = await this.populateMany(entityName, entities, populate, options);
472
+ if (!populate.children && !populate.all) {
473
+ return;
474
+ }
475
+ const children = [];
476
+ for (const entity of entities) {
477
+ const ref = entity[field];
478
+ if (Utils.isEntity(ref)) {
479
+ children.push(ref);
480
+ }
481
+ else if (Reference.isReference(ref)) {
482
+ children.push(ref.unwrap());
483
+ }
484
+ else if (Utils.isCollection(ref)) {
485
+ children.push(...ref.getItems());
486
+ }
487
+ else if (ref && prop.kind === ReferenceKind.EMBEDDED) {
488
+ children.push(...Utils.asArray(ref));
489
+ }
490
+ }
491
+ if (populated.length === 0 && !populate.children) {
492
+ // Populate child-specific relations for TPT entities already in the identity map (GH #7529).
493
+ // Pass a sentinel hint so the existing TPT child handling in `populate()` does the real work.
494
+ if (populate.all && prop.targetMeta?.inheritanceType === 'tpt') {
495
+ const visited = options.visited;
496
+ const pending = Utils.unique(children).filter(c => helper(c).__meta !== prop.targetMeta && !visited.has(c));
497
+ if (pending.length > 0) {
498
+ const hint = [
499
+ { field: prop.targetMeta.primaryKeys[0], strategy: LoadStrategy.SELECT_IN, all: true },
500
+ ];
501
+ await this.populate(prop.targetMeta.class, pending, hint, {
502
+ ...options,
503
+ lookup: false,
504
+ validate: false,
505
+ });
506
+ }
507
+ }
508
+ return;
509
+ }
510
+ const fields = this.buildFields(options.fields, prop);
511
+ const innerOrderBy = Utils.asArray(options.orderBy)
512
+ .filter(orderBy => Utils.isObject(orderBy[prop.name]))
513
+ .map(orderBy => orderBy[prop.name]);
514
+ const { refresh, filters, ignoreLazyScalarProperties, populateWhere, connectionType, logging, schema } = options;
515
+ // oxfmt-ignore
516
+ const exclude = Array.isArray(options.exclude) ? Utils.extractChildElements(options.exclude, prop.name) : options.exclude;
510
517
  const visited = options.visited;
511
- const pending = Utils.unique(children).filter(c => helper(c).__meta !== prop.targetMeta && !visited.has(c));
512
- if (pending.length > 0) {
513
- const hint = [{ field: prop.targetMeta.primaryKeys[0], strategy: LoadStrategy.SELECT_IN, all: true }];
514
- await this.populate(prop.targetMeta.class, pending, hint, {
515
- ...options,
516
- lookup: false,
517
- validate: false,
518
- });
518
+ for (const entity of entities) {
519
+ visited.delete(entity);
520
+ }
521
+ const unique = Utils.unique(children);
522
+ const filtered = unique.filter(e => !visited.has(e));
523
+ for (const entity of entities) {
524
+ visited.add(entity);
525
+ }
526
+ if (!prop.targetMeta) {
527
+ return;
528
+ }
529
+ const populateChildren = async (targetMeta, items) => {
530
+ await this.populate(targetMeta.class, items, populate.children ?? populate.all, {
531
+ where: (await this.extractChildCondition(options, prop, false)),
532
+ orderBy: innerOrderBy,
533
+ fields,
534
+ exclude,
535
+ validate: false,
536
+ lookup: false,
537
+ filters,
538
+ ignoreLazyScalarProperties,
539
+ populateWhere,
540
+ connectionType,
541
+ logging,
542
+ schema,
543
+ // @ts-ignore not a public option, will be propagated to the populate call
544
+ refresh: refresh && !filtered.every(item => options.visited.has(item)),
545
+ // @ts-ignore not a public option, will be propagated to the populate call
546
+ visited: options.visited,
547
+ // @ts-ignore not a public option
548
+ filtered,
549
+ });
550
+ };
551
+ if (prop.polymorphic && prop.polymorphTargets) {
552
+ await Promise.all(prop.polymorphTargets.map(async (targetMeta) => {
553
+ const targetChildren = unique.filter(child => helper(child).__meta.className === targetMeta.className);
554
+ if (targetChildren.length > 0) {
555
+ await populateChildren(targetMeta, targetChildren);
556
+ }
557
+ }));
558
+ }
559
+ else {
560
+ await populateChildren(prop.targetMeta, unique);
519
561
  }
520
- }
521
- return;
522
- }
523
- const fields = this.buildFields(options.fields, prop);
524
- const innerOrderBy = Utils.asArray(options.orderBy)
525
- .filter(orderBy => Utils.isObject(orderBy[prop.name]))
526
- .map(orderBy => orderBy[prop.name]);
527
- const { refresh, filters, ignoreLazyScalarProperties, populateWhere, connectionType, logging, schema } = options;
528
- // oxfmt-ignore
529
- const exclude = Array.isArray(options.exclude) ? Utils.extractChildElements(options.exclude, prop.name) : options.exclude;
530
- const visited = options.visited;
531
- for (const entity of entities) {
532
- visited.delete(entity);
533
- }
534
- const unique = Utils.unique(children);
535
- const filtered = unique.filter(e => !visited.has(e));
536
- for (const entity of entities) {
537
- visited.add(entity);
538
- }
539
- if (!prop.targetMeta) {
540
- return;
541
- }
542
- const populateChildren = async (targetMeta, items) => {
543
- await this.populate(targetMeta.class, items, populate.children ?? populate.all, {
544
- where: await this.extractChildCondition(options, prop, false),
545
- orderBy: innerOrderBy,
546
- fields,
547
- exclude,
548
- validate: false,
549
- lookup: false,
550
- filters,
551
- ignoreLazyScalarProperties,
552
- populateWhere,
553
- connectionType,
554
- logging,
555
- schema,
556
- // @ts-ignore not a public option, will be propagated to the populate call
557
- refresh: refresh && !filtered.every(item => options.visited.has(item)),
558
- // @ts-ignore not a public option, will be propagated to the populate call
559
- visited: options.visited,
560
- // @ts-ignore not a public option
561
- filtered,
562
- });
563
- };
564
- if (prop.polymorphic && prop.polymorphTargets) {
565
- await Promise.all(
566
- prop.polymorphTargets.map(async targetMeta => {
567
- const targetChildren = unique.filter(child => helper(child).__meta.className === targetMeta.className);
568
- if (targetChildren.length > 0) {
569
- await populateChildren(targetMeta, targetChildren);
570
- }
571
- }),
572
- );
573
- } else {
574
- await populateChildren(prop.targetMeta, unique);
575
- }
576
- }
577
- /** @internal */
578
- async findChildrenFromPivotTable(filtered, prop, options, orderBy, populate, pivotJoin) {
579
- const ids = filtered.map(e => e.__helper.__primaryKeys);
580
- const refresh = options.refresh;
581
- let where = await this.extractChildCondition(options, prop, true);
582
- const fields = this.buildFields(options.fields, prop);
583
- // oxfmt-ignore
584
- const exclude = Array.isArray(options.exclude) ? Utils.extractChildElements(options.exclude, prop.name) : options.exclude;
585
- const populateFilter = options.populateFilter?.[prop.name];
586
- const options2 = { ...options, fields, exclude, populateFilter };
587
- ['limit', 'offset', 'first', 'last', 'before', 'after', 'overfetch'].forEach(prop => delete options2[prop]);
588
- options2.populate = populate?.children ?? [];
589
- if (!Utils.isEmpty(prop.where)) {
590
- where = { $and: [where, prop.where] };
591
562
  }
592
- const map = await this.#driver.loadFromPivotTable(
593
- prop,
594
- ids,
595
- where,
596
- orderBy,
597
- this.#em.getTransactionContext(),
598
- options2,
599
- pivotJoin,
600
- );
601
- const children = [];
602
- for (let i = 0; i < filtered.length; i++) {
603
- const entity = filtered[i];
604
- const items = map[Utils.getPrimaryKeyHash(ids[i])].map(item => {
605
- if (pivotJoin) {
606
- return this.#em.getReference(prop.targetMeta.class, item, {
607
- convertCustomTypes: true,
608
- schema: options.schema ?? this.#em.config.get('schema'),
609
- });
610
- }
611
- const entity = this.#em.getEntityFactory().create(prop.targetMeta.class, item, {
612
- refresh,
613
- merge: true,
614
- convertCustomTypes: true,
615
- schema: options.schema ?? this.#em.config.get('schema'),
563
+ /** @internal */
564
+ async findChildrenFromPivotTable(filtered, prop, options, orderBy, populate, pivotJoin) {
565
+ const ids = filtered.map(e => e.__helper.__primaryKeys);
566
+ const refresh = options.refresh;
567
+ let where = await this.extractChildCondition(options, prop, true);
568
+ const fields = this.buildFields(options.fields, prop);
569
+ // oxfmt-ignore
570
+ const exclude = Array.isArray(options.exclude) ? Utils.extractChildElements(options.exclude, prop.name) : options.exclude;
571
+ const populateFilter = options.populateFilter?.[prop.name];
572
+ const options2 = { ...options, fields, exclude, populateFilter };
573
+ ['limit', 'offset', 'first', 'last', 'before', 'after', 'overfetch'].forEach(prop => delete options2[prop]);
574
+ options2.populate = populate?.children ?? [];
575
+ if (!Utils.isEmpty(prop.where)) {
576
+ where = { $and: [where, prop.where] };
577
+ }
578
+ const map = await this.#driver.loadFromPivotTable(prop, ids, where, orderBy, this.#em.getTransactionContext(), options2, pivotJoin);
579
+ const children = [];
580
+ for (let i = 0; i < filtered.length; i++) {
581
+ const entity = filtered[i];
582
+ const items = map[Utils.getPrimaryKeyHash(ids[i])].map(item => {
583
+ if (pivotJoin) {
584
+ return this.#em.getReference(prop.targetMeta.class, item, {
585
+ convertCustomTypes: true,
586
+ schema: options.schema ?? this.#em.config.get('schema'),
587
+ });
588
+ }
589
+ const entity = this.#em.getEntityFactory().create(prop.targetMeta.class, item, {
590
+ refresh,
591
+ merge: true,
592
+ convertCustomTypes: true,
593
+ schema: options.schema ?? this.#em.config.get('schema'),
594
+ });
595
+ return this.#em.getUnitOfWork().register(entity, item, { refresh, loaded: true });
596
+ });
597
+ entity[prop.name].hydrate(items, true);
598
+ children.push(items);
599
+ }
600
+ return children;
601
+ }
602
+ async extractChildCondition(options, prop, filters = false) {
603
+ const where = options.where;
604
+ const subCond = Utils.isPlainObject(where[prop.name]) ? where[prop.name] : {};
605
+ const meta2 = prop.targetMeta;
606
+ const pk = Utils.getPrimaryKeyHash(meta2.primaryKeys);
607
+ ['$and', '$or'].forEach(op => {
608
+ if (where[op]) {
609
+ const child = where[op]
610
+ .map((cond) => cond[prop.name])
611
+ .filter((sub) => sub != null &&
612
+ !(Utils.isPlainObject(sub) && Utils.getObjectQueryKeys(sub).every(key => Utils.isOperator(key, false))))
613
+ .map((cond) => {
614
+ if (Utils.isPrimaryKey(cond)) {
615
+ return { [pk]: cond };
616
+ }
617
+ return cond;
618
+ });
619
+ if (child.length > 0) {
620
+ subCond[op] = child;
621
+ }
622
+ }
616
623
  });
617
- return this.#em.getUnitOfWork().register(entity, item, { refresh, loaded: true });
618
- });
619
- entity[prop.name].hydrate(items, true);
620
- children.push(items);
624
+ const operators = Object.keys(subCond).filter(key => Utils.isOperator(key, false));
625
+ if (operators.length > 0) {
626
+ operators.forEach(op => {
627
+ subCond[pk] ??= {};
628
+ subCond[pk][op] = subCond[op];
629
+ delete subCond[op];
630
+ });
631
+ }
632
+ if (filters) {
633
+ return this.#em.applyFilters(meta2.class, subCond, options.filters, 'read', options);
634
+ }
635
+ return subCond;
621
636
  }
622
- return children;
623
- }
624
- async extractChildCondition(options, prop, filters = false) {
625
- const where = options.where;
626
- const subCond = Utils.isPlainObject(where[prop.name]) ? where[prop.name] : {};
627
- const meta2 = prop.targetMeta;
628
- const pk = Utils.getPrimaryKeyHash(meta2.primaryKeys);
629
- ['$and', '$or'].forEach(op => {
630
- if (where[op]) {
631
- const child = where[op]
632
- .map(cond => cond[prop.name])
633
- .filter(
634
- sub =>
635
- sub != null &&
636
- !(Utils.isPlainObject(sub) && Utils.getObjectQueryKeys(sub).every(key => Utils.isOperator(key, false))),
637
- )
638
- .map(cond => {
639
- if (Utils.isPrimaryKey(cond)) {
640
- return { [pk]: cond };
641
- }
642
- return cond;
643
- });
644
- if (child.length > 0) {
645
- subCond[op] = child;
646
- }
647
- }
648
- });
649
- const operators = Object.keys(subCond).filter(key => Utils.isOperator(key, false));
650
- if (operators.length > 0) {
651
- operators.forEach(op => {
652
- subCond[pk] ??= {};
653
- subCond[pk][op] = subCond[op];
654
- delete subCond[op];
655
- });
637
+ buildFields(fields = [], prop, ref) {
638
+ if (ref) {
639
+ fields = prop.targetMeta.primaryKeys.map(targetPkName => `${prop.name}.${targetPkName}`);
640
+ }
641
+ const ret = fields.reduce((ret, f) => {
642
+ if (Utils.isPlainObject(f)) {
643
+ Utils.keys(f)
644
+ .filter(ff => ff === prop.name)
645
+ .forEach(ff => ret.push(...f[ff]));
646
+ }
647
+ else if (f.toString().includes('.')) {
648
+ const parts = f.toString().split('.');
649
+ const propName = parts.shift();
650
+ const childPropName = parts.join('.');
651
+ /* v8 ignore next */
652
+ if (propName === prop.name) {
653
+ ret.push(childPropName);
654
+ }
655
+ }
656
+ return ret;
657
+ }, []);
658
+ // we need to automatically select the FKs too, e.g. for 1:m relations to be able to wire them with the items
659
+ if (prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY) {
660
+ const owner = prop.targetMeta.properties[prop.mappedBy];
661
+ // when the owning FK is lazy, we need to explicitly select it even without user-provided fields,
662
+ // otherwise the driver will exclude it and we won't be able to map children to their parent collections
663
+ if (owner && !ret.includes(owner.name) && (ret.length > 0 || owner.lazy)) {
664
+ ret.push(owner.name);
665
+ }
666
+ }
667
+ if (ret.length === 0) {
668
+ return undefined;
669
+ }
670
+ return ret;
656
671
  }
657
- if (filters) {
658
- return this.#em.applyFilters(meta2.class, subCond, options.filters, 'read', options);
672
+ getChildReferences(entities, prop, options, ref) {
673
+ const filtered = this.filterCollections(entities, prop.name, options, ref);
674
+ if (prop.kind === ReferenceKind.ONE_TO_MANY) {
675
+ return filtered.map(e => e[prop.name].owner);
676
+ }
677
+ if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner) {
678
+ return filtered.reduce((a, b) => {
679
+ a.push(...b[prop.name].getItems());
680
+ return a;
681
+ }, []);
682
+ }
683
+ if (prop.kind === ReferenceKind.MANY_TO_MANY) {
684
+ // inverse side
685
+ return filtered;
686
+ }
687
+ // MANY_TO_ONE or ONE_TO_ONE
688
+ return this.filterReferences(entities, prop.name, options, ref);
659
689
  }
660
- return subCond;
661
- }
662
- buildFields(fields = [], prop, ref) {
663
- if (ref) {
664
- fields = prop.targetMeta.primaryKeys.map(targetPkName => `${prop.name}.${targetPkName}`);
690
+ filterCollections(entities, field, options, ref) {
691
+ if (options.refresh) {
692
+ return entities.filter(e => e[field]);
693
+ }
694
+ return entities.filter(e => Utils.isCollection(e[field]) && !e[field].isInitialized(!ref));
665
695
  }
666
- const ret = fields.reduce((ret, f) => {
667
- if (Utils.isPlainObject(f)) {
668
- Utils.keys(f)
669
- .filter(ff => ff === prop.name)
670
- .forEach(ff => ret.push(...f[ff]));
671
- } else if (f.toString().includes('.')) {
672
- const parts = f.toString().split('.');
673
- const propName = parts.shift();
674
- const childPropName = parts.join('.');
696
+ isPropertyLoaded(entity, field) {
697
+ if (!entity || field === '*') {
698
+ return true;
699
+ }
700
+ const wrapped = helper(entity);
701
+ if (!field.includes('.')) {
702
+ return wrapped.__loadedProperties.has(field);
703
+ }
704
+ const [f, ...r] = field.split('.');
675
705
  /* v8 ignore next */
676
- if (propName === prop.name) {
677
- ret.push(childPropName);
678
- }
679
- }
680
- return ret;
681
- }, []);
682
- // we need to automatically select the FKs too, e.g. for 1:m relations to be able to wire them with the items
683
- if (prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY) {
684
- const owner = prop.targetMeta.properties[prop.mappedBy];
685
- // when the owning FK is lazy, we need to explicitly select it even without user-provided fields,
686
- // otherwise the driver will exclude it and we won't be able to map children to their parent collections
687
- if (owner && !ret.includes(owner.name) && (ret.length > 0 || owner.lazy)) {
688
- ret.push(owner.name);
689
- }
690
- }
691
- if (ret.length === 0) {
692
- return undefined;
693
- }
694
- return ret;
695
- }
696
- getChildReferences(entities, prop, options, ref) {
697
- const filtered = this.filterCollections(entities, prop.name, options, ref);
698
- if (prop.kind === ReferenceKind.ONE_TO_MANY) {
699
- return filtered.map(e => e[prop.name].owner);
700
- }
701
- if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner) {
702
- return filtered.reduce((a, b) => {
703
- a.push(...b[prop.name].getItems());
704
- return a;
705
- }, []);
706
- }
707
- if (prop.kind === ReferenceKind.MANY_TO_MANY) {
708
- // inverse side
709
- return filtered;
710
- }
711
- // MANY_TO_ONE or ONE_TO_ONE
712
- return this.filterReferences(entities, prop.name, options, ref);
713
- }
714
- filterCollections(entities, field, options, ref) {
715
- if (options.refresh) {
716
- return entities.filter(e => e[field]);
717
- }
718
- return entities.filter(e => Utils.isCollection(e[field]) && !e[field].isInitialized(!ref));
719
- }
720
- isPropertyLoaded(entity, field) {
721
- if (!entity || field === '*') {
722
- return true;
723
- }
724
- const wrapped = helper(entity);
725
- if (!field.includes('.')) {
726
- return wrapped.__loadedProperties.has(field);
727
- }
728
- const [f, ...r] = field.split('.');
729
- /* v8 ignore next */
730
- if (!wrapped.__loadedProperties.has(f) || !wrapped.__meta.properties[f]?.targetMeta) {
731
- return false;
732
- }
733
- if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(wrapped.__meta.properties[f].kind)) {
734
- return entity[f].getItems(false).every(item => this.isPropertyLoaded(item, r.join('.')));
735
- }
736
- return this.isPropertyLoaded(entity[f], r.join('.'));
737
- }
738
- filterReferences(entities, field, options, ref) {
739
- if (ref) {
740
- return [];
706
+ if (!wrapped.__loadedProperties.has(f) || !wrapped.__meta.properties[f]?.targetMeta) {
707
+ return false;
708
+ }
709
+ if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(wrapped.__meta.properties[f].kind)) {
710
+ return entity[f].getItems(false).every((item) => this.isPropertyLoaded(item, r.join('.')));
711
+ }
712
+ return this.isPropertyLoaded(entity[f], r.join('.'));
741
713
  }
742
- const children = entities.filter(e => Utils.isEntity(e[field], true));
743
- if (options.refresh) {
744
- return children.map(e => Reference.unwrapReference(e[field]));
714
+ filterReferences(entities, field, options, ref) {
715
+ if (ref) {
716
+ return [];
717
+ }
718
+ const children = entities.filter(e => Utils.isEntity(e[field], true));
719
+ if (options.refresh) {
720
+ return children.map(e => Reference.unwrapReference(e[field]));
721
+ }
722
+ if (options.fields) {
723
+ return children
724
+ .map(e => Reference.unwrapReference(e[field]))
725
+ .filter(target => {
726
+ const wrapped = helper(target);
727
+ const childFields = options.fields
728
+ .filter(f => f.startsWith(`${field}.`))
729
+ .map(f => f.substring(field.length + 1));
730
+ return !wrapped.__initialized || !childFields.every(cf => this.isPropertyLoaded(target, cf));
731
+ });
732
+ }
733
+ return children
734
+ .filter(e => !e[field].__helper.__initialized)
735
+ .map(e => Reference.unwrapReference(e[field]));
745
736
  }
746
- if (options.fields) {
747
- return children
748
- .map(e => Reference.unwrapReference(e[field]))
749
- .filter(target => {
750
- const wrapped = helper(target);
751
- const childFields = options.fields
752
- .filter(f => f.startsWith(`${field}.`))
753
- .map(f => f.substring(field.length + 1));
754
- return !wrapped.__initialized || !childFields.every(cf => this.isPropertyLoaded(target, cf));
737
+ filterByReferences(entities, field, refresh) {
738
+ /* v8 ignore next */
739
+ if (refresh) {
740
+ return entities;
741
+ }
742
+ return entities.filter(e => e[field] !== null && !e[field]?.__helper?.__initialized);
743
+ }
744
+ lookupAllRelationships(entityName) {
745
+ const ret = [];
746
+ const meta = this.#metadata.find(entityName);
747
+ meta.relations.forEach(prop => {
748
+ ret.push({
749
+ field: this.getRelationName(meta, prop),
750
+ // force select-in strategy when populating all relations as otherwise we could cause infinite loops when self-referencing
751
+ strategy: LoadStrategy.SELECT_IN,
752
+ // no need to look up populate children recursively as we just pass `all: true` here
753
+ all: true,
754
+ });
755
755
  });
756
+ // For TPT parents with child types, keep an all:true sentinel so the populate
757
+ // loop doesn't exit early and the TPT child relation population can run after it.
758
+ if (ret.length === 0 && meta.inheritanceType === 'tpt' && meta.tptChildren?.length) {
759
+ ret.push({ field: meta.primaryKeys[0], strategy: LoadStrategy.SELECT_IN, all: true });
760
+ }
761
+ return ret;
756
762
  }
757
- return children.filter(e => !e[field].__helper.__initialized).map(e => Reference.unwrapReference(e[field]));
758
- }
759
- filterByReferences(entities, field, refresh) {
760
- /* v8 ignore next */
761
- if (refresh) {
762
- return entities;
763
- }
764
- return entities.filter(e => e[field] !== null && !e[field]?.__helper?.__initialized);
765
- }
766
- lookupAllRelationships(entityName) {
767
- const ret = [];
768
- const meta = this.#metadata.find(entityName);
769
- meta.relations.forEach(prop => {
770
- ret.push({
771
- field: this.getRelationName(meta, prop),
772
- // force select-in strategy when populating all relations as otherwise we could cause infinite loops when self-referencing
773
- strategy: LoadStrategy.SELECT_IN,
774
- // no need to look up populate children recursively as we just pass `all: true` here
775
- all: true,
776
- });
777
- });
778
- // For TPT parents with child types, keep an all:true sentinel so the populate
779
- // loop doesn't exit early and the TPT child relation population can run after it.
780
- if (ret.length === 0 && meta.inheritanceType === 'tpt' && meta.tptChildren?.length) {
781
- ret.push({ field: meta.primaryKeys[0], strategy: LoadStrategy.SELECT_IN, all: true });
782
- }
783
- return ret;
784
- }
785
- getRelationName(meta, prop) {
786
- if (!prop.embedded) {
787
- return prop.name;
788
- }
789
- return `${this.getRelationName(meta, meta.properties[prop.embedded[0]])}.${prop.embedded[1]}`;
790
- }
791
- lookupEagerLoadedRelationships(entityName, populate, strategy, prefix = '', visited = [], exclude) {
792
- const meta = this.#metadata.find(entityName);
793
- if (!meta && !prefix) {
794
- return populate;
763
+ getRelationName(meta, prop) {
764
+ if (!prop.embedded) {
765
+ return prop.name;
766
+ }
767
+ return `${this.getRelationName(meta, meta.properties[prop.embedded[0]])}.${prop.embedded[1]}`;
795
768
  }
796
- if (!meta || visited.includes(meta)) {
797
- return [];
769
+ lookupEagerLoadedRelationships(entityName, populate, strategy, prefix = '', visited = [], exclude) {
770
+ const meta = this.#metadata.find(entityName);
771
+ if (!meta && !prefix) {
772
+ return populate;
773
+ }
774
+ if (!meta || visited.includes(meta)) {
775
+ return [];
776
+ }
777
+ visited.push(meta);
778
+ const ret = prefix === '' ? [...populate] : [];
779
+ meta.relations
780
+ .filter(prop => {
781
+ const field = this.getRelationName(meta, prop);
782
+ const prefixed = prefix ? `${prefix}.${field}` : field;
783
+ const isExcluded = exclude?.includes(prefixed);
784
+ const eager = prop.eager && !populate.some(p => p.field === `${prop.name}:ref`);
785
+ const populated = populate.some(p => p.field === prop.name);
786
+ const disabled = populate.some(p => p.field === prop.name && p.all === false);
787
+ return !disabled && !isExcluded && (eager || populated);
788
+ })
789
+ .forEach(prop => {
790
+ const field = this.getRelationName(meta, prop);
791
+ const prefixed = prefix ? `${prefix}.${field}` : field;
792
+ const nestedPopulate = populate
793
+ .filter(p => p.field === prop.name)
794
+ .flatMap(p => p.children)
795
+ .filter(Boolean);
796
+ const nested = this.lookupEagerLoadedRelationships(prop.targetMeta.class, nestedPopulate, strategy, prefixed, visited.slice(), exclude);
797
+ if (nested.length > 0) {
798
+ ret.push(...nested);
799
+ }
800
+ else {
801
+ const selfReferencing = [meta.tableName, ...visited.map(m => m.tableName)].includes(prop.targetMeta.tableName) && prop.eager;
802
+ ret.push({
803
+ field: prefixed,
804
+ // enforce select-in strategy for self-referencing relations
805
+ strategy: selfReferencing ? LoadStrategy.SELECT_IN : (strategy ?? prop.strategy),
806
+ });
807
+ }
808
+ });
809
+ return ret;
798
810
  }
799
- visited.push(meta);
800
- const ret = prefix === '' ? [...populate] : [];
801
- meta.relations
802
- .filter(prop => {
803
- const field = this.getRelationName(meta, prop);
804
- const prefixed = prefix ? `${prefix}.${field}` : field;
805
- const isExcluded = exclude?.includes(prefixed);
806
- const eager = prop.eager && !populate.some(p => p.field === `${prop.name}:ref`);
807
- const populated = populate.some(p => p.field === prop.name);
808
- const disabled = populate.some(p => p.field === prop.name && p.all === false);
809
- return !disabled && !isExcluded && (eager || populated);
810
- })
811
- .forEach(prop => {
812
- const field = this.getRelationName(meta, prop);
813
- const prefixed = prefix ? `${prefix}.${field}` : field;
814
- const nestedPopulate = populate
815
- .filter(p => p.field === prop.name)
816
- .flatMap(p => p.children)
817
- .filter(Boolean);
818
- const nested = this.lookupEagerLoadedRelationships(
819
- prop.targetMeta.class,
820
- nestedPopulate,
821
- strategy,
822
- prefixed,
823
- visited.slice(),
824
- exclude,
825
- );
826
- if (nested.length > 0) {
827
- ret.push(...nested);
828
- } else {
829
- const selfReferencing =
830
- [meta.tableName, ...visited.map(m => m.tableName)].includes(prop.targetMeta.tableName) && prop.eager;
831
- ret.push({
832
- field: prefixed,
833
- // enforce select-in strategy for self-referencing relations
834
- strategy: selfReferencing ? LoadStrategy.SELECT_IN : (strategy ?? prop.strategy),
835
- });
836
- }
837
- });
838
- return ret;
839
- }
840
811
  }