@mikro-orm/core 7.3.0-dev.22 → 7.3.0-dev.24
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 +23 -8
- package/entity/Collection.d.ts +7 -2
- package/entity/Collection.js +33 -16
- package/entity/EntityAssigner.js +4 -0
- package/entity/EntityFactory.js +19 -7
- package/entity/EntityHelper.js +4 -0
- package/entity/EntityLoader.js +56 -11
- package/entity/Reference.d.ts +1 -1
- package/entity/Reference.js +12 -10
- package/entity/WrappedEntity.js +1 -1
- package/entity/utils.js +3 -1
- package/entity/validators.d.ts +6 -0
- package/entity/validators.js +14 -0
- package/errors.d.ts +1 -0
- package/errors.js +3 -0
- package/hydration/ObjectHydrator.js +16 -10
- package/naming-strategy/AbstractNamingStrategy.js +2 -1
- package/package.json +1 -1
- package/platforms/Platform.d.ts +5 -2
- package/platforms/Platform.js +19 -9
- package/serialization/EntitySerializer.js +3 -3
- package/serialization/EntityTransformer.js +29 -30
- package/serialization/SerializationContext.js +1 -1
- package/typings.d.ts +1 -0
- package/typings.js +36 -34
- package/unit-of-work/ChangeSetComputer.js +55 -50
- package/unit-of-work/UnitOfWork.d.ts +7 -0
- package/unit-of-work/UnitOfWork.js +70 -26
- package/utils/Configuration.d.ts +2 -2
- package/utils/Configuration.js +2 -2
- package/utils/Utils.js +52 -8
- package/utils/clone.js +4 -0
- package/utils/upsert-utils.js +13 -6
|
@@ -35,6 +35,7 @@ export class UnitOfWork {
|
|
|
35
35
|
#loadedEntities = new Set();
|
|
36
36
|
#flushQueue = [];
|
|
37
37
|
#working = false;
|
|
38
|
+
#reindexQueue;
|
|
38
39
|
#em;
|
|
39
40
|
/**
|
|
40
41
|
* UoW lives in `@mikro-orm/core` alongside `EntityManager` and reaches `getAbortOptions`
|
|
@@ -249,7 +250,7 @@ export class UnitOfWork {
|
|
|
249
250
|
/** Attempts to extract a primary key from the where condition and look up the entity in the identity map. */
|
|
250
251
|
tryGetById(entityName, where, schema, strict = true, convertCustomTypes) {
|
|
251
252
|
const pk = Utils.extractPK(where, this.#metadata.find(entityName), strict);
|
|
252
|
-
if (
|
|
253
|
+
if (pk == null) {
|
|
253
254
|
return null;
|
|
254
255
|
}
|
|
255
256
|
return this.getById(entityName, pk, schema, convertCustomTypes);
|
|
@@ -437,20 +438,40 @@ export class UnitOfWork {
|
|
|
437
438
|
for (const owner of relation.getItems(false)) {
|
|
438
439
|
const ownerColl = owner[inverseProp];
|
|
439
440
|
if (Utils.isCollection(ownerColl)) {
|
|
440
|
-
|
|
441
|
+
this.removeFromCollection(ownerColl, entity);
|
|
441
442
|
}
|
|
442
443
|
}
|
|
443
444
|
continue;
|
|
444
445
|
}
|
|
445
446
|
const target = relation?.[inverseProp];
|
|
446
447
|
if (relation && Utils.isCollection(target)) {
|
|
447
|
-
|
|
448
|
+
this.removeFromCollection(target, entity);
|
|
448
449
|
}
|
|
449
450
|
}
|
|
450
451
|
if (options.cascade ?? true) {
|
|
451
452
|
this.cascade(entity, Cascade.REMOVE, visited);
|
|
452
453
|
}
|
|
453
454
|
}
|
|
455
|
+
/**
|
|
456
|
+
* Runs `cb` and updates the array indexes of the collections that `remove()` changed only once at the end,
|
|
457
|
+
* so removing many items of one loaded collection stays linear.
|
|
458
|
+
* @internal
|
|
459
|
+
*/
|
|
460
|
+
batchRemovals(cb) {
|
|
461
|
+
const queue = (this.#reindexQueue = new Set());
|
|
462
|
+
try {
|
|
463
|
+
cb();
|
|
464
|
+
}
|
|
465
|
+
finally {
|
|
466
|
+
this.#reindexQueue = undefined;
|
|
467
|
+
queue.forEach(coll => coll.reindex());
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
removeFromCollection(coll, entity) {
|
|
471
|
+
if (coll.removeWithoutPropagation(entity, !this.#reindexQueue)) {
|
|
472
|
+
this.#reindexQueue?.add(coll);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
454
475
|
/** Flushes all pending changes to the database within a transaction. */
|
|
455
476
|
async commit() {
|
|
456
477
|
if (this.#working) {
|
|
@@ -572,28 +593,37 @@ export class UnitOfWork {
|
|
|
572
593
|
}
|
|
573
594
|
}
|
|
574
595
|
}
|
|
575
|
-
delete
|
|
576
|
-
|
|
596
|
+
// `delete` would switch the wrapper to dictionary mode
|
|
597
|
+
wrapped.__identifier = undefined;
|
|
598
|
+
wrapped.__originalEntityData = undefined;
|
|
577
599
|
wrapped.__managed = false;
|
|
578
600
|
}
|
|
579
601
|
computeChangeSets() {
|
|
580
602
|
this.#changeSets.clear();
|
|
581
603
|
const visited = new Set();
|
|
582
|
-
|
|
583
|
-
this.
|
|
604
|
+
if (this.#removeStack.size > 0) {
|
|
605
|
+
this.batchRemovals(() => {
|
|
606
|
+
for (const entity of this.#removeStack) {
|
|
607
|
+
this.cascade(entity, Cascade.REMOVE, visited);
|
|
608
|
+
}
|
|
609
|
+
});
|
|
584
610
|
}
|
|
585
611
|
visited.clear();
|
|
612
|
+
const persistOptions = { checkRemoveStack: true };
|
|
586
613
|
for (const entity of this.#identityMap) {
|
|
587
614
|
if (!this.#removeStack.has(entity) && !this.#persistStack.has(entity) && !this.#orphanRemoveStack.has(entity)) {
|
|
588
|
-
this.cascade(entity, Cascade.PERSIST, visited,
|
|
615
|
+
this.cascade(entity, Cascade.PERSIST, visited, persistOptions);
|
|
589
616
|
}
|
|
590
617
|
}
|
|
591
618
|
for (const entity of this.#persistStack) {
|
|
592
|
-
this.cascade(entity, Cascade.PERSIST, visited,
|
|
619
|
+
this.cascade(entity, Cascade.PERSIST, visited, persistOptions);
|
|
593
620
|
}
|
|
594
621
|
visited.clear();
|
|
622
|
+
// perf: every traversal starts with an empty `processed` set, reuse one instead of allocating it per entity
|
|
623
|
+
const processed = new Set();
|
|
595
624
|
for (const entity of this.#persistStack) {
|
|
596
|
-
|
|
625
|
+
processed.clear();
|
|
626
|
+
this.findNewEntities(entity, visited, 0, processed);
|
|
597
627
|
}
|
|
598
628
|
for (const entity of this.#orphanRemoveStack) {
|
|
599
629
|
if (!helper(entity).__processing) {
|
|
@@ -632,8 +662,10 @@ export class UnitOfWork {
|
|
|
632
662
|
}
|
|
633
663
|
}
|
|
634
664
|
}
|
|
635
|
-
|
|
636
|
-
const
|
|
665
|
+
// without inserts of this type (the common case), there is nothing to match the removed entity against
|
|
666
|
+
const early = hashes.size > 0 &&
|
|
667
|
+
[wrapped.getSerializedPrimaryKey(), ...this.expandUniqueProps(entity)].some(hash => hashes.has(hash));
|
|
668
|
+
const type = early ? ChangeSetType.DELETE_EARLY : ChangeSetType.DELETE;
|
|
637
669
|
this.computeChangeSet(entity, type);
|
|
638
670
|
}
|
|
639
671
|
}
|
|
@@ -690,7 +722,7 @@ export class UnitOfWork {
|
|
|
690
722
|
getChangeSetPersister() {
|
|
691
723
|
return this.#changeSetPersister;
|
|
692
724
|
}
|
|
693
|
-
findNewEntities(entity, visited, idx
|
|
725
|
+
findNewEntities(entity, visited, idx, processed) {
|
|
694
726
|
if (visited.has(entity)) {
|
|
695
727
|
return;
|
|
696
728
|
}
|
|
@@ -704,6 +736,13 @@ export class UnitOfWork {
|
|
|
704
736
|
wrapped.__schema ??= this.#em.schema;
|
|
705
737
|
this.initIdentifier(entity);
|
|
706
738
|
for (const prop of wrapped.__meta.relations) {
|
|
739
|
+
// perf: a relation outside of embeddables has a single target, so skip the allocations of `unwrapProperty`
|
|
740
|
+
if (!prop.object && !prop.array && !prop.embedded) {
|
|
741
|
+
if (entity[prop.name] != null) {
|
|
742
|
+
this.processReference(entity, prop, Reference.unwrapReference(entity[prop.name]), visited, processed, idx);
|
|
743
|
+
}
|
|
744
|
+
continue;
|
|
745
|
+
}
|
|
707
746
|
const targets = Utils.unwrapProperty(entity, wrapped.__meta, prop);
|
|
708
747
|
for (const [target] of targets) {
|
|
709
748
|
const kind = Reference.unwrapReference(target);
|
|
@@ -855,13 +894,12 @@ export class UnitOfWork {
|
|
|
855
894
|
return this.processToOneReference(kind, visited, processed, idx);
|
|
856
895
|
}
|
|
857
896
|
if (Utils.isCollection(kind)) {
|
|
858
|
-
kind
|
|
859
|
-
.getItems(false)
|
|
860
|
-
.filter(item => !item.__helper.__originalEntityData)
|
|
861
|
-
.forEach(item => {
|
|
897
|
+
for (const item of kind.getItems(false)) {
|
|
862
898
|
// propagate schema from parent
|
|
863
|
-
item.__helper.
|
|
864
|
-
|
|
899
|
+
if (!item.__helper.__originalEntityData) {
|
|
900
|
+
item.__helper.__schema ??= helper(parent).__schema;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
865
903
|
if (prop.kind === ReferenceKind.MANY_TO_MANY && kind.isDirty()) {
|
|
866
904
|
this.processToManyReference(kind, visited, processed, parent, prop);
|
|
867
905
|
}
|
|
@@ -915,7 +953,8 @@ export class UnitOfWork {
|
|
|
915
953
|
for (const cs of this.#changeSets.values()) {
|
|
916
954
|
const wrapped = helper(cs.entity);
|
|
917
955
|
wrapped.__processing = false;
|
|
918
|
-
delete
|
|
956
|
+
// `delete` would switch the wrapper to dictionary mode for the rest of the entity's life
|
|
957
|
+
wrapped.__pk = undefined;
|
|
919
958
|
}
|
|
920
959
|
this.#persistStack.clear();
|
|
921
960
|
this.#removeStack.clear();
|
|
@@ -958,11 +997,11 @@ export class UnitOfWork {
|
|
|
958
997
|
return;
|
|
959
998
|
}
|
|
960
999
|
const kind = Reference.unwrapReference(entity[prop.name]);
|
|
961
|
-
if (
|
|
1000
|
+
if ((prop.kind === ReferenceKind.MANY_TO_ONE || prop.kind === ReferenceKind.ONE_TO_ONE) && Utils.isEntity(kind)) {
|
|
962
1001
|
return this.cascade(kind, type, visited, options);
|
|
963
1002
|
}
|
|
964
1003
|
const collection = kind;
|
|
965
|
-
if (
|
|
1004
|
+
if ((prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY) && collection) {
|
|
966
1005
|
for (const item of collection.getItems(false)) {
|
|
967
1006
|
this.cascade(item, type, visited, options);
|
|
968
1007
|
}
|
|
@@ -973,8 +1012,11 @@ export class UnitOfWork {
|
|
|
973
1012
|
return filtered.some(items => processed.has(items));
|
|
974
1013
|
}
|
|
975
1014
|
shouldCascade(prop, type) {
|
|
976
|
-
if (
|
|
977
|
-
|
|
1015
|
+
if (prop.orphanRemoval &&
|
|
1016
|
+
(type === Cascade.REMOVE ||
|
|
1017
|
+
type === Cascade.SCHEDULE_ORPHAN_REMOVAL ||
|
|
1018
|
+
type === Cascade.CANCEL_ORPHAN_REMOVAL ||
|
|
1019
|
+
type === Cascade.ALL)) {
|
|
978
1020
|
return true;
|
|
979
1021
|
}
|
|
980
1022
|
// ignore user settings for merge, it is kept only for back compatibility, this should have never been configurable
|
|
@@ -1008,7 +1050,9 @@ export class UnitOfWork {
|
|
|
1008
1050
|
fixMissingReference(entity, prop) {
|
|
1009
1051
|
const reference = entity[prop.name];
|
|
1010
1052
|
const target = Reference.unwrapReference(reference);
|
|
1011
|
-
if (
|
|
1053
|
+
if ((prop.kind === ReferenceKind.MANY_TO_ONE || prop.kind === ReferenceKind.ONE_TO_ONE) &&
|
|
1054
|
+
target &&
|
|
1055
|
+
!prop.mapToPk) {
|
|
1012
1056
|
if (!Utils.isEntity(target)) {
|
|
1013
1057
|
entity[prop.name] = this.#em.getReference(prop.targetMeta.class, target, {
|
|
1014
1058
|
wrapped: !!prop.ref,
|
|
@@ -1025,7 +1069,7 @@ export class UnitOfWork {
|
|
|
1025
1069
|
if (Utils.isCollection(target)) {
|
|
1026
1070
|
target.property = prop;
|
|
1027
1071
|
}
|
|
1028
|
-
const isCollection =
|
|
1072
|
+
const isCollection = prop.kind === ReferenceKind.ONE_TO_MANY || prop.kind === ReferenceKind.MANY_TO_MANY;
|
|
1029
1073
|
if (isCollection && Array.isArray(target)) {
|
|
1030
1074
|
const collection = new Collection(entity);
|
|
1031
1075
|
collection.property = prop;
|
package/utils/Configuration.d.ts
CHANGED
|
@@ -972,8 +972,8 @@ export interface Options<Driver extends IDatabaseDriver = IDatabaseDriver, EM ex
|
|
|
972
972
|
};
|
|
973
973
|
/**
|
|
974
974
|
* Metadata provider class for entity discovery.
|
|
975
|
-
* Built-in options: `
|
|
976
|
-
* @default
|
|
975
|
+
* Built-in options: `MetadataProvider` (default), `ReflectMetadataProvider` (from `@mikro-orm/decorators/legacy`), `TsMorphMetadataProvider`.
|
|
976
|
+
* @default MetadataProvider
|
|
977
977
|
* @see https://mikro-orm.io/docs/metadata-providers
|
|
978
978
|
*/
|
|
979
979
|
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.3.0-dev.
|
|
156
|
+
static #ORM_VERSION = '7.3.0-dev.24';
|
|
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}`;
|
|
@@ -335,23 +335,55 @@ export class Utils {
|
|
|
335
335
|
}
|
|
336
336
|
let depth = 0;
|
|
337
337
|
let end = start;
|
|
338
|
+
let paramStart = start + 1;
|
|
339
|
+
const params = [];
|
|
340
|
+
let quote;
|
|
338
341
|
for (; end < source.length; end++) {
|
|
339
|
-
if (
|
|
342
|
+
if (quote) {
|
|
343
|
+
if (source[end] === '\\') {
|
|
344
|
+
end++;
|
|
345
|
+
}
|
|
346
|
+
else if (source[end] === quote) {
|
|
347
|
+
quote = undefined;
|
|
348
|
+
}
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const commentEnd = source.startsWith('/*', end) ? '*/' : source.startsWith('//', end) ? '\n' : undefined;
|
|
352
|
+
if (commentEnd) {
|
|
353
|
+
const close = source.indexOf(commentEnd, end + 2);
|
|
354
|
+
end = close === -1 ? source.length : close + commentEnd.length - 1;
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
// a slash that does not follow an operand starts a regex literal
|
|
358
|
+
if (source[end] === '/' && !/[\w$)\]]\s*$/.test(source.slice(start, end))) {
|
|
359
|
+
const regex = /\/(?:\\.|\[(?:\\.|[^\]\\])*\]|[^/\\[\n])+\//y;
|
|
360
|
+
regex.lastIndex = end;
|
|
361
|
+
end += (regex.exec(source)?.[0].length ?? 1) - 1;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (`'"\``.includes(source[end])) {
|
|
365
|
+
quote = source[end];
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
if ('([{'.includes(source[end])) {
|
|
340
369
|
depth++;
|
|
341
370
|
}
|
|
342
|
-
if (source[end]
|
|
371
|
+
if (')]}'.includes(source[end])) {
|
|
343
372
|
depth--;
|
|
344
373
|
}
|
|
345
374
|
if (depth === 0) {
|
|
346
375
|
break;
|
|
347
376
|
}
|
|
377
|
+
if (depth === 1 && source[end] === ',') {
|
|
378
|
+
params.push(source.slice(paramStart, end));
|
|
379
|
+
paramStart = end + 1;
|
|
380
|
+
}
|
|
348
381
|
}
|
|
349
|
-
|
|
350
|
-
return
|
|
351
|
-
.
|
|
352
|
-
.map(s => s.trim().replace(/=.*$/, '').trim())
|
|
382
|
+
params.push(source.slice(paramStart, end));
|
|
383
|
+
return params
|
|
384
|
+
.map(s => s.replace(/\/\*.*?\*\/|\/\/[^\n]*/gs, '').trim())
|
|
353
385
|
.filter(Boolean)
|
|
354
|
-
.map(raw => (
|
|
386
|
+
.map(raw => (/^[{[]/.test(raw) ? '' : raw.replace(/=.*$/s, '').trim()));
|
|
355
387
|
}
|
|
356
388
|
/**
|
|
357
389
|
* Checks whether the argument looks like primary key (string, number or ObjectId).
|
|
@@ -640,6 +672,18 @@ export class Utils {
|
|
|
640
672
|
* Checks whether the value is POJO (e.g. `{ foo: 'bar' }`, and not instance of `Foo`)
|
|
641
673
|
*/
|
|
642
674
|
static isPlainObject(value) {
|
|
675
|
+
// primitives and object literals short-circuit with the full check's result, functions can still be null-prototype objects
|
|
676
|
+
if (typeof value === 'object') {
|
|
677
|
+
if (value === null) {
|
|
678
|
+
return false;
|
|
679
|
+
}
|
|
680
|
+
if (value.constructor === Object) {
|
|
681
|
+
return true;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
else if (typeof value !== 'function') {
|
|
685
|
+
return false;
|
|
686
|
+
}
|
|
643
687
|
return ((value !== null &&
|
|
644
688
|
typeof value === 'object' &&
|
|
645
689
|
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) {
|