@mikro-orm/core 7.0.0-dev.7 → 7.0.0-dev.71
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 +85 -42
- package/EntityManager.js +282 -194
- package/MikroORM.d.ts +11 -29
- package/MikroORM.js +33 -127
- package/README.md +3 -2
- package/cache/FileCacheAdapter.js +1 -2
- package/connections/Connection.d.ts +11 -7
- package/connections/Connection.js +16 -14
- package/drivers/DatabaseDriver.d.ts +11 -5
- package/drivers/DatabaseDriver.js +13 -4
- package/drivers/IDatabaseDriver.d.ts +27 -5
- package/entity/BaseEntity.d.ts +0 -1
- package/entity/BaseEntity.js +0 -3
- package/entity/Collection.d.ts +98 -30
- package/entity/Collection.js +432 -93
- package/entity/EntityAssigner.d.ts +1 -1
- package/entity/EntityAssigner.js +9 -1
- package/entity/EntityFactory.d.ts +7 -0
- package/entity/EntityFactory.js +63 -40
- package/entity/EntityHelper.js +26 -9
- package/entity/EntityLoader.d.ts +5 -4
- package/entity/EntityLoader.js +69 -36
- package/entity/EntityRepository.d.ts +1 -1
- package/entity/EntityValidator.js +4 -4
- package/entity/Reference.d.ts +9 -7
- package/entity/Reference.js +32 -5
- package/entity/WrappedEntity.d.ts +0 -2
- package/entity/WrappedEntity.js +1 -5
- package/entity/defineEntity.d.ts +549 -0
- package/entity/defineEntity.js +529 -0
- package/entity/index.d.ts +2 -1
- package/entity/index.js +2 -1
- package/entity/utils.d.ts +7 -0
- package/entity/utils.js +15 -3
- package/enums.d.ts +20 -5
- package/enums.js +13 -0
- package/errors.d.ts +6 -1
- package/errors.js +14 -4
- package/events/EventSubscriber.d.ts +3 -1
- package/hydration/ObjectHydrator.d.ts +4 -4
- package/hydration/ObjectHydrator.js +35 -24
- package/index.d.ts +2 -2
- package/index.js +1 -2
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/SimpleLogger.d.ts +1 -1
- package/metadata/EntitySchema.d.ts +9 -13
- package/metadata/EntitySchema.js +44 -26
- package/metadata/MetadataDiscovery.d.ts +6 -7
- package/metadata/MetadataDiscovery.js +161 -162
- package/metadata/MetadataProvider.d.ts +2 -2
- package/metadata/MetadataProvider.js +15 -0
- package/metadata/MetadataStorage.d.ts +0 -4
- package/metadata/MetadataStorage.js +6 -10
- package/metadata/MetadataValidator.d.ts +0 -7
- package/metadata/MetadataValidator.js +4 -13
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +39 -0
- package/metadata/index.d.ts +1 -1
- package/metadata/index.js +1 -1
- package/metadata/types.d.ts +480 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +5 -1
- package/naming-strategy/AbstractNamingStrategy.js +7 -1
- package/naming-strategy/NamingStrategy.d.ts +11 -1
- package/package.json +11 -10
- package/platforms/Platform.d.ts +6 -10
- package/platforms/Platform.js +6 -22
- package/serialization/EntitySerializer.d.ts +2 -0
- package/serialization/EntitySerializer.js +29 -11
- package/serialization/EntityTransformer.js +22 -12
- package/serialization/SerializationContext.js +14 -11
- package/types/ArrayType.d.ts +1 -1
- package/types/ArrayType.js +1 -2
- package/types/BigIntType.d.ts +8 -6
- package/types/BlobType.d.ts +0 -1
- package/types/BlobType.js +0 -3
- package/types/BooleanType.d.ts +2 -1
- package/types/BooleanType.js +3 -0
- package/types/DecimalType.d.ts +6 -4
- package/types/DecimalType.js +1 -1
- package/types/DoubleType.js +1 -1
- package/types/JsonType.d.ts +1 -1
- package/types/JsonType.js +7 -2
- package/types/Type.d.ts +2 -1
- package/types/Type.js +1 -1
- package/types/Uint8ArrayType.d.ts +0 -1
- package/types/Uint8ArrayType.js +0 -3
- package/types/index.d.ts +1 -1
- package/typings.d.ts +112 -77
- package/typings.js +32 -32
- package/unit-of-work/ChangeSetComputer.js +8 -3
- package/unit-of-work/ChangeSetPersister.d.ts +4 -2
- package/unit-of-work/ChangeSetPersister.js +37 -16
- package/unit-of-work/UnitOfWork.d.ts +8 -1
- package/unit-of-work/UnitOfWork.js +111 -54
- package/utils/AbstractSchemaGenerator.d.ts +5 -5
- package/utils/AbstractSchemaGenerator.js +10 -8
- package/utils/Configuration.d.ts +200 -191
- package/utils/Configuration.js +141 -152
- package/utils/ConfigurationLoader.d.ts +3 -44
- package/utils/ConfigurationLoader.js +26 -239
- package/utils/Cursor.d.ts +3 -3
- package/utils/Cursor.js +3 -0
- package/utils/DataloaderUtils.d.ts +15 -5
- package/utils/DataloaderUtils.js +53 -7
- package/utils/EntityComparator.d.ts +8 -4
- package/utils/EntityComparator.js +107 -60
- package/utils/QueryHelper.d.ts +9 -1
- package/utils/QueryHelper.js +69 -8
- package/utils/RawQueryFragment.d.ts +36 -4
- package/utils/RawQueryFragment.js +34 -13
- package/utils/TransactionManager.d.ts +65 -0
- package/utils/TransactionManager.js +223 -0
- package/utils/Utils.d.ts +17 -84
- package/utils/Utils.js +132 -252
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/upsert-utils.d.ts +7 -2
- package/utils/upsert-utils.js +52 -1
- 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 -18
- package/decorators/Embedded.js +0 -18
- package/decorators/Entity.d.ts +0 -18
- 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 -40
- package/decorators/ManyToMany.js +0 -14
- package/decorators/ManyToOne.d.ts +0 -30
- 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 -24
- 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 -13
- 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 -116
- package/entity/ArrayCollection.js +0 -402
- 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
|
@@ -1,402 +0,0 @@
|
|
|
1
|
-
import { inspect } from 'node:util';
|
|
2
|
-
import { Reference } from './Reference.js';
|
|
3
|
-
import { helper, wrap } from './wrap.js';
|
|
4
|
-
import { MetadataError, ValidationError } from '../errors.js';
|
|
5
|
-
import { ReferenceKind } from '../enums.js';
|
|
6
|
-
import { Utils } from '../utils/Utils.js';
|
|
7
|
-
export class ArrayCollection {
|
|
8
|
-
owner;
|
|
9
|
-
items = new Set();
|
|
10
|
-
initialized = true;
|
|
11
|
-
dirty = false;
|
|
12
|
-
snapshot = []; // used to create a diff of the collection at commit time, undefined marks overridden values so we need to wipe when flushing
|
|
13
|
-
_count;
|
|
14
|
-
_property;
|
|
15
|
-
constructor(owner, items) {
|
|
16
|
-
this.owner = owner;
|
|
17
|
-
/* v8 ignore next 5 */
|
|
18
|
-
if (items) {
|
|
19
|
-
let i = 0;
|
|
20
|
-
this.items = new Set(items);
|
|
21
|
-
this.items.forEach(item => this[i++] = item);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
async loadCount() {
|
|
25
|
-
return this.items.size;
|
|
26
|
-
}
|
|
27
|
-
getItems() {
|
|
28
|
-
return [...this.items];
|
|
29
|
-
}
|
|
30
|
-
toArray() {
|
|
31
|
-
if (this.items.size === 0) {
|
|
32
|
-
return [];
|
|
33
|
-
}
|
|
34
|
-
const meta = this.property.targetMeta;
|
|
35
|
-
const args = meta.toJsonParams.map(() => undefined);
|
|
36
|
-
return this.map(item => wrap(item).toJSON(...args));
|
|
37
|
-
}
|
|
38
|
-
toJSON() {
|
|
39
|
-
return this.toArray();
|
|
40
|
-
}
|
|
41
|
-
getIdentifiers(field) {
|
|
42
|
-
const items = this.getItems();
|
|
43
|
-
const targetMeta = this.property.targetMeta;
|
|
44
|
-
if (items.length === 0) {
|
|
45
|
-
return [];
|
|
46
|
-
}
|
|
47
|
-
field ??= targetMeta.compositePK ? targetMeta.primaryKeys : targetMeta.serializedPrimaryKey;
|
|
48
|
-
const cb = (i, f) => {
|
|
49
|
-
if (Utils.isEntity(i[f], true)) {
|
|
50
|
-
return wrap(i[f], true).getPrimaryKey();
|
|
51
|
-
}
|
|
52
|
-
return i[f];
|
|
53
|
-
};
|
|
54
|
-
return items.map(i => {
|
|
55
|
-
if (Array.isArray(field)) {
|
|
56
|
-
return field.map(f => cb(i, f));
|
|
57
|
-
}
|
|
58
|
-
return cb(i, field);
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
add(entity, ...entities) {
|
|
62
|
-
entities = Utils.asArray(entity).concat(entities);
|
|
63
|
-
for (const item of entities) {
|
|
64
|
-
const entity = Reference.unwrapReference(item);
|
|
65
|
-
if (!this.contains(entity, false)) {
|
|
66
|
-
this.incrementCount(1);
|
|
67
|
-
this[this.items.size] = entity;
|
|
68
|
-
this.items.add(entity);
|
|
69
|
-
this.propagate(entity, 'add');
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* @internal
|
|
75
|
-
*/
|
|
76
|
-
addWithoutPropagation(entity) {
|
|
77
|
-
if (!this.contains(entity, false)) {
|
|
78
|
-
this.incrementCount(1);
|
|
79
|
-
this[this.items.size] = entity;
|
|
80
|
-
this.items.add(entity);
|
|
81
|
-
this.dirty = true;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
set(items) {
|
|
85
|
-
if (!this.initialized) {
|
|
86
|
-
this.initialized = true;
|
|
87
|
-
this.snapshot = undefined;
|
|
88
|
-
}
|
|
89
|
-
if (this.compare(Utils.asArray(items).map(item => Reference.unwrapReference(item)))) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
this.remove(this.items);
|
|
93
|
-
this.add(items);
|
|
94
|
-
}
|
|
95
|
-
compare(items) {
|
|
96
|
-
if (items.length !== this.items.size) {
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
let idx = 0;
|
|
100
|
-
for (const item of this.items) {
|
|
101
|
-
if (item !== items[idx++]) {
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* @internal
|
|
109
|
-
*/
|
|
110
|
-
hydrate(items, forcePropagate) {
|
|
111
|
-
for (let i = 0; i < this.items.size; i++) {
|
|
112
|
-
delete this[i];
|
|
113
|
-
}
|
|
114
|
-
this.initialized = true;
|
|
115
|
-
this.items.clear();
|
|
116
|
-
this._count = 0;
|
|
117
|
-
this.add(items);
|
|
118
|
-
this.takeSnapshot(forcePropagate);
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Remove specified item(s) from the collection. Note that removing item from collection does not necessarily imply deleting the target entity,
|
|
122
|
-
* it means we are disconnecting the relation - removing items from collection, not removing entities from database - `Collection.remove()`
|
|
123
|
-
* 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`,
|
|
124
|
-
* which tells the ORM we don't want orphaned entities to exist, so we know those should be removed.
|
|
125
|
-
*/
|
|
126
|
-
remove(entity, ...entities) {
|
|
127
|
-
entities = Utils.asArray(entity).concat(entities);
|
|
128
|
-
let modified = false;
|
|
129
|
-
for (const item of entities) {
|
|
130
|
-
if (!item) {
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
const entity = Reference.unwrapReference(item);
|
|
134
|
-
if (this.items.delete(entity)) {
|
|
135
|
-
this.incrementCount(-1);
|
|
136
|
-
delete this[this.items.size]; // remove last item
|
|
137
|
-
this.propagate(entity, 'remove');
|
|
138
|
-
modified = true;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
if (modified) {
|
|
142
|
-
Object.assign(this, [...this.items]); // reassign array access
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Remove all items from the collection. Note that removing items from collection does not necessarily imply deleting the target entity,
|
|
147
|
-
* it means we are disconnecting the relation - removing items from collection, not removing entities from database - `Collection.remove()`
|
|
148
|
-
* 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`,
|
|
149
|
-
* which tells the ORM we don't want orphaned entities to exist, so we know those should be removed.
|
|
150
|
-
*/
|
|
151
|
-
removeAll() {
|
|
152
|
-
if (!this.initialized) {
|
|
153
|
-
this.initialized = true;
|
|
154
|
-
this.snapshot = undefined;
|
|
155
|
-
}
|
|
156
|
-
this.remove(this.items);
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* @internal
|
|
160
|
-
*/
|
|
161
|
-
removeWithoutPropagation(entity) {
|
|
162
|
-
if (!this.items.delete(entity)) {
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
this.incrementCount(-1);
|
|
166
|
-
delete this[this.items.size];
|
|
167
|
-
Object.assign(this, [...this.items]);
|
|
168
|
-
this.dirty = true;
|
|
169
|
-
}
|
|
170
|
-
contains(item, check) {
|
|
171
|
-
const entity = Reference.unwrapReference(item);
|
|
172
|
-
return this.items.has(entity);
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Extracts a slice of the collection items starting at position start to end (exclusive) of the collection.
|
|
176
|
-
* If end is null it returns all elements from start to the end of the collection.
|
|
177
|
-
*/
|
|
178
|
-
slice(start = 0, end) {
|
|
179
|
-
let index = 0;
|
|
180
|
-
end ??= this.items.size;
|
|
181
|
-
const items = [];
|
|
182
|
-
for (const item of this.items) {
|
|
183
|
-
if (index === end) {
|
|
184
|
-
break;
|
|
185
|
-
}
|
|
186
|
-
if (index >= start && index < end) {
|
|
187
|
-
items.push(item);
|
|
188
|
-
}
|
|
189
|
-
index++;
|
|
190
|
-
}
|
|
191
|
-
return items;
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Tests for the existence of an element that satisfies the given predicate.
|
|
195
|
-
*/
|
|
196
|
-
exists(cb) {
|
|
197
|
-
for (const item of this.items) {
|
|
198
|
-
if (cb(item)) {
|
|
199
|
-
return true;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Returns the first element of this collection that satisfies the predicate.
|
|
206
|
-
*/
|
|
207
|
-
find(cb) {
|
|
208
|
-
let index = 0;
|
|
209
|
-
for (const item of this.items) {
|
|
210
|
-
if (cb(item, index++)) {
|
|
211
|
-
return item;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return undefined;
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Extracts a subset of the collection items.
|
|
218
|
-
*/
|
|
219
|
-
filter(cb) {
|
|
220
|
-
const items = [];
|
|
221
|
-
let index = 0;
|
|
222
|
-
for (const item of this.items) {
|
|
223
|
-
if (cb(item, index++)) {
|
|
224
|
-
items.push(item);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return items;
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Maps the collection items based on your provided mapper function.
|
|
231
|
-
*/
|
|
232
|
-
map(mapper) {
|
|
233
|
-
const items = [];
|
|
234
|
-
let index = 0;
|
|
235
|
-
for (const item of this.items) {
|
|
236
|
-
items.push(mapper(item, index++));
|
|
237
|
-
}
|
|
238
|
-
return items;
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Maps the collection items based on your provided mapper function to a single object.
|
|
242
|
-
*/
|
|
243
|
-
reduce(cb, initial = {}) {
|
|
244
|
-
let index = 0;
|
|
245
|
-
for (const item of this.items) {
|
|
246
|
-
initial = cb(initial, item, index++);
|
|
247
|
-
}
|
|
248
|
-
return initial;
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Maps the collection items to a dictionary, indexed by the key you specify.
|
|
252
|
-
* If there are more items with the same key, only the first one will be present.
|
|
253
|
-
*/
|
|
254
|
-
indexBy(key, valueKey) {
|
|
255
|
-
return this.reduce((obj, item) => {
|
|
256
|
-
obj[item[key]] ??= valueKey ? item[valueKey] : item;
|
|
257
|
-
return obj;
|
|
258
|
-
}, {});
|
|
259
|
-
}
|
|
260
|
-
count() {
|
|
261
|
-
return this.items.size;
|
|
262
|
-
}
|
|
263
|
-
isInitialized(fully = false) {
|
|
264
|
-
if (!this.initialized || !fully) {
|
|
265
|
-
return this.initialized;
|
|
266
|
-
}
|
|
267
|
-
for (const item of this.items) {
|
|
268
|
-
if (!helper(item).__initialized) {
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
return true;
|
|
273
|
-
}
|
|
274
|
-
isDirty() {
|
|
275
|
-
return this.dirty;
|
|
276
|
-
}
|
|
277
|
-
isEmpty() {
|
|
278
|
-
return this.count() === 0;
|
|
279
|
-
}
|
|
280
|
-
setDirty(dirty = true) {
|
|
281
|
-
this.dirty = dirty;
|
|
282
|
-
}
|
|
283
|
-
get length() {
|
|
284
|
-
return this.count();
|
|
285
|
-
}
|
|
286
|
-
*[Symbol.iterator]() {
|
|
287
|
-
for (const item of this.getItems()) {
|
|
288
|
-
yield item;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* @internal
|
|
293
|
-
*/
|
|
294
|
-
takeSnapshot(forcePropagate) {
|
|
295
|
-
this.snapshot = [...this.items];
|
|
296
|
-
this.setDirty(false);
|
|
297
|
-
if (this.property.owner || forcePropagate) {
|
|
298
|
-
this.items.forEach(item => {
|
|
299
|
-
this.propagate(item, 'takeSnapshot');
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* @internal
|
|
305
|
-
*/
|
|
306
|
-
getSnapshot() {
|
|
307
|
-
return this.snapshot;
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* @internal
|
|
311
|
-
*/
|
|
312
|
-
get property() {
|
|
313
|
-
if (!this._property) {
|
|
314
|
-
const meta = wrap(this.owner, true).__meta;
|
|
315
|
-
/* v8 ignore next 3 */
|
|
316
|
-
if (!meta) {
|
|
317
|
-
throw MetadataError.fromUnknownEntity(this.owner.constructor.name, 'Collection.property getter, maybe you just forgot to initialize the ORM?');
|
|
318
|
-
}
|
|
319
|
-
this._property = meta.relations.find(prop => this.owner[prop.name] === this);
|
|
320
|
-
}
|
|
321
|
-
return this._property;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* @internal
|
|
325
|
-
*/
|
|
326
|
-
set property(prop) {
|
|
327
|
-
this._property = prop;
|
|
328
|
-
}
|
|
329
|
-
propagate(item, method) {
|
|
330
|
-
if (this.property.owner && this.property.inversedBy) {
|
|
331
|
-
this.propagateToInverseSide(item, method);
|
|
332
|
-
}
|
|
333
|
-
else if (!this.property.owner && this.property.mappedBy) {
|
|
334
|
-
this.propagateToOwningSide(item, method);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
propagateToInverseSide(item, method) {
|
|
338
|
-
const collection = item[this.property.inversedBy];
|
|
339
|
-
if (this.shouldPropagateToCollection(collection, method)) {
|
|
340
|
-
method = method === 'takeSnapshot' ? method : (method + 'WithoutPropagation');
|
|
341
|
-
collection[method](this.owner);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
propagateToOwningSide(item, method) {
|
|
345
|
-
const mappedBy = this.property.mappedBy;
|
|
346
|
-
const collection = item[mappedBy];
|
|
347
|
-
if (this.property.kind === ReferenceKind.MANY_TO_MANY) {
|
|
348
|
-
if (this.shouldPropagateToCollection(collection, method)) {
|
|
349
|
-
collection[method](this.owner);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
else if (this.property.kind === ReferenceKind.ONE_TO_MANY && method !== 'takeSnapshot') {
|
|
353
|
-
const prop2 = this.property.targetMeta.properties[mappedBy];
|
|
354
|
-
const owner = prop2.mapToPk ? helper(this.owner).getPrimaryKey() : this.owner;
|
|
355
|
-
const value = method === 'add' ? owner : null;
|
|
356
|
-
if (this.property.orphanRemoval && method === 'remove') {
|
|
357
|
-
// cache the PK before we propagate, as its value might be needed when flushing
|
|
358
|
-
helper(item).__pk = helper(item).getPrimaryKey();
|
|
359
|
-
}
|
|
360
|
-
if (!prop2.nullable && prop2.deleteRule !== 'cascade' && method === 'remove') {
|
|
361
|
-
if (!this.property.orphanRemoval) {
|
|
362
|
-
throw ValidationError.cannotRemoveFromCollectionWithoutOrphanRemoval(this.owner, this.property);
|
|
363
|
-
}
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
// skip if already propagated
|
|
367
|
-
if (Reference.unwrapReference(item[mappedBy]) !== value) {
|
|
368
|
-
item[mappedBy] = value;
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
shouldPropagateToCollection(collection, method) {
|
|
373
|
-
if (!collection) {
|
|
374
|
-
return false;
|
|
375
|
-
}
|
|
376
|
-
switch (method) {
|
|
377
|
-
case 'add':
|
|
378
|
-
return !collection.contains(this.owner, false);
|
|
379
|
-
case 'remove':
|
|
380
|
-
return collection.isInitialized() && collection.contains(this.owner, false);
|
|
381
|
-
case 'takeSnapshot':
|
|
382
|
-
return collection.isDirty();
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
incrementCount(value) {
|
|
386
|
-
if (typeof this._count === 'number' && this.initialized) {
|
|
387
|
-
this._count += value;
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
/** @ignore */
|
|
391
|
-
[inspect.custom](depth = 2) {
|
|
392
|
-
const object = { ...this };
|
|
393
|
-
const hidden = ['items', 'owner', '_property', '_count', 'snapshot', '_populated', '_snapshot', '_lazyInitialized', '_em', 'readonly'];
|
|
394
|
-
hidden.forEach(k => delete object[k]);
|
|
395
|
-
const ret = inspect(object, { depth });
|
|
396
|
-
const name = `${this.constructor.name}<${this.property?.type ?? 'unknown'}>`;
|
|
397
|
-
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
Object.defineProperties(ArrayCollection.prototype, {
|
|
401
|
-
__collection: { value: true, enumerable: false, writable: false },
|
|
402
|
-
});
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import type { EntityMetadata, EntityProperty } from '../typings.js';
|
|
3
|
-
import { MetadataProvider } from './MetadataProvider.js';
|
|
4
|
-
export declare class ReflectMetadataProvider extends MetadataProvider {
|
|
5
|
-
loadEntityMetadata(meta: EntityMetadata, name: string): void;
|
|
6
|
-
protected initProperties(meta: EntityMetadata): void;
|
|
7
|
-
protected initPropertyType(meta: EntityMetadata, prop: EntityProperty): void;
|
|
8
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import { MetadataProvider } from './MetadataProvider.js';
|
|
3
|
-
import { ReferenceKind } from '../enums.js';
|
|
4
|
-
import { Utils } from '../utils/Utils.js';
|
|
5
|
-
export class ReflectMetadataProvider extends MetadataProvider {
|
|
6
|
-
loadEntityMetadata(meta, name) {
|
|
7
|
-
this.initProperties(meta);
|
|
8
|
-
}
|
|
9
|
-
initProperties(meta) {
|
|
10
|
-
// load types and column names
|
|
11
|
-
for (const prop of meta.props) {
|
|
12
|
-
if (typeof prop.entity === 'string') {
|
|
13
|
-
prop.type = prop.entity;
|
|
14
|
-
}
|
|
15
|
-
else if (prop.entity) {
|
|
16
|
-
const tmp = prop.entity();
|
|
17
|
-
prop.type = Array.isArray(tmp) ? tmp.map(t => Utils.className(t)).sort().join(' | ') : Utils.className(tmp);
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
this.initPropertyType(meta, prop);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
initPropertyType(meta, prop) {
|
|
25
|
-
const type = Reflect.getMetadata('design:type', meta.prototype, prop.name);
|
|
26
|
-
if (!prop.type && (!type || (type === Object && prop.kind !== ReferenceKind.SCALAR)) && !(prop.enum && (prop.items?.length ?? 0) > 0)) {
|
|
27
|
-
throw new Error(`Please provide either 'type' or 'entity' attribute in ${meta.className}.${prop.name}. If you are using decorators, ensure you have 'emitDecoratorMetadata' enabled in your tsconfig.json.`);
|
|
28
|
-
}
|
|
29
|
-
// Force mapping to UnknownType which is a string when we see just `Object`, as that often means failed inference.
|
|
30
|
-
// This is to prevent defaulting to JSON column type, which can often be hard to revert and cause hard to understand issues with PKs.
|
|
31
|
-
// If there are explicitly provided `columnTypes`, we use those instead for the inference, this way
|
|
32
|
-
// we can have things like `columnType: 'timestamp'` be respected as `type: 'Date'`.
|
|
33
|
-
if (prop.kind === ReferenceKind.SCALAR && type === Object && !prop.columnTypes) {
|
|
34
|
-
prop.type ??= 'any';
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
let typeName = type?.name;
|
|
38
|
-
if (typeName && ['string', 'number', 'boolean', 'array', 'object'].includes(typeName.toLowerCase())) {
|
|
39
|
-
typeName = typeName.toLowerCase();
|
|
40
|
-
}
|
|
41
|
-
prop.type ??= typeName;
|
|
42
|
-
prop.runtimeType ??= typeName;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { EntityManager } from '../EntityManager.js';
|
|
2
|
-
import { MikroORM } from '../MikroORM.js';
|
|
3
|
-
import type { ContextProvider, MaybePromise } from '../typings.js';
|
|
4
|
-
/**
|
|
5
|
-
* Find `EntityManager` in provided context, or else in instance's `orm` or `em` properties.
|
|
6
|
-
*/
|
|
7
|
-
export declare function resolveContextProvider<T>(caller: T & {
|
|
8
|
-
orm?: MaybePromise<MikroORM>;
|
|
9
|
-
em?: MaybePromise<EntityManager>;
|
|
10
|
-
}, provider?: ContextProvider<T>): Promise<EntityManager | undefined>;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { EntityRepository } from '../entity/EntityRepository.js';
|
|
2
|
-
import { EntityManager } from '../EntityManager.js';
|
|
3
|
-
import { MikroORM } from '../MikroORM.js';
|
|
4
|
-
function getEntityManager(caller, context) {
|
|
5
|
-
if (context instanceof EntityManager) {
|
|
6
|
-
return context;
|
|
7
|
-
}
|
|
8
|
-
if (context instanceof EntityRepository) {
|
|
9
|
-
return context.getEntityManager();
|
|
10
|
-
}
|
|
11
|
-
if (context instanceof MikroORM) {
|
|
12
|
-
return context.em;
|
|
13
|
-
}
|
|
14
|
-
if (caller.em instanceof EntityManager) {
|
|
15
|
-
return caller.em;
|
|
16
|
-
}
|
|
17
|
-
if (caller.orm instanceof MikroORM) {
|
|
18
|
-
return caller.orm.em;
|
|
19
|
-
}
|
|
20
|
-
return undefined;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Find `EntityManager` in provided context, or else in instance's `orm` or `em` properties.
|
|
24
|
-
*/
|
|
25
|
-
export async function resolveContextProvider(caller, provider) {
|
|
26
|
-
const context = typeof provider === 'function' ? await provider(caller) : await provider;
|
|
27
|
-
return getEntityManager({ orm: await caller.orm, em: await caller.em }, context);
|
|
28
|
-
}
|