@mikro-orm/core 7.2.11-dev.7 → 7.2.11-dev.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/EntityManager.js +8 -6
- package/entity/Collection.d.ts +5 -1
- package/entity/Collection.js +12 -3
- package/entity/EntityLoader.js +7 -5
- package/entity/Reference.d.ts +1 -1
- package/entity/Reference.js +6 -5
- package/package.json +1 -1
- package/unit-of-work/UnitOfWork.d.ts +7 -0
- package/unit-of-work/UnitOfWork.js +29 -4
- package/utils/Utils.js +1 -1
package/EntityManager.js
CHANGED
|
@@ -1932,13 +1932,15 @@ export class EntityManager {
|
|
|
1932
1932
|
return em;
|
|
1933
1933
|
}
|
|
1934
1934
|
const entities = Utils.asArray(entity, true);
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1935
|
+
em.#unitOfWork.batchRemovals(() => {
|
|
1936
|
+
for (const ent of entities) {
|
|
1937
|
+
if (!Utils.isEntity(ent, true)) {
|
|
1938
|
+
throw new Error(`You need to pass entity instance or reference to 'em.remove()'. To remove entities by condition, use 'em.nativeDelete()'.`);
|
|
1939
|
+
}
|
|
1940
|
+
// do not cascade just yet, cascading of entities in remove stack is done when flushing
|
|
1941
|
+
em.#unitOfWork.remove(Reference.unwrapReference(ent), undefined, { cascade: false });
|
|
1938
1942
|
}
|
|
1939
|
-
|
|
1940
|
-
em.#unitOfWork.remove(Reference.unwrapReference(ent), undefined, { cascade: false });
|
|
1941
|
-
}
|
|
1943
|
+
});
|
|
1942
1944
|
return em;
|
|
1943
1945
|
}
|
|
1944
1946
|
/**
|
package/entity/Collection.d.ts
CHANGED
|
@@ -108,7 +108,11 @@ export declare class Collection<T extends object, O extends object = object> {
|
|
|
108
108
|
/**
|
|
109
109
|
* @internal
|
|
110
110
|
*/
|
|
111
|
-
removeWithoutPropagation(entity: T):
|
|
111
|
+
removeWithoutPropagation(entity: T, reindex?: boolean): boolean;
|
|
112
|
+
/**
|
|
113
|
+
* @internal
|
|
114
|
+
*/
|
|
115
|
+
reindex(): void;
|
|
112
116
|
/**
|
|
113
117
|
* Extracts a slice of the collection items starting at position start to end (exclusive) of the collection.
|
|
114
118
|
* If end is null it returns all elements from start to the end of the collection.
|
package/entity/Collection.js
CHANGED
|
@@ -520,14 +520,23 @@ export class Collection {
|
|
|
520
520
|
/**
|
|
521
521
|
* @internal
|
|
522
522
|
*/
|
|
523
|
-
removeWithoutPropagation(entity) {
|
|
523
|
+
removeWithoutPropagation(entity, reindex = true) {
|
|
524
524
|
if (!this.#items.delete(entity)) {
|
|
525
|
-
return;
|
|
525
|
+
return false;
|
|
526
526
|
}
|
|
527
527
|
this.incrementCount(-1);
|
|
528
528
|
delete this[this.#items.size];
|
|
529
|
-
|
|
529
|
+
if (reindex) {
|
|
530
|
+
this.reindex();
|
|
531
|
+
}
|
|
530
532
|
this.#dirty = true;
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* @internal
|
|
537
|
+
*/
|
|
538
|
+
reindex() {
|
|
539
|
+
Object.assign(this, [...this.#items]); // reassign array access
|
|
531
540
|
}
|
|
532
541
|
/**
|
|
533
542
|
* Extracts a slice of the collection items starting at position start to end (exclusive) of the collection.
|
package/entity/EntityLoader.js
CHANGED
|
@@ -905,14 +905,14 @@ export class EntityLoader {
|
|
|
905
905
|
.map(c => c.field.split(':'))
|
|
906
906
|
.filter(parts => parts[1] === 'ref')
|
|
907
907
|
.map(parts => parts[0]));
|
|
908
|
+
const childFields = options.fields
|
|
909
|
+
.filter(f => f.startsWith(`${field}.`))
|
|
910
|
+
.map(f => f.substring(field.length + 1))
|
|
911
|
+
.filter(cf => !refChildren.has(cf.split('.')[0]));
|
|
908
912
|
return children
|
|
909
913
|
.map(e => Reference.unwrapReference(e[field]))
|
|
910
914
|
.filter(target => {
|
|
911
915
|
const wrapped = helper(target);
|
|
912
|
-
const childFields = options.fields
|
|
913
|
-
.filter(f => f.startsWith(`${field}.`))
|
|
914
|
-
.map(f => f.substring(field.length + 1))
|
|
915
|
-
.filter(cf => !refChildren.has(cf.split('.')[0]));
|
|
916
916
|
return !wrapped.__initialized || !childFields.every(cf => this.isPropertyLoaded(target, cf));
|
|
917
917
|
});
|
|
918
918
|
}
|
|
@@ -926,6 +926,8 @@ export class EntityLoader {
|
|
|
926
926
|
return entities;
|
|
927
927
|
}
|
|
928
928
|
const field = prop.name;
|
|
929
|
+
// the same for every entity, so compute it only once
|
|
930
|
+
let childFields;
|
|
929
931
|
return entities.filter(e => {
|
|
930
932
|
const value = e[field];
|
|
931
933
|
if (value === null) {
|
|
@@ -947,7 +949,7 @@ export class EntityLoader {
|
|
|
947
949
|
return false;
|
|
948
950
|
}
|
|
949
951
|
if (options.fields) {
|
|
950
|
-
|
|
952
|
+
childFields ??= options.fields
|
|
951
953
|
.filter(f => f.startsWith(`${prop.name}.`))
|
|
952
954
|
.map(f => f.substring(prop.name.length + 1));
|
|
953
955
|
return childFields.length > 0 && !childFields.every(f => this.isPropertyLoaded(target, f));
|
package/entity/Reference.d.ts
CHANGED
|
@@ -67,7 +67,7 @@ export declare class ScalarReference<Value> {
|
|
|
67
67
|
load(options?: Omit<LoadReferenceOptions<any, any>, 'populate' | 'fields' | 'exclude'>): Promise<Value | undefined>;
|
|
68
68
|
/**
|
|
69
69
|
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
70
|
-
* Returns the
|
|
70
|
+
* Returns the value or throws an error naming the property, unless a `failHandler` is passed in the options.
|
|
71
71
|
*/
|
|
72
72
|
loadOrFail(options?: Omit<LoadReferenceOrFailOptions<any, any>, 'populate' | 'fields' | 'exclude'>): Promise<Value>;
|
|
73
73
|
/** Sets the scalar value and marks the reference as initialized. */
|
package/entity/Reference.js
CHANGED
|
@@ -119,10 +119,10 @@ export class Reference {
|
|
|
119
119
|
const ret = await this.load(options);
|
|
120
120
|
if (!ret) {
|
|
121
121
|
const wrapped = helper(this.entity);
|
|
122
|
-
options.failHandler
|
|
122
|
+
const failHandler = options.failHandler ?? wrapped.__em.config.get('findOneOrFailHandler');
|
|
123
123
|
const entityName = this.entity.constructor.name;
|
|
124
124
|
const where = wrapped.getPrimaryKey();
|
|
125
|
-
throw
|
|
125
|
+
throw failHandler(entityName, where);
|
|
126
126
|
}
|
|
127
127
|
return ret;
|
|
128
128
|
}
|
|
@@ -202,15 +202,16 @@ export class ScalarReference {
|
|
|
202
202
|
}
|
|
203
203
|
/**
|
|
204
204
|
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
205
|
-
* Returns the
|
|
205
|
+
* Returns the value or throws an error naming the property, unless a `failHandler` is passed in the options.
|
|
206
206
|
*/
|
|
207
207
|
async loadOrFail(options = {}) {
|
|
208
208
|
const ret = await this.load(options);
|
|
209
209
|
if (ret == null) {
|
|
210
210
|
const wrapped = helper(this.entity);
|
|
211
|
-
options.failHandler ??= wrapped.__em.config.get('findOneOrFailHandler');
|
|
212
211
|
const entityName = this.entity.constructor.name;
|
|
213
|
-
|
|
212
|
+
const where = wrapped.getPrimaryKey();
|
|
213
|
+
throw (options.failHandler?.(entityName, where) ??
|
|
214
|
+
NotFoundError.failedToLoadProperty(entityName, this.#property, where));
|
|
214
215
|
}
|
|
215
216
|
return ret;
|
|
216
217
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
|
-
"version": "7.2.11-dev.
|
|
3
|
+
"version": "7.2.11-dev.9",
|
|
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",
|
|
@@ -90,6 +90,13 @@ export declare class UnitOfWork {
|
|
|
90
90
|
remove<T extends object>(entity: T, visited?: Set<AnyEntity>, options?: {
|
|
91
91
|
cascade?: boolean;
|
|
92
92
|
}): void;
|
|
93
|
+
/**
|
|
94
|
+
* Runs `cb` and updates the array indexes of the collections that `remove()` changed only once at the end,
|
|
95
|
+
* so removing many items of one loaded collection stays linear.
|
|
96
|
+
* @internal
|
|
97
|
+
*/
|
|
98
|
+
batchRemovals(cb: () => void): void;
|
|
99
|
+
private removeFromCollection;
|
|
93
100
|
/** Flushes all pending changes to the database within a transaction. */
|
|
94
101
|
commit(): Promise<void>;
|
|
95
102
|
private doCommit;
|
|
@@ -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`
|
|
@@ -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) {
|
|
@@ -580,8 +601,12 @@ export class UnitOfWork {
|
|
|
580
601
|
computeChangeSets() {
|
|
581
602
|
this.#changeSets.clear();
|
|
582
603
|
const visited = new Set();
|
|
583
|
-
|
|
584
|
-
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
|
+
});
|
|
585
610
|
}
|
|
586
611
|
visited.clear();
|
|
587
612
|
const persistOptions = { checkRemoveStack: true };
|
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.11-dev.
|
|
156
|
+
static #ORM_VERSION = '7.2.11-dev.9';
|
|
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}`;
|