@mikro-orm/core 7.0.0-dev.23 → 7.0.0-dev.231
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.d.ts +91 -59
- package/EntityManager.js +303 -251
- package/MikroORM.d.ts +44 -35
- package/MikroORM.js +109 -143
- package/README.md +2 -0
- package/cache/FileCacheAdapter.d.ts +1 -1
- package/cache/FileCacheAdapter.js +17 -8
- package/cache/GeneratedCacheAdapter.d.ts +0 -1
- package/cache/GeneratedCacheAdapter.js +0 -2
- package/cache/index.d.ts +0 -1
- package/cache/index.js +0 -1
- package/connections/Connection.d.ts +12 -5
- package/connections/Connection.js +21 -12
- package/drivers/DatabaseDriver.d.ts +25 -16
- package/drivers/DatabaseDriver.js +118 -35
- package/drivers/IDatabaseDriver.d.ts +42 -19
- package/entity/BaseEntity.d.ts +61 -2
- package/entity/BaseEntity.js +0 -3
- package/entity/Collection.d.ts +101 -29
- package/entity/Collection.js +436 -104
- package/entity/EntityAssigner.d.ts +1 -1
- package/entity/EntityAssigner.js +26 -18
- package/entity/EntityFactory.d.ts +7 -1
- package/entity/EntityFactory.js +83 -54
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +48 -15
- package/entity/EntityLoader.d.ts +7 -6
- package/entity/EntityLoader.js +221 -93
- package/entity/EntityRepository.d.ts +27 -7
- package/entity/EntityRepository.js +8 -2
- package/entity/PolymorphicRef.d.ts +12 -0
- package/entity/PolymorphicRef.js +18 -0
- package/entity/Reference.d.ts +1 -5
- package/entity/Reference.js +21 -12
- package/entity/WrappedEntity.d.ts +0 -5
- package/entity/WrappedEntity.js +2 -7
- package/entity/defineEntity.d.ts +380 -310
- package/entity/defineEntity.js +124 -273
- package/entity/index.d.ts +2 -2
- package/entity/index.js +2 -2
- package/entity/utils.js +1 -1
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +65 -0
- package/enums.d.ts +8 -6
- package/enums.js +2 -1
- package/errors.d.ts +20 -10
- package/errors.js +55 -23
- package/events/EventManager.d.ts +2 -1
- package/events/EventManager.js +19 -11
- package/hydration/Hydrator.js +1 -2
- package/hydration/ObjectHydrator.d.ts +4 -4
- package/hydration/ObjectHydrator.js +87 -35
- package/index.d.ts +2 -2
- package/index.js +1 -2
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/DefaultLogger.js +1 -0
- package/logging/SimpleLogger.d.ts +1 -1
- package/logging/colors.d.ts +1 -1
- package/logging/colors.js +7 -6
- package/logging/index.d.ts +1 -0
- package/logging/index.js +1 -0
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +11 -0
- package/metadata/EntitySchema.d.ts +47 -23
- package/metadata/EntitySchema.js +92 -33
- package/metadata/MetadataDiscovery.d.ts +64 -9
- package/metadata/MetadataDiscovery.js +778 -325
- package/metadata/MetadataProvider.d.ts +11 -2
- package/metadata/MetadataProvider.js +46 -2
- package/metadata/MetadataStorage.d.ts +13 -11
- package/metadata/MetadataStorage.js +70 -37
- package/metadata/MetadataValidator.d.ts +32 -9
- package/metadata/MetadataValidator.js +196 -41
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +40 -0
- package/metadata/index.d.ts +1 -1
- package/metadata/index.js +1 -1
- package/metadata/types.d.ts +526 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +16 -4
- package/naming-strategy/AbstractNamingStrategy.js +20 -2
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
- package/naming-strategy/EntityCaseNamingStrategy.js +6 -5
- package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
- package/naming-strategy/MongoNamingStrategy.js +6 -6
- package/naming-strategy/NamingStrategy.d.ts +28 -4
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
- package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
- package/not-supported.d.ts +2 -0
- package/not-supported.js +4 -0
- package/package.json +19 -11
- package/platforms/ExceptionConverter.js +1 -1
- package/platforms/Platform.d.ts +7 -14
- package/platforms/Platform.js +20 -43
- package/serialization/EntitySerializer.d.ts +5 -0
- package/serialization/EntitySerializer.js +47 -27
- package/serialization/EntityTransformer.js +28 -18
- package/serialization/SerializationContext.d.ts +6 -6
- package/serialization/SerializationContext.js +3 -3
- package/types/ArrayType.d.ts +1 -1
- package/types/ArrayType.js +2 -3
- package/types/BigIntType.d.ts +8 -6
- package/types/BigIntType.js +1 -1
- package/types/BlobType.d.ts +0 -1
- package/types/BlobType.js +0 -3
- package/types/BooleanType.d.ts +1 -0
- package/types/BooleanType.js +3 -0
- package/types/DecimalType.d.ts +6 -4
- package/types/DecimalType.js +2 -2
- package/types/DoubleType.js +1 -1
- package/types/EnumArrayType.js +1 -2
- package/types/JsonType.d.ts +1 -1
- package/types/JsonType.js +7 -2
- package/types/TinyIntType.js +1 -1
- package/types/Type.d.ts +2 -4
- package/types/Type.js +3 -3
- package/types/Uint8ArrayType.d.ts +0 -1
- package/types/Uint8ArrayType.js +1 -4
- package/types/index.d.ts +1 -1
- package/typings.d.ts +381 -171
- package/typings.js +97 -44
- package/unit-of-work/ChangeSet.d.ts +4 -6
- package/unit-of-work/ChangeSet.js +4 -5
- package/unit-of-work/ChangeSetComputer.d.ts +1 -3
- package/unit-of-work/ChangeSetComputer.js +35 -14
- package/unit-of-work/ChangeSetPersister.d.ts +7 -3
- package/unit-of-work/ChangeSetPersister.js +83 -25
- package/unit-of-work/CommitOrderCalculator.d.ts +12 -10
- package/unit-of-work/CommitOrderCalculator.js +13 -13
- package/unit-of-work/IdentityMap.d.ts +12 -0
- package/unit-of-work/IdentityMap.js +39 -1
- package/unit-of-work/UnitOfWork.d.ts +27 -3
- package/unit-of-work/UnitOfWork.js +258 -92
- package/utils/AbstractSchemaGenerator.d.ts +5 -5
- package/utils/AbstractSchemaGenerator.js +28 -17
- package/utils/AsyncContext.d.ts +6 -0
- package/utils/AsyncContext.js +42 -0
- package/utils/Configuration.d.ts +795 -209
- package/utils/Configuration.js +150 -192
- package/utils/ConfigurationLoader.d.ts +1 -54
- package/utils/ConfigurationLoader.js +1 -352
- package/utils/Cursor.d.ts +0 -3
- package/utils/Cursor.js +24 -11
- package/utils/DataloaderUtils.d.ts +10 -5
- package/utils/DataloaderUtils.js +29 -12
- package/utils/EntityComparator.d.ts +16 -9
- package/utils/EntityComparator.js +158 -58
- package/utils/QueryHelper.d.ts +18 -6
- package/utils/QueryHelper.js +76 -23
- package/utils/RawQueryFragment.d.ts +28 -34
- package/utils/RawQueryFragment.js +35 -71
- package/utils/RequestContext.js +2 -2
- package/utils/TransactionContext.js +2 -2
- package/utils/TransactionManager.js +28 -4
- package/utils/Utils.d.ts +14 -127
- package/utils/Utils.js +85 -397
- package/utils/clone.js +8 -23
- package/utils/env-vars.d.ts +7 -0
- package/utils/env-vars.js +97 -0
- package/utils/fs-utils.d.ts +33 -0
- package/utils/fs-utils.js +192 -0
- package/utils/index.d.ts +1 -1
- package/utils/index.js +1 -1
- package/utils/upsert-utils.d.ts +9 -4
- package/utils/upsert-utils.js +46 -3
- package/decorators/Check.d.ts +0 -3
- package/decorators/Check.js +0 -13
- package/decorators/CreateRequestContext.d.ts +0 -3
- package/decorators/CreateRequestContext.js +0 -32
- package/decorators/Embeddable.d.ts +0 -8
- package/decorators/Embeddable.js +0 -11
- package/decorators/Embedded.d.ts +0 -12
- package/decorators/Embedded.js +0 -18
- package/decorators/Entity.d.ts +0 -33
- package/decorators/Entity.js +0 -12
- package/decorators/Enum.d.ts +0 -9
- package/decorators/Enum.js +0 -16
- package/decorators/Filter.d.ts +0 -2
- package/decorators/Filter.js +0 -8
- package/decorators/Formula.d.ts +0 -4
- package/decorators/Formula.js +0 -15
- package/decorators/Indexed.d.ts +0 -19
- package/decorators/Indexed.js +0 -20
- package/decorators/ManyToMany.d.ts +0 -42
- package/decorators/ManyToMany.js +0 -14
- package/decorators/ManyToOne.d.ts +0 -34
- package/decorators/ManyToOne.js +0 -14
- package/decorators/OneToMany.d.ts +0 -28
- package/decorators/OneToMany.js +0 -17
- package/decorators/OneToOne.d.ts +0 -28
- package/decorators/OneToOne.js +0 -7
- package/decorators/PrimaryKey.d.ts +0 -8
- package/decorators/PrimaryKey.js +0 -20
- package/decorators/Property.d.ts +0 -250
- package/decorators/Property.js +0 -32
- package/decorators/Transactional.d.ts +0 -14
- package/decorators/Transactional.js +0 -28
- package/decorators/hooks.d.ts +0 -16
- package/decorators/hooks.js +0 -47
- package/decorators/index.d.ts +0 -17
- package/decorators/index.js +0 -17
- package/entity/ArrayCollection.d.ts +0 -118
- package/entity/ArrayCollection.js +0 -407
- package/entity/EntityValidator.d.ts +0 -19
- package/entity/EntityValidator.js +0 -150
- package/metadata/ReflectMetadataProvider.d.ts +0 -8
- package/metadata/ReflectMetadataProvider.js +0 -44
- package/utils/resolveContextProvider.d.ts +0 -10
- package/utils/resolveContextProvider.js +0 -28
package/entity/Collection.js
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
|
-
import { ArrayCollection } from './ArrayCollection.js';
|
|
2
1
|
import { Utils } from '../utils/Utils.js';
|
|
3
|
-
import { ValidationError } from '../errors.js';
|
|
4
|
-
import {
|
|
2
|
+
import { MetadataError, ValidationError } from '../errors.js';
|
|
3
|
+
import { DataloaderType, ReferenceKind } from '../enums.js';
|
|
5
4
|
import { Reference } from './Reference.js';
|
|
6
|
-
import { helper } from './wrap.js';
|
|
7
|
-
|
|
5
|
+
import { helper, wrap } from './wrap.js';
|
|
6
|
+
import { QueryHelper } from '../utils/QueryHelper.js';
|
|
7
|
+
import { inspect } from '../logging/inspect.js';
|
|
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
|
|
8
15
|
readonly;
|
|
16
|
+
_count;
|
|
17
|
+
_property;
|
|
9
18
|
_populated;
|
|
10
|
-
// this is for some reason needed for TS, otherwise it can fail with `Type instantiation is excessively deep and possibly infinite.`
|
|
11
|
-
_snapshot;
|
|
12
19
|
constructor(owner, items, initialized = true) {
|
|
13
|
-
|
|
20
|
+
this.owner = owner;
|
|
21
|
+
/* v8 ignore next */
|
|
22
|
+
if (items) {
|
|
23
|
+
let i = 0;
|
|
24
|
+
this.items = new Set(items);
|
|
25
|
+
this.items.forEach(item => this[i++] = item);
|
|
26
|
+
}
|
|
14
27
|
this.initialized = !!items || initialized;
|
|
15
28
|
}
|
|
16
29
|
/**
|
|
@@ -32,6 +45,7 @@ export class Collection extends ArrayCollection {
|
|
|
32
45
|
async load(options = {}) {
|
|
33
46
|
if (this.isInitialized(true) && !options.refresh) {
|
|
34
47
|
const em = this.getEntityManager(this.items, false);
|
|
48
|
+
options = { ...options, filters: QueryHelper.mergePropertyFilters(this.property.filters, options.filters) };
|
|
35
49
|
await em?.populate(this.items, options.populate, options);
|
|
36
50
|
this.setSerializationContext(options);
|
|
37
51
|
}
|
|
@@ -52,7 +66,7 @@ export class Collection extends ArrayCollection {
|
|
|
52
66
|
*/
|
|
53
67
|
async loadItems(options) {
|
|
54
68
|
await this.load(options);
|
|
55
|
-
return
|
|
69
|
+
return this.getItems(false);
|
|
56
70
|
}
|
|
57
71
|
/**
|
|
58
72
|
* Gets the count of collection items from database instead of counting loaded items.
|
|
@@ -61,7 +75,7 @@ export class Collection extends ArrayCollection {
|
|
|
61
75
|
async loadCount(options = {}) {
|
|
62
76
|
options = typeof options === 'boolean' ? { refresh: options } : options;
|
|
63
77
|
const { refresh, where, ...countOptions } = options;
|
|
64
|
-
if (!refresh && !where &&
|
|
78
|
+
if (!refresh && !where && this._count != null) {
|
|
65
79
|
return this._count;
|
|
66
80
|
}
|
|
67
81
|
const em = this.getEntityManager();
|
|
@@ -69,7 +83,7 @@ export class Collection extends ArrayCollection {
|
|
|
69
83
|
return this._count = this.length;
|
|
70
84
|
}
|
|
71
85
|
const cond = this.createLoadCountCondition(where ?? {});
|
|
72
|
-
const count = await em.count(this.property.
|
|
86
|
+
const count = await em.count(this.property.targetMeta.class, cond, countOptions);
|
|
73
87
|
if (!where) {
|
|
74
88
|
this._count = count;
|
|
75
89
|
}
|
|
@@ -78,15 +92,20 @@ export class Collection extends ArrayCollection {
|
|
|
78
92
|
async matching(options) {
|
|
79
93
|
const em = this.getEntityManager();
|
|
80
94
|
const { where, ctx, ...opts } = options;
|
|
81
|
-
opts.orderBy = this.createOrderBy(opts.orderBy);
|
|
82
95
|
let items;
|
|
83
96
|
if (this.property.kind === ReferenceKind.MANY_TO_MANY && em.getPlatform().usesPivotTable()) {
|
|
84
|
-
|
|
97
|
+
// M:N via pivot table bypasses em.find(), so merge all 3 levels here
|
|
98
|
+
opts.orderBy = QueryHelper.mergeOrderBy(opts.orderBy, this.property.orderBy, this.property.targetMeta?.orderBy);
|
|
99
|
+
options.populate = await em.preparePopulate(this.property.targetMeta.class, options);
|
|
100
|
+
const cond = await em.applyFilters(this.property.targetMeta.class, where, options.filters ?? {}, 'read');
|
|
85
101
|
const map = await em.getDriver().loadFromPivotTable(this.property, [helper(this.owner).__primaryKeys], cond, opts.orderBy, ctx, options);
|
|
86
|
-
items = map[helper(this.owner).getSerializedPrimaryKey()].map((item) => em.merge(this.property.
|
|
102
|
+
items = map[helper(this.owner).getSerializedPrimaryKey()].map((item) => em.merge(this.property.targetMeta.class, item, { convertCustomTypes: true }));
|
|
103
|
+
await em.populate(items, options.populate, options);
|
|
87
104
|
}
|
|
88
105
|
else {
|
|
89
|
-
|
|
106
|
+
// em.find() merges entity-level orderBy, so only merge runtime + relation here
|
|
107
|
+
opts.orderBy = QueryHelper.mergeOrderBy(opts.orderBy, this.property.orderBy);
|
|
108
|
+
items = await em.find(this.property.targetMeta.class, this.createCondition(where), opts);
|
|
90
109
|
}
|
|
91
110
|
if (options.store) {
|
|
92
111
|
this.hydrate(items, true);
|
|
@@ -103,98 +122,97 @@ export class Collection extends ArrayCollection {
|
|
|
103
122
|
if (check) {
|
|
104
123
|
this.checkInitialized();
|
|
105
124
|
}
|
|
106
|
-
return
|
|
125
|
+
return [...this.items];
|
|
107
126
|
}
|
|
108
127
|
toJSON() {
|
|
109
128
|
if (!this.isInitialized()) {
|
|
110
129
|
return [];
|
|
111
130
|
}
|
|
112
|
-
return
|
|
131
|
+
return this.toArray();
|
|
113
132
|
}
|
|
114
133
|
add(entity, ...entities) {
|
|
115
134
|
entities = Utils.asArray(entity).concat(entities);
|
|
116
135
|
const unwrapped = entities.map(i => Reference.unwrapReference(i));
|
|
117
|
-
|
|
118
|
-
this.
|
|
136
|
+
this.validateModification(unwrapped);
|
|
137
|
+
const em = this.getEntityManager(entities, false);
|
|
138
|
+
let added = 0;
|
|
139
|
+
for (const item of entities) {
|
|
140
|
+
const entity = Reference.unwrapReference(item);
|
|
141
|
+
if (!this.contains(entity, false)) {
|
|
142
|
+
this.incrementCount(1);
|
|
143
|
+
this[this.items.size] = entity;
|
|
144
|
+
this.items.add(entity);
|
|
145
|
+
added++;
|
|
146
|
+
this.dirty = true;
|
|
147
|
+
this.propagate(entity, 'add');
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (this.property.kind === ReferenceKind.ONE_TO_MANY && em) {
|
|
151
|
+
em.persist(entities);
|
|
152
|
+
}
|
|
119
153
|
this.cancelOrphanRemoval(unwrapped);
|
|
154
|
+
return added;
|
|
120
155
|
}
|
|
121
156
|
/**
|
|
122
|
-
*
|
|
157
|
+
* Remove specified item(s) from the collection. Note that removing item from collection does not necessarily imply deleting the target entity,
|
|
158
|
+
* it means we are disconnecting the relation - removing items from collection, not removing entities from database - `Collection.remove()`
|
|
159
|
+
* 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`,
|
|
160
|
+
* which tells the ORM we don't want orphaned entities to exist, so we know those should be removed.
|
|
123
161
|
*/
|
|
124
162
|
remove(entity, ...entities) {
|
|
125
163
|
if (entity instanceof Function) {
|
|
164
|
+
let removed = 0;
|
|
126
165
|
for (const item of this.items) {
|
|
127
166
|
if (entity(item)) {
|
|
128
|
-
this.remove(item);
|
|
167
|
+
removed += this.remove(item);
|
|
129
168
|
}
|
|
130
169
|
}
|
|
131
|
-
return;
|
|
170
|
+
return removed;
|
|
132
171
|
}
|
|
172
|
+
this.checkInitialized();
|
|
133
173
|
entities = Utils.asArray(entity).concat(entities);
|
|
134
174
|
const unwrapped = entities.map(i => Reference.unwrapReference(i));
|
|
135
|
-
this.
|
|
136
|
-
const em = this.getEntityManager(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
175
|
+
this.validateModification(unwrapped);
|
|
176
|
+
const em = this.getEntityManager(entities, false);
|
|
177
|
+
let removed = 0;
|
|
178
|
+
for (const item of entities) {
|
|
179
|
+
if (!item) {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const entity = Reference.unwrapReference(item);
|
|
183
|
+
if (this.items.delete(entity)) {
|
|
184
|
+
this.incrementCount(-1);
|
|
185
|
+
delete this[this.items.size]; // remove last item
|
|
186
|
+
this.propagate(entity, 'remove');
|
|
187
|
+
removed++;
|
|
188
|
+
this.dirty = true;
|
|
140
189
|
}
|
|
190
|
+
if (this.property.orphanRemoval && em) {
|
|
191
|
+
em.getUnitOfWork().scheduleOrphanRemoval(entity);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (this.property.kind === ReferenceKind.ONE_TO_MANY && !this.property.orphanRemoval && em) {
|
|
195
|
+
em.persist(entities);
|
|
196
|
+
}
|
|
197
|
+
if (removed > 0) {
|
|
198
|
+
Object.assign(this, [...this.items]); // reassign array access
|
|
141
199
|
}
|
|
200
|
+
return removed;
|
|
142
201
|
}
|
|
143
202
|
contains(item, check = true) {
|
|
144
203
|
if (check) {
|
|
145
204
|
this.checkInitialized();
|
|
146
205
|
}
|
|
147
|
-
|
|
206
|
+
const entity = Reference.unwrapReference(item);
|
|
207
|
+
return this.items.has(entity);
|
|
148
208
|
}
|
|
149
209
|
count() {
|
|
150
210
|
this.checkInitialized();
|
|
151
|
-
return
|
|
211
|
+
return this.items.size;
|
|
152
212
|
}
|
|
153
213
|
isEmpty() {
|
|
154
214
|
this.checkInitialized();
|
|
155
|
-
return
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* @inheritDoc
|
|
159
|
-
*/
|
|
160
|
-
slice(start, end) {
|
|
161
|
-
this.checkInitialized();
|
|
162
|
-
return super.slice(start, end);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* @inheritDoc
|
|
166
|
-
*/
|
|
167
|
-
exists(cb) {
|
|
168
|
-
this.checkInitialized();
|
|
169
|
-
return super.exists(cb);
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* @inheritDoc
|
|
173
|
-
*/
|
|
174
|
-
find(cb) {
|
|
175
|
-
this.checkInitialized();
|
|
176
|
-
return super.find(cb);
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* @inheritDoc
|
|
180
|
-
*/
|
|
181
|
-
filter(cb) {
|
|
182
|
-
this.checkInitialized();
|
|
183
|
-
return super.filter(cb);
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* @inheritDoc
|
|
187
|
-
*/
|
|
188
|
-
map(mapper) {
|
|
189
|
-
this.checkInitialized();
|
|
190
|
-
return super.map(mapper);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* @inheritDoc
|
|
194
|
-
*/
|
|
195
|
-
indexBy(key, valueKey) {
|
|
196
|
-
this.checkInitialized();
|
|
197
|
-
return super.indexBy(key, valueKey);
|
|
215
|
+
return this.count() === 0;
|
|
198
216
|
}
|
|
199
217
|
shouldPopulate(populated) {
|
|
200
218
|
if (!this.isInitialized(true)) {
|
|
@@ -217,16 +235,14 @@ export class Collection extends ArrayCollection {
|
|
|
217
235
|
return this;
|
|
218
236
|
}
|
|
219
237
|
const em = this.getEntityManager();
|
|
238
|
+
options = { ...options, filters: QueryHelper.mergePropertyFilters(this.property.filters, options.filters) };
|
|
220
239
|
if (options.dataloader ?? [DataloaderType.ALL, DataloaderType.COLLECTION].includes(em.config.getDataloaderType())) {
|
|
221
240
|
const order = [...this.items]; // copy order of references
|
|
222
|
-
const orderBy =
|
|
241
|
+
const orderBy = QueryHelper.mergeOrderBy(options.orderBy, this.property.orderBy, this.property.targetMeta?.orderBy);
|
|
223
242
|
const customOrder = orderBy.length > 0;
|
|
224
243
|
const pivotTable = this.property.kind === ReferenceKind.MANY_TO_MANY && em.getPlatform().usesPivotTable();
|
|
225
|
-
const loader = pivotTable ? '
|
|
226
|
-
const items = await
|
|
227
|
-
this,
|
|
228
|
-
{ ...options, orderBy },
|
|
229
|
-
]);
|
|
244
|
+
const loader = await em.getDataLoader(pivotTable ? 'm:n' : '1:m');
|
|
245
|
+
const items = await loader.load([this, { ...options, orderBy }]);
|
|
230
246
|
if (this.property.kind === ReferenceKind.MANY_TO_MANY) {
|
|
231
247
|
this.initialized = true;
|
|
232
248
|
this.dirty = false;
|
|
@@ -286,12 +302,6 @@ export class Collection extends ArrayCollection {
|
|
|
286
302
|
}
|
|
287
303
|
return cond;
|
|
288
304
|
}
|
|
289
|
-
createOrderBy(orderBy = []) {
|
|
290
|
-
if (Utils.isEmpty(orderBy) && this.property.orderBy) {
|
|
291
|
-
orderBy = this.property.orderBy;
|
|
292
|
-
}
|
|
293
|
-
return Utils.asArray(orderBy);
|
|
294
|
-
}
|
|
295
305
|
createManyToManyCondition(cond) {
|
|
296
306
|
const dict = cond;
|
|
297
307
|
if (this.property.owner || this.property.pivotTable) {
|
|
@@ -317,17 +327,9 @@ export class Collection extends ArrayCollection {
|
|
|
317
327
|
}
|
|
318
328
|
return cond;
|
|
319
329
|
}
|
|
320
|
-
modify(method, items) {
|
|
321
|
-
if (method === 'remove') {
|
|
322
|
-
this.checkInitialized();
|
|
323
|
-
}
|
|
324
|
-
this.validateModification(items);
|
|
325
|
-
super[method](items);
|
|
326
|
-
this.setDirty();
|
|
327
|
-
}
|
|
328
330
|
checkInitialized() {
|
|
329
331
|
if (!this.isInitialized()) {
|
|
330
|
-
throw new Error(`Collection<${this.property.type}> of entity ${this.owner.
|
|
332
|
+
throw new Error(`Collection<${this.property.type}> of entity ${helper(this.owner).__meta.name}[${helper(this.owner).getSerializedPrimaryKey()}] not initialized`);
|
|
331
333
|
}
|
|
332
334
|
}
|
|
333
335
|
/**
|
|
@@ -347,32 +349,362 @@ export class Collection extends ArrayCollection {
|
|
|
347
349
|
em.getUnitOfWork().cancelOrphanRemoval(item);
|
|
348
350
|
}
|
|
349
351
|
}
|
|
350
|
-
validateItemType(item) {
|
|
351
|
-
if (!Utils.isEntity(item)) {
|
|
352
|
-
throw ValidationError.notEntity(this.owner, this.property, item);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
352
|
validateModification(items) {
|
|
356
353
|
if (this.readonly) {
|
|
357
354
|
throw ValidationError.cannotModifyReadonlyCollection(this.owner, this.property);
|
|
358
355
|
}
|
|
359
|
-
// currently we allow persisting to inverse sides only in SQL drivers
|
|
360
|
-
if (this.property.pivotTable || !this.property.mappedBy) {
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
356
|
const check = (item) => {
|
|
364
|
-
if (!item
|
|
357
|
+
if (!item) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
if (!Utils.isEntity(item)) {
|
|
361
|
+
throw ValidationError.notEntity(this.owner, this.property, item);
|
|
362
|
+
}
|
|
363
|
+
// currently we allow persisting to inverse sides only in SQL drivers
|
|
364
|
+
if (this.property.pivotTable || !this.property.mappedBy) {
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
if (helper(item).__initialized) {
|
|
365
368
|
return false;
|
|
366
369
|
}
|
|
367
370
|
return !item[this.property.mappedBy] && this.property.kind === ReferenceKind.MANY_TO_MANY;
|
|
368
371
|
};
|
|
369
372
|
// throw if we are modifying inverse side of M:N collection when owning side is initialized (would be ignored when persisting)
|
|
370
|
-
if (items.
|
|
373
|
+
if (items.some(item => check(item))) {
|
|
371
374
|
throw ValidationError.cannotModifyInverseCollection(this.owner, this.property);
|
|
372
375
|
}
|
|
373
376
|
}
|
|
377
|
+
toArray() {
|
|
378
|
+
if (this.items.size === 0) {
|
|
379
|
+
return [];
|
|
380
|
+
}
|
|
381
|
+
return this.map(item => wrap(item).toJSON());
|
|
382
|
+
}
|
|
383
|
+
getIdentifiers(field) {
|
|
384
|
+
const items = this.getItems();
|
|
385
|
+
const targetMeta = this.property.targetMeta;
|
|
386
|
+
if (items.length === 0) {
|
|
387
|
+
return [];
|
|
388
|
+
}
|
|
389
|
+
field ??= targetMeta.compositePK ? targetMeta.primaryKeys : (targetMeta.serializedPrimaryKey ?? targetMeta.primaryKeys[0]);
|
|
390
|
+
const cb = (i, f) => {
|
|
391
|
+
if (Utils.isEntity(i[f], true)) {
|
|
392
|
+
return wrap(i[f], true).getPrimaryKey();
|
|
393
|
+
}
|
|
394
|
+
return i[f];
|
|
395
|
+
};
|
|
396
|
+
return items.map(i => {
|
|
397
|
+
if (Array.isArray(field)) {
|
|
398
|
+
return field.map(f => cb(i, f));
|
|
399
|
+
}
|
|
400
|
+
return cb(i, field);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* @internal
|
|
405
|
+
*/
|
|
406
|
+
addWithoutPropagation(entity) {
|
|
407
|
+
if (!this.contains(entity, false)) {
|
|
408
|
+
this.incrementCount(1);
|
|
409
|
+
this[this.items.size] = entity;
|
|
410
|
+
this.items.add(entity);
|
|
411
|
+
this.dirty = true;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
set(items) {
|
|
415
|
+
if (!this.initialized) {
|
|
416
|
+
this.initialized = true;
|
|
417
|
+
this.snapshot = undefined;
|
|
418
|
+
}
|
|
419
|
+
if (this.compare(Utils.asArray(items).map(item => Reference.unwrapReference(item)))) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
this.remove(this.items);
|
|
423
|
+
this.add(items);
|
|
424
|
+
}
|
|
425
|
+
compare(items) {
|
|
426
|
+
if (items.length !== this.items.size) {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
let idx = 0;
|
|
430
|
+
for (const item of this.items) {
|
|
431
|
+
if (item !== items[idx++]) {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return true;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* @internal
|
|
439
|
+
*/
|
|
440
|
+
hydrate(items, forcePropagate, partial) {
|
|
441
|
+
for (let i = 0; i < this.items.size; i++) {
|
|
442
|
+
delete this[i];
|
|
443
|
+
}
|
|
444
|
+
this.initialized = true;
|
|
445
|
+
this.partial = !!partial;
|
|
446
|
+
this.items.clear();
|
|
447
|
+
this._count = 0;
|
|
448
|
+
this.add(items);
|
|
449
|
+
this.takeSnapshot(forcePropagate);
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Remove all items from the collection. Note that removing items from collection does not necessarily imply deleting the target entity,
|
|
453
|
+
* it means we are disconnecting the relation - removing items from collection, not removing entities from database - `Collection.remove()`
|
|
454
|
+
* 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`,
|
|
455
|
+
* which tells the ORM we don't want orphaned entities to exist, so we know those should be removed.
|
|
456
|
+
*/
|
|
457
|
+
removeAll() {
|
|
458
|
+
if (!this.initialized) {
|
|
459
|
+
this.initialized = true;
|
|
460
|
+
this.snapshot = undefined;
|
|
461
|
+
}
|
|
462
|
+
this.remove(this.items);
|
|
463
|
+
this.dirty = true;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* @internal
|
|
467
|
+
*/
|
|
468
|
+
removeWithoutPropagation(entity) {
|
|
469
|
+
if (!this.items.delete(entity)) {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
this.incrementCount(-1);
|
|
473
|
+
delete this[this.items.size];
|
|
474
|
+
Object.assign(this, [...this.items]);
|
|
475
|
+
this.dirty = true;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Extracts a slice of the collection items starting at position start to end (exclusive) of the collection.
|
|
479
|
+
* If end is null it returns all elements from start to the end of the collection.
|
|
480
|
+
*/
|
|
481
|
+
slice(start = 0, end) {
|
|
482
|
+
this.checkInitialized();
|
|
483
|
+
let index = 0;
|
|
484
|
+
end ??= this.items.size;
|
|
485
|
+
const items = [];
|
|
486
|
+
for (const item of this.items) {
|
|
487
|
+
if (index === end) {
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
490
|
+
if (index >= start && index < end) {
|
|
491
|
+
items.push(item);
|
|
492
|
+
}
|
|
493
|
+
index++;
|
|
494
|
+
}
|
|
495
|
+
return items;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Tests for the existence of an element that satisfies the given predicate.
|
|
499
|
+
*/
|
|
500
|
+
exists(cb) {
|
|
501
|
+
this.checkInitialized();
|
|
502
|
+
for (const item of this.items) {
|
|
503
|
+
if (cb(item)) {
|
|
504
|
+
return true;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return false;
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Returns the first element of this collection that satisfies the predicate.
|
|
511
|
+
*/
|
|
512
|
+
find(cb) {
|
|
513
|
+
this.checkInitialized();
|
|
514
|
+
let index = 0;
|
|
515
|
+
for (const item of this.items) {
|
|
516
|
+
if (cb(item, index++)) {
|
|
517
|
+
return item;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
return undefined;
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Extracts a subset of the collection items.
|
|
524
|
+
*/
|
|
525
|
+
filter(cb) {
|
|
526
|
+
this.checkInitialized();
|
|
527
|
+
const items = [];
|
|
528
|
+
let index = 0;
|
|
529
|
+
for (const item of this.items) {
|
|
530
|
+
if (cb(item, index++)) {
|
|
531
|
+
items.push(item);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return items;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Maps the collection items based on your provided mapper function.
|
|
538
|
+
*/
|
|
539
|
+
map(mapper) {
|
|
540
|
+
this.checkInitialized();
|
|
541
|
+
const items = [];
|
|
542
|
+
let index = 0;
|
|
543
|
+
for (const item of this.items) {
|
|
544
|
+
items.push(mapper(item, index++));
|
|
545
|
+
}
|
|
546
|
+
return items;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Maps the collection items based on your provided mapper function to a single object.
|
|
550
|
+
*/
|
|
551
|
+
reduce(cb, initial = {}) {
|
|
552
|
+
this.checkInitialized();
|
|
553
|
+
let index = 0;
|
|
554
|
+
for (const item of this.items) {
|
|
555
|
+
initial = cb(initial, item, index++);
|
|
556
|
+
}
|
|
557
|
+
return initial;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Maps the collection items to a dictionary, indexed by the key you specify.
|
|
561
|
+
* If there are more items with the same key, only the first one will be present.
|
|
562
|
+
*/
|
|
563
|
+
indexBy(key, valueKey) {
|
|
564
|
+
return this.reduce((obj, item) => {
|
|
565
|
+
obj[item[key]] ??= valueKey ? item[valueKey] : item;
|
|
566
|
+
return obj;
|
|
567
|
+
}, {});
|
|
568
|
+
}
|
|
569
|
+
isInitialized(fully = false) {
|
|
570
|
+
if (!this.initialized || !fully) {
|
|
571
|
+
return this.initialized;
|
|
572
|
+
}
|
|
573
|
+
for (const item of this.items) {
|
|
574
|
+
if (!helper(item).__initialized) {
|
|
575
|
+
return false;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return true;
|
|
579
|
+
}
|
|
580
|
+
isDirty() {
|
|
581
|
+
return this.dirty;
|
|
582
|
+
}
|
|
583
|
+
isPartial() {
|
|
584
|
+
return this.partial;
|
|
585
|
+
}
|
|
586
|
+
setDirty(dirty = true) {
|
|
587
|
+
this.dirty = dirty;
|
|
588
|
+
}
|
|
589
|
+
get length() {
|
|
590
|
+
return this.count();
|
|
591
|
+
}
|
|
592
|
+
*[Symbol.iterator]() {
|
|
593
|
+
for (const item of this.getItems()) {
|
|
594
|
+
yield item;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* @internal
|
|
599
|
+
*/
|
|
600
|
+
takeSnapshot(forcePropagate) {
|
|
601
|
+
this.snapshot = [...this.items];
|
|
602
|
+
this.dirty = false;
|
|
603
|
+
if (this.property.owner || forcePropagate) {
|
|
604
|
+
this.items.forEach(item => {
|
|
605
|
+
this.propagate(item, 'takeSnapshot');
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* @internal
|
|
611
|
+
*/
|
|
612
|
+
getSnapshot() {
|
|
613
|
+
return this.snapshot;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* @internal
|
|
617
|
+
*/
|
|
618
|
+
get property() {
|
|
619
|
+
if (!this._property) {
|
|
620
|
+
const meta = wrap(this.owner, true).__meta;
|
|
621
|
+
/* v8 ignore next */
|
|
622
|
+
if (!meta) {
|
|
623
|
+
throw MetadataError.fromUnknownEntity(this.owner.constructor.name, 'Collection.property getter, maybe you just forgot to initialize the ORM?');
|
|
624
|
+
}
|
|
625
|
+
this._property = meta.relations.find(prop => this.owner[prop.name] === this);
|
|
626
|
+
}
|
|
627
|
+
return this._property;
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* @internal
|
|
631
|
+
*/
|
|
632
|
+
set property(prop) {
|
|
633
|
+
this._property = prop;
|
|
634
|
+
}
|
|
635
|
+
propagate(item, method) {
|
|
636
|
+
if (this.property.owner && this.property.inversedBy) {
|
|
637
|
+
this.propagateToInverseSide(item, method);
|
|
638
|
+
}
|
|
639
|
+
else if (!this.property.owner && this.property.mappedBy) {
|
|
640
|
+
this.propagateToOwningSide(item, method);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
propagateToInverseSide(item, method) {
|
|
644
|
+
const collection = item[this.property.inversedBy];
|
|
645
|
+
if (this.shouldPropagateToCollection(collection, method)) {
|
|
646
|
+
method = method === 'takeSnapshot' ? method : (method + 'WithoutPropagation');
|
|
647
|
+
collection[method](this.owner);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
propagateToOwningSide(item, method) {
|
|
651
|
+
const mappedBy = this.property.mappedBy;
|
|
652
|
+
const collection = item[mappedBy];
|
|
653
|
+
if (this.property.kind === ReferenceKind.MANY_TO_MANY) {
|
|
654
|
+
if (this.shouldPropagateToCollection(collection, method)) {
|
|
655
|
+
collection[method](this.owner);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
else if (this.property.kind === ReferenceKind.ONE_TO_MANY && method !== 'takeSnapshot') {
|
|
659
|
+
const prop2 = this.property.targetMeta.properties[mappedBy];
|
|
660
|
+
const owner = prop2.mapToPk ? helper(this.owner).getPrimaryKey() : this.owner;
|
|
661
|
+
const value = method === 'add' ? owner : null;
|
|
662
|
+
if (this.property.orphanRemoval && method === 'remove') {
|
|
663
|
+
// cache the PK before we propagate, as its value might be needed when flushing
|
|
664
|
+
helper(item).__pk = helper(item).getPrimaryKey();
|
|
665
|
+
}
|
|
666
|
+
if (!prop2.nullable && prop2.deleteRule !== 'cascade' && method === 'remove') {
|
|
667
|
+
if (!this.property.orphanRemoval) {
|
|
668
|
+
throw ValidationError.cannotRemoveFromCollectionWithoutOrphanRemoval(this.owner, this.property);
|
|
669
|
+
}
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
// skip if already propagated
|
|
673
|
+
if (Reference.unwrapReference(item[mappedBy]) !== value) {
|
|
674
|
+
item[mappedBy] = value;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
shouldPropagateToCollection(collection, method) {
|
|
679
|
+
if (!collection) {
|
|
680
|
+
return false;
|
|
681
|
+
}
|
|
682
|
+
switch (method) {
|
|
683
|
+
case 'add':
|
|
684
|
+
return !collection.contains(this.owner, false);
|
|
685
|
+
case 'remove':
|
|
686
|
+
return collection.isInitialized() && collection.contains(this.owner, false);
|
|
687
|
+
case 'takeSnapshot':
|
|
688
|
+
return collection.isDirty();
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
incrementCount(value) {
|
|
692
|
+
if (typeof this._count === 'number' && this.initialized) {
|
|
693
|
+
this._count += value;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
/** @ignore */
|
|
697
|
+
[Symbol.for('nodejs.util.inspect.custom')](depth = 2) {
|
|
698
|
+
const object = { ...this };
|
|
699
|
+
const hidden = ['items', 'owner', '_property', '_count', 'snapshot', '_populated', '_lazyInitialized', '_em', 'readonly', 'partial'];
|
|
700
|
+
hidden.forEach(k => delete object[k]);
|
|
701
|
+
const ret = inspect(object, { depth });
|
|
702
|
+
const name = `${this.constructor.name}<${this.property?.type ?? 'unknown'}>`;
|
|
703
|
+
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
704
|
+
}
|
|
374
705
|
}
|
|
375
706
|
Object.defineProperties(Collection.prototype, {
|
|
376
707
|
$: { get() { return this; } },
|
|
377
708
|
get: { get() { return () => this; } },
|
|
709
|
+
__collection: { value: true, enumerable: false, writable: false },
|
|
378
710
|
});
|
|
@@ -36,7 +36,7 @@ export interface AssignOptions<Convert extends boolean> {
|
|
|
36
36
|
*/
|
|
37
37
|
onlyProperties?: boolean;
|
|
38
38
|
/**
|
|
39
|
-
* With `onlyOwnProperties` enabled, to-many relations are skipped, and payloads of
|
|
39
|
+
* With `onlyOwnProperties` enabled, inverse sides of to-many relations are skipped, and payloads of other relations are converted
|
|
40
40
|
* to foreign keys. Defaults to `false`.
|
|
41
41
|
*/
|
|
42
42
|
onlyOwnProperties?: boolean;
|