@mikro-orm/core 7.0.9-dev.8 → 7.0.9
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.
- package/EntityManager.d.ts +884 -583
- package/EntityManager.js +1926 -1899
- package/MikroORM.d.ts +103 -74
- package/MikroORM.js +177 -179
- package/README.md +1 -1
- package/cache/CacheAdapter.d.ts +36 -36
- package/cache/FileCacheAdapter.d.ts +30 -24
- package/cache/FileCacheAdapter.js +80 -78
- package/cache/GeneratedCacheAdapter.d.ts +18 -20
- package/cache/GeneratedCacheAdapter.js +30 -30
- package/cache/MemoryCacheAdapter.d.ts +18 -20
- package/cache/MemoryCacheAdapter.js +35 -36
- package/cache/NullCacheAdapter.d.ts +16 -16
- package/cache/NullCacheAdapter.js +24 -24
- package/connections/Connection.d.ts +95 -84
- package/connections/Connection.js +165 -168
- package/drivers/DatabaseDriver.d.ts +187 -81
- package/drivers/DatabaseDriver.js +450 -444
- package/drivers/IDatabaseDriver.d.ts +440 -301
- package/entity/BaseEntity.d.ts +120 -83
- package/entity/BaseEntity.js +43 -43
- package/entity/Collection.d.ts +215 -181
- package/entity/Collection.js +730 -724
- package/entity/EntityAssigner.d.ts +88 -77
- package/entity/EntityAssigner.js +231 -230
- package/entity/EntityFactory.d.ts +67 -55
- package/entity/EntityFactory.js +457 -414
- package/entity/EntityHelper.d.ts +35 -23
- package/entity/EntityHelper.js +291 -279
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +98 -72
- package/entity/EntityLoader.js +792 -761
- package/entity/EntityRepository.d.ts +316 -201
- package/entity/EntityRepository.js +213 -213
- package/entity/PolymorphicRef.d.ts +5 -5
- package/entity/PolymorphicRef.js +10 -10
- package/entity/Reference.d.ts +127 -83
- package/entity/Reference.js +281 -277
- package/entity/WrappedEntity.d.ts +115 -72
- package/entity/WrappedEntity.js +168 -166
- package/entity/defineEntity.d.ts +1359 -654
- package/entity/defineEntity.js +527 -518
- package/entity/utils.d.ts +13 -3
- package/entity/utils.js +71 -73
- package/entity/validators.js +43 -43
- package/entity/wrap.js +8 -8
- package/enums.d.ts +258 -253
- package/enums.js +251 -252
- package/errors.d.ts +114 -72
- package/errors.js +350 -253
- package/events/EventManager.d.ts +26 -14
- package/events/EventManager.js +79 -77
- package/events/EventSubscriber.d.ts +29 -29
- package/events/TransactionEventBroadcaster.d.ts +15 -8
- package/events/TransactionEventBroadcaster.js +14 -14
- package/exceptions.d.ts +23 -40
- package/exceptions.js +35 -52
- package/hydration/Hydrator.d.ts +42 -17
- package/hydration/Hydrator.js +43 -43
- package/hydration/ObjectHydrator.d.ts +50 -17
- package/hydration/ObjectHydrator.js +483 -418
- package/index.d.ts +116 -2
- package/index.js +10 -1
- package/logging/DefaultLogger.d.ts +34 -32
- package/logging/DefaultLogger.js +86 -86
- package/logging/Logger.d.ts +41 -41
- package/logging/SimpleLogger.d.ts +13 -11
- package/logging/SimpleLogger.js +22 -22
- package/logging/colors.d.ts +6 -6
- package/logging/colors.js +11 -10
- package/logging/inspect.js +7 -7
- package/metadata/EntitySchema.d.ts +214 -130
- package/metadata/EntitySchema.js +411 -412
- package/metadata/MetadataDiscovery.d.ts +114 -114
- package/metadata/MetadataDiscovery.js +1957 -1879
- package/metadata/MetadataProvider.d.ts +29 -26
- package/metadata/MetadataProvider.js +95 -97
- package/metadata/MetadataStorage.d.ts +38 -32
- package/metadata/MetadataStorage.js +118 -118
- package/metadata/MetadataValidator.d.ts +39 -39
- package/metadata/MetadataValidator.js +381 -338
- package/metadata/discover-entities.d.ts +5 -2
- package/metadata/discover-entities.js +35 -37
- package/metadata/types.d.ts +615 -531
- package/naming-strategy/AbstractNamingStrategy.d.ts +54 -39
- package/naming-strategy/AbstractNamingStrategy.js +90 -85
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +6 -6
- package/naming-strategy/EntityCaseNamingStrategy.js +22 -22
- package/naming-strategy/MongoNamingStrategy.d.ts +6 -6
- package/naming-strategy/MongoNamingStrategy.js +18 -18
- package/naming-strategy/NamingStrategy.d.ts +109 -99
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
- package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
- package/not-supported.js +7 -4
- package/package.json +1 -1
- package/platforms/ExceptionConverter.d.ts +1 -1
- package/platforms/ExceptionConverter.js +4 -4
- package/platforms/Platform.d.ts +312 -303
- package/platforms/Platform.js +695 -642
- package/serialization/EntitySerializer.d.ts +49 -26
- package/serialization/EntitySerializer.js +224 -218
- package/serialization/EntityTransformer.d.ts +10 -6
- package/serialization/EntityTransformer.js +219 -217
- package/serialization/SerializationContext.d.ts +27 -23
- package/serialization/SerializationContext.js +105 -105
- package/types/ArrayType.d.ts +8 -8
- package/types/ArrayType.js +33 -33
- package/types/BigIntType.d.ts +17 -10
- package/types/BigIntType.js +37 -37
- package/types/BlobType.d.ts +3 -3
- package/types/BlobType.js +13 -13
- package/types/BooleanType.d.ts +4 -4
- package/types/BooleanType.js +12 -12
- package/types/CharacterType.d.ts +2 -2
- package/types/CharacterType.js +6 -6
- package/types/DateTimeType.d.ts +5 -5
- package/types/DateTimeType.js +15 -15
- package/types/DateType.d.ts +5 -5
- package/types/DateType.js +15 -15
- package/types/DecimalType.d.ts +7 -7
- package/types/DecimalType.js +26 -26
- package/types/DoubleType.d.ts +3 -3
- package/types/DoubleType.js +12 -12
- package/types/EnumArrayType.d.ts +5 -5
- package/types/EnumArrayType.js +24 -24
- package/types/EnumType.d.ts +3 -3
- package/types/EnumType.js +11 -11
- package/types/FloatType.d.ts +3 -3
- package/types/FloatType.js +9 -9
- package/types/IntegerType.d.ts +3 -3
- package/types/IntegerType.js +9 -9
- package/types/IntervalType.d.ts +4 -4
- package/types/IntervalType.js +12 -12
- package/types/JsonType.d.ts +8 -8
- package/types/JsonType.js +32 -32
- package/types/MediumIntType.d.ts +1 -1
- package/types/MediumIntType.js +3 -3
- package/types/SmallIntType.d.ts +3 -3
- package/types/SmallIntType.js +9 -9
- package/types/StringType.d.ts +4 -4
- package/types/StringType.js +12 -12
- package/types/TextType.d.ts +3 -3
- package/types/TextType.js +9 -9
- package/types/TimeType.d.ts +5 -5
- package/types/TimeType.js +17 -17
- package/types/TinyIntType.d.ts +3 -3
- package/types/TinyIntType.js +10 -10
- package/types/Type.d.ts +83 -79
- package/types/Type.js +82 -82
- package/types/Uint8ArrayType.d.ts +4 -4
- package/types/Uint8ArrayType.js +21 -21
- package/types/UnknownType.d.ts +4 -4
- package/types/UnknownType.js +12 -12
- package/types/UuidType.d.ts +5 -5
- package/types/UuidType.js +19 -19
- package/types/index.d.ts +75 -49
- package/types/index.js +52 -26
- package/typings.d.ts +1254 -741
- package/typings.js +244 -233
- package/unit-of-work/ChangeSet.d.ts +26 -26
- package/unit-of-work/ChangeSet.js +56 -56
- package/unit-of-work/ChangeSetComputer.d.ts +12 -12
- package/unit-of-work/ChangeSetComputer.js +187 -179
- package/unit-of-work/ChangeSetPersister.d.ts +69 -50
- package/unit-of-work/ChangeSetPersister.js +465 -442
- package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
- package/unit-of-work/CommitOrderCalculator.js +89 -88
- package/unit-of-work/IdentityMap.d.ts +31 -31
- package/unit-of-work/IdentityMap.js +105 -105
- package/unit-of-work/UnitOfWork.d.ts +181 -141
- package/unit-of-work/UnitOfWork.js +1236 -1222
- package/utils/AbstractMigrator.d.ts +111 -91
- package/utils/AbstractMigrator.js +275 -275
- package/utils/AbstractSchemaGenerator.d.ts +43 -34
- package/utils/AbstractSchemaGenerator.js +121 -122
- package/utils/AsyncContext.d.ts +3 -3
- package/utils/AsyncContext.js +34 -35
- package/utils/Configuration.d.ts +852 -808
- package/utils/Configuration.js +359 -344
- package/utils/Cursor.d.ts +40 -22
- package/utils/Cursor.js +135 -127
- package/utils/DataloaderUtils.d.ts +58 -43
- package/utils/DataloaderUtils.js +203 -198
- package/utils/EntityComparator.d.ts +99 -82
- package/utils/EntityComparator.js +829 -737
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +3 -3
- package/utils/QueryHelper.d.ts +79 -51
- package/utils/QueryHelper.js +372 -361
- package/utils/RawQueryFragment.d.ts +50 -34
- package/utils/RawQueryFragment.js +107 -105
- package/utils/RequestContext.d.ts +32 -32
- package/utils/RequestContext.js +52 -53
- package/utils/TransactionContext.d.ts +16 -16
- package/utils/TransactionContext.js +27 -27
- package/utils/TransactionManager.d.ts +58 -58
- package/utils/TransactionManager.js +199 -197
- package/utils/Utils.d.ts +204 -145
- package/utils/Utils.js +815 -815
- package/utils/clone.js +105 -114
- package/utils/env-vars.js +90 -88
- package/utils/fs-utils.d.ts +15 -15
- package/utils/fs-utils.js +180 -181
- package/utils/upsert-utils.d.ts +20 -5
- package/utils/upsert-utils.js +114 -116
package/entity/EntityLoader.js
CHANGED
|
@@ -1,795 +1,826 @@
|
|
|
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
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
}
|
|
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);
|
|
85
26
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
// oxfmt-ignore
|
|
91
|
-
return typeof field === 'boolean' || field.field === PopulatePath.ALL ? { all: !!field, field: meta.primaryKeys[0] } : field;
|
|
92
|
-
});
|
|
93
|
-
if (normalized.some(p => p.all)) {
|
|
94
|
-
normalized = this.lookupAllRelationships(entityName);
|
|
95
|
-
}
|
|
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
|
-
});
|
|
113
|
-
}
|
|
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');
|
|
114
31
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
-
});
|
|
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 });
|
|
143
42
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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: [],
|
|
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 (
|
|
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,
|
|
207
80
|
});
|
|
81
|
+
}
|
|
208
82
|
}
|
|
209
|
-
|
|
210
|
-
|
|
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
|
-
}
|
|
83
|
+
for (const entity of entities) {
|
|
84
|
+
visited.delete(entity);
|
|
274
85
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
}
|
|
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);
|
|
293
96
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
}
|
|
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);
|
|
312
103
|
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
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;
|
|
493
424
|
}
|
|
494
|
-
const fields = this.buildFields(options.fields, prop);
|
|
495
|
-
const innerOrderBy = Utils.asArray(options.orderBy)
|
|
496
|
-
.filter(orderBy => Utils.isObject(orderBy[prop.name]))
|
|
497
|
-
.map(orderBy => orderBy[prop.name]);
|
|
498
|
-
const { refresh, filters, ignoreLazyScalarProperties, populateWhere, connectionType, logging, schema } = options;
|
|
499
425
|
// oxfmt-ignore
|
|
500
|
-
const
|
|
501
|
-
const
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
}
|
|
505
|
-
const unique = Utils.unique(children);
|
|
506
|
-
const filtered = unique.filter(e => !visited.has(e));
|
|
507
|
-
for (const entity of entities) {
|
|
508
|
-
visited.add(entity);
|
|
509
|
-
}
|
|
510
|
-
if (!prop.targetMeta) {
|
|
511
|
-
return;
|
|
512
|
-
}
|
|
513
|
-
const populateChildren = async (targetMeta, items) => {
|
|
514
|
-
await this.populate(targetMeta.class, items, populate.children ?? populate.all, {
|
|
515
|
-
where: (await this.extractChildCondition(options, prop, false)),
|
|
516
|
-
orderBy: innerOrderBy,
|
|
517
|
-
fields,
|
|
518
|
-
exclude,
|
|
519
|
-
validate: false,
|
|
520
|
-
lookup: false,
|
|
521
|
-
filters,
|
|
522
|
-
ignoreLazyScalarProperties,
|
|
523
|
-
populateWhere,
|
|
524
|
-
connectionType,
|
|
525
|
-
logging,
|
|
526
|
-
schema,
|
|
527
|
-
// @ts-ignore not a public option, will be propagated to the populate call
|
|
528
|
-
refresh: refresh && !filtered.every(item => options.visited.has(item)),
|
|
529
|
-
// @ts-ignore not a public option, will be propagated to the populate call
|
|
530
|
-
visited: options.visited,
|
|
531
|
-
// @ts-ignore not a public option
|
|
532
|
-
filtered,
|
|
533
|
-
});
|
|
534
|
-
};
|
|
535
|
-
if (prop.polymorphic && prop.polymorphTargets) {
|
|
536
|
-
await Promise.all(prop.polymorphTargets.map(async (targetMeta) => {
|
|
537
|
-
const targetChildren = unique.filter(child => helper(child).__meta.className === targetMeta.className);
|
|
538
|
-
if (targetChildren.length > 0) {
|
|
539
|
-
await populateChildren(targetMeta, targetChildren);
|
|
540
|
-
}
|
|
541
|
-
}));
|
|
542
|
-
}
|
|
543
|
-
else {
|
|
544
|
-
await populateChildren(prop.targetMeta, unique);
|
|
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;
|
|
545
430
|
}
|
|
431
|
+
}
|
|
546
432
|
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
const
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
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
|
+
}
|
|
461
|
+
}
|
|
462
|
+
for (const item of items) {
|
|
463
|
+
if (ref && !helper(item).__onLoadFired) {
|
|
464
|
+
helper(item).__initialized = false;
|
|
465
|
+
this.#em.getUnitOfWork().unmarkAsLoaded(item);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
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;
|
|
487
|
+
}
|
|
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;
|
|
492
|
+
}
|
|
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
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (populated.length === 0 && !populate.children) {
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
const fields = this.buildFields(options.fields, prop);
|
|
510
|
+
const innerOrderBy = Utils.asArray(options.orderBy)
|
|
511
|
+
.filter(orderBy => Utils.isObject(orderBy[prop.name]))
|
|
512
|
+
.map(orderBy => orderBy[prop.name]);
|
|
513
|
+
const { refresh, filters, ignoreLazyScalarProperties, populateWhere, connectionType, logging, schema } = options;
|
|
514
|
+
// oxfmt-ignore
|
|
515
|
+
const exclude = Array.isArray(options.exclude) ? Utils.extractChildElements(options.exclude, prop.name) : options.exclude;
|
|
516
|
+
const visited = options.visited;
|
|
517
|
+
for (const entity of entities) {
|
|
518
|
+
visited.delete(entity);
|
|
519
|
+
}
|
|
520
|
+
const unique = Utils.unique(children);
|
|
521
|
+
const filtered = unique.filter(e => !visited.has(e));
|
|
522
|
+
for (const entity of entities) {
|
|
523
|
+
visited.add(entity);
|
|
524
|
+
}
|
|
525
|
+
if (!prop.targetMeta) {
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
const populateChildren = async (targetMeta, items) => {
|
|
529
|
+
await this.populate(targetMeta.class, items, populate.children ?? populate.all, {
|
|
530
|
+
where: await this.extractChildCondition(options, prop, false),
|
|
531
|
+
orderBy: innerOrderBy,
|
|
532
|
+
fields,
|
|
533
|
+
exclude,
|
|
534
|
+
validate: false,
|
|
535
|
+
lookup: false,
|
|
536
|
+
filters,
|
|
537
|
+
ignoreLazyScalarProperties,
|
|
538
|
+
populateWhere,
|
|
539
|
+
connectionType,
|
|
540
|
+
logging,
|
|
541
|
+
schema,
|
|
542
|
+
// @ts-ignore not a public option, will be propagated to the populate call
|
|
543
|
+
refresh: refresh && !filtered.every(item => options.visited.has(item)),
|
|
544
|
+
// @ts-ignore not a public option, will be propagated to the populate call
|
|
545
|
+
visited: options.visited,
|
|
546
|
+
// @ts-ignore not a public option
|
|
547
|
+
filtered,
|
|
548
|
+
});
|
|
549
|
+
};
|
|
550
|
+
if (prop.polymorphic && prop.polymorphTargets) {
|
|
551
|
+
await Promise.all(
|
|
552
|
+
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);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
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(
|
|
579
|
+
prop,
|
|
580
|
+
ids,
|
|
581
|
+
where,
|
|
582
|
+
orderBy,
|
|
583
|
+
this.#em.getTransactionContext(),
|
|
584
|
+
options2,
|
|
585
|
+
pivotJoin,
|
|
586
|
+
);
|
|
587
|
+
const children = [];
|
|
588
|
+
for (let i = 0; i < filtered.length; i++) {
|
|
589
|
+
const entity = filtered[i];
|
|
590
|
+
const items = map[Utils.getPrimaryKeyHash(ids[i])].map(item => {
|
|
591
|
+
if (pivotJoin) {
|
|
592
|
+
return this.#em.getReference(prop.targetMeta.class, item, {
|
|
593
|
+
convertCustomTypes: true,
|
|
594
|
+
schema: options.schema ?? this.#em.config.get('schema'),
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
const entity = this.#em.getEntityFactory().create(prop.targetMeta.class, item, {
|
|
598
|
+
refresh,
|
|
599
|
+
merge: true,
|
|
600
|
+
convertCustomTypes: true,
|
|
601
|
+
schema: options.schema ?? this.#em.config.get('schema'),
|
|
607
602
|
});
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
subCond[pk][op] = subCond[op];
|
|
613
|
-
delete subCond[op];
|
|
614
|
-
});
|
|
615
|
-
}
|
|
616
|
-
if (filters) {
|
|
617
|
-
return this.#em.applyFilters(meta2.class, subCond, options.filters, 'read', options);
|
|
618
|
-
}
|
|
619
|
-
return subCond;
|
|
603
|
+
return this.#em.getUnitOfWork().register(entity, item, { refresh, loaded: true });
|
|
604
|
+
});
|
|
605
|
+
entity[prop.name].hydrate(items, true);
|
|
606
|
+
children.push(items);
|
|
620
607
|
}
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
if (
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
608
|
+
return children;
|
|
609
|
+
}
|
|
610
|
+
async extractChildCondition(options, prop, filters = false) {
|
|
611
|
+
const where = options.where;
|
|
612
|
+
const subCond = Utils.isPlainObject(where[prop.name]) ? where[prop.name] : {};
|
|
613
|
+
const meta2 = prop.targetMeta;
|
|
614
|
+
const pk = Utils.getPrimaryKeyHash(meta2.primaryKeys);
|
|
615
|
+
['$and', '$or'].forEach(op => {
|
|
616
|
+
if (where[op]) {
|
|
617
|
+
const child = where[op]
|
|
618
|
+
.map(cond => cond[prop.name])
|
|
619
|
+
.filter(
|
|
620
|
+
sub =>
|
|
621
|
+
sub != null &&
|
|
622
|
+
!(Utils.isPlainObject(sub) && Utils.getObjectQueryKeys(sub).every(key => Utils.isOperator(key, false))),
|
|
623
|
+
)
|
|
624
|
+
.map(cond => {
|
|
625
|
+
if (Utils.isPrimaryKey(cond)) {
|
|
626
|
+
return { [pk]: cond };
|
|
627
|
+
}
|
|
628
|
+
return cond;
|
|
629
|
+
});
|
|
630
|
+
if (child.length > 0) {
|
|
631
|
+
subCond[op] = child;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
const operators = Object.keys(subCond).filter(key => Utils.isOperator(key, false));
|
|
636
|
+
if (operators.length > 0) {
|
|
637
|
+
operators.forEach(op => {
|
|
638
|
+
subCond[pk] ??= {};
|
|
639
|
+
subCond[pk][op] = subCond[op];
|
|
640
|
+
delete subCond[op];
|
|
641
|
+
});
|
|
655
642
|
}
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
if (prop.kind === ReferenceKind.ONE_TO_MANY) {
|
|
659
|
-
return filtered.map(e => e[prop.name].owner);
|
|
660
|
-
}
|
|
661
|
-
if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner) {
|
|
662
|
-
return filtered.reduce((a, b) => {
|
|
663
|
-
a.push(...b[prop.name].getItems());
|
|
664
|
-
return a;
|
|
665
|
-
}, []);
|
|
666
|
-
}
|
|
667
|
-
if (prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
668
|
-
// inverse side
|
|
669
|
-
return filtered;
|
|
670
|
-
}
|
|
671
|
-
// MANY_TO_ONE or ONE_TO_ONE
|
|
672
|
-
return this.filterReferences(entities, prop.name, options, ref);
|
|
643
|
+
if (filters) {
|
|
644
|
+
return this.#em.applyFilters(meta2.class, subCond, options.filters, 'read', options);
|
|
673
645
|
}
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
646
|
+
return subCond;
|
|
647
|
+
}
|
|
648
|
+
buildFields(fields = [], prop, ref) {
|
|
649
|
+
if (ref) {
|
|
650
|
+
fields = prop.targetMeta.primaryKeys.map(targetPkName => `${prop.name}.${targetPkName}`);
|
|
679
651
|
}
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
const
|
|
652
|
+
const ret = fields.reduce((ret, f) => {
|
|
653
|
+
if (Utils.isPlainObject(f)) {
|
|
654
|
+
Utils.keys(f)
|
|
655
|
+
.filter(ff => ff === prop.name)
|
|
656
|
+
.forEach(ff => ret.push(...f[ff]));
|
|
657
|
+
} else if (f.toString().includes('.')) {
|
|
658
|
+
const parts = f.toString().split('.');
|
|
659
|
+
const propName = parts.shift();
|
|
660
|
+
const childPropName = parts.join('.');
|
|
689
661
|
/* v8 ignore next */
|
|
690
|
-
if (
|
|
691
|
-
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
662
|
+
if (propName === prop.name) {
|
|
663
|
+
ret.push(childPropName);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
return ret;
|
|
667
|
+
}, []);
|
|
668
|
+
// we need to automatically select the FKs too, e.g. for 1:m relations to be able to wire them with the items
|
|
669
|
+
if (prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
670
|
+
const owner = prop.targetMeta.properties[prop.mappedBy];
|
|
671
|
+
// when the owning FK is lazy, we need to explicitly select it even without user-provided fields,
|
|
672
|
+
// otherwise the driver will exclude it and we won't be able to map children to their parent collections
|
|
673
|
+
if (owner && !ret.includes(owner.name) && (ret.length > 0 || owner.lazy)) {
|
|
674
|
+
ret.push(owner.name);
|
|
675
|
+
}
|
|
697
676
|
}
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
return [];
|
|
701
|
-
}
|
|
702
|
-
const children = entities.filter(e => Utils.isEntity(e[field], true));
|
|
703
|
-
if (options.refresh) {
|
|
704
|
-
return children.map(e => Reference.unwrapReference(e[field]));
|
|
705
|
-
}
|
|
706
|
-
if (options.fields) {
|
|
707
|
-
return children
|
|
708
|
-
.map(e => Reference.unwrapReference(e[field]))
|
|
709
|
-
.filter(target => {
|
|
710
|
-
const wrapped = helper(target);
|
|
711
|
-
const childFields = options.fields
|
|
712
|
-
.filter(f => f.startsWith(`${field}.`))
|
|
713
|
-
.map(f => f.substring(field.length + 1));
|
|
714
|
-
return !wrapped.__initialized || !childFields.every(cf => this.isPropertyLoaded(target, cf));
|
|
715
|
-
});
|
|
716
|
-
}
|
|
717
|
-
return children
|
|
718
|
-
.filter(e => !e[field].__helper.__initialized)
|
|
719
|
-
.map(e => Reference.unwrapReference(e[field]));
|
|
677
|
+
if (ret.length === 0) {
|
|
678
|
+
return undefined;
|
|
720
679
|
}
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
}
|
|
728
|
-
lookupAllRelationships(entityName) {
|
|
729
|
-
const ret = [];
|
|
730
|
-
const meta = this.#metadata.find(entityName);
|
|
731
|
-
meta.relations.forEach(prop => {
|
|
732
|
-
ret.push({
|
|
733
|
-
field: this.getRelationName(meta, prop),
|
|
734
|
-
// force select-in strategy when populating all relations as otherwise we could cause infinite loops when self-referencing
|
|
735
|
-
strategy: LoadStrategy.SELECT_IN,
|
|
736
|
-
// no need to look up populate children recursively as we just pass `all: true` here
|
|
737
|
-
all: true,
|
|
738
|
-
});
|
|
739
|
-
});
|
|
740
|
-
// For TPT parents with child types, keep an all:true sentinel so the populate
|
|
741
|
-
// loop doesn't exit early and the TPT child relation population can run after it.
|
|
742
|
-
if (ret.length === 0 && meta.inheritanceType === 'tpt' && meta.tptChildren?.length) {
|
|
743
|
-
ret.push({ field: meta.primaryKeys[0], strategy: LoadStrategy.SELECT_IN, all: true });
|
|
744
|
-
}
|
|
745
|
-
return ret;
|
|
680
|
+
return ret;
|
|
681
|
+
}
|
|
682
|
+
getChildReferences(entities, prop, options, ref) {
|
|
683
|
+
const filtered = this.filterCollections(entities, prop.name, options, ref);
|
|
684
|
+
if (prop.kind === ReferenceKind.ONE_TO_MANY) {
|
|
685
|
+
return filtered.map(e => e[prop.name].owner);
|
|
746
686
|
}
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
687
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner) {
|
|
688
|
+
return filtered.reduce((a, b) => {
|
|
689
|
+
a.push(...b[prop.name].getItems());
|
|
690
|
+
return a;
|
|
691
|
+
}, []);
|
|
752
692
|
}
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
693
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
694
|
+
// inverse side
|
|
695
|
+
return filtered;
|
|
696
|
+
}
|
|
697
|
+
// MANY_TO_ONE or ONE_TO_ONE
|
|
698
|
+
return this.filterReferences(entities, prop.name, options, ref);
|
|
699
|
+
}
|
|
700
|
+
filterCollections(entities, field, options, ref) {
|
|
701
|
+
if (options.refresh) {
|
|
702
|
+
return entities.filter(e => e[field]);
|
|
703
|
+
}
|
|
704
|
+
return entities.filter(e => Utils.isCollection(e[field]) && !e[field].isInitialized(!ref));
|
|
705
|
+
}
|
|
706
|
+
isPropertyLoaded(entity, field) {
|
|
707
|
+
if (!entity || field === '*') {
|
|
708
|
+
return true;
|
|
709
|
+
}
|
|
710
|
+
const wrapped = helper(entity);
|
|
711
|
+
if (!field.includes('.')) {
|
|
712
|
+
return wrapped.__loadedProperties.has(field);
|
|
713
|
+
}
|
|
714
|
+
const [f, ...r] = field.split('.');
|
|
715
|
+
/* v8 ignore next */
|
|
716
|
+
if (!wrapped.__loadedProperties.has(f) || !wrapped.__meta.properties[f]?.targetMeta) {
|
|
717
|
+
return false;
|
|
718
|
+
}
|
|
719
|
+
if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(wrapped.__meta.properties[f].kind)) {
|
|
720
|
+
return entity[f].getItems(false).every(item => this.isPropertyLoaded(item, r.join('.')));
|
|
721
|
+
}
|
|
722
|
+
return this.isPropertyLoaded(entity[f], r.join('.'));
|
|
723
|
+
}
|
|
724
|
+
filterReferences(entities, field, options, ref) {
|
|
725
|
+
if (ref) {
|
|
726
|
+
return [];
|
|
727
|
+
}
|
|
728
|
+
const children = entities.filter(e => Utils.isEntity(e[field], true));
|
|
729
|
+
if (options.refresh) {
|
|
730
|
+
return children.map(e => Reference.unwrapReference(e[field]));
|
|
731
|
+
}
|
|
732
|
+
if (options.fields) {
|
|
733
|
+
return children
|
|
734
|
+
.map(e => Reference.unwrapReference(e[field]))
|
|
735
|
+
.filter(target => {
|
|
736
|
+
const wrapped = helper(target);
|
|
737
|
+
const childFields = options.fields
|
|
738
|
+
.filter(f => f.startsWith(`${field}.`))
|
|
739
|
+
.map(f => f.substring(field.length + 1));
|
|
740
|
+
return !wrapped.__initialized || !childFields.every(cf => this.isPropertyLoaded(target, cf));
|
|
792
741
|
});
|
|
793
|
-
return ret;
|
|
794
742
|
}
|
|
743
|
+
return children.filter(e => !e[field].__helper.__initialized).map(e => Reference.unwrapReference(e[field]));
|
|
744
|
+
}
|
|
745
|
+
filterByReferences(entities, field, refresh) {
|
|
746
|
+
/* v8 ignore next */
|
|
747
|
+
if (refresh) {
|
|
748
|
+
return entities;
|
|
749
|
+
}
|
|
750
|
+
return entities.filter(e => e[field] !== null && !e[field]?.__helper?.__initialized);
|
|
751
|
+
}
|
|
752
|
+
lookupAllRelationships(entityName) {
|
|
753
|
+
const ret = [];
|
|
754
|
+
const meta = this.#metadata.find(entityName);
|
|
755
|
+
meta.relations.forEach(prop => {
|
|
756
|
+
ret.push({
|
|
757
|
+
field: this.getRelationName(meta, prop),
|
|
758
|
+
// force select-in strategy when populating all relations as otherwise we could cause infinite loops when self-referencing
|
|
759
|
+
strategy: LoadStrategy.SELECT_IN,
|
|
760
|
+
// no need to look up populate children recursively as we just pass `all: true` here
|
|
761
|
+
all: true,
|
|
762
|
+
});
|
|
763
|
+
});
|
|
764
|
+
// For TPT parents with child types, keep an all:true sentinel so the populate
|
|
765
|
+
// loop doesn't exit early and the TPT child relation population can run after it.
|
|
766
|
+
if (ret.length === 0 && meta.inheritanceType === 'tpt' && meta.tptChildren?.length) {
|
|
767
|
+
ret.push({ field: meta.primaryKeys[0], strategy: LoadStrategy.SELECT_IN, all: true });
|
|
768
|
+
}
|
|
769
|
+
return ret;
|
|
770
|
+
}
|
|
771
|
+
getRelationName(meta, prop) {
|
|
772
|
+
if (!prop.embedded) {
|
|
773
|
+
return prop.name;
|
|
774
|
+
}
|
|
775
|
+
return `${this.getRelationName(meta, meta.properties[prop.embedded[0]])}.${prop.embedded[1]}`;
|
|
776
|
+
}
|
|
777
|
+
lookupEagerLoadedRelationships(entityName, populate, strategy, prefix = '', visited = [], exclude) {
|
|
778
|
+
const meta = this.#metadata.find(entityName);
|
|
779
|
+
if (!meta && !prefix) {
|
|
780
|
+
return populate;
|
|
781
|
+
}
|
|
782
|
+
if (!meta || visited.includes(meta)) {
|
|
783
|
+
return [];
|
|
784
|
+
}
|
|
785
|
+
visited.push(meta);
|
|
786
|
+
const ret = prefix === '' ? [...populate] : [];
|
|
787
|
+
meta.relations
|
|
788
|
+
.filter(prop => {
|
|
789
|
+
const field = this.getRelationName(meta, prop);
|
|
790
|
+
const prefixed = prefix ? `${prefix}.${field}` : field;
|
|
791
|
+
const isExcluded = exclude?.includes(prefixed);
|
|
792
|
+
const eager = prop.eager && !populate.some(p => p.field === `${prop.name}:ref`);
|
|
793
|
+
const populated = populate.some(p => p.field === prop.name);
|
|
794
|
+
const disabled = populate.some(p => p.field === prop.name && p.all === false);
|
|
795
|
+
return !disabled && !isExcluded && (eager || populated);
|
|
796
|
+
})
|
|
797
|
+
.forEach(prop => {
|
|
798
|
+
const field = this.getRelationName(meta, prop);
|
|
799
|
+
const prefixed = prefix ? `${prefix}.${field}` : field;
|
|
800
|
+
const nestedPopulate = populate
|
|
801
|
+
.filter(p => p.field === prop.name)
|
|
802
|
+
.flatMap(p => p.children)
|
|
803
|
+
.filter(Boolean);
|
|
804
|
+
const nested = this.lookupEagerLoadedRelationships(
|
|
805
|
+
prop.targetMeta.class,
|
|
806
|
+
nestedPopulate,
|
|
807
|
+
strategy,
|
|
808
|
+
prefixed,
|
|
809
|
+
visited.slice(),
|
|
810
|
+
exclude,
|
|
811
|
+
);
|
|
812
|
+
if (nested.length > 0) {
|
|
813
|
+
ret.push(...nested);
|
|
814
|
+
} else {
|
|
815
|
+
const selfReferencing =
|
|
816
|
+
[meta.tableName, ...visited.map(m => m.tableName)].includes(prop.targetMeta.tableName) && prop.eager;
|
|
817
|
+
ret.push({
|
|
818
|
+
field: prefixed,
|
|
819
|
+
// enforce select-in strategy for self-referencing relations
|
|
820
|
+
strategy: selfReferencing ? LoadStrategy.SELECT_IN : (strategy ?? prop.strategy),
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
});
|
|
824
|
+
return ret;
|
|
825
|
+
}
|
|
795
826
|
}
|