@mikro-orm/core 7.0.0-dev.9 → 7.0.0-dev.91
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 +77 -48
- package/EntityManager.js +288 -225
- package/MikroORM.d.ts +40 -31
- package/MikroORM.js +98 -137
- package/README.md +3 -2
- package/cache/FileCacheAdapter.d.ts +1 -1
- package/cache/FileCacheAdapter.js +6 -5
- 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 +11 -7
- package/connections/Connection.js +16 -14
- package/drivers/DatabaseDriver.d.ts +11 -5
- package/drivers/DatabaseDriver.js +23 -11
- package/drivers/IDatabaseDriver.d.ts +25 -4
- package/entity/BaseEntity.d.ts +0 -1
- package/entity/BaseEntity.js +0 -3
- package/entity/Collection.d.ts +95 -30
- package/entity/Collection.js +432 -93
- package/entity/EntityAssigner.d.ts +1 -1
- package/entity/EntityAssigner.js +17 -9
- package/entity/EntityFactory.d.ts +7 -0
- package/entity/EntityFactory.js +63 -41
- package/entity/EntityHelper.js +26 -12
- package/entity/EntityLoader.d.ts +5 -4
- package/entity/EntityLoader.js +63 -38
- package/entity/EntityRepository.d.ts +1 -1
- package/entity/Reference.d.ts +6 -5
- package/entity/Reference.js +34 -9
- package/entity/WrappedEntity.d.ts +2 -7
- package/entity/WrappedEntity.js +2 -7
- package/entity/defineEntity.d.ts +568 -0
- package/entity/defineEntity.js +529 -0
- package/entity/index.d.ts +3 -2
- package/entity/index.js +3 -2
- package/entity/utils.d.ts +7 -0
- package/entity/utils.js +16 -4
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +65 -0
- package/enums.d.ts +21 -6
- package/enums.js +14 -1
- package/errors.d.ts +10 -2
- package/errors.js +29 -10
- package/events/EventManager.d.ts +2 -1
- package/events/EventManager.js +19 -11
- package/events/EventSubscriber.d.ts +3 -1
- package/hydration/Hydrator.js +1 -2
- package/hydration/ObjectHydrator.d.ts +4 -4
- package/hydration/ObjectHydrator.js +35 -25
- 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/index.d.ts +1 -0
- package/logging/index.js +1 -0
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +16 -0
- package/metadata/EntitySchema.d.ts +9 -13
- package/metadata/EntitySchema.js +44 -26
- package/metadata/MetadataDiscovery.d.ts +6 -9
- package/metadata/MetadataDiscovery.js +165 -205
- package/metadata/MetadataProvider.d.ts +11 -2
- package/metadata/MetadataProvider.js +44 -2
- package/metadata/MetadataStorage.d.ts +1 -6
- package/metadata/MetadataStorage.js +6 -18
- package/metadata/MetadataValidator.d.ts +0 -7
- package/metadata/MetadataValidator.js +0 -10
- 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 +480 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +5 -1
- package/naming-strategy/AbstractNamingStrategy.js +8 -2
- package/naming-strategy/NamingStrategy.d.ts +11 -1
- package/not-supported.d.ts +2 -0
- package/not-supported.js +4 -0
- package/package.json +18 -10
- package/platforms/ExceptionConverter.js +1 -1
- package/platforms/Platform.d.ts +6 -13
- package/platforms/Platform.js +15 -41
- package/serialization/EntitySerializer.d.ts +2 -0
- package/serialization/EntitySerializer.js +32 -14
- package/serialization/EntityTransformer.js +22 -12
- package/serialization/SerializationContext.js +16 -13
- 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 +2 -1
- package/types/BooleanType.js +3 -0
- package/types/DecimalType.d.ts +6 -4
- package/types/DecimalType.js +3 -3
- package/types/DoubleType.js +2 -2
- 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 +109 -73
- package/typings.js +38 -35
- package/unit-of-work/ChangeSet.d.ts +0 -3
- package/unit-of-work/ChangeSet.js +2 -2
- package/unit-of-work/ChangeSetComputer.d.ts +1 -3
- package/unit-of-work/ChangeSetComputer.js +11 -9
- package/unit-of-work/ChangeSetPersister.d.ts +5 -4
- package/unit-of-work/ChangeSetPersister.js +51 -19
- package/unit-of-work/UnitOfWork.d.ts +8 -1
- package/unit-of-work/UnitOfWork.js +91 -49
- package/utils/AbstractSchemaGenerator.d.ts +5 -5
- package/utils/AbstractSchemaGenerator.js +11 -9
- package/utils/Configuration.d.ts +757 -206
- package/utils/Configuration.js +140 -188
- package/utils/ConfigurationLoader.d.ts +1 -54
- package/utils/ConfigurationLoader.js +1 -352
- package/utils/Cursor.d.ts +0 -3
- package/utils/Cursor.js +6 -3
- package/utils/DataloaderUtils.d.ts +15 -5
- package/utils/DataloaderUtils.js +54 -8
- package/utils/EntityComparator.d.ts +8 -4
- package/utils/EntityComparator.js +52 -17
- package/utils/QueryHelper.d.ts +9 -1
- package/utils/QueryHelper.js +70 -9
- package/utils/RawQueryFragment.d.ts +36 -13
- package/utils/RawQueryFragment.js +36 -16
- package/utils/TransactionManager.d.ts +65 -0
- package/utils/TransactionManager.js +223 -0
- package/utils/Utils.d.ts +9 -97
- package/utils/Utils.js +83 -302
- package/utils/clone.js +2 -3
- package/utils/env-vars.d.ts +3 -0
- package/utils/env-vars.js +87 -0
- package/utils/fs-utils.d.ts +12 -0
- package/utils/fs-utils.js +97 -0
- package/utils/index.d.ts +2 -1
- package/utils/index.js +2 -1
- package/utils/upsert-utils.d.ts +7 -2
- package/utils/upsert-utils.js +55 -4
- 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/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
|
@@ -42,8 +42,8 @@ export class UnitOfWork {
|
|
|
42
42
|
this.identityMap = new IdentityMap(this.platform.getDefaultSchemaName());
|
|
43
43
|
this.eventManager = this.em.getEventManager();
|
|
44
44
|
this.comparator = this.em.getComparator();
|
|
45
|
-
this.changeSetComputer = new ChangeSetComputer(this.
|
|
46
|
-
this.changeSetPersister = new ChangeSetPersister(this.em.getDriver(), this.metadata, this.em.config.getHydrator(this.metadata), this.em.getEntityFactory(), this.em.
|
|
45
|
+
this.changeSetComputer = new ChangeSetComputer(this.collectionUpdates, this.metadata, this.platform, this.em.config, this.em);
|
|
46
|
+
this.changeSetPersister = new ChangeSetPersister(this.em.getDriver(), this.metadata, this.em.config.getHydrator(this.metadata), this.em.getEntityFactory(), this.em.config, this.em);
|
|
47
47
|
}
|
|
48
48
|
merge(entity, visited) {
|
|
49
49
|
const wrapped = helper(entity);
|
|
@@ -61,10 +61,43 @@ export class UnitOfWork {
|
|
|
61
61
|
// as there can be some entity with already changed state that is not yet flushed
|
|
62
62
|
if (wrapped.__initialized && (!visited || !wrapped.__originalEntityData)) {
|
|
63
63
|
wrapped.__originalEntityData = this.comparator.prepareEntity(entity);
|
|
64
|
-
wrapped.__touched = false;
|
|
65
64
|
}
|
|
66
65
|
this.cascade(entity, Cascade.MERGE, visited ?? new Set());
|
|
67
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Entity data can wary in its shape, e.g. we might get a deep relation graph with joined strategy, but for diffing,
|
|
69
|
+
* we need to normalize the shape, so relation values are only raw FKs. This method handles that.
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
normalizeEntityData(meta, data) {
|
|
73
|
+
const forceUndefined = this.em.config.get('forceUndefined');
|
|
74
|
+
for (const key of Utils.keys(data)) {
|
|
75
|
+
const prop = meta.properties[key];
|
|
76
|
+
if (!prop) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils.isPlainObject(data[prop.name])) {
|
|
80
|
+
data[prop.name] = Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta, true);
|
|
81
|
+
}
|
|
82
|
+
else if (prop.kind === ReferenceKind.EMBEDDED && !prop.object && Utils.isPlainObject(data[prop.name])) {
|
|
83
|
+
for (const p of prop.targetMeta.props) {
|
|
84
|
+
/* v8 ignore next */
|
|
85
|
+
const prefix = prop.prefix === false ? '' : prop.prefix === true ? prop.name + '_' : prop.prefix;
|
|
86
|
+
data[prefix + p.name] = data[prop.name][p.name];
|
|
87
|
+
}
|
|
88
|
+
data[prop.name] = Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta, true);
|
|
89
|
+
}
|
|
90
|
+
if (prop.hydrate === false && prop.customType?.ensureComparable(meta, prop)) {
|
|
91
|
+
const converted = prop.customType.convertToJSValue(data[key], this.platform, { key, mode: 'hydration', force: true });
|
|
92
|
+
data[key] = prop.customType.convertToDatabaseValue(converted, this.platform, { key, mode: 'hydration' });
|
|
93
|
+
}
|
|
94
|
+
if (forceUndefined) {
|
|
95
|
+
if (data[key] === null) {
|
|
96
|
+
data[key] = undefined;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
68
101
|
/**
|
|
69
102
|
* @internal
|
|
70
103
|
*/
|
|
@@ -82,31 +115,14 @@ export class UnitOfWork {
|
|
|
82
115
|
wrapped.__em ??= this.em;
|
|
83
116
|
wrapped.__managed = true;
|
|
84
117
|
if (data && (options?.refresh || !wrapped.__originalEntityData)) {
|
|
118
|
+
this.normalizeEntityData(wrapped.__meta, data);
|
|
85
119
|
for (const key of Utils.keys(data)) {
|
|
86
120
|
const prop = wrapped.__meta.properties[key];
|
|
87
|
-
if (
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
wrapped.__loadedProperties.add(key);
|
|
91
|
-
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils.isPlainObject(data[prop.name])) {
|
|
92
|
-
data[prop.name] = Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta.primaryKeys, true);
|
|
93
|
-
}
|
|
94
|
-
else if (prop.kind === ReferenceKind.EMBEDDED && !prop.object && Utils.isPlainObject(data[prop.name])) {
|
|
95
|
-
for (const p of prop.targetMeta.props) {
|
|
96
|
-
/* v8 ignore next */
|
|
97
|
-
const prefix = prop.prefix === false ? '' : prop.prefix === true ? prop.name + '_' : prop.prefix;
|
|
98
|
-
data[prefix + p.name] = data[prop.name][p.name];
|
|
99
|
-
}
|
|
100
|
-
data[prop.name] = Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta.primaryKeys, true);
|
|
101
|
-
}
|
|
102
|
-
if (forceUndefined) {
|
|
103
|
-
if (data[key] === null) {
|
|
104
|
-
data[key] = undefined;
|
|
105
|
-
}
|
|
121
|
+
if (prop) {
|
|
122
|
+
wrapped.__loadedProperties.add(key);
|
|
106
123
|
}
|
|
107
124
|
}
|
|
108
125
|
wrapped.__originalEntityData = data;
|
|
109
|
-
wrapped.__touched = false;
|
|
110
126
|
}
|
|
111
127
|
return entity;
|
|
112
128
|
}
|
|
@@ -125,7 +141,7 @@ export class UnitOfWork {
|
|
|
125
141
|
/**
|
|
126
142
|
* Returns entity from the identity map. For composite keys, you need to pass an array of PKs in the same order as they are defined in `meta.primaryKeys`.
|
|
127
143
|
*/
|
|
128
|
-
getById(entityName, id, schema) {
|
|
144
|
+
getById(entityName, id, schema, convertCustomTypes) {
|
|
129
145
|
if (id == null || (Array.isArray(id) && id.length === 0)) {
|
|
130
146
|
return undefined;
|
|
131
147
|
}
|
|
@@ -135,7 +151,16 @@ export class UnitOfWork {
|
|
|
135
151
|
hash = '' + id;
|
|
136
152
|
}
|
|
137
153
|
else {
|
|
138
|
-
|
|
154
|
+
let keys = Array.isArray(id) ? Utils.flatten(id) : [id];
|
|
155
|
+
keys = meta.getPrimaryProps(true).map((p, i) => {
|
|
156
|
+
if (!convertCustomTypes && p.customType) {
|
|
157
|
+
return p.customType.convertToDatabaseValue(keys[i], this.platform, {
|
|
158
|
+
key: p.name,
|
|
159
|
+
mode: 'hydration',
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return keys[i];
|
|
163
|
+
});
|
|
139
164
|
hash = Utils.getPrimaryKeyHash(keys);
|
|
140
165
|
}
|
|
141
166
|
schema ??= meta.schema ?? this.em.config.getSchema();
|
|
@@ -185,10 +210,8 @@ export class UnitOfWork {
|
|
|
185
210
|
if (this.queuedActions.has(meta.className) || this.queuedActions.has(meta.root.className)) {
|
|
186
211
|
return true;
|
|
187
212
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
return true;
|
|
191
|
-
}
|
|
213
|
+
if (meta.discriminatorMap && Object.values(meta.discriminatorMap).some(v => this.queuedActions.has(v))) {
|
|
214
|
+
return true;
|
|
192
215
|
}
|
|
193
216
|
return false;
|
|
194
217
|
}
|
|
@@ -209,7 +232,6 @@ export class UnitOfWork {
|
|
|
209
232
|
this.changeSets.set(entity, cs);
|
|
210
233
|
this.persistStack.delete(entity);
|
|
211
234
|
wrapped.__originalEntityData = this.comparator.prepareEntity(entity);
|
|
212
|
-
wrapped.__touched = false;
|
|
213
235
|
}
|
|
214
236
|
recomputeSingleChangeSet(entity) {
|
|
215
237
|
const changeSet = this.changeSets.get(entity);
|
|
@@ -220,7 +242,6 @@ export class UnitOfWork {
|
|
|
220
242
|
if (cs && !this.checkUniqueProps(cs)) {
|
|
221
243
|
Object.assign(changeSet.payload, cs.payload);
|
|
222
244
|
helper(entity).__originalEntityData = this.comparator.prepareEntity(entity);
|
|
223
|
-
helper(entity).__touched = false;
|
|
224
245
|
}
|
|
225
246
|
}
|
|
226
247
|
persist(entity, visited, options = {}) {
|
|
@@ -303,17 +324,21 @@ export class UnitOfWork {
|
|
|
303
324
|
cs.entity.__helper.__processing = true;
|
|
304
325
|
}
|
|
305
326
|
await this.eventManager.dispatchEvent(EventType.onFlush, { em: this.em, uow: this });
|
|
327
|
+
this.filterCollectionUpdates();
|
|
306
328
|
// nothing to do, do not start transaction
|
|
307
329
|
if (this.changeSets.size === 0 && this.collectionUpdates.size === 0 && this.extraUpdates.size === 0) {
|
|
308
|
-
|
|
330
|
+
await this.eventManager.dispatchEvent(EventType.afterFlush, { em: this.em, uow: this });
|
|
331
|
+
return;
|
|
309
332
|
}
|
|
310
333
|
const groups = this.getChangeSetGroups();
|
|
311
334
|
const platform = this.em.getPlatform();
|
|
312
335
|
const runInTransaction = !this.em.isInTransaction() && platform.supportsTransactions() && this.em.config.get('implicitTransactions');
|
|
313
336
|
if (runInTransaction) {
|
|
337
|
+
const loggerContext = Utils.merge({ id: this.em._id }, this.em.getLoggerContext({ disableContextResolution: true }));
|
|
314
338
|
await this.em.getConnection('write').transactional(trx => this.persistToDatabase(groups, trx), {
|
|
315
339
|
ctx: oldTx,
|
|
316
340
|
eventBroadcaster: new TransactionEventBroadcaster(this.em),
|
|
341
|
+
loggerContext,
|
|
317
342
|
});
|
|
318
343
|
}
|
|
319
344
|
else {
|
|
@@ -369,7 +394,6 @@ export class UnitOfWork {
|
|
|
369
394
|
}
|
|
370
395
|
delete wrapped.__identifier;
|
|
371
396
|
delete wrapped.__originalEntityData;
|
|
372
|
-
wrapped.__touched = false;
|
|
373
397
|
wrapped.__managed = false;
|
|
374
398
|
}
|
|
375
399
|
computeChangeSets() {
|
|
@@ -379,14 +403,14 @@ export class UnitOfWork {
|
|
|
379
403
|
this.cascade(entity, Cascade.REMOVE, visited);
|
|
380
404
|
}
|
|
381
405
|
visited.clear();
|
|
382
|
-
for (const entity of this.persistStack) {
|
|
383
|
-
this.cascade(entity, Cascade.PERSIST, visited, { checkRemoveStack: true });
|
|
384
|
-
}
|
|
385
406
|
for (const entity of this.identityMap) {
|
|
386
407
|
if (!this.removeStack.has(entity) && !this.persistStack.has(entity) && !this.orphanRemoveStack.has(entity)) {
|
|
387
408
|
this.cascade(entity, Cascade.PERSIST, visited, { checkRemoveStack: true });
|
|
388
409
|
}
|
|
389
410
|
}
|
|
411
|
+
for (const entity of this.persistStack) {
|
|
412
|
+
this.cascade(entity, Cascade.PERSIST, visited, { checkRemoveStack: true });
|
|
413
|
+
}
|
|
390
414
|
visited.clear();
|
|
391
415
|
for (const entity of this.persistStack) {
|
|
392
416
|
this.findNewEntities(entity, visited);
|
|
@@ -411,7 +435,7 @@ export class UnitOfWork {
|
|
|
411
435
|
}
|
|
412
436
|
for (const entity of this.removeStack) {
|
|
413
437
|
const wrapped = helper(entity);
|
|
414
|
-
/* v8 ignore next
|
|
438
|
+
/* v8 ignore next */
|
|
415
439
|
if (wrapped.__processing) {
|
|
416
440
|
continue;
|
|
417
441
|
}
|
|
@@ -701,7 +725,7 @@ export class UnitOfWork {
|
|
|
701
725
|
if (!meta.versionProperty) {
|
|
702
726
|
throw OptimisticLockError.notVersioned(meta);
|
|
703
727
|
}
|
|
704
|
-
if (
|
|
728
|
+
if (typeof version === 'undefined') {
|
|
705
729
|
return;
|
|
706
730
|
}
|
|
707
731
|
const wrapped = helper(entity);
|
|
@@ -841,9 +865,16 @@ export class UnitOfWork {
|
|
|
841
865
|
}
|
|
842
866
|
await this.changeSetPersister.executeUpdates(changeSets, batched, { ctx });
|
|
843
867
|
for (const changeSet of changeSets) {
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
868
|
+
const wrapped = helper(changeSet.entity);
|
|
869
|
+
wrapped.__originalEntityData = this.comparator.prepareEntity(changeSet.entity);
|
|
870
|
+
if (!wrapped.__initialized) {
|
|
871
|
+
for (const prop of changeSet.meta.relations) {
|
|
872
|
+
if ([ReferenceKind.MANY_TO_MANY, ReferenceKind.ONE_TO_MANY].includes(prop.kind) && changeSet.entity[prop.name] == null) {
|
|
873
|
+
changeSet.entity[prop.name] = Collection.create(changeSet.entity, prop.name, undefined, wrapped.isInitialized());
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
wrapped.__initialized = true;
|
|
877
|
+
}
|
|
847
878
|
await this.runHooks(EventType.afterUpdate, changeSet);
|
|
848
879
|
}
|
|
849
880
|
}
|
|
@@ -886,23 +917,34 @@ export class UnitOfWork {
|
|
|
886
917
|
}
|
|
887
918
|
}
|
|
888
919
|
async commitCollectionUpdates(ctx) {
|
|
889
|
-
|
|
920
|
+
this.filterCollectionUpdates();
|
|
921
|
+
const loggerContext = Utils.merge({ id: this.em._id }, this.em.getLoggerContext({ disableContextResolution: true }));
|
|
922
|
+
await this.em.getDriver().syncCollections(this.collectionUpdates, {
|
|
923
|
+
ctx,
|
|
924
|
+
schema: this.em.schema,
|
|
925
|
+
loggerContext,
|
|
926
|
+
});
|
|
927
|
+
for (const coll of this.collectionUpdates) {
|
|
928
|
+
coll.takeSnapshot();
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
filterCollectionUpdates() {
|
|
890
932
|
for (const coll of this.collectionUpdates) {
|
|
933
|
+
let skip = true;
|
|
891
934
|
if (coll.property.owner || coll.getItems(false).filter(item => !item.__helper.__initialized).length > 0) {
|
|
892
935
|
if (this.platform.usesPivotTable()) {
|
|
893
|
-
|
|
936
|
+
skip = false;
|
|
894
937
|
}
|
|
895
938
|
}
|
|
896
939
|
else if (coll.property.kind === ReferenceKind.ONE_TO_MANY && coll.getSnapshot() === undefined) {
|
|
897
|
-
|
|
940
|
+
skip = false;
|
|
898
941
|
}
|
|
899
942
|
else if (coll.property.kind === ReferenceKind.MANY_TO_MANY && !coll.property.owner) {
|
|
900
|
-
|
|
943
|
+
skip = false;
|
|
944
|
+
}
|
|
945
|
+
if (skip) {
|
|
946
|
+
this.collectionUpdates.delete(coll);
|
|
901
947
|
}
|
|
902
|
-
}
|
|
903
|
-
await this.em.getDriver().syncCollections(collectionUpdates, { ctx, schema: this.em.schema });
|
|
904
|
-
for (const coll of this.collectionUpdates) {
|
|
905
|
-
coll.takeSnapshot();
|
|
906
948
|
}
|
|
907
949
|
}
|
|
908
950
|
/**
|
|
@@ -10,18 +10,18 @@ export declare abstract class AbstractSchemaGenerator<D extends IDatabaseDriver>
|
|
|
10
10
|
protected readonly platform: ReturnType<D['getPlatform']>;
|
|
11
11
|
protected readonly connection: ReturnType<D['getConnection']>;
|
|
12
12
|
constructor(em: D | D[typeof EntityManagerType]);
|
|
13
|
-
|
|
13
|
+
create(options?: CreateSchemaOptions): Promise<void>;
|
|
14
14
|
/**
|
|
15
15
|
* Returns true if the database was created.
|
|
16
16
|
*/
|
|
17
17
|
ensureDatabase(options?: EnsureDatabaseOptions): Promise<boolean>;
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
refresh(options?: RefreshDatabaseOptions): Promise<void>;
|
|
19
|
+
clear(options?: ClearDatabaseOptions): Promise<void>;
|
|
20
20
|
protected clearIdentityMap(): void;
|
|
21
21
|
getCreateSchemaSQL(options?: CreateSchemaOptions): Promise<string>;
|
|
22
|
-
|
|
22
|
+
drop(options?: DropSchemaOptions): Promise<void>;
|
|
23
23
|
getDropSchemaSQL(options?: Omit<DropSchemaOptions, 'dropDb'>): Promise<string>;
|
|
24
|
-
|
|
24
|
+
update(options?: UpdateSchemaOptions): Promise<void>;
|
|
25
25
|
getUpdateSchemaSQL(options?: UpdateSchemaOptions): Promise<string>;
|
|
26
26
|
getUpdateSchemaMigrationSQL(options?: UpdateSchemaOptions): Promise<{
|
|
27
27
|
up: string;
|
|
@@ -15,7 +15,7 @@ export class AbstractSchemaGenerator {
|
|
|
15
15
|
this.platform = this.driver.getPlatform();
|
|
16
16
|
this.connection = this.driver.getConnection();
|
|
17
17
|
}
|
|
18
|
-
async
|
|
18
|
+
async create(options) {
|
|
19
19
|
this.notImplemented();
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
@@ -24,7 +24,7 @@ export class AbstractSchemaGenerator {
|
|
|
24
24
|
async ensureDatabase(options) {
|
|
25
25
|
this.notImplemented();
|
|
26
26
|
}
|
|
27
|
-
async
|
|
27
|
+
async refresh(options) {
|
|
28
28
|
if (options?.dropDb) {
|
|
29
29
|
const name = this.config.get('dbName');
|
|
30
30
|
await this.dropDatabase(name);
|
|
@@ -32,20 +32,22 @@ export class AbstractSchemaGenerator {
|
|
|
32
32
|
}
|
|
33
33
|
else {
|
|
34
34
|
await this.ensureDatabase();
|
|
35
|
-
await this.
|
|
35
|
+
await this.drop(options);
|
|
36
36
|
}
|
|
37
37
|
if (options?.createSchema !== false) {
|
|
38
|
-
await this.
|
|
38
|
+
await this.create(options);
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
-
async
|
|
41
|
+
async clear(options) {
|
|
42
42
|
for (const meta of this.getOrderedMetadata(options?.schema).reverse()) {
|
|
43
43
|
await this.driver.nativeDelete(meta.className, {}, options);
|
|
44
44
|
}
|
|
45
|
-
|
|
45
|
+
if (options?.clearIdentityMap ?? true) {
|
|
46
|
+
this.clearIdentityMap();
|
|
47
|
+
}
|
|
46
48
|
}
|
|
47
49
|
clearIdentityMap() {
|
|
48
|
-
/* v8 ignore next
|
|
50
|
+
/* v8 ignore next */
|
|
49
51
|
if (!this.em) {
|
|
50
52
|
return;
|
|
51
53
|
}
|
|
@@ -57,13 +59,13 @@ export class AbstractSchemaGenerator {
|
|
|
57
59
|
async getCreateSchemaSQL(options) {
|
|
58
60
|
this.notImplemented();
|
|
59
61
|
}
|
|
60
|
-
async
|
|
62
|
+
async drop(options) {
|
|
61
63
|
this.notImplemented();
|
|
62
64
|
}
|
|
63
65
|
async getDropSchemaSQL(options) {
|
|
64
66
|
this.notImplemented();
|
|
65
67
|
}
|
|
66
|
-
async
|
|
68
|
+
async update(options) {
|
|
67
69
|
this.notImplemented();
|
|
68
70
|
}
|
|
69
71
|
async getUpdateSchemaSQL(options) {
|