@mikro-orm/core 7.2.10-dev.2 → 7.2.10-dev.20
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 +19 -7
- package/entity/WrappedEntity.js +1 -1
- package/hydration/ObjectHydrator.js +13 -7
- package/package.json +1 -1
- package/platforms/Platform.js +4 -1
- package/typings.d.ts +1 -0
- package/typings.js +1 -0
- package/unit-of-work/ChangeSetComputer.js +48 -45
- package/unit-of-work/UnitOfWork.js +31 -20
- package/utils/Configuration.d.ts +2 -2
- package/utils/Configuration.js +2 -2
- package/utils/Utils.js +13 -1
- package/utils/clone.js +4 -0
- package/utils/upsert-utils.js +13 -6
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
|
@@ -4,6 +4,7 @@ import { EventType, ReferenceKind } from '../enums.js';
|
|
|
4
4
|
import { Reference } from './Reference.js';
|
|
5
5
|
import { helper } from './wrap.js';
|
|
6
6
|
import { EntityHelper } from './EntityHelper.js';
|
|
7
|
+
import { PolymorphicRef } from './PolymorphicRef.js';
|
|
7
8
|
import { JsonType } from '../types/JsonType.js';
|
|
8
9
|
import { isRaw } from '../utils/RawQueryFragment.js';
|
|
9
10
|
/** @internal Factory responsible for creating, merging, and hydrating entity instances. */
|
|
@@ -52,8 +53,11 @@ export class EntityFactory {
|
|
|
52
53
|
if (wrapped && !options.refresh) {
|
|
53
54
|
const wasInitialized = wrapped.isInitialized();
|
|
54
55
|
wrapped.__processing = true;
|
|
55
|
-
|
|
56
|
-
this.
|
|
56
|
+
// the no-op check rejects `undefined` values itself, so the recursive cleanup is only needed for a real merge
|
|
57
|
+
if (!this.isNoopMerge(meta2, exists, data, options)) {
|
|
58
|
+
Utils.dropUndefinedProperties(data);
|
|
59
|
+
this.mergeData(meta2, exists, data, options);
|
|
60
|
+
}
|
|
57
61
|
wrapped.__processing = false;
|
|
58
62
|
// index a merged `targetKey` value, `getByKey` ignores the old one as stale
|
|
59
63
|
if (meta2.root.targetKeys) {
|
|
@@ -234,12 +238,20 @@ export class EntityFactory {
|
|
|
234
238
|
const nullVal = this.#config.get('forceUndefined') ? undefined : null;
|
|
235
239
|
Utils.keys(diff2).forEach(key => {
|
|
236
240
|
const prop = meta.properties[key];
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
+
// a joined polymorphic row carries its target class in `constructor`, so it is not a plain object
|
|
242
|
+
const joined = Utils.isPlainObject(data[prop.name]) ||
|
|
243
|
+
(prop.polymorphic && Utils.isObject(data[prop.name]) && !(data[prop.name] instanceof PolymorphicRef));
|
|
244
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && joined) {
|
|
245
|
+
const target = entity[prop.name] ? Reference.unwrapReference(entity[prop.name]) : null;
|
|
246
|
+
// use the actual target's meta, a polymorphic relation has several
|
|
247
|
+
let value = target ? EntityHelper.getForeignKeyValue(target, prop, helper(target).__meta) : null;
|
|
248
|
+
if (target && prop.polymorphic) {
|
|
249
|
+
const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, target.constructor);
|
|
250
|
+
value = new PolymorphicRef(discriminator, value);
|
|
251
|
+
}
|
|
252
|
+
diff2[key] = value;
|
|
241
253
|
}
|
|
242
|
-
if (!options.convertCustomTypes &&
|
|
254
|
+
else if (!options.convertCustomTypes &&
|
|
243
255
|
[ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE, ReferenceKind.SCALAR].includes(prop.kind) &&
|
|
244
256
|
prop.customType?.ensureComparable(meta, prop) &&
|
|
245
257
|
diff2[key] != null) {
|
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.20",
|
|
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",
|
package/platforms/Platform.js
CHANGED
|
@@ -366,7 +366,7 @@ export class Platform {
|
|
|
366
366
|
}
|
|
367
367
|
/** Serializes a string array into its database storage format. */
|
|
368
368
|
marshallArray(values) {
|
|
369
|
-
return values.map(v => (/[,",\\]/.test(v) ? JSON.stringify(v) : v)).join(',');
|
|
369
|
+
return values.map(v => (/[,",\\]/.test(v) || (v === '' && values.length === 1) ? JSON.stringify(v) : v)).join(',');
|
|
370
370
|
}
|
|
371
371
|
/** Deserializes a database-stored array string back into a string array. */
|
|
372
372
|
unmarshallArray(value) {
|
|
@@ -406,6 +406,9 @@ export class Platform {
|
|
|
406
406
|
i = comma + 1;
|
|
407
407
|
}
|
|
408
408
|
}
|
|
409
|
+
if (value.endsWith(',')) {
|
|
410
|
+
result.push('');
|
|
411
|
+
}
|
|
409
412
|
return result;
|
|
410
413
|
}
|
|
411
414
|
getBlobDeclarationSQL() {
|
package/typings.d.ts
CHANGED
|
@@ -1190,6 +1190,7 @@ export interface EntityMetadata<Entity = any, Class extends EntityCtor<Entity> =
|
|
|
1190
1190
|
comparableProps: EntityProperty<Entity>[];
|
|
1191
1191
|
trackingProps: EntityProperty<Entity>[];
|
|
1192
1192
|
hydrateProps: EntityProperty<Entity>[];
|
|
1193
|
+
updateHookProps: EntityProperty<Entity>[];
|
|
1193
1194
|
validateProps: EntityProperty<Entity>[];
|
|
1194
1195
|
uniqueProps: EntityProperty<Entity>[];
|
|
1195
1196
|
getterProps: EntityProperty<Entity>[];
|
package/typings.js
CHANGED
|
@@ -216,6 +216,7 @@ export class EntityMetadata {
|
|
|
216
216
|
const onlyGetter = prop.getter && !prop.setter && prop.persist === false;
|
|
217
217
|
return !prop.inherited && prop.hydrate !== false && !discriminator && !prop.embedded && !onlyGetter;
|
|
218
218
|
});
|
|
219
|
+
this.updateHookProps = this.hydrateProps.filter(prop => prop.onUpdate || prop.kind === ReferenceKind.EMBEDDED);
|
|
219
220
|
this.trackingProps = this.hydrateProps.filter(prop => {
|
|
220
221
|
return (!prop.getter &&
|
|
221
222
|
!prop.setter &&
|
|
@@ -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,10 +63,11 @@ 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) {
|
|
72
|
-
for (const prop of meta.
|
|
70
|
+
for (const prop of meta.updateHookProps) {
|
|
73
71
|
this.processPropertyInitializers(entity, prop, type, map);
|
|
74
72
|
}
|
|
75
73
|
}
|
|
@@ -109,8 +107,9 @@ export class ChangeSetComputer {
|
|
|
109
107
|
}
|
|
110
108
|
if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
|
|
111
109
|
const items = prop.array ? entity[prop.name] : [entity[prop.name]];
|
|
110
|
+
const embeddedProps = type === ChangeSetType.UPDATE ? prop.targetMeta.updateHookProps : prop.targetMeta.hydrateProps;
|
|
112
111
|
for (const item of items) {
|
|
113
|
-
for (const embeddedProp of
|
|
112
|
+
for (const embeddedProp of embeddedProps) {
|
|
114
113
|
this.processPropertyInitializers(item, embeddedProp, type, map, nested || prop.object);
|
|
115
114
|
}
|
|
116
115
|
}
|
|
@@ -146,53 +145,57 @@ export class ChangeSetComputer {
|
|
|
146
145
|
return data;
|
|
147
146
|
}
|
|
148
147
|
processProperty(changeSet, prop) {
|
|
149
|
-
|
|
150
|
-
|
|
148
|
+
if (prop.kind !== ReferenceKind.MANY_TO_ONE && prop.kind !== ReferenceKind.ONE_TO_ONE) {
|
|
149
|
+
// m:n or 1:m
|
|
150
|
+
if (Utils.isCollection(changeSet.entity[prop.name])) {
|
|
151
|
+
this.processToMany(prop, changeSet);
|
|
152
|
+
}
|
|
151
153
|
return;
|
|
152
154
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
+
// only owning to-one sides referencing an entity can add an FK value to the payload
|
|
156
|
+
if ((prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner) || prop.mapToPk) {
|
|
155
157
|
return;
|
|
156
158
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
159
|
+
// perf: a relation outside of embeddables has a single target, so skip the allocations of `unwrapProperty`
|
|
160
|
+
if (!prop.object && !prop.array && !prop.embedded) {
|
|
161
|
+
const target = changeSet.entity[prop.name];
|
|
162
|
+
if (target != null) {
|
|
163
|
+
this.processToOne(prop, changeSet, target, []);
|
|
161
164
|
}
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
168
|
+
for (const [target, idx] of targets) {
|
|
169
|
+
this.processToOne(prop, changeSet, target, idx);
|
|
162
170
|
}
|
|
163
171
|
}
|
|
164
|
-
processToOne(prop, changeSet,
|
|
165
|
-
const
|
|
166
|
-
|
|
172
|
+
processToOne(prop, changeSet, rawTarget, idx) {
|
|
173
|
+
const target = Reference.unwrapReference(rawTarget);
|
|
174
|
+
// a same-named scalar of another polymorphic embeddable variant is not a reference
|
|
175
|
+
const needsProcessing = Utils.isEntity(target) && (prop.targetKey != null || !target.__helper.hasPrimaryKey());
|
|
176
|
+
if (!needsProcessing) {
|
|
167
177
|
return;
|
|
168
178
|
}
|
|
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
|
-
});
|
|
179
|
+
// a reference known only by its `targetKey` has no PK, and is never inserted to generate one
|
|
180
|
+
if (!prop.targetKey && !target.__helper.__initialized) {
|
|
181
|
+
throw ValidationError.referenceWithoutPK(target);
|
|
182
|
+
}
|
|
183
|
+
// use the actual target's meta, a polymorphic relation has several
|
|
184
|
+
let value = prop.targetKey
|
|
185
|
+
? EntityHelper.getForeignKeyValue(target, prop, target.__meta)
|
|
186
|
+
: target.__helper.__identifier;
|
|
187
|
+
if (prop.polymorphic) {
|
|
188
|
+
const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, target.constructor);
|
|
189
|
+
value = new PolymorphicRef(discriminator, value);
|
|
190
|
+
}
|
|
191
|
+
// an unchanged `targetKey` FK is not part of the diff
|
|
192
|
+
if (prop.targetKey &&
|
|
193
|
+
changeSet.type === ChangeSetType.UPDATE &&
|
|
194
|
+
!(prop.name in changeSet.payload) &&
|
|
195
|
+
Utils.equals(changeSet.originalEntity?.[prop.name], value)) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, value, idx);
|
|
196
199
|
}
|
|
197
200
|
processToMany(prop, changeSet) {
|
|
198
201
|
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/Configuration.d.ts
CHANGED
|
@@ -961,8 +961,8 @@ export interface Options<Driver extends IDatabaseDriver = IDatabaseDriver, EM ex
|
|
|
961
961
|
};
|
|
962
962
|
/**
|
|
963
963
|
* Metadata provider class for entity discovery.
|
|
964
|
-
* Built-in options: `
|
|
965
|
-
* @default
|
|
964
|
+
* Built-in options: `MetadataProvider` (default), `ReflectMetadataProvider` (from `@mikro-orm/decorators/legacy`), `TsMorphMetadataProvider`.
|
|
965
|
+
* @default MetadataProvider
|
|
966
966
|
* @see https://mikro-orm.io/docs/metadata-providers
|
|
967
967
|
*/
|
|
968
968
|
metadataProvider: {
|
package/utils/Configuration.js
CHANGED
|
@@ -25,7 +25,6 @@ const DEFAULTS = {
|
|
|
25
25
|
warnWhenNoEntities: true,
|
|
26
26
|
checkDuplicateTableNames: true,
|
|
27
27
|
checkDuplicateFieldNames: true,
|
|
28
|
-
checkDuplicateEntities: true,
|
|
29
28
|
checkNonPersistentCompositeProps: true,
|
|
30
29
|
inferDefaultValues: true,
|
|
31
30
|
},
|
|
@@ -149,7 +148,8 @@ export class Configuration {
|
|
|
149
148
|
#routines = [];
|
|
150
149
|
#routinesNormalised = false;
|
|
151
150
|
constructor(options, validate = true) {
|
|
152
|
-
|
|
151
|
+
// the default is present when re-creating from `getAll()`, it must not override an already registered provider
|
|
152
|
+
if (options.dynamicImportProvider && options.dynamicImportProvider !== DEFAULTS.dynamicImportProvider) {
|
|
153
153
|
globalThis.dynamicImportProvider = options.dynamicImportProvider;
|
|
154
154
|
}
|
|
155
155
|
this.#options = Utils.mergeConfig({}, DEFAULTS, options);
|
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.20';
|
|
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) {
|
package/utils/upsert-utils.js
CHANGED
|
@@ -155,14 +155,21 @@ export function getWhereCondition(meta, onConflictFields, data, where) {
|
|
|
155
155
|
unique.findIndex(p => data[p] ?? data[p.substring(0, p.indexOf('.'))] != null);
|
|
156
156
|
if (onConflictFields || where == null) {
|
|
157
157
|
if (propIndex !== false && propIndex >= 0) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
158
|
+
// `onConflictFields` form one composite key, while the unique props are each unique on their own
|
|
159
|
+
const keys = onConflictFields
|
|
160
|
+
? unique.filter(p => (data[p] ?? data[p.substring(0, p.indexOf('.'))]) != null)
|
|
161
|
+
: [unique[propIndex]];
|
|
162
|
+
where = {};
|
|
163
|
+
for (const field of keys) {
|
|
164
|
+
let key = field;
|
|
165
|
+
if (key.includes('.')) {
|
|
166
|
+
const prop = meta.properties[key.substring(0, key.indexOf('.'))];
|
|
167
|
+
if (prop) {
|
|
168
|
+
key = `${prop.fieldNames[0]}${key.substring(key.indexOf('.'))}`;
|
|
169
|
+
}
|
|
163
170
|
}
|
|
171
|
+
where[key] = getPropertyValue(data, field);
|
|
164
172
|
}
|
|
165
|
-
where = { [key]: getPropertyValue(data, unique[propIndex]) };
|
|
166
173
|
}
|
|
167
174
|
else {
|
|
168
175
|
for (const u of uniques) {
|