@mikro-orm/core 7.0.0-rc.3 → 7.0.1-dev.0
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 +2 -15
- package/EntityManager.js +155 -152
- package/MikroORM.d.ts +4 -6
- package/MikroORM.js +20 -20
- package/README.md +5 -4
- package/cache/FileCacheAdapter.d.ts +1 -5
- package/cache/FileCacheAdapter.js +22 -22
- package/cache/GeneratedCacheAdapter.d.ts +1 -1
- package/cache/GeneratedCacheAdapter.js +6 -6
- package/cache/MemoryCacheAdapter.d.ts +1 -2
- package/cache/MemoryCacheAdapter.js +8 -8
- package/cache/index.d.ts +1 -1
- package/cache/index.js +0 -1
- package/connections/Connection.d.ts +1 -0
- package/connections/Connection.js +27 -11
- package/drivers/DatabaseDriver.d.ts +0 -2
- package/drivers/DatabaseDriver.js +2 -4
- package/entity/Collection.d.ts +1 -9
- package/entity/Collection.js +95 -105
- package/entity/EntityFactory.d.ts +1 -8
- package/entity/EntityFactory.js +48 -48
- package/entity/EntityLoader.d.ts +1 -3
- package/entity/EntityLoader.js +36 -39
- package/entity/Reference.d.ts +1 -2
- package/entity/Reference.js +11 -11
- package/entity/WrappedEntity.d.ts +4 -2
- package/entity/defineEntity.d.ts +18 -73
- package/enums.d.ts +2 -1
- package/enums.js +1 -0
- package/errors.d.ts +11 -11
- package/errors.js +3 -13
- package/events/EventManager.d.ts +1 -4
- package/events/EventManager.js +25 -22
- package/events/index.d.ts +1 -1
- package/events/index.js +0 -1
- package/exceptions.js +8 -6
- package/hydration/ObjectHydrator.d.ts +1 -2
- package/hydration/ObjectHydrator.js +16 -16
- package/logging/DefaultLogger.js +3 -2
- package/logging/Logger.d.ts +2 -1
- package/logging/colors.js +1 -1
- package/logging/index.d.ts +1 -1
- package/logging/index.js +0 -1
- package/metadata/EntitySchema.d.ts +1 -1
- package/metadata/MetadataDiscovery.d.ts +1 -9
- package/metadata/MetadataDiscovery.js +162 -149
- package/metadata/MetadataStorage.d.ts +1 -5
- package/metadata/MetadataStorage.js +36 -36
- package/metadata/discover-entities.js +1 -1
- package/metadata/index.d.ts +1 -1
- package/metadata/index.js +0 -1
- package/naming-strategy/AbstractNamingStrategy.js +1 -1
- package/naming-strategy/EntityCaseNamingStrategy.js +1 -1
- package/naming-strategy/index.d.ts +1 -1
- package/naming-strategy/index.js +0 -1
- package/package.json +1 -1
- package/platforms/Platform.d.ts +23 -1
- package/platforms/Platform.js +57 -4
- package/serialization/EntitySerializer.js +1 -1
- package/serialization/EntityTransformer.js +4 -1
- package/serialization/SerializationContext.d.ts +4 -8
- package/serialization/SerializationContext.js +20 -15
- package/types/UuidType.d.ts +2 -0
- package/types/UuidType.js +14 -2
- package/types/index.d.ts +2 -1
- package/typings.d.ts +12 -1
- package/unit-of-work/ChangeSetComputer.d.ts +1 -6
- package/unit-of-work/ChangeSetComputer.js +21 -21
- package/unit-of-work/ChangeSetPersister.d.ts +1 -9
- package/unit-of-work/ChangeSetPersister.js +52 -52
- package/unit-of-work/CommitOrderCalculator.d.ts +1 -4
- package/unit-of-work/CommitOrderCalculator.js +13 -13
- package/unit-of-work/IdentityMap.d.ts +2 -5
- package/unit-of-work/IdentityMap.js +18 -18
- package/unit-of-work/UnitOfWork.d.ts +5 -19
- package/unit-of-work/UnitOfWork.js +182 -174
- package/utils/AbstractMigrator.d.ts +1 -1
- package/utils/AbstractMigrator.js +7 -7
- package/utils/Configuration.d.ts +90 -189
- package/utils/Configuration.js +94 -78
- package/utils/Cursor.d.ts +3 -3
- package/utils/Cursor.js +4 -4
- package/utils/EntityComparator.d.ts +8 -15
- package/utils/EntityComparator.js +49 -49
- package/utils/QueryHelper.d.ts +16 -1
- package/utils/QueryHelper.js +70 -24
- package/utils/RawQueryFragment.d.ts +4 -4
- package/utils/TransactionManager.js +1 -2
- package/utils/Utils.d.ts +1 -1
- package/utils/Utils.js +5 -4
- package/utils/clone.js +5 -0
- package/utils/fs-utils.d.ts +3 -17
- package/utils/fs-utils.js +1 -1
- package/utils/upsert-utils.js +1 -1
package/EntityManager.js
CHANGED
|
@@ -27,26 +27,27 @@ export class EntityManager {
|
|
|
27
27
|
config;
|
|
28
28
|
driver;
|
|
29
29
|
metadata;
|
|
30
|
-
useContext;
|
|
31
30
|
eventManager;
|
|
32
|
-
static counter = 1;
|
|
33
|
-
|
|
31
|
+
static #counter = 1;
|
|
32
|
+
/** @internal */
|
|
33
|
+
_id = EntityManager.#counter++;
|
|
34
34
|
global = false;
|
|
35
35
|
name;
|
|
36
|
-
loaders = {};
|
|
37
|
-
repositoryMap = new Map();
|
|
38
|
-
entityLoader;
|
|
39
|
-
comparator;
|
|
40
|
-
entityFactory;
|
|
41
|
-
unitOfWork;
|
|
42
|
-
resultCache;
|
|
43
|
-
filters = {};
|
|
44
|
-
filterParams = {};
|
|
36
|
+
#loaders = {};
|
|
37
|
+
#repositoryMap = new Map();
|
|
38
|
+
#entityLoader;
|
|
39
|
+
#comparator;
|
|
40
|
+
#entityFactory;
|
|
41
|
+
#unitOfWork;
|
|
42
|
+
#resultCache;
|
|
43
|
+
#filters = {};
|
|
44
|
+
#filterParams = {};
|
|
45
45
|
loggerContext;
|
|
46
|
-
transactionContext;
|
|
47
|
-
disableTransactions;
|
|
48
|
-
flushMode;
|
|
49
|
-
|
|
46
|
+
#transactionContext;
|
|
47
|
+
#disableTransactions;
|
|
48
|
+
#flushMode;
|
|
49
|
+
#schema;
|
|
50
|
+
#useContext;
|
|
50
51
|
/**
|
|
51
52
|
* @internal
|
|
52
53
|
*/
|
|
@@ -54,15 +55,15 @@ export class EntityManager {
|
|
|
54
55
|
this.config = config;
|
|
55
56
|
this.driver = driver;
|
|
56
57
|
this.metadata = metadata;
|
|
57
|
-
this.useContext = useContext;
|
|
58
58
|
this.eventManager = eventManager;
|
|
59
|
-
this
|
|
59
|
+
this.#useContext = useContext;
|
|
60
|
+
this.#entityLoader = new EntityLoader(this);
|
|
60
61
|
this.name = this.config.get('contextName');
|
|
61
|
-
this
|
|
62
|
-
this
|
|
63
|
-
this
|
|
64
|
-
this
|
|
65
|
-
this
|
|
62
|
+
this.#comparator = this.config.getComparator(this.metadata);
|
|
63
|
+
this.#resultCache = this.config.getResultCacheAdapter();
|
|
64
|
+
this.#disableTransactions = this.config.get('disableTransactions');
|
|
65
|
+
this.#entityFactory = new EntityFactory(this);
|
|
66
|
+
this.#unitOfWork = new UnitOfWork(this);
|
|
66
67
|
}
|
|
67
68
|
/**
|
|
68
69
|
* Gets the Driver instance used by this EntityManager.
|
|
@@ -88,11 +89,11 @@ export class EntityManager {
|
|
|
88
89
|
*/
|
|
89
90
|
getRepository(entityName) {
|
|
90
91
|
const meta = this.metadata.get(entityName);
|
|
91
|
-
if (!this
|
|
92
|
+
if (!this.#repositoryMap.has(meta)) {
|
|
92
93
|
const RepositoryClass = this.config.getRepositoryClass(meta.repository);
|
|
93
|
-
this
|
|
94
|
+
this.#repositoryMap.set(meta, new RepositoryClass(this, entityName));
|
|
94
95
|
}
|
|
95
|
-
return this
|
|
96
|
+
return this.#repositoryMap.get(meta);
|
|
96
97
|
}
|
|
97
98
|
/**
|
|
98
99
|
* Shortcut for `em.getRepository()`.
|
|
@@ -128,7 +129,7 @@ export class EntityManager {
|
|
|
128
129
|
const cacheKey = em.cacheKey(entityName, options, 'em.find', where);
|
|
129
130
|
const cached = await em.tryCache(entityName, options.cache, cacheKey, options.refresh, true);
|
|
130
131
|
if (cached?.data) {
|
|
131
|
-
await em
|
|
132
|
+
await em.#entityLoader.populate(entityName, cached.data, populate, {
|
|
132
133
|
...options,
|
|
133
134
|
...em.getPopulateWhere(where, options),
|
|
134
135
|
ignoreLazyScalarProperties: true,
|
|
@@ -142,14 +143,14 @@ export class EntityManager {
|
|
|
142
143
|
options.populateWhere = this.createPopulateWhere({ ...where }, options);
|
|
143
144
|
options.populateFilter = await this.getJoinedFilters(meta, options);
|
|
144
145
|
await em.processUnionWhere(entityName, options, 'read');
|
|
145
|
-
const results = await em.driver.find(entityName, where, { ctx: em
|
|
146
|
+
const results = await em.driver.find(entityName, where, { ctx: em.#transactionContext, em, ...options });
|
|
146
147
|
if (results.length === 0) {
|
|
147
148
|
await em.storeCache(options.cache, cached, []);
|
|
148
149
|
return [];
|
|
149
150
|
}
|
|
150
151
|
const ret = [];
|
|
151
152
|
for (const data of results) {
|
|
152
|
-
const entity = em
|
|
153
|
+
const entity = em.#entityFactory.create(entityName, data, {
|
|
153
154
|
merge: true,
|
|
154
155
|
refresh: options.refresh,
|
|
155
156
|
schema: options.schema,
|
|
@@ -158,13 +159,13 @@ export class EntityManager {
|
|
|
158
159
|
ret.push(entity);
|
|
159
160
|
}
|
|
160
161
|
const unique = Utils.unique(ret);
|
|
161
|
-
await em
|
|
162
|
+
await em.#entityLoader.populate(entityName, unique, populate, {
|
|
162
163
|
...options,
|
|
163
164
|
...em.getPopulateWhere(where, options),
|
|
164
165
|
ignoreLazyScalarProperties: true,
|
|
165
166
|
lookup: false,
|
|
166
167
|
});
|
|
167
|
-
await em
|
|
168
|
+
await em.#unitOfWork.dispatchOnLoadEvent();
|
|
168
169
|
if (meta.virtual) {
|
|
169
170
|
await em.storeCache(options.cache, cached, () => ret);
|
|
170
171
|
}
|
|
@@ -206,13 +207,13 @@ export class EntityManager {
|
|
|
206
207
|
options.populateWhere = this.createPopulateWhere({ ...where }, options);
|
|
207
208
|
options.populateFilter = await this.getJoinedFilters(meta, options);
|
|
208
209
|
const stream = em.driver.stream(entityName, where, {
|
|
209
|
-
ctx: em
|
|
210
|
+
ctx: em.#transactionContext,
|
|
210
211
|
mapResults: false,
|
|
211
212
|
...options,
|
|
212
213
|
});
|
|
213
214
|
for await (const data of stream) {
|
|
214
215
|
const fork = em.fork();
|
|
215
|
-
const entity = fork
|
|
216
|
+
const entity = fork.#entityFactory.create(entityName, data, {
|
|
216
217
|
refresh: options.refresh,
|
|
217
218
|
schema: options.schema,
|
|
218
219
|
convertCustomTypes: true,
|
|
@@ -222,7 +223,7 @@ export class EntityManager {
|
|
|
222
223
|
fields: options.fields,
|
|
223
224
|
exclude: options.exclude,
|
|
224
225
|
});
|
|
225
|
-
await fork
|
|
226
|
+
await fork.#unitOfWork.dispatchOnLoadEvent();
|
|
226
227
|
fork.clear();
|
|
227
228
|
yield entity;
|
|
228
229
|
}
|
|
@@ -254,20 +255,20 @@ export class EntityManager {
|
|
|
254
255
|
options.entity = Utils.asArray(options.entity).map(n => Utils.className(n));
|
|
255
256
|
}
|
|
256
257
|
options.default ??= true;
|
|
257
|
-
this.getContext(false)
|
|
258
|
+
this.getContext(false).#filters[options.name] = options;
|
|
258
259
|
}
|
|
259
260
|
/**
|
|
260
261
|
* Sets filter parameter values globally inside context defined by this entity manager.
|
|
261
262
|
* If you want to set shared value for all contexts, be sure to use the root entity manager.
|
|
262
263
|
*/
|
|
263
264
|
setFilterParams(name, args) {
|
|
264
|
-
this.getContext()
|
|
265
|
+
this.getContext().#filterParams[name] = args;
|
|
265
266
|
}
|
|
266
267
|
/**
|
|
267
268
|
* Returns filter parameters for given filter set in this context.
|
|
268
269
|
*/
|
|
269
270
|
getFilterParams(name) {
|
|
270
|
-
return this.getContext()
|
|
271
|
+
return this.getContext().#filterParams[name];
|
|
271
272
|
}
|
|
272
273
|
/**
|
|
273
274
|
* Sets logger context for this entity manager.
|
|
@@ -284,7 +285,7 @@ export class EntityManager {
|
|
|
284
285
|
return em.loggerContext;
|
|
285
286
|
}
|
|
286
287
|
setFlushMode(flushMode) {
|
|
287
|
-
this.getContext(false)
|
|
288
|
+
this.getContext(false).#flushMode = flushMode;
|
|
288
289
|
}
|
|
289
290
|
async processWhere(entityName, where, options, type) {
|
|
290
291
|
where = QueryHelper.processWhere({
|
|
@@ -442,7 +443,7 @@ export class EntityManager {
|
|
|
442
443
|
activeFilters.forEach(f => active.add(f.name));
|
|
443
444
|
};
|
|
444
445
|
push(this.config.get('filters'));
|
|
445
|
-
push(this
|
|
446
|
+
push(this.#filters);
|
|
446
447
|
push(meta.filters);
|
|
447
448
|
if (filters.length === 0) {
|
|
448
449
|
return where;
|
|
@@ -452,7 +453,7 @@ export class EntityManager {
|
|
|
452
453
|
if (filter.cond instanceof Function) {
|
|
453
454
|
// @ts-ignore
|
|
454
455
|
// oxfmt-ignore
|
|
455
|
-
const args = Utils.isPlainObject(options?.[filter.name]) ? options[filter.name] : this.getContext()
|
|
456
|
+
const args = Utils.isPlainObject(options?.[filter.name]) ? options[filter.name] : this.getContext().#filterParams[filter.name];
|
|
456
457
|
if (!args && filter.cond.length > 0 && filter.args !== false) {
|
|
457
458
|
throw new Error(`No arguments provided for filter '${filter.name}'`);
|
|
458
459
|
}
|
|
@@ -587,17 +588,17 @@ export class EntityManager {
|
|
|
587
588
|
});
|
|
588
589
|
const em = this.getContext();
|
|
589
590
|
if (!reloaded) {
|
|
590
|
-
em
|
|
591
|
+
em.#unitOfWork.unsetIdentity(entity);
|
|
591
592
|
return null;
|
|
592
593
|
}
|
|
593
594
|
let found = false;
|
|
594
|
-
for (const e of fork
|
|
595
|
+
for (const e of fork.#unitOfWork.getIdentityMap()) {
|
|
595
596
|
const ref = em.getReference(e.constructor, helper(e).getPrimaryKey());
|
|
596
597
|
const data = helper(e).serialize({ ignoreSerializers: true, includeHidden: true, convertCustomTypes: false });
|
|
597
598
|
em.config
|
|
598
599
|
.getHydrator(this.metadata)
|
|
599
|
-
.hydrate(ref, helper(ref).__meta, data, em
|
|
600
|
-
Utils.merge(helper(ref).__originalEntityData, this
|
|
600
|
+
.hydrate(ref, helper(ref).__meta, data, em.#entityFactory, 'full', false, false);
|
|
601
|
+
Utils.merge(helper(ref).__originalEntityData, this.#comparator.prepareEntity(e));
|
|
601
602
|
found ||= ref === entity;
|
|
602
603
|
}
|
|
603
604
|
if (!found) {
|
|
@@ -606,8 +607,10 @@ export class EntityManager {
|
|
|
606
607
|
includeHidden: true,
|
|
607
608
|
convertCustomTypes: true,
|
|
608
609
|
});
|
|
609
|
-
em.config
|
|
610
|
-
|
|
610
|
+
em.config
|
|
611
|
+
.getHydrator(this.metadata)
|
|
612
|
+
.hydrate(entity, wrapped.__meta, data, em.#entityFactory, 'full', false, true);
|
|
613
|
+
Utils.merge(wrapped.__originalEntityData, this.#comparator.prepareEntity(reloaded));
|
|
611
614
|
}
|
|
612
615
|
return entity;
|
|
613
616
|
}
|
|
@@ -624,7 +627,7 @@ export class EntityManager {
|
|
|
624
627
|
}
|
|
625
628
|
const em = this.getContext();
|
|
626
629
|
em.prepareOptions(options);
|
|
627
|
-
let entity = em
|
|
630
|
+
let entity = em.#unitOfWork.tryGetById(entityName, where, options.schema);
|
|
628
631
|
// query for a not managed entity which is already in the identity map as it
|
|
629
632
|
// was provided with a PK this entity does not exist in the db, there can't
|
|
630
633
|
// be any relations to it, so no need to deal with the populate hint
|
|
@@ -646,7 +649,7 @@ export class EntityManager {
|
|
|
646
649
|
const cached = await em.tryCache(entityName, options.cache, cacheKey, options.refresh, true);
|
|
647
650
|
if (cached?.data !== undefined) {
|
|
648
651
|
if (cached.data) {
|
|
649
|
-
await em
|
|
652
|
+
await em.#entityLoader.populate(entityName, [cached.data], options.populate, {
|
|
650
653
|
...options,
|
|
651
654
|
...em.getPopulateWhere(where, options),
|
|
652
655
|
ignoreLazyScalarProperties: true,
|
|
@@ -662,7 +665,7 @@ export class EntityManager {
|
|
|
662
665
|
options.populateFilter = await this.getJoinedFilters(meta, options);
|
|
663
666
|
await em.processUnionWhere(entityName, options, 'read');
|
|
664
667
|
const data = await em.driver.findOne(entityName, where, {
|
|
665
|
-
ctx: em
|
|
668
|
+
ctx: em.#transactionContext,
|
|
666
669
|
em,
|
|
667
670
|
...options,
|
|
668
671
|
});
|
|
@@ -670,14 +673,14 @@ export class EntityManager {
|
|
|
670
673
|
await em.storeCache(options.cache, cached, null);
|
|
671
674
|
return null;
|
|
672
675
|
}
|
|
673
|
-
entity = em
|
|
676
|
+
entity = em.#entityFactory.create(entityName, data, {
|
|
674
677
|
merge: true,
|
|
675
678
|
refresh: options.refresh,
|
|
676
679
|
schema: options.schema,
|
|
677
680
|
convertCustomTypes: true,
|
|
678
681
|
});
|
|
679
682
|
await em.lockAndPopulate(meta, entity, where, options);
|
|
680
|
-
await em
|
|
683
|
+
await em.#unitOfWork.dispatchOnLoadEvent();
|
|
681
684
|
await em.storeCache(options.cache, cached, () => helper(entity).toPOJO());
|
|
682
685
|
return entity;
|
|
683
686
|
}
|
|
@@ -755,17 +758,17 @@ export class EntityManager {
|
|
|
755
758
|
if (Utils.isEntity(data)) {
|
|
756
759
|
entity = data;
|
|
757
760
|
if (helper(entity).__managed && helper(entity).__em === em && !this.config.get('upsertManaged')) {
|
|
758
|
-
em
|
|
761
|
+
em.#entityFactory.mergeData(meta, entity, data, { initialized: true });
|
|
759
762
|
return entity;
|
|
760
763
|
}
|
|
761
764
|
where = helper(entity).getPrimaryKey();
|
|
762
|
-
data = em
|
|
765
|
+
data = em.#comparator.prepareEntity(entity);
|
|
763
766
|
}
|
|
764
767
|
else {
|
|
765
768
|
data = Utils.copy(QueryHelper.processParams(data));
|
|
766
769
|
where = Utils.extractPK(data, meta);
|
|
767
770
|
if (where && !this.config.get('upsertManaged')) {
|
|
768
|
-
const exists = em
|
|
771
|
+
const exists = em.#unitOfWork.getById(entityName, where, options.schema);
|
|
769
772
|
if (exists) {
|
|
770
773
|
return em.assign(exists, data);
|
|
771
774
|
}
|
|
@@ -778,13 +781,13 @@ export class EntityManager {
|
|
|
778
781
|
await em.eventManager.dispatchEvent(EventType.beforeUpsert, { entity: data, em, meta }, meta);
|
|
779
782
|
}
|
|
780
783
|
const ret = await em.driver.nativeUpdate(entityName, where, data, {
|
|
781
|
-
ctx: em
|
|
784
|
+
ctx: em.#transactionContext,
|
|
782
785
|
upsert: true,
|
|
783
786
|
convertCustomTypes,
|
|
784
787
|
...options,
|
|
785
788
|
});
|
|
786
|
-
em
|
|
787
|
-
entity ??= em
|
|
789
|
+
em.#unitOfWork.getChangeSetPersister().mapReturnedValues(entity, data, ret.row, meta, true);
|
|
790
|
+
entity ??= em.#entityFactory.create(entityName, data, {
|
|
788
791
|
refresh: true,
|
|
789
792
|
initialized: true,
|
|
790
793
|
schema: options.schema,
|
|
@@ -816,16 +819,16 @@ export class EntityManager {
|
|
|
816
819
|
}
|
|
817
820
|
const data2 = await this.driver.findOne(meta.class, where, {
|
|
818
821
|
fields: returning,
|
|
819
|
-
ctx: em
|
|
822
|
+
ctx: em.#transactionContext,
|
|
820
823
|
convertCustomTypes: true,
|
|
821
824
|
connectionType: 'write',
|
|
822
825
|
schema: options.schema,
|
|
823
826
|
});
|
|
824
|
-
em.getHydrator().hydrate(entity, meta, data2, em
|
|
827
|
+
em.getHydrator().hydrate(entity, meta, data2, em.#entityFactory, 'full', false, true);
|
|
825
828
|
}
|
|
826
829
|
// recompute the data as there might be some values missing (e.g. those with db column defaults)
|
|
827
|
-
const snapshot = this
|
|
828
|
-
em
|
|
830
|
+
const snapshot = this.#comparator.prepareEntity(entity);
|
|
831
|
+
em.#unitOfWork.register(entity, snapshot, { refresh: true });
|
|
829
832
|
if (em.eventManager.hasListeners(EventType.afterUpsert, meta)) {
|
|
830
833
|
await em.eventManager.dispatchEvent(EventType.afterUpsert, { entity, em, meta }, meta);
|
|
831
834
|
}
|
|
@@ -896,19 +899,19 @@ export class EntityManager {
|
|
|
896
899
|
if (Utils.isEntity(row)) {
|
|
897
900
|
const entity = row;
|
|
898
901
|
if (helper(entity).__managed && helper(entity).__em === em && !this.config.get('upsertManaged')) {
|
|
899
|
-
em
|
|
902
|
+
em.#entityFactory.mergeData(meta, entity, row, { initialized: true });
|
|
900
903
|
entities.set(entity, row);
|
|
901
904
|
entitiesByData.set(row, entity);
|
|
902
905
|
continue;
|
|
903
906
|
}
|
|
904
907
|
where = helper(entity).getPrimaryKey();
|
|
905
|
-
row = em
|
|
908
|
+
row = em.#comparator.prepareEntity(entity);
|
|
906
909
|
}
|
|
907
910
|
else {
|
|
908
911
|
row = data[i] = Utils.copy(QueryHelper.processParams(row));
|
|
909
912
|
where = Utils.extractPK(row, meta);
|
|
910
913
|
if (where && !this.config.get('upsertManaged')) {
|
|
911
|
-
const exists = em
|
|
914
|
+
const exists = em.#unitOfWork.getById(entityName, where, options.schema);
|
|
912
915
|
if (exists) {
|
|
913
916
|
em.assign(exists, row);
|
|
914
917
|
entities.set(exists, row);
|
|
@@ -944,7 +947,7 @@ export class EntityManager {
|
|
|
944
947
|
}
|
|
945
948
|
}
|
|
946
949
|
const res = await em.driver.nativeUpdateMany(entityName, allWhere, allData, {
|
|
947
|
-
ctx: em
|
|
950
|
+
ctx: em.#transactionContext,
|
|
948
951
|
upsert: true,
|
|
949
952
|
convertCustomTypes,
|
|
950
953
|
...options,
|
|
@@ -953,12 +956,12 @@ export class EntityManager {
|
|
|
953
956
|
entitiesByData.clear();
|
|
954
957
|
const loadPK = new Map();
|
|
955
958
|
allData.forEach((row, i) => {
|
|
956
|
-
em
|
|
959
|
+
em.#unitOfWork
|
|
957
960
|
.getChangeSetPersister()
|
|
958
961
|
.mapReturnedValues(Utils.isEntity(data[i]) ? data[i] : null, Utils.isEntity(data[i]) ? {} : data[i], res.rows?.[i], meta, true);
|
|
959
962
|
const entity = Utils.isEntity(data[i])
|
|
960
963
|
? data[i]
|
|
961
|
-
: em
|
|
964
|
+
: em.#entityFactory.create(entityName, row, {
|
|
962
965
|
refresh: true,
|
|
963
966
|
initialized: true,
|
|
964
967
|
schema: options.schema,
|
|
@@ -994,7 +997,7 @@ export class EntityManager {
|
|
|
994
997
|
fields: returning
|
|
995
998
|
.concat(...add)
|
|
996
999
|
.concat(...(Array.isArray(uniqueFields) ? uniqueFields : [])),
|
|
997
|
-
ctx: em
|
|
1000
|
+
ctx: em.#transactionContext,
|
|
998
1001
|
convertCustomTypes: true,
|
|
999
1002
|
connectionType: 'write',
|
|
1000
1003
|
schema: options.schema,
|
|
@@ -1007,13 +1010,13 @@ export class EntityManager {
|
|
|
1007
1010
|
tmp[k] = row[k];
|
|
1008
1011
|
}
|
|
1009
1012
|
});
|
|
1010
|
-
return this
|
|
1013
|
+
return this.#comparator.matching(entityName, cond, tmp);
|
|
1011
1014
|
});
|
|
1012
1015
|
/* v8 ignore next */
|
|
1013
1016
|
if (!row) {
|
|
1014
1017
|
throw new Error(`Cannot find matching entity for condition ${JSON.stringify(cond)}`);
|
|
1015
1018
|
}
|
|
1016
|
-
em.getHydrator().hydrate(entity, meta, row, em
|
|
1019
|
+
em.getHydrator().hydrate(entity, meta, row, em.#entityFactory, 'full', false, true);
|
|
1017
1020
|
}
|
|
1018
1021
|
if (loadPK.size !== data2.length && Array.isArray(uniqueFields)) {
|
|
1019
1022
|
for (let i = 0; i < allData.length; i++) {
|
|
@@ -1030,20 +1033,20 @@ export class EntityManager {
|
|
|
1030
1033
|
a[b] = item[b];
|
|
1031
1034
|
return a;
|
|
1032
1035
|
}, {});
|
|
1033
|
-
return this
|
|
1036
|
+
return this.#comparator.matching(entityName, cond, pk);
|
|
1034
1037
|
});
|
|
1035
1038
|
/* v8 ignore next */
|
|
1036
1039
|
if (!row) {
|
|
1037
1040
|
throw new Error(`Cannot find matching entity for condition ${JSON.stringify(cond)}`);
|
|
1038
1041
|
}
|
|
1039
|
-
em.getHydrator().hydrate(entity, meta, row, em
|
|
1042
|
+
em.getHydrator().hydrate(entity, meta, row, em.#entityFactory, 'full');
|
|
1040
1043
|
}
|
|
1041
1044
|
}
|
|
1042
1045
|
}
|
|
1043
1046
|
for (const [entity] of entities) {
|
|
1044
1047
|
// recompute the data as there might be some values missing (e.g. those with db column defaults)
|
|
1045
|
-
const snapshot = this
|
|
1046
|
-
em
|
|
1048
|
+
const snapshot = this.#comparator.prepareEntity(entity);
|
|
1049
|
+
em.#unitOfWork.register(entity, snapshot, { refresh: true });
|
|
1047
1050
|
}
|
|
1048
1051
|
if (em.eventManager.hasListeners(EventType.afterUpsert, meta)) {
|
|
1049
1052
|
for (const [entity] of entities) {
|
|
@@ -1080,7 +1083,7 @@ export class EntityManager {
|
|
|
1080
1083
|
*/
|
|
1081
1084
|
async transactional(cb, options = {}) {
|
|
1082
1085
|
const em = this.getContext(false);
|
|
1083
|
-
if (this
|
|
1086
|
+
if (this.#disableTransactions || em.#disableTransactions) {
|
|
1084
1087
|
return cb(em);
|
|
1085
1088
|
}
|
|
1086
1089
|
const manager = new TransactionManager(this);
|
|
@@ -1090,11 +1093,11 @@ export class EntityManager {
|
|
|
1090
1093
|
* Starts new transaction bound to this EntityManager. Use `ctx` parameter to provide the parent when nesting transactions.
|
|
1091
1094
|
*/
|
|
1092
1095
|
async begin(options = {}) {
|
|
1093
|
-
if (this
|
|
1096
|
+
if (this.#disableTransactions) {
|
|
1094
1097
|
return;
|
|
1095
1098
|
}
|
|
1096
1099
|
const em = this.getContext(false);
|
|
1097
|
-
em
|
|
1100
|
+
em.#transactionContext = await em.getConnection('write').begin({
|
|
1098
1101
|
...options,
|
|
1099
1102
|
eventBroadcaster: new TransactionEventBroadcaster(em, { topLevelTransaction: !options.ctx }),
|
|
1100
1103
|
});
|
|
@@ -1104,31 +1107,31 @@ export class EntityManager {
|
|
|
1104
1107
|
*/
|
|
1105
1108
|
async commit() {
|
|
1106
1109
|
const em = this.getContext(false);
|
|
1107
|
-
if (this
|
|
1110
|
+
if (this.#disableTransactions) {
|
|
1108
1111
|
await em.flush();
|
|
1109
1112
|
return;
|
|
1110
1113
|
}
|
|
1111
|
-
if (!em
|
|
1114
|
+
if (!em.#transactionContext) {
|
|
1112
1115
|
throw ValidationError.transactionRequired();
|
|
1113
1116
|
}
|
|
1114
1117
|
await em.flush();
|
|
1115
|
-
await em.getConnection('write').commit(em
|
|
1116
|
-
|
|
1118
|
+
await em.getConnection('write').commit(em.#transactionContext, new TransactionEventBroadcaster(em));
|
|
1119
|
+
em.#transactionContext = undefined;
|
|
1117
1120
|
}
|
|
1118
1121
|
/**
|
|
1119
1122
|
* Rollbacks the transaction bound to this EntityManager.
|
|
1120
1123
|
*/
|
|
1121
1124
|
async rollback() {
|
|
1122
|
-
if (this
|
|
1125
|
+
if (this.#disableTransactions) {
|
|
1123
1126
|
return;
|
|
1124
1127
|
}
|
|
1125
1128
|
const em = this.getContext(false);
|
|
1126
|
-
if (!em
|
|
1129
|
+
if (!em.#transactionContext) {
|
|
1127
1130
|
throw ValidationError.transactionRequired();
|
|
1128
1131
|
}
|
|
1129
|
-
await em.getConnection('write').rollback(em
|
|
1130
|
-
|
|
1131
|
-
em
|
|
1132
|
+
await em.getConnection('write').rollback(em.#transactionContext, new TransactionEventBroadcaster(em));
|
|
1133
|
+
em.#transactionContext = undefined;
|
|
1134
|
+
em.#unitOfWork.clearActionsQueue();
|
|
1132
1135
|
}
|
|
1133
1136
|
/**
|
|
1134
1137
|
* Runs your callback wrapped inside a database transaction.
|
|
@@ -1157,20 +1160,20 @@ export class EntityManager {
|
|
|
1157
1160
|
}
|
|
1158
1161
|
if (!helper(data).__managed) {
|
|
1159
1162
|
// the entity might have been created via `em.create()`, which adds it to the persist stack automatically
|
|
1160
|
-
em
|
|
1163
|
+
em.#unitOfWork.getPersistStack().delete(data);
|
|
1161
1164
|
// it can be also in the identity map if it had a PK value already
|
|
1162
|
-
em
|
|
1165
|
+
em.#unitOfWork.unsetIdentity(data);
|
|
1163
1166
|
}
|
|
1164
1167
|
const meta = helper(data).__meta;
|
|
1165
|
-
const payload = em
|
|
1168
|
+
const payload = em.#comparator.prepareEntity(data);
|
|
1166
1169
|
const cs = new ChangeSet(data, ChangeSetType.CREATE, payload, meta);
|
|
1167
|
-
await em
|
|
1170
|
+
await em.#unitOfWork.getChangeSetPersister().executeInserts([cs], { ctx: em.#transactionContext, ...options });
|
|
1168
1171
|
return cs.getPrimaryKey();
|
|
1169
1172
|
}
|
|
1170
1173
|
data = QueryHelper.processObjectParams(data);
|
|
1171
1174
|
validateParams(data, 'insert data');
|
|
1172
1175
|
const res = await em.driver.nativeInsert(entityName, data, {
|
|
1173
|
-
ctx: em
|
|
1176
|
+
ctx: em.#transactionContext,
|
|
1174
1177
|
...options,
|
|
1175
1178
|
});
|
|
1176
1179
|
return res.insertId;
|
|
@@ -1200,20 +1203,20 @@ export class EntityManager {
|
|
|
1200
1203
|
}
|
|
1201
1204
|
if (!helper(row).__managed) {
|
|
1202
1205
|
// the entity might have been created via `em.create()`, which adds it to the persist stack automatically
|
|
1203
|
-
em
|
|
1206
|
+
em.#unitOfWork.getPersistStack().delete(row);
|
|
1204
1207
|
// it can be also in the identity map if it had a PK value already
|
|
1205
|
-
em
|
|
1208
|
+
em.#unitOfWork.unsetIdentity(row);
|
|
1206
1209
|
}
|
|
1207
|
-
const payload = em
|
|
1210
|
+
const payload = em.#comparator.prepareEntity(row);
|
|
1208
1211
|
return new ChangeSet(row, ChangeSetType.CREATE, payload, meta);
|
|
1209
1212
|
});
|
|
1210
|
-
await em
|
|
1213
|
+
await em.#unitOfWork.getChangeSetPersister().executeInserts(css, { ctx: em.#transactionContext, ...options });
|
|
1211
1214
|
return css.map(cs => cs.getPrimaryKey());
|
|
1212
1215
|
}
|
|
1213
1216
|
data = data.map(row => QueryHelper.processObjectParams(row));
|
|
1214
1217
|
data.forEach(row => validateParams(row, 'insert data'));
|
|
1215
1218
|
const res = await em.driver.nativeInsertMany(entityName, data, {
|
|
1216
|
-
ctx: em
|
|
1219
|
+
ctx: em.#transactionContext,
|
|
1217
1220
|
...options,
|
|
1218
1221
|
});
|
|
1219
1222
|
if (res.insertedIds) {
|
|
@@ -1233,7 +1236,7 @@ export class EntityManager {
|
|
|
1233
1236
|
validateParams(data, 'update data');
|
|
1234
1237
|
validateParams(where, 'update condition');
|
|
1235
1238
|
const res = await em.driver.nativeUpdate(entityName, where, data, {
|
|
1236
|
-
ctx: em
|
|
1239
|
+
ctx: em.#transactionContext,
|
|
1237
1240
|
em,
|
|
1238
1241
|
...options,
|
|
1239
1242
|
});
|
|
@@ -1249,7 +1252,7 @@ export class EntityManager {
|
|
|
1249
1252
|
where = (await em.processWhere(entityName, where, options, 'delete'));
|
|
1250
1253
|
validateParams(where, 'delete condition');
|
|
1251
1254
|
const res = await em.driver.nativeDelete(entityName, where, {
|
|
1252
|
-
ctx: em
|
|
1255
|
+
ctx: em.#transactionContext,
|
|
1253
1256
|
em,
|
|
1254
1257
|
...options,
|
|
1255
1258
|
});
|
|
@@ -1286,20 +1289,20 @@ export class EntityManager {
|
|
|
1286
1289
|
return this.merge(entityName.constructor, entityName, data);
|
|
1287
1290
|
}
|
|
1288
1291
|
const em = options.disableContextResolution ? this : this.getContext();
|
|
1289
|
-
options.schema ??= em
|
|
1292
|
+
options.schema ??= em.#schema;
|
|
1290
1293
|
options.validate ??= true;
|
|
1291
1294
|
options.cascade ??= true;
|
|
1292
1295
|
validatePrimaryKey(data, em.metadata.get(entityName));
|
|
1293
|
-
let entity = em
|
|
1296
|
+
let entity = em.#unitOfWork.tryGetById(entityName, data, options.schema, false);
|
|
1294
1297
|
if (entity && helper(entity).__managed && helper(entity).__initialized && !options.refresh) {
|
|
1295
1298
|
return entity;
|
|
1296
1299
|
}
|
|
1297
1300
|
const dataIsEntity = Utils.isEntity(data);
|
|
1298
1301
|
entity = dataIsEntity
|
|
1299
1302
|
? data
|
|
1300
|
-
: em
|
|
1303
|
+
: em.#entityFactory.create(entityName, data, { merge: true, ...options });
|
|
1301
1304
|
const visited = options.cascade ? undefined : new Set([entity]);
|
|
1302
|
-
em
|
|
1305
|
+
em.#unitOfWork.merge(entity, visited);
|
|
1303
1306
|
return entity;
|
|
1304
1307
|
}
|
|
1305
1308
|
/**
|
|
@@ -1319,8 +1322,8 @@ export class EntityManager {
|
|
|
1319
1322
|
*/
|
|
1320
1323
|
create(entityName, data, options = {}) {
|
|
1321
1324
|
const em = this.getContext();
|
|
1322
|
-
options.schema ??= em
|
|
1323
|
-
const entity = em
|
|
1325
|
+
options.schema ??= em.#schema;
|
|
1326
|
+
const entity = em.#entityFactory.create(entityName, data, {
|
|
1324
1327
|
...options,
|
|
1325
1328
|
newEntity: !options.managed,
|
|
1326
1329
|
merge: options.managed,
|
|
@@ -1382,7 +1385,7 @@ export class EntityManager {
|
|
|
1382
1385
|
if (cached?.data !== undefined) {
|
|
1383
1386
|
return cached.data;
|
|
1384
1387
|
}
|
|
1385
|
-
const count = await em.driver.count(entityName, where, { ctx: em
|
|
1388
|
+
const count = await em.driver.count(entityName, where, { ctx: em.#transactionContext, em, ...options });
|
|
1386
1389
|
await em.storeCache(options.cache, cached, () => +count);
|
|
1387
1390
|
return +count;
|
|
1388
1391
|
}
|
|
@@ -1394,7 +1397,7 @@ export class EntityManager {
|
|
|
1394
1397
|
const em = this.getContext();
|
|
1395
1398
|
if (Utils.isEntity(entity)) {
|
|
1396
1399
|
// do not cascade just yet, cascading of entities in persist stack is done when flushing
|
|
1397
|
-
em
|
|
1400
|
+
em.#unitOfWork.persist(entity, undefined, { cascade: false });
|
|
1398
1401
|
return em;
|
|
1399
1402
|
}
|
|
1400
1403
|
const entities = Utils.asArray(entity);
|
|
@@ -1405,7 +1408,7 @@ export class EntityManager {
|
|
|
1405
1408
|
throw ValidationError.notDiscoveredEntity(ent, meta);
|
|
1406
1409
|
}
|
|
1407
1410
|
// do not cascade just yet, cascading of entities in persist stack is done when flushing
|
|
1408
|
-
em
|
|
1411
|
+
em.#unitOfWork.persist(Reference.unwrapReference(ent), undefined, { cascade: false });
|
|
1409
1412
|
}
|
|
1410
1413
|
return this;
|
|
1411
1414
|
}
|
|
@@ -1419,7 +1422,7 @@ export class EntityManager {
|
|
|
1419
1422
|
const em = this.getContext();
|
|
1420
1423
|
if (Utils.isEntity(entity)) {
|
|
1421
1424
|
// do not cascade just yet, cascading of entities in persist stack is done when flushing
|
|
1422
|
-
em
|
|
1425
|
+
em.#unitOfWork.remove(entity, undefined, { cascade: false });
|
|
1423
1426
|
return em;
|
|
1424
1427
|
}
|
|
1425
1428
|
const entities = Utils.asArray(entity, true);
|
|
@@ -1428,7 +1431,7 @@ export class EntityManager {
|
|
|
1428
1431
|
throw new Error(`You need to pass entity instance or reference to 'em.remove()'. To remove entities by condition, use 'em.nativeDelete()'.`);
|
|
1429
1432
|
}
|
|
1430
1433
|
// do not cascade just yet, cascading of entities in remove stack is done when flushing
|
|
1431
|
-
em
|
|
1434
|
+
em.#unitOfWork.remove(Reference.unwrapReference(ent), undefined, { cascade: false });
|
|
1432
1435
|
}
|
|
1433
1436
|
return em;
|
|
1434
1437
|
}
|
|
@@ -1444,7 +1447,7 @@ export class EntityManager {
|
|
|
1444
1447
|
*/
|
|
1445
1448
|
async tryFlush(entityName, options) {
|
|
1446
1449
|
const em = this.getContext();
|
|
1447
|
-
const flushMode = options.flushMode ?? em
|
|
1450
|
+
const flushMode = options.flushMode ?? em.#flushMode ?? em.config.get('flushMode');
|
|
1448
1451
|
const meta = em.metadata.get(entityName);
|
|
1449
1452
|
if (flushMode === FlushMode.COMMIT) {
|
|
1450
1453
|
return;
|
|
@@ -1457,7 +1460,7 @@ export class EntityManager {
|
|
|
1457
1460
|
* Clears the EntityManager. All entities that are currently managed by this EntityManager become detached.
|
|
1458
1461
|
*/
|
|
1459
1462
|
clear() {
|
|
1460
|
-
this.getContext()
|
|
1463
|
+
this.getContext().#unitOfWork.clear();
|
|
1461
1464
|
}
|
|
1462
1465
|
/**
|
|
1463
1466
|
* Checks whether given property can be populated on the entity.
|
|
@@ -1492,7 +1495,7 @@ export class EntityManager {
|
|
|
1492
1495
|
em.prepareOptions(options);
|
|
1493
1496
|
const entityName = arr[0].constructor;
|
|
1494
1497
|
const preparedPopulate = await em.preparePopulate(entityName, { populate: populate, filters: options.filters }, options.validate);
|
|
1495
|
-
await em
|
|
1498
|
+
await em.#entityLoader.populate(entityName, arr, preparedPopulate, options);
|
|
1496
1499
|
return entities;
|
|
1497
1500
|
}
|
|
1498
1501
|
/**
|
|
@@ -1513,26 +1516,26 @@ export class EntityManager {
|
|
|
1513
1516
|
const allowGlobalContext = em.config.get('allowGlobalContext');
|
|
1514
1517
|
em.config.set('allowGlobalContext', true);
|
|
1515
1518
|
const fork = new em.constructor(em.config, em.driver, em.metadata, options.useContext, eventManager);
|
|
1516
|
-
fork.setFlushMode(options.flushMode ?? em
|
|
1517
|
-
fork
|
|
1518
|
-
options.disableTransactions ?? this
|
|
1519
|
+
fork.setFlushMode(options.flushMode ?? em.#flushMode);
|
|
1520
|
+
fork.#disableTransactions =
|
|
1521
|
+
options.disableTransactions ?? this.#disableTransactions ?? this.config.get('disableTransactions');
|
|
1519
1522
|
em.config.set('allowGlobalContext', allowGlobalContext);
|
|
1520
1523
|
if (options.keepTransactionContext) {
|
|
1521
|
-
fork
|
|
1524
|
+
fork.#transactionContext = em.#transactionContext;
|
|
1522
1525
|
}
|
|
1523
|
-
fork
|
|
1524
|
-
fork
|
|
1526
|
+
fork.#filters = { ...em.#filters };
|
|
1527
|
+
fork.#filterParams = Utils.copy(em.#filterParams);
|
|
1525
1528
|
fork.loggerContext = Utils.merge({}, em.loggerContext, options.loggerContext);
|
|
1526
|
-
fork
|
|
1529
|
+
fork.#schema = options.schema ?? em.#schema;
|
|
1527
1530
|
if (!options.clear) {
|
|
1528
|
-
for (const entity of em
|
|
1529
|
-
fork
|
|
1531
|
+
for (const entity of em.#unitOfWork.getIdentityMap()) {
|
|
1532
|
+
fork.#unitOfWork.register(entity);
|
|
1530
1533
|
}
|
|
1531
|
-
for (const entity of em
|
|
1532
|
-
fork
|
|
1534
|
+
for (const entity of em.#unitOfWork.getPersistStack()) {
|
|
1535
|
+
fork.#unitOfWork.persist(entity);
|
|
1533
1536
|
}
|
|
1534
|
-
for (const entity of em
|
|
1535
|
-
fork
|
|
1537
|
+
for (const entity of em.#unitOfWork.getOrphanRemoveStack()) {
|
|
1538
|
+
fork.#unitOfWork.getOrphanRemoveStack().add(entity);
|
|
1536
1539
|
}
|
|
1537
1540
|
}
|
|
1538
1541
|
return fork;
|
|
@@ -1542,21 +1545,21 @@ export class EntityManager {
|
|
|
1542
1545
|
*/
|
|
1543
1546
|
getUnitOfWork(useContext = true) {
|
|
1544
1547
|
if (!useContext) {
|
|
1545
|
-
return this
|
|
1548
|
+
return this.#unitOfWork;
|
|
1546
1549
|
}
|
|
1547
|
-
return this.getContext()
|
|
1550
|
+
return this.getContext().#unitOfWork;
|
|
1548
1551
|
}
|
|
1549
1552
|
/**
|
|
1550
1553
|
* Gets the EntityFactory used by the EntityManager.
|
|
1551
1554
|
*/
|
|
1552
1555
|
getEntityFactory() {
|
|
1553
|
-
return this.getContext()
|
|
1556
|
+
return this.getContext().#entityFactory;
|
|
1554
1557
|
}
|
|
1555
1558
|
/**
|
|
1556
1559
|
* @internal use `em.populate()` as the user facing API, this is exposed only for internal usage
|
|
1557
1560
|
*/
|
|
1558
1561
|
getEntityLoader() {
|
|
1559
|
-
return this.getContext()
|
|
1562
|
+
return this.getContext().#entityLoader;
|
|
1560
1563
|
}
|
|
1561
1564
|
/**
|
|
1562
1565
|
* Gets the Hydrator used by the EntityManager.
|
|
@@ -1569,7 +1572,7 @@ export class EntityManager {
|
|
|
1569
1572
|
* @internal
|
|
1570
1573
|
*/
|
|
1571
1574
|
getContext(validate = true) {
|
|
1572
|
-
if (!this
|
|
1575
|
+
if (!this.#useContext) {
|
|
1573
1576
|
return this;
|
|
1574
1577
|
}
|
|
1575
1578
|
let em = TransactionContext.getEntityManager(this.name); // prefer the tx context
|
|
@@ -1590,13 +1593,13 @@ export class EntityManager {
|
|
|
1590
1593
|
* Checks whether this EntityManager is currently operating inside a database transaction.
|
|
1591
1594
|
*/
|
|
1592
1595
|
isInTransaction() {
|
|
1593
|
-
return !!this.getContext(false)
|
|
1596
|
+
return !!this.getContext(false).#transactionContext;
|
|
1594
1597
|
}
|
|
1595
1598
|
/**
|
|
1596
1599
|
* Gets the transaction context (driver dependent object used to make sure queries are executed on same connection).
|
|
1597
1600
|
*/
|
|
1598
1601
|
getTransactionContext() {
|
|
1599
|
-
return this.getContext(false)
|
|
1602
|
+
return this.getContext(false).#transactionContext;
|
|
1600
1603
|
}
|
|
1601
1604
|
/**
|
|
1602
1605
|
* Sets the transaction context.
|
|
@@ -1606,14 +1609,14 @@ export class EntityManager {
|
|
|
1606
1609
|
this.resetTransactionContext();
|
|
1607
1610
|
}
|
|
1608
1611
|
else {
|
|
1609
|
-
this.getContext(false)
|
|
1612
|
+
this.getContext(false).#transactionContext = ctx;
|
|
1610
1613
|
}
|
|
1611
1614
|
}
|
|
1612
1615
|
/**
|
|
1613
1616
|
* Resets the transaction context.
|
|
1614
1617
|
*/
|
|
1615
1618
|
resetTransactionContext() {
|
|
1616
|
-
|
|
1619
|
+
this.getContext(false).#transactionContext = undefined;
|
|
1617
1620
|
}
|
|
1618
1621
|
/**
|
|
1619
1622
|
* Gets the `MetadataStorage` (without parameters) or `EntityMetadata` instance when provided with the `entityName` parameter.
|
|
@@ -1628,7 +1631,7 @@ export class EntityManager {
|
|
|
1628
1631
|
* Gets the EntityComparator.
|
|
1629
1632
|
*/
|
|
1630
1633
|
getComparator() {
|
|
1631
|
-
return this
|
|
1634
|
+
return this.#comparator;
|
|
1632
1635
|
}
|
|
1633
1636
|
checkLockRequirements(mode, meta) {
|
|
1634
1637
|
if (!mode) {
|
|
@@ -1649,7 +1652,7 @@ export class EntityManager {
|
|
|
1649
1652
|
});
|
|
1650
1653
|
}
|
|
1651
1654
|
const preparedPopulate = await this.preparePopulate(meta.class, options);
|
|
1652
|
-
await this
|
|
1655
|
+
await this.#entityLoader.populate(meta.class, [entity], preparedPopulate, {
|
|
1653
1656
|
...options,
|
|
1654
1657
|
...this.getPopulateWhere(where, options),
|
|
1655
1658
|
orderBy: options.populateOrderBy ?? options.orderBy,
|
|
@@ -1713,7 +1716,7 @@ export class EntityManager {
|
|
|
1713
1716
|
options.populate = pruneToOneRelations(meta, this.buildFields(options.fields));
|
|
1714
1717
|
}
|
|
1715
1718
|
if (!options.populate) {
|
|
1716
|
-
const populate = this
|
|
1719
|
+
const populate = this.#entityLoader.normalizePopulate(entityName, [], options.strategy, true, options.exclude);
|
|
1717
1720
|
await this.autoJoinRefsForFilters(meta, { ...options, populate });
|
|
1718
1721
|
return populate;
|
|
1719
1722
|
}
|
|
@@ -1737,7 +1740,7 @@ export class EntityManager {
|
|
|
1737
1740
|
})
|
|
1738
1741
|
.flat();
|
|
1739
1742
|
}
|
|
1740
|
-
const populate = this
|
|
1743
|
+
const populate = this.#entityLoader.normalizePopulate(entityName, options.populate, options.strategy, true, options.exclude);
|
|
1741
1744
|
const invalid = populate.find(({ field }) => !this.canPopulate(entityName, field));
|
|
1742
1745
|
if (validate && invalid) {
|
|
1743
1746
|
throw ValidationError.invalidPropertyName(entityName, invalid.field);
|
|
@@ -1783,7 +1786,7 @@ export class EntityManager {
|
|
|
1783
1786
|
if (!Utils.isEmpty(options.fields) && !Utils.isEmpty(options.exclude)) {
|
|
1784
1787
|
throw new ValidationError(`Cannot combine 'fields' and 'exclude' option.`);
|
|
1785
1788
|
}
|
|
1786
|
-
options.schema ??= this
|
|
1789
|
+
options.schema ??= this.#schema;
|
|
1787
1790
|
options.logging = options.loggerContext = Utils.merge({ id: this.id }, this.loggerContext, options.loggerContext, options.logging);
|
|
1788
1791
|
}
|
|
1789
1792
|
/**
|
|
@@ -1807,7 +1810,7 @@ export class EntityManager {
|
|
|
1807
1810
|
}
|
|
1808
1811
|
const em = this.getContext();
|
|
1809
1812
|
const cacheKey = Array.isArray(config) ? config[0] : JSON.stringify(key);
|
|
1810
|
-
const cached = await em
|
|
1813
|
+
const cached = await em.#resultCache.get(cacheKey);
|
|
1811
1814
|
if (!cached) {
|
|
1812
1815
|
return { key: cacheKey, data: cached };
|
|
1813
1816
|
}
|
|
@@ -1819,15 +1822,15 @@ export class EntityManager {
|
|
|
1819
1822
|
recomputeSnapshot: true,
|
|
1820
1823
|
};
|
|
1821
1824
|
if (Array.isArray(cached) && merge) {
|
|
1822
|
-
data = cached.map(item => em
|
|
1825
|
+
data = cached.map(item => em.#entityFactory.create(entityName, item, createOptions));
|
|
1823
1826
|
}
|
|
1824
1827
|
else if (Utils.isObject(cached) && merge) {
|
|
1825
|
-
data = em
|
|
1828
|
+
data = em.#entityFactory.create(entityName, cached, createOptions);
|
|
1826
1829
|
}
|
|
1827
1830
|
else {
|
|
1828
1831
|
data = cached;
|
|
1829
1832
|
}
|
|
1830
|
-
await em
|
|
1833
|
+
await em.#unitOfWork.dispatchOnLoadEvent();
|
|
1831
1834
|
return { key: cacheKey, data };
|
|
1832
1835
|
}
|
|
1833
1836
|
/**
|
|
@@ -1838,7 +1841,7 @@ export class EntityManager {
|
|
|
1838
1841
|
if (config) {
|
|
1839
1842
|
const em = this.getContext();
|
|
1840
1843
|
const expiration = Array.isArray(config) ? config[1] : typeof config === 'number' ? config : undefined;
|
|
1841
|
-
await em
|
|
1844
|
+
await em.#resultCache.set(key.key, data instanceof Function ? data() : data, '', expiration);
|
|
1842
1845
|
}
|
|
1843
1846
|
}
|
|
1844
1847
|
/**
|
|
@@ -1854,37 +1857,37 @@ export class EntityManager {
|
|
|
1854
1857
|
* ```
|
|
1855
1858
|
*/
|
|
1856
1859
|
async clearCache(cacheKey) {
|
|
1857
|
-
await this.getContext()
|
|
1860
|
+
await this.getContext().#resultCache.remove(cacheKey);
|
|
1858
1861
|
}
|
|
1859
1862
|
/**
|
|
1860
1863
|
* Returns the default schema of this EntityManager. Respects the context, so global EM will give you the contextual schema
|
|
1861
1864
|
* if executed inside request context handler.
|
|
1862
1865
|
*/
|
|
1863
1866
|
get schema() {
|
|
1864
|
-
return this.getContext(false)
|
|
1867
|
+
return this.getContext(false).#schema;
|
|
1865
1868
|
}
|
|
1866
1869
|
/**
|
|
1867
1870
|
* Sets the default schema of this EntityManager. Respects the context, so global EM will set the contextual schema
|
|
1868
1871
|
* if executed inside request context handler.
|
|
1869
1872
|
*/
|
|
1870
1873
|
set schema(schema) {
|
|
1871
|
-
this.getContext(false)
|
|
1874
|
+
this.getContext(false).#schema = schema ?? undefined;
|
|
1872
1875
|
}
|
|
1873
1876
|
/** @internal */
|
|
1874
1877
|
async getDataLoader(type) {
|
|
1875
1878
|
const em = this.getContext();
|
|
1876
|
-
if (em
|
|
1877
|
-
return em
|
|
1879
|
+
if (em.#loaders[type]) {
|
|
1880
|
+
return em.#loaders[type];
|
|
1878
1881
|
}
|
|
1879
1882
|
const { DataloaderUtils } = await import('@mikro-orm/core/dataloader');
|
|
1880
1883
|
const DataLoader = await DataloaderUtils.getDataLoader();
|
|
1881
1884
|
switch (type) {
|
|
1882
1885
|
case 'ref':
|
|
1883
|
-
return (em
|
|
1886
|
+
return (em.#loaders[type] ??= new DataLoader(DataloaderUtils.getRefBatchLoadFn(em)));
|
|
1884
1887
|
case '1:m':
|
|
1885
|
-
return (em
|
|
1888
|
+
return (em.#loaders[type] ??= new DataLoader(DataloaderUtils.getColBatchLoadFn(em)));
|
|
1886
1889
|
case 'm:n':
|
|
1887
|
-
return (em
|
|
1890
|
+
return (em.#loaders[type] ??= new DataLoader(DataloaderUtils.getManyToManyColBatchLoadFn(em)));
|
|
1888
1891
|
}
|
|
1889
1892
|
}
|
|
1890
1893
|
/**
|