@mikro-orm/core 7.0.0-dev.65 → 7.0.0-dev.67
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 +3 -3
- package/entity/Collection.d.ts +95 -27
- package/entity/Collection.js +413 -91
- package/entity/EntityFactory.js +1 -1
- package/entity/EntityValidator.js +2 -2
- package/entity/index.d.ts +0 -1
- package/entity/index.js +0 -1
- package/metadata/EntitySchema.js +3 -3
- package/metadata/MetadataDiscovery.d.ts +1 -0
- package/metadata/MetadataDiscovery.js +15 -4
- package/metadata/MetadataStorage.d.ts +0 -4
- package/metadata/MetadataStorage.js +6 -10
- package/metadata/MetadataValidator.d.ts +0 -7
- package/metadata/MetadataValidator.js +0 -10
- package/package.json +2 -2
- package/platforms/Platform.d.ts +1 -1
- package/platforms/Platform.js +2 -2
- package/types/ArrayType.d.ts +1 -1
- package/types/ArrayType.js +1 -2
- package/typings.d.ts +1 -1
- package/typings.js +1 -1
- package/unit-of-work/UnitOfWork.js +1 -1
- package/utils/Configuration.js +1 -1
- package/utils/EntityComparator.js +2 -2
- package/utils/QueryHelper.js +5 -5
- package/utils/Utils.d.ts +2 -50
- package/utils/Utils.js +2 -140
- package/entity/ArrayCollection.d.ts +0 -118
- package/entity/ArrayCollection.js +0 -410
package/entity/Collection.js
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { inspect } from 'node:util';
|
|
2
2
|
import { Utils } from '../utils/Utils.js';
|
|
3
|
-
import { ValidationError } from '../errors.js';
|
|
3
|
+
import { MetadataError, ValidationError } from '../errors.js';
|
|
4
4
|
import { DataloaderType, ReferenceKind } from '../enums.js';
|
|
5
5
|
import { Reference } from './Reference.js';
|
|
6
|
-
import { helper } from './wrap.js';
|
|
6
|
+
import { helper, wrap } from './wrap.js';
|
|
7
7
|
import { QueryHelper } from '../utils/QueryHelper.js';
|
|
8
|
-
export class Collection
|
|
8
|
+
export class Collection {
|
|
9
|
+
owner;
|
|
10
|
+
items = new Set();
|
|
11
|
+
initialized = true;
|
|
12
|
+
dirty = false;
|
|
13
|
+
partial = false; // mark partially loaded collections, propagation is disabled for those
|
|
14
|
+
snapshot = []; // used to create a diff of the collection at commit time, undefined marks overridden values so we need to wipe when flushing
|
|
9
15
|
readonly;
|
|
16
|
+
_count;
|
|
17
|
+
_property;
|
|
10
18
|
_populated;
|
|
11
|
-
// this is for some reason needed for TS, otherwise it can fail with `Type instantiation is excessively deep and possibly infinite.`
|
|
12
|
-
_snapshot;
|
|
13
19
|
constructor(owner, items, initialized = true) {
|
|
14
|
-
|
|
20
|
+
this.owner = owner;
|
|
21
|
+
/* v8 ignore next 5 */
|
|
22
|
+
if (items) {
|
|
23
|
+
let i = 0;
|
|
24
|
+
this.items = new Set(items);
|
|
25
|
+
this.items.forEach(item => this[i++] = item);
|
|
26
|
+
}
|
|
15
27
|
this.initialized = !!items || initialized;
|
|
16
28
|
}
|
|
17
29
|
/**
|
|
@@ -54,7 +66,7 @@ export class Collection extends ArrayCollection {
|
|
|
54
66
|
*/
|
|
55
67
|
async loadItems(options) {
|
|
56
68
|
await this.load(options);
|
|
57
|
-
return
|
|
69
|
+
return this.getItems(false);
|
|
58
70
|
}
|
|
59
71
|
/**
|
|
60
72
|
* Gets the count of collection items from database instead of counting loaded items.
|
|
@@ -63,7 +75,7 @@ export class Collection extends ArrayCollection {
|
|
|
63
75
|
async loadCount(options = {}) {
|
|
64
76
|
options = typeof options === 'boolean' ? { refresh: options } : options;
|
|
65
77
|
const { refresh, where, ...countOptions } = options;
|
|
66
|
-
if (!refresh && !where &&
|
|
78
|
+
if (!refresh && !where && this._count != null) {
|
|
67
79
|
return this._count;
|
|
68
80
|
}
|
|
69
81
|
const em = this.getEntityManager();
|
|
@@ -105,24 +117,42 @@ export class Collection extends ArrayCollection {
|
|
|
105
117
|
if (check) {
|
|
106
118
|
this.checkInitialized();
|
|
107
119
|
}
|
|
108
|
-
return
|
|
120
|
+
return [...this.items];
|
|
109
121
|
}
|
|
110
122
|
toJSON() {
|
|
111
123
|
if (!this.isInitialized()) {
|
|
112
124
|
return [];
|
|
113
125
|
}
|
|
114
|
-
return
|
|
126
|
+
return this.toArray();
|
|
115
127
|
}
|
|
116
128
|
add(entity, ...entities) {
|
|
117
129
|
entities = Utils.asArray(entity).concat(entities);
|
|
118
130
|
const unwrapped = entities.map(i => Reference.unwrapReference(i));
|
|
119
|
-
|
|
120
|
-
const
|
|
131
|
+
this.validateModification(unwrapped);
|
|
132
|
+
const em = this.getEntityManager(entities, false);
|
|
133
|
+
let added = 0;
|
|
134
|
+
for (const item of entities) {
|
|
135
|
+
const entity = Reference.unwrapReference(item);
|
|
136
|
+
if (!this.contains(entity, false)) {
|
|
137
|
+
this.incrementCount(1);
|
|
138
|
+
this[this.items.size] = entity;
|
|
139
|
+
this.items.add(entity);
|
|
140
|
+
added++;
|
|
141
|
+
this.dirty = true;
|
|
142
|
+
this.propagate(entity, 'add');
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (this.property.kind === ReferenceKind.ONE_TO_MANY && em) {
|
|
146
|
+
em.persist(entities);
|
|
147
|
+
}
|
|
121
148
|
this.cancelOrphanRemoval(unwrapped);
|
|
122
149
|
return added;
|
|
123
150
|
}
|
|
124
151
|
/**
|
|
125
|
-
*
|
|
152
|
+
* Remove specified item(s) from the collection. Note that removing item from collection does not necessarily imply deleting the target entity,
|
|
153
|
+
* it means we are disconnecting the relation - removing items from collection, not removing entities from database - `Collection.remove()`
|
|
154
|
+
* is not the same as `em.remove()`. If we want to delete the entity by removing it from collection, we need to enable `orphanRemoval: true`,
|
|
155
|
+
* which tells the ORM we don't want orphaned entities to exist, so we know those should be removed.
|
|
126
156
|
*/
|
|
127
157
|
remove(entity, ...entities) {
|
|
128
158
|
if (entity instanceof Function) {
|
|
@@ -134,72 +164,50 @@ export class Collection extends ArrayCollection {
|
|
|
134
164
|
}
|
|
135
165
|
return removed;
|
|
136
166
|
}
|
|
167
|
+
this.checkInitialized();
|
|
137
168
|
entities = Utils.asArray(entity).concat(entities);
|
|
138
169
|
const unwrapped = entities.map(i => Reference.unwrapReference(i));
|
|
139
|
-
|
|
140
|
-
const em = this.getEntityManager(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
170
|
+
this.validateModification(unwrapped);
|
|
171
|
+
const em = this.getEntityManager(entities, false);
|
|
172
|
+
let removed = 0;
|
|
173
|
+
for (const item of entities) {
|
|
174
|
+
if (!item) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const entity = Reference.unwrapReference(item);
|
|
178
|
+
if (this.items.delete(entity)) {
|
|
179
|
+
this.incrementCount(-1);
|
|
180
|
+
delete this[this.items.size]; // remove last item
|
|
181
|
+
this.propagate(entity, 'remove');
|
|
182
|
+
removed++;
|
|
183
|
+
this.dirty = true;
|
|
184
|
+
}
|
|
185
|
+
if (this.property.orphanRemoval && em) {
|
|
186
|
+
em.getUnitOfWork().scheduleOrphanRemoval(entity);
|
|
144
187
|
}
|
|
145
188
|
}
|
|
189
|
+
if (this.property.kind === ReferenceKind.ONE_TO_MANY && !this.property.orphanRemoval && em) {
|
|
190
|
+
em.persist(entities);
|
|
191
|
+
}
|
|
192
|
+
if (removed > 0) {
|
|
193
|
+
Object.assign(this, [...this.items]); // reassign array access
|
|
194
|
+
}
|
|
146
195
|
return removed;
|
|
147
196
|
}
|
|
148
197
|
contains(item, check = true) {
|
|
149
198
|
if (check) {
|
|
150
199
|
this.checkInitialized();
|
|
151
200
|
}
|
|
152
|
-
|
|
201
|
+
const entity = Reference.unwrapReference(item);
|
|
202
|
+
return this.items.has(entity);
|
|
153
203
|
}
|
|
154
204
|
count() {
|
|
155
205
|
this.checkInitialized();
|
|
156
|
-
return
|
|
206
|
+
return this.items.size;
|
|
157
207
|
}
|
|
158
208
|
isEmpty() {
|
|
159
209
|
this.checkInitialized();
|
|
160
|
-
return
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* @inheritDoc
|
|
164
|
-
*/
|
|
165
|
-
slice(start, end) {
|
|
166
|
-
this.checkInitialized();
|
|
167
|
-
return super.slice(start, end);
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* @inheritDoc
|
|
171
|
-
*/
|
|
172
|
-
exists(cb) {
|
|
173
|
-
this.checkInitialized();
|
|
174
|
-
return super.exists(cb);
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* @inheritDoc
|
|
178
|
-
*/
|
|
179
|
-
find(cb) {
|
|
180
|
-
this.checkInitialized();
|
|
181
|
-
return super.find(cb);
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* @inheritDoc
|
|
185
|
-
*/
|
|
186
|
-
filter(cb) {
|
|
187
|
-
this.checkInitialized();
|
|
188
|
-
return super.filter(cb);
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* @inheritDoc
|
|
192
|
-
*/
|
|
193
|
-
map(mapper) {
|
|
194
|
-
this.checkInitialized();
|
|
195
|
-
return super.map(mapper);
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* @inheritDoc
|
|
199
|
-
*/
|
|
200
|
-
indexBy(key, valueKey) {
|
|
201
|
-
this.checkInitialized();
|
|
202
|
-
return super.indexBy(key, valueKey);
|
|
210
|
+
return this.count() === 0;
|
|
203
211
|
}
|
|
204
212
|
shouldPopulate(populated) {
|
|
205
213
|
if (!this.isInitialized(true)) {
|
|
@@ -267,10 +275,8 @@ export class Collection extends ArrayCollection {
|
|
|
267
275
|
getEntityManager(items = [], required = true) {
|
|
268
276
|
const wrapped = helper(this.owner);
|
|
269
277
|
let em = wrapped.__em;
|
|
270
|
-
// console.log('wat 1', em, this.owner);
|
|
271
278
|
if (!em) {
|
|
272
279
|
for (const i of items) {
|
|
273
|
-
// console.log('wat 2', i, i && helper(i).__em);
|
|
274
280
|
if (i && helper(i).__em) {
|
|
275
281
|
em = helper(i).__em;
|
|
276
282
|
break;
|
|
@@ -322,20 +328,6 @@ export class Collection extends ArrayCollection {
|
|
|
322
328
|
}
|
|
323
329
|
return cond;
|
|
324
330
|
}
|
|
325
|
-
modify(method, items) {
|
|
326
|
-
if (method === 'remove') {
|
|
327
|
-
this.checkInitialized();
|
|
328
|
-
}
|
|
329
|
-
this.validateModification(items);
|
|
330
|
-
const modified = super[method](items);
|
|
331
|
-
if (modified > 0) {
|
|
332
|
-
this.setDirty();
|
|
333
|
-
}
|
|
334
|
-
if (this.property.kind === ReferenceKind.ONE_TO_MANY && (method === 'add' || !this.property.orphanRemoval)) {
|
|
335
|
-
this.getEntityManager(items, false)?.persist(items);
|
|
336
|
-
}
|
|
337
|
-
return modified;
|
|
338
|
-
}
|
|
339
331
|
checkInitialized() {
|
|
340
332
|
if (!this.isInitialized()) {
|
|
341
333
|
throw new Error(`Collection<${this.property.type}> of entity ${this.owner.constructor.name}[${helper(this.owner).getSerializedPrimaryKey()}] not initialized`);
|
|
@@ -358,32 +350,362 @@ export class Collection extends ArrayCollection {
|
|
|
358
350
|
em.getUnitOfWork().cancelOrphanRemoval(item);
|
|
359
351
|
}
|
|
360
352
|
}
|
|
361
|
-
validateItemType(item) {
|
|
362
|
-
if (!Utils.isEntity(item)) {
|
|
363
|
-
throw ValidationError.notEntity(this.owner, this.property, item);
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
353
|
validateModification(items) {
|
|
367
354
|
if (this.readonly) {
|
|
368
355
|
throw ValidationError.cannotModifyReadonlyCollection(this.owner, this.property);
|
|
369
356
|
}
|
|
370
|
-
// currently we allow persisting to inverse sides only in SQL drivers
|
|
371
|
-
if (this.property.pivotTable || !this.property.mappedBy) {
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
357
|
const check = (item) => {
|
|
375
|
-
if (!item
|
|
358
|
+
if (!item) {
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
if (!Utils.isEntity(item)) {
|
|
362
|
+
throw ValidationError.notEntity(this.owner, this.property, item);
|
|
363
|
+
}
|
|
364
|
+
// currently we allow persisting to inverse sides only in SQL drivers
|
|
365
|
+
if (this.property.pivotTable || !this.property.mappedBy) {
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
if (helper(item).__initialized) {
|
|
376
369
|
return false;
|
|
377
370
|
}
|
|
378
371
|
return !item[this.property.mappedBy] && this.property.kind === ReferenceKind.MANY_TO_MANY;
|
|
379
372
|
};
|
|
380
373
|
// throw if we are modifying inverse side of M:N collection when owning side is initialized (would be ignored when persisting)
|
|
381
|
-
if (items.
|
|
374
|
+
if (items.some(item => check(item))) {
|
|
382
375
|
throw ValidationError.cannotModifyInverseCollection(this.owner, this.property);
|
|
383
376
|
}
|
|
384
377
|
}
|
|
378
|
+
toArray() {
|
|
379
|
+
if (this.items.size === 0) {
|
|
380
|
+
return [];
|
|
381
|
+
}
|
|
382
|
+
return this.map(item => wrap(item).toJSON());
|
|
383
|
+
}
|
|
384
|
+
getIdentifiers(field) {
|
|
385
|
+
const items = this.getItems();
|
|
386
|
+
const targetMeta = this.property.targetMeta;
|
|
387
|
+
if (items.length === 0) {
|
|
388
|
+
return [];
|
|
389
|
+
}
|
|
390
|
+
field ??= targetMeta.compositePK ? targetMeta.primaryKeys : (targetMeta.serializedPrimaryKey ?? targetMeta.primaryKeys[0]);
|
|
391
|
+
const cb = (i, f) => {
|
|
392
|
+
if (Utils.isEntity(i[f], true)) {
|
|
393
|
+
return wrap(i[f], true).getPrimaryKey();
|
|
394
|
+
}
|
|
395
|
+
return i[f];
|
|
396
|
+
};
|
|
397
|
+
return items.map(i => {
|
|
398
|
+
if (Array.isArray(field)) {
|
|
399
|
+
return field.map(f => cb(i, f));
|
|
400
|
+
}
|
|
401
|
+
return cb(i, field);
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* @internal
|
|
406
|
+
*/
|
|
407
|
+
addWithoutPropagation(entity) {
|
|
408
|
+
if (!this.contains(entity, false)) {
|
|
409
|
+
this.incrementCount(1);
|
|
410
|
+
this[this.items.size] = entity;
|
|
411
|
+
this.items.add(entity);
|
|
412
|
+
this.dirty = true;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
set(items) {
|
|
416
|
+
if (!this.initialized) {
|
|
417
|
+
this.initialized = true;
|
|
418
|
+
this.snapshot = undefined;
|
|
419
|
+
}
|
|
420
|
+
if (this.compare(Utils.asArray(items).map(item => Reference.unwrapReference(item)))) {
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
this.remove(this.items);
|
|
424
|
+
this.add(items);
|
|
425
|
+
}
|
|
426
|
+
compare(items) {
|
|
427
|
+
if (items.length !== this.items.size) {
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
let idx = 0;
|
|
431
|
+
for (const item of this.items) {
|
|
432
|
+
if (item !== items[idx++]) {
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return true;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* @internal
|
|
440
|
+
*/
|
|
441
|
+
hydrate(items, forcePropagate, partial) {
|
|
442
|
+
for (let i = 0; i < this.items.size; i++) {
|
|
443
|
+
delete this[i];
|
|
444
|
+
}
|
|
445
|
+
this.initialized = true;
|
|
446
|
+
this.partial = !!partial;
|
|
447
|
+
this.items.clear();
|
|
448
|
+
this._count = 0;
|
|
449
|
+
this.add(items);
|
|
450
|
+
this.takeSnapshot(forcePropagate);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Remove all items from the collection. Note that removing items from collection does not necessarily imply deleting the target entity,
|
|
454
|
+
* it means we are disconnecting the relation - removing items from collection, not removing entities from database - `Collection.remove()`
|
|
455
|
+
* is not the same as `em.remove()`. If we want to delete the entity by removing it from collection, we need to enable `orphanRemoval: true`,
|
|
456
|
+
* which tells the ORM we don't want orphaned entities to exist, so we know those should be removed.
|
|
457
|
+
*/
|
|
458
|
+
removeAll() {
|
|
459
|
+
if (!this.initialized) {
|
|
460
|
+
this.initialized = true;
|
|
461
|
+
this.snapshot = undefined;
|
|
462
|
+
}
|
|
463
|
+
this.remove(this.items);
|
|
464
|
+
this.dirty = true;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* @internal
|
|
468
|
+
*/
|
|
469
|
+
removeWithoutPropagation(entity) {
|
|
470
|
+
if (!this.items.delete(entity)) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
this.incrementCount(-1);
|
|
474
|
+
delete this[this.items.size];
|
|
475
|
+
Object.assign(this, [...this.items]);
|
|
476
|
+
this.dirty = true;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Extracts a slice of the collection items starting at position start to end (exclusive) of the collection.
|
|
480
|
+
* If end is null it returns all elements from start to the end of the collection.
|
|
481
|
+
*/
|
|
482
|
+
slice(start = 0, end) {
|
|
483
|
+
this.checkInitialized();
|
|
484
|
+
let index = 0;
|
|
485
|
+
end ??= this.items.size;
|
|
486
|
+
const items = [];
|
|
487
|
+
for (const item of this.items) {
|
|
488
|
+
if (index === end) {
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
if (index >= start && index < end) {
|
|
492
|
+
items.push(item);
|
|
493
|
+
}
|
|
494
|
+
index++;
|
|
495
|
+
}
|
|
496
|
+
return items;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* Tests for the existence of an element that satisfies the given predicate.
|
|
500
|
+
*/
|
|
501
|
+
exists(cb) {
|
|
502
|
+
this.checkInitialized();
|
|
503
|
+
for (const item of this.items) {
|
|
504
|
+
if (cb(item)) {
|
|
505
|
+
return true;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Returns the first element of this collection that satisfies the predicate.
|
|
512
|
+
*/
|
|
513
|
+
find(cb) {
|
|
514
|
+
this.checkInitialized();
|
|
515
|
+
let index = 0;
|
|
516
|
+
for (const item of this.items) {
|
|
517
|
+
if (cb(item, index++)) {
|
|
518
|
+
return item;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return undefined;
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Extracts a subset of the collection items.
|
|
525
|
+
*/
|
|
526
|
+
filter(cb) {
|
|
527
|
+
this.checkInitialized();
|
|
528
|
+
const items = [];
|
|
529
|
+
let index = 0;
|
|
530
|
+
for (const item of this.items) {
|
|
531
|
+
if (cb(item, index++)) {
|
|
532
|
+
items.push(item);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return items;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Maps the collection items based on your provided mapper function.
|
|
539
|
+
*/
|
|
540
|
+
map(mapper) {
|
|
541
|
+
this.checkInitialized();
|
|
542
|
+
const items = [];
|
|
543
|
+
let index = 0;
|
|
544
|
+
for (const item of this.items) {
|
|
545
|
+
items.push(mapper(item, index++));
|
|
546
|
+
}
|
|
547
|
+
return items;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Maps the collection items based on your provided mapper function to a single object.
|
|
551
|
+
*/
|
|
552
|
+
reduce(cb, initial = {}) {
|
|
553
|
+
this.checkInitialized();
|
|
554
|
+
let index = 0;
|
|
555
|
+
for (const item of this.items) {
|
|
556
|
+
initial = cb(initial, item, index++);
|
|
557
|
+
}
|
|
558
|
+
return initial;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Maps the collection items to a dictionary, indexed by the key you specify.
|
|
562
|
+
* If there are more items with the same key, only the first one will be present.
|
|
563
|
+
*/
|
|
564
|
+
indexBy(key, valueKey) {
|
|
565
|
+
return this.reduce((obj, item) => {
|
|
566
|
+
obj[item[key]] ??= valueKey ? item[valueKey] : item;
|
|
567
|
+
return obj;
|
|
568
|
+
}, {});
|
|
569
|
+
}
|
|
570
|
+
isInitialized(fully = false) {
|
|
571
|
+
if (!this.initialized || !fully) {
|
|
572
|
+
return this.initialized;
|
|
573
|
+
}
|
|
574
|
+
for (const item of this.items) {
|
|
575
|
+
if (!helper(item).__initialized) {
|
|
576
|
+
return false;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
isDirty() {
|
|
582
|
+
return this.dirty;
|
|
583
|
+
}
|
|
584
|
+
isPartial() {
|
|
585
|
+
return this.partial;
|
|
586
|
+
}
|
|
587
|
+
setDirty(dirty = true) {
|
|
588
|
+
this.dirty = dirty;
|
|
589
|
+
}
|
|
590
|
+
get length() {
|
|
591
|
+
return this.count();
|
|
592
|
+
}
|
|
593
|
+
*[Symbol.iterator]() {
|
|
594
|
+
for (const item of this.getItems()) {
|
|
595
|
+
yield item;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* @internal
|
|
600
|
+
*/
|
|
601
|
+
takeSnapshot(forcePropagate) {
|
|
602
|
+
this.snapshot = [...this.items];
|
|
603
|
+
this.dirty = false;
|
|
604
|
+
if (this.property.owner || forcePropagate) {
|
|
605
|
+
this.items.forEach(item => {
|
|
606
|
+
this.propagate(item, 'takeSnapshot');
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* @internal
|
|
612
|
+
*/
|
|
613
|
+
getSnapshot() {
|
|
614
|
+
return this.snapshot;
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* @internal
|
|
618
|
+
*/
|
|
619
|
+
get property() {
|
|
620
|
+
if (!this._property) {
|
|
621
|
+
const meta = wrap(this.owner, true).__meta;
|
|
622
|
+
/* v8 ignore next 3 */
|
|
623
|
+
if (!meta) {
|
|
624
|
+
throw MetadataError.fromUnknownEntity(this.owner.constructor.name, 'Collection.property getter, maybe you just forgot to initialize the ORM?');
|
|
625
|
+
}
|
|
626
|
+
this._property = meta.relations.find(prop => this.owner[prop.name] === this);
|
|
627
|
+
}
|
|
628
|
+
return this._property;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* @internal
|
|
632
|
+
*/
|
|
633
|
+
set property(prop) {
|
|
634
|
+
this._property = prop;
|
|
635
|
+
}
|
|
636
|
+
propagate(item, method) {
|
|
637
|
+
if (this.property.owner && this.property.inversedBy) {
|
|
638
|
+
this.propagateToInverseSide(item, method);
|
|
639
|
+
}
|
|
640
|
+
else if (!this.property.owner && this.property.mappedBy) {
|
|
641
|
+
this.propagateToOwningSide(item, method);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
propagateToInverseSide(item, method) {
|
|
645
|
+
const collection = item[this.property.inversedBy];
|
|
646
|
+
if (this.shouldPropagateToCollection(collection, method)) {
|
|
647
|
+
method = method === 'takeSnapshot' ? method : (method + 'WithoutPropagation');
|
|
648
|
+
collection[method](this.owner);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
propagateToOwningSide(item, method) {
|
|
652
|
+
const mappedBy = this.property.mappedBy;
|
|
653
|
+
const collection = item[mappedBy];
|
|
654
|
+
if (this.property.kind === ReferenceKind.MANY_TO_MANY) {
|
|
655
|
+
if (this.shouldPropagateToCollection(collection, method)) {
|
|
656
|
+
collection[method](this.owner);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
else if (this.property.kind === ReferenceKind.ONE_TO_MANY && method !== 'takeSnapshot') {
|
|
660
|
+
const prop2 = this.property.targetMeta.properties[mappedBy];
|
|
661
|
+
const owner = prop2.mapToPk ? helper(this.owner).getPrimaryKey() : this.owner;
|
|
662
|
+
const value = method === 'add' ? owner : null;
|
|
663
|
+
if (this.property.orphanRemoval && method === 'remove') {
|
|
664
|
+
// cache the PK before we propagate, as its value might be needed when flushing
|
|
665
|
+
helper(item).__pk = helper(item).getPrimaryKey();
|
|
666
|
+
}
|
|
667
|
+
if (!prop2.nullable && prop2.deleteRule !== 'cascade' && method === 'remove') {
|
|
668
|
+
if (!this.property.orphanRemoval) {
|
|
669
|
+
throw ValidationError.cannotRemoveFromCollectionWithoutOrphanRemoval(this.owner, this.property);
|
|
670
|
+
}
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
// skip if already propagated
|
|
674
|
+
if (Reference.unwrapReference(item[mappedBy]) !== value) {
|
|
675
|
+
item[mappedBy] = value;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
shouldPropagateToCollection(collection, method) {
|
|
680
|
+
if (!collection) {
|
|
681
|
+
return false;
|
|
682
|
+
}
|
|
683
|
+
switch (method) {
|
|
684
|
+
case 'add':
|
|
685
|
+
return !collection.contains(this.owner, false);
|
|
686
|
+
case 'remove':
|
|
687
|
+
return collection.isInitialized() && collection.contains(this.owner, false);
|
|
688
|
+
case 'takeSnapshot':
|
|
689
|
+
return collection.isDirty();
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
incrementCount(value) {
|
|
693
|
+
if (typeof this._count === 'number' && this.initialized) {
|
|
694
|
+
this._count += value;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
/** @ignore */
|
|
698
|
+
[inspect.custom](depth = 2) {
|
|
699
|
+
const object = { ...this };
|
|
700
|
+
const hidden = ['items', 'owner', '_property', '_count', 'snapshot', '_populated', '_lazyInitialized', '_em', 'readonly', 'partial'];
|
|
701
|
+
hidden.forEach(k => delete object[k]);
|
|
702
|
+
const ret = inspect(object, { depth });
|
|
703
|
+
const name = `${this.constructor.name}<${this.property?.type ?? 'unknown'}>`;
|
|
704
|
+
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
705
|
+
}
|
|
385
706
|
}
|
|
386
707
|
Object.defineProperties(Collection.prototype, {
|
|
387
708
|
$: { get() { return this; } },
|
|
388
709
|
get: { get() { return () => this; } },
|
|
710
|
+
__collection: { value: true, enumerable: false, writable: false },
|
|
389
711
|
});
|
package/entity/EntityFactory.js
CHANGED
|
@@ -290,7 +290,7 @@ export class EntityFactory {
|
|
|
290
290
|
if (!spk?.serializedPrimaryKey) {
|
|
291
291
|
return;
|
|
292
292
|
}
|
|
293
|
-
if (pk.type
|
|
293
|
+
if (pk.type === 'ObjectId' && (data[pk.name] != null || data[spk.name] != null)) {
|
|
294
294
|
data[pk.name] = this.platform.denormalizePrimaryKey((data[spk.name] || data[pk.name]));
|
|
295
295
|
delete data[spk.name];
|
|
296
296
|
}
|
|
@@ -43,7 +43,7 @@ export class EntityValidator {
|
|
|
43
43
|
!prop.embedded &&
|
|
44
44
|
![ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind) &&
|
|
45
45
|
prop.name !== wrapped.__meta.root.discriminatorColumn &&
|
|
46
|
-
prop.type
|
|
46
|
+
prop.type !== 'ObjectId' &&
|
|
47
47
|
prop.persist !== false &&
|
|
48
48
|
entity[prop.name] == null) {
|
|
49
49
|
throw ValidationError.propertyRequired(entity, prop);
|
|
@@ -131,7 +131,7 @@ export class EntityValidator {
|
|
|
131
131
|
}
|
|
132
132
|
fixDateType(givenValue) {
|
|
133
133
|
let date;
|
|
134
|
-
if (
|
|
134
|
+
if (typeof givenValue === 'string' && givenValue.match(/^-?\d+(\.\d+)?$/)) {
|
|
135
135
|
date = new Date(+givenValue);
|
|
136
136
|
}
|
|
137
137
|
else {
|
package/entity/index.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ export * from './EntityValidator.js';
|
|
|
4
4
|
export * from './EntityAssigner.js';
|
|
5
5
|
export * from './EntityHelper.js';
|
|
6
6
|
export * from './EntityFactory.js';
|
|
7
|
-
export * from './ArrayCollection.js';
|
|
8
7
|
export * from './Collection.js';
|
|
9
8
|
export * from './EntityLoader.js';
|
|
10
9
|
export * from './Reference.js';
|
package/entity/index.js
CHANGED
|
@@ -4,7 +4,6 @@ export * from './EntityValidator.js';
|
|
|
4
4
|
export * from './EntityAssigner.js';
|
|
5
5
|
export * from './EntityHelper.js';
|
|
6
6
|
export * from './EntityFactory.js';
|
|
7
|
-
export * from './ArrayCollection.js';
|
|
8
7
|
export * from './Collection.js';
|
|
9
8
|
export * from './EntityLoader.js';
|
|
10
9
|
export * from './Reference.js';
|
package/metadata/EntitySchema.js
CHANGED
|
@@ -39,8 +39,8 @@ export class EntitySchema {
|
|
|
39
39
|
if (type && Type.isMappedType(type.prototype)) {
|
|
40
40
|
prop.type = type;
|
|
41
41
|
}
|
|
42
|
-
if (
|
|
43
|
-
const formula = prop.formula;
|
|
42
|
+
if (typeof prop.formula === 'string') {
|
|
43
|
+
const formula = prop.formula;
|
|
44
44
|
prop.formula = () => formula;
|
|
45
45
|
}
|
|
46
46
|
if (prop.formula) {
|
|
@@ -263,7 +263,7 @@ export class EntitySchema {
|
|
|
263
263
|
}
|
|
264
264
|
normalizeType(options, type) {
|
|
265
265
|
if ('entity' in options) {
|
|
266
|
-
if (
|
|
266
|
+
if (typeof options.entity === 'string') {
|
|
267
267
|
type = options.type = options.entity;
|
|
268
268
|
}
|
|
269
269
|
else if (options.entity) {
|
|
@@ -27,6 +27,7 @@ export declare class MetadataDiscovery {
|
|
|
27
27
|
discoverReferences<T>(refs: Iterable<EntityClass<T> | EntitySchema<T>>, validate?: boolean): EntityMetadata<T>[];
|
|
28
28
|
reset(className: string): void;
|
|
29
29
|
private getSchema;
|
|
30
|
+
private getRootEntity;
|
|
30
31
|
private discoverEntity;
|
|
31
32
|
private saveToCache;
|
|
32
33
|
private initNullability;
|