@mikro-orm/core 7.2.10-dev.3 → 7.2.10-dev.5
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.js +4 -1
- package/entity/EntityFactory.js +5 -2
- package/entity/WrappedEntity.js +1 -1
- package/hydration/ObjectHydrator.js +13 -7
- package/package.json +1 -1
- package/unit-of-work/ChangeSetComputer.js +45 -43
- package/unit-of-work/UnitOfWork.js +31 -20
- package/utils/Utils.js +13 -1
- package/utils/clone.js +4 -0
package/EntityManager.js
CHANGED
|
@@ -1782,7 +1782,10 @@ export class EntityManager {
|
|
|
1782
1782
|
if (!options.cascade) {
|
|
1783
1783
|
helper(entity).__managed = true;
|
|
1784
1784
|
}
|
|
1785
|
-
em
|
|
1785
|
+
// the factory already registered it with everything it hydrated, and `em.find()` does not cascade a merge either
|
|
1786
|
+
if (!(snapshotted && helper(entity).__managed)) {
|
|
1787
|
+
em.#unitOfWork.merge(entity, visited);
|
|
1788
|
+
}
|
|
1786
1789
|
return entity;
|
|
1787
1790
|
}
|
|
1788
1791
|
/**
|
package/entity/EntityFactory.js
CHANGED
|
@@ -52,8 +52,11 @@ export class EntityFactory {
|
|
|
52
52
|
if (wrapped && !options.refresh) {
|
|
53
53
|
const wasInitialized = wrapped.isInitialized();
|
|
54
54
|
wrapped.__processing = true;
|
|
55
|
-
|
|
56
|
-
this.
|
|
55
|
+
// the no-op check rejects `undefined` values itself, so the recursive cleanup is only needed for a real merge
|
|
56
|
+
if (!this.isNoopMerge(meta2, exists, data, options)) {
|
|
57
|
+
Utils.dropUndefinedProperties(data);
|
|
58
|
+
this.mergeData(meta2, exists, data, options);
|
|
59
|
+
}
|
|
57
60
|
wrapped.__processing = false;
|
|
58
61
|
// index a merged `targetKey` value, `getByKey` ignores the old one as stale
|
|
59
62
|
if (meta2.root.targetKeys) {
|
package/entity/WrappedEntity.js
CHANGED
|
@@ -110,7 +110,7 @@ export class WrappedEntity {
|
|
|
110
110
|
}
|
|
111
111
|
/** Returns the primary key value, optionally converting custom types to their database representation. */
|
|
112
112
|
getPrimaryKey(convertCustomTypes = false) {
|
|
113
|
-
const prop = this.__meta.
|
|
113
|
+
const prop = this.__meta.getPrimaryProp();
|
|
114
114
|
if (!prop) {
|
|
115
115
|
return null;
|
|
116
116
|
}
|
|
@@ -9,11 +9,13 @@ import { Raw } from '../utils/RawQueryFragment.js';
|
|
|
9
9
|
import { ValidationError } from '../errors.js';
|
|
10
10
|
/** @internal JIT-compiled hydrator that converts raw database rows into entity instances with optimized generated code. */
|
|
11
11
|
export class ObjectHydrator extends Hydrator {
|
|
12
|
+
// keyed by type and `normalizeAccessors` without building a string key on every lookup
|
|
12
13
|
#hydrators = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
full: { normalized: new Map(), raw: new Map() },
|
|
15
|
+
reference: {
|
|
16
|
+
normalized: new Map(),
|
|
17
|
+
raw: new Map(),
|
|
18
|
+
},
|
|
17
19
|
};
|
|
18
20
|
#tmpIndex = 0;
|
|
19
21
|
/**
|
|
@@ -42,8 +44,8 @@ export class ObjectHydrator extends Hydrator {
|
|
|
42
44
|
* @internal Highly performance-sensitive method.
|
|
43
45
|
*/
|
|
44
46
|
getEntityHydrator(meta, type, normalizeAccessors = false) {
|
|
45
|
-
const
|
|
46
|
-
const exists =
|
|
47
|
+
const cache = this.#hydrators[type][normalizeAccessors ? 'normalized' : 'raw'];
|
|
48
|
+
const exists = cache.get(meta.class);
|
|
47
49
|
if (exists) {
|
|
48
50
|
return exists;
|
|
49
51
|
}
|
|
@@ -106,6 +108,10 @@ export class ObjectHydrator extends Hydrator {
|
|
|
106
108
|
else if (prop.runtimeType === 'boolean') {
|
|
107
109
|
ret.push(` entity${entityKey} = !!data${dataKey};`);
|
|
108
110
|
}
|
|
111
|
+
else if (prop.formula && prop.runtimeType === 'number') {
|
|
112
|
+
// drivers can return numeric expressions as strings, e.g. `count(*)` is a bigint in postgres
|
|
113
|
+
ret.push(` entity${entityKey} = Number(data${dataKey});`);
|
|
114
|
+
}
|
|
109
115
|
else if (prop.runtimeType === 'Date' && !this.platform.isNumericProperty(prop)) {
|
|
110
116
|
ret.push(` if (data${dataKey} instanceof Date) {`);
|
|
111
117
|
ret.push(` entity${entityKey} = data${dataKey};`);
|
|
@@ -413,7 +419,7 @@ export class ObjectHydrator extends Hydrator {
|
|
|
413
419
|
`${lines.join('\n')}\n}`;
|
|
414
420
|
const fnKey = `hydrator-${meta.compiledName}-${type}-${normalizeAccessors}`;
|
|
415
421
|
const hydrator = Utils.createFunction(context, code, this.config.get('compiledFunctions'), fnKey);
|
|
416
|
-
|
|
422
|
+
cache.set(meta.class, hydrator);
|
|
417
423
|
return hydrator;
|
|
418
424
|
}
|
|
419
425
|
createCollectionItemMapper(prop, context) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
|
-
"version": "7.2.10-dev.
|
|
3
|
+
"version": "7.2.10-dev.5",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
|
@@ -33,13 +33,10 @@ export class ChangeSetComputer {
|
|
|
33
33
|
}
|
|
34
34
|
const wrapped = helper(entity);
|
|
35
35
|
const type = wrapped.__originalEntityData ? ChangeSetType.UPDATE : ChangeSetType.CREATE;
|
|
36
|
-
|
|
37
|
-
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
38
|
-
// to the `map` as we want to apply those only if something else changed.
|
|
36
|
+
// execute `onCreate` on properties recursively, update hooks run only after we know there are other changes
|
|
39
37
|
if (type === ChangeSetType.CREATE) {
|
|
40
|
-
// run update hooks only after we know there are other changes
|
|
41
38
|
for (const prop of meta.hydrateProps) {
|
|
42
|
-
this.processPropertyInitializers(entity, prop, type
|
|
39
|
+
this.processPropertyInitializers(entity, prop, type);
|
|
43
40
|
}
|
|
44
41
|
}
|
|
45
42
|
if (type === ChangeSetType.UPDATE && !wrapped.__initialized) {
|
|
@@ -66,6 +63,7 @@ export class ChangeSetComputer {
|
|
|
66
63
|
return null;
|
|
67
64
|
}
|
|
68
65
|
validateEntity(changeSet.entity, meta);
|
|
66
|
+
const map = new Map();
|
|
69
67
|
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
70
68
|
// to the `map` as we want to apply those only if something else changed.
|
|
71
69
|
if (type === ChangeSetType.UPDATE) {
|
|
@@ -146,53 +144,57 @@ export class ChangeSetComputer {
|
|
|
146
144
|
return data;
|
|
147
145
|
}
|
|
148
146
|
processProperty(changeSet, prop) {
|
|
149
|
-
|
|
150
|
-
|
|
147
|
+
if (prop.kind !== ReferenceKind.MANY_TO_ONE && prop.kind !== ReferenceKind.ONE_TO_ONE) {
|
|
148
|
+
// m:n or 1:m
|
|
149
|
+
if (Utils.isCollection(changeSet.entity[prop.name])) {
|
|
150
|
+
this.processToMany(prop, changeSet);
|
|
151
|
+
}
|
|
151
152
|
return;
|
|
152
153
|
}
|
|
153
|
-
|
|
154
|
-
|
|
154
|
+
// only owning to-one sides referencing an entity can add an FK value to the payload
|
|
155
|
+
if ((prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner) || prop.mapToPk) {
|
|
155
156
|
return;
|
|
156
157
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
158
|
+
// perf: a relation outside of embeddables has a single target, so skip the allocations of `unwrapProperty`
|
|
159
|
+
if (!prop.object && !prop.array && !prop.embedded) {
|
|
160
|
+
const target = changeSet.entity[prop.name];
|
|
161
|
+
if (target != null) {
|
|
162
|
+
this.processToOne(prop, changeSet, target, []);
|
|
161
163
|
}
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
167
|
+
for (const [target, idx] of targets) {
|
|
168
|
+
this.processToOne(prop, changeSet, target, idx);
|
|
162
169
|
}
|
|
163
170
|
}
|
|
164
|
-
processToOne(prop, changeSet,
|
|
165
|
-
const
|
|
166
|
-
|
|
171
|
+
processToOne(prop, changeSet, rawTarget, idx) {
|
|
172
|
+
const target = Reference.unwrapReference(rawTarget);
|
|
173
|
+
// a same-named scalar of another polymorphic embeddable variant is not a reference
|
|
174
|
+
const needsProcessing = Utils.isEntity(target) && (prop.targetKey != null || !target.__helper.hasPrimaryKey());
|
|
175
|
+
if (!needsProcessing) {
|
|
167
176
|
return;
|
|
168
177
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
!(prop.name in changeSet.payload) &&
|
|
190
|
-
Utils.equals(changeSet.originalEntity?.[prop.name], value)) {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, value, idx);
|
|
194
|
-
}
|
|
195
|
-
});
|
|
178
|
+
// a reference known only by its `targetKey` has no PK, and is never inserted to generate one
|
|
179
|
+
if (!prop.targetKey && !target.__helper.__initialized) {
|
|
180
|
+
throw ValidationError.referenceWithoutPK(target);
|
|
181
|
+
}
|
|
182
|
+
// use the actual target's meta, a polymorphic relation has several
|
|
183
|
+
let value = prop.targetKey
|
|
184
|
+
? EntityHelper.getForeignKeyValue(target, prop, target.__meta)
|
|
185
|
+
: target.__helper.__identifier;
|
|
186
|
+
if (prop.polymorphic) {
|
|
187
|
+
const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, target.constructor);
|
|
188
|
+
value = new PolymorphicRef(discriminator, value);
|
|
189
|
+
}
|
|
190
|
+
// an unchanged `targetKey` FK is not part of the diff
|
|
191
|
+
if (prop.targetKey &&
|
|
192
|
+
changeSet.type === ChangeSetType.UPDATE &&
|
|
193
|
+
!(prop.name in changeSet.payload) &&
|
|
194
|
+
Utils.equals(changeSet.originalEntity?.[prop.name], value)) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, value, idx);
|
|
196
198
|
}
|
|
197
199
|
processToMany(prop, changeSet) {
|
|
198
200
|
const target = changeSet.entity[prop.name];
|
|
@@ -572,8 +572,9 @@ export class UnitOfWork {
|
|
|
572
572
|
}
|
|
573
573
|
}
|
|
574
574
|
}
|
|
575
|
-
delete
|
|
576
|
-
|
|
575
|
+
// `delete` would switch the wrapper to dictionary mode
|
|
576
|
+
wrapped.__identifier = undefined;
|
|
577
|
+
wrapped.__originalEntityData = undefined;
|
|
577
578
|
wrapped.__managed = false;
|
|
578
579
|
}
|
|
579
580
|
computeChangeSets() {
|
|
@@ -583,17 +584,21 @@ export class UnitOfWork {
|
|
|
583
584
|
this.cascade(entity, Cascade.REMOVE, visited);
|
|
584
585
|
}
|
|
585
586
|
visited.clear();
|
|
587
|
+
const persistOptions = { checkRemoveStack: true };
|
|
586
588
|
for (const entity of this.#identityMap) {
|
|
587
589
|
if (!this.#removeStack.has(entity) && !this.#persistStack.has(entity) && !this.#orphanRemoveStack.has(entity)) {
|
|
588
|
-
this.cascade(entity, Cascade.PERSIST, visited,
|
|
590
|
+
this.cascade(entity, Cascade.PERSIST, visited, persistOptions);
|
|
589
591
|
}
|
|
590
592
|
}
|
|
591
593
|
for (const entity of this.#persistStack) {
|
|
592
|
-
this.cascade(entity, Cascade.PERSIST, visited,
|
|
594
|
+
this.cascade(entity, Cascade.PERSIST, visited, persistOptions);
|
|
593
595
|
}
|
|
594
596
|
visited.clear();
|
|
597
|
+
// perf: every traversal starts with an empty `processed` set, reuse one instead of allocating it per entity
|
|
598
|
+
const processed = new Set();
|
|
595
599
|
for (const entity of this.#persistStack) {
|
|
596
|
-
|
|
600
|
+
processed.clear();
|
|
601
|
+
this.findNewEntities(entity, visited, 0, processed);
|
|
597
602
|
}
|
|
598
603
|
for (const entity of this.#orphanRemoveStack) {
|
|
599
604
|
if (!helper(entity).__processing) {
|
|
@@ -632,8 +637,10 @@ export class UnitOfWork {
|
|
|
632
637
|
}
|
|
633
638
|
}
|
|
634
639
|
}
|
|
635
|
-
|
|
636
|
-
const
|
|
640
|
+
// without inserts of this type (the common case), there is nothing to match the removed entity against
|
|
641
|
+
const early = hashes.size > 0 &&
|
|
642
|
+
[wrapped.getSerializedPrimaryKey(), ...this.expandUniqueProps(entity)].some(hash => hashes.has(hash));
|
|
643
|
+
const type = early ? ChangeSetType.DELETE_EARLY : ChangeSetType.DELETE;
|
|
637
644
|
this.computeChangeSet(entity, type);
|
|
638
645
|
}
|
|
639
646
|
}
|
|
@@ -690,7 +697,7 @@ export class UnitOfWork {
|
|
|
690
697
|
getChangeSetPersister() {
|
|
691
698
|
return this.#changeSetPersister;
|
|
692
699
|
}
|
|
693
|
-
findNewEntities(entity, visited, idx
|
|
700
|
+
findNewEntities(entity, visited, idx, processed) {
|
|
694
701
|
if (visited.has(entity)) {
|
|
695
702
|
return;
|
|
696
703
|
}
|
|
@@ -862,13 +869,12 @@ export class UnitOfWork {
|
|
|
862
869
|
return this.processToOneReference(kind, visited, processed, idx);
|
|
863
870
|
}
|
|
864
871
|
if (Utils.isCollection(kind)) {
|
|
865
|
-
kind
|
|
866
|
-
.getItems(false)
|
|
867
|
-
.filter(item => !item.__helper.__originalEntityData)
|
|
868
|
-
.forEach(item => {
|
|
872
|
+
for (const item of kind.getItems(false)) {
|
|
869
873
|
// propagate schema from parent
|
|
870
|
-
item.__helper.
|
|
871
|
-
|
|
874
|
+
if (!item.__helper.__originalEntityData) {
|
|
875
|
+
item.__helper.__schema ??= helper(parent).__schema;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
872
878
|
if (prop.kind === ReferenceKind.MANY_TO_MANY && kind.isDirty()) {
|
|
873
879
|
this.processToManyReference(kind, visited, processed, parent, prop);
|
|
874
880
|
}
|
|
@@ -966,11 +972,11 @@ export class UnitOfWork {
|
|
|
966
972
|
return;
|
|
967
973
|
}
|
|
968
974
|
const kind = Reference.unwrapReference(entity[prop.name]);
|
|
969
|
-
if (
|
|
975
|
+
if ((prop.kind === ReferenceKind.MANY_TO_ONE || prop.kind === ReferenceKind.ONE_TO_ONE) && Utils.isEntity(kind)) {
|
|
970
976
|
return this.cascade(kind, type, visited, options);
|
|
971
977
|
}
|
|
972
978
|
const collection = kind;
|
|
973
|
-
if (
|
|
979
|
+
if ((prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY) && collection) {
|
|
974
980
|
for (const item of collection.getItems(false)) {
|
|
975
981
|
this.cascade(item, type, visited, options);
|
|
976
982
|
}
|
|
@@ -981,8 +987,11 @@ export class UnitOfWork {
|
|
|
981
987
|
return filtered.some(items => processed.has(items));
|
|
982
988
|
}
|
|
983
989
|
shouldCascade(prop, type) {
|
|
984
|
-
if (
|
|
985
|
-
|
|
990
|
+
if (prop.orphanRemoval &&
|
|
991
|
+
(type === Cascade.REMOVE ||
|
|
992
|
+
type === Cascade.SCHEDULE_ORPHAN_REMOVAL ||
|
|
993
|
+
type === Cascade.CANCEL_ORPHAN_REMOVAL ||
|
|
994
|
+
type === Cascade.ALL)) {
|
|
986
995
|
return true;
|
|
987
996
|
}
|
|
988
997
|
// ignore user settings for merge, it is kept only for back compatibility, this should have never been configurable
|
|
@@ -1016,7 +1025,9 @@ export class UnitOfWork {
|
|
|
1016
1025
|
fixMissingReference(entity, prop) {
|
|
1017
1026
|
const reference = entity[prop.name];
|
|
1018
1027
|
const target = Reference.unwrapReference(reference);
|
|
1019
|
-
if (
|
|
1028
|
+
if ((prop.kind === ReferenceKind.MANY_TO_ONE || prop.kind === ReferenceKind.ONE_TO_ONE) &&
|
|
1029
|
+
target &&
|
|
1030
|
+
!prop.mapToPk) {
|
|
1020
1031
|
if (!Utils.isEntity(target)) {
|
|
1021
1032
|
entity[prop.name] = this.#em.getReference(prop.targetMeta.class, target, {
|
|
1022
1033
|
wrapped: !!prop.ref,
|
|
@@ -1033,7 +1044,7 @@ export class UnitOfWork {
|
|
|
1033
1044
|
if (Utils.isCollection(target)) {
|
|
1034
1045
|
target.property = prop;
|
|
1035
1046
|
}
|
|
1036
|
-
const isCollection =
|
|
1047
|
+
const isCollection = prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY;
|
|
1037
1048
|
if (isCollection && Array.isArray(target)) {
|
|
1038
1049
|
const collection = new Collection(entity);
|
|
1039
1050
|
collection.property = prop;
|
package/utils/Utils.js
CHANGED
|
@@ -153,7 +153,7 @@ export function parseJsonSafe(value) {
|
|
|
153
153
|
/** Collection of general-purpose utility methods used throughout the ORM. */
|
|
154
154
|
export class Utils {
|
|
155
155
|
static PK_SEPARATOR = '~~~';
|
|
156
|
-
static #ORM_VERSION = '7.2.10-dev.
|
|
156
|
+
static #ORM_VERSION = '7.2.10-dev.5';
|
|
157
157
|
/** Default session variable name backing an RLS filter argument (`current_setting('mikro.<filter>.<arg>')`). */
|
|
158
158
|
static getRlsSettingName(filterName, argName) {
|
|
159
159
|
return `mikro.${filterName}.${argName}`;
|
|
@@ -640,6 +640,18 @@ export class Utils {
|
|
|
640
640
|
* Checks whether the value is POJO (e.g. `{ foo: 'bar' }`, and not instance of `Foo`)
|
|
641
641
|
*/
|
|
642
642
|
static isPlainObject(value) {
|
|
643
|
+
// primitives and object literals short-circuit with the full check's result, functions can still be null-prototype objects
|
|
644
|
+
if (typeof value === 'object') {
|
|
645
|
+
if (value === null) {
|
|
646
|
+
return false;
|
|
647
|
+
}
|
|
648
|
+
if (value.constructor === Object) {
|
|
649
|
+
return true;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
else if (typeof value !== 'function') {
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
643
655
|
return ((value !== null &&
|
|
644
656
|
typeof value === 'object' &&
|
|
645
657
|
typeof value.constructor === 'function' &&
|
package/utils/clone.js
CHANGED
|
@@ -22,6 +22,10 @@ function getPropertyDescriptor(obj, prop) {
|
|
|
22
22
|
}
|
|
23
23
|
const TypedArray = Object.getPrototypeOf(Uint8Array);
|
|
24
24
|
export function clone(parent, respectCustomCloneMethod = true) {
|
|
25
|
+
// perf: snapshots clone every scalar, skip the cycle map for primitives
|
|
26
|
+
if (parent === null || typeof parent !== 'object') {
|
|
27
|
+
return parent;
|
|
28
|
+
}
|
|
25
29
|
// already cloned objects, so shared references and cycles map to a single clone
|
|
26
30
|
const clones = new Map();
|
|
27
31
|
function _clone(parent) {
|