@mikro-orm/core 7.0.0-dev.1 → 7.0.0-dev.11
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 +25 -16
- package/EntityManager.js +219 -209
- package/MikroORM.d.ts +7 -6
- package/MikroORM.js +33 -45
- package/cache/CacheAdapter.js +1 -2
- package/cache/FileCacheAdapter.d.ts +1 -1
- package/cache/FileCacheAdapter.js +18 -26
- package/cache/GeneratedCacheAdapter.d.ts +2 -2
- package/cache/GeneratedCacheAdapter.js +1 -5
- package/cache/MemoryCacheAdapter.d.ts +1 -1
- package/cache/MemoryCacheAdapter.js +1 -5
- package/cache/NullCacheAdapter.d.ts +1 -1
- package/cache/NullCacheAdapter.js +1 -5
- package/cache/index.d.ts +5 -5
- package/cache/index.js +5 -21
- package/connections/Connection.d.ts +7 -7
- package/connections/Connection.js +8 -12
- package/connections/index.d.ts +1 -1
- package/connections/index.js +1 -17
- package/decorators/Check.d.ts +2 -2
- package/decorators/Check.js +5 -8
- package/decorators/CreateRequestContext.d.ts +1 -1
- package/decorators/CreateRequestContext.js +13 -14
- package/decorators/Embeddable.d.ts +5 -5
- package/decorators/Embeddable.js +4 -7
- package/decorators/Embedded.d.ts +3 -3
- package/decorators/Embedded.js +10 -12
- package/decorators/Entity.d.ts +6 -6
- package/decorators/Entity.js +5 -9
- package/decorators/Enum.d.ts +3 -3
- package/decorators/Enum.js +7 -10
- package/decorators/Filter.d.ts +1 -1
- package/decorators/Filter.js +3 -6
- package/decorators/Formula.d.ts +2 -3
- package/decorators/Formula.js +7 -10
- package/decorators/Indexed.d.ts +10 -8
- package/decorators/Indexed.js +7 -11
- package/decorators/ManyToMany.d.ts +4 -4
- package/decorators/ManyToMany.js +10 -12
- package/decorators/ManyToOne.d.ts +4 -4
- package/decorators/ManyToOne.js +10 -12
- package/decorators/OneToMany.d.ts +6 -6
- package/decorators/OneToMany.js +11 -14
- package/decorators/OneToOne.d.ts +4 -4
- package/decorators/OneToOne.js +4 -7
- package/decorators/PrimaryKey.d.ts +3 -4
- package/decorators/PrimaryKey.js +10 -13
- package/decorators/Property.d.ts +6 -6
- package/decorators/Property.js +10 -12
- package/decorators/Transactional.d.ts +2 -2
- package/decorators/Transactional.js +7 -10
- package/decorators/hooks.js +23 -35
- package/decorators/index.d.ts +17 -17
- package/decorators/index.js +17 -36
- package/drivers/DatabaseDriver.d.ts +13 -12
- package/drivers/DatabaseDriver.js +60 -64
- package/drivers/IDatabaseDriver.d.ts +16 -13
- package/drivers/IDatabaseDriver.js +1 -4
- package/drivers/index.d.ts +2 -2
- package/drivers/index.js +2 -18
- package/entity/ArrayCollection.d.ts +3 -3
- package/entity/ArrayCollection.js +38 -35
- package/entity/BaseEntity.d.ts +6 -6
- package/entity/BaseEntity.js +17 -21
- package/entity/Collection.d.ts +6 -7
- package/entity/Collection.js +47 -51
- package/entity/EntityAssigner.d.ts +2 -2
- package/entity/EntityAssigner.js +58 -63
- package/entity/EntityFactory.d.ts +3 -3
- package/entity/EntityFactory.js +62 -63
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +44 -45
- package/entity/EntityIdentifier.d.ts +1 -1
- package/entity/EntityIdentifier.js +1 -5
- package/entity/EntityLoader.d.ts +5 -5
- package/entity/EntityLoader.js +106 -98
- package/entity/EntityRepository.d.ts +7 -7
- package/entity/EntityRepository.js +7 -11
- package/entity/EntityValidator.d.ts +1 -1
- package/entity/EntityValidator.js +25 -29
- package/entity/Reference.d.ts +4 -8
- package/entity/Reference.js +35 -42
- package/entity/WrappedEntity.d.ts +12 -12
- package/entity/WrappedEntity.js +23 -27
- package/entity/index.d.ts +13 -13
- package/entity/index.js +13 -29
- package/entity/utils.d.ts +1 -1
- package/entity/utils.js +9 -12
- package/entity/wrap.d.ts +1 -1
- package/entity/wrap.js +2 -6
- package/enums.d.ts +3 -3
- package/enums.js +37 -41
- package/errors.d.ts +1 -1
- package/errors.js +15 -24
- package/events/EventManager.d.ts +3 -3
- package/events/EventManager.js +8 -12
- package/events/EventSubscriber.d.ts +8 -5
- package/events/EventSubscriber.js +1 -2
- package/events/TransactionEventBroadcaster.d.ts +3 -3
- package/events/TransactionEventBroadcaster.js +1 -5
- package/events/index.d.ts +3 -3
- package/events/index.js +3 -19
- package/exceptions.js +18 -39
- package/hydration/Hydrator.d.ts +5 -5
- package/hydration/Hydrator.js +3 -6
- package/hydration/ObjectHydrator.d.ts +3 -3
- package/hydration/ObjectHydrator.js +26 -28
- package/hydration/index.d.ts +2 -2
- package/hydration/index.js +2 -18
- package/index.d.ts +21 -21
- package/index.js +21 -46
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/DefaultLogger.js +9 -13
- package/logging/Logger.d.ts +1 -1
- package/logging/Logger.js +1 -2
- package/logging/SimpleLogger.d.ts +2 -2
- package/logging/SimpleLogger.js +2 -6
- package/logging/colors.js +1 -5
- package/logging/index.d.ts +4 -4
- package/logging/index.js +4 -20
- package/metadata/EntitySchema.d.ts +14 -6
- package/metadata/EntitySchema.js +41 -45
- package/metadata/MetadataDiscovery.d.ts +7 -7
- package/metadata/MetadataDiscovery.js +181 -180
- package/metadata/MetadataProvider.d.ts +2 -2
- package/metadata/MetadataProvider.js +4 -7
- package/metadata/MetadataStorage.d.ts +2 -2
- package/metadata/MetadataStorage.js +15 -19
- package/metadata/MetadataValidator.d.ts +4 -4
- package/metadata/MetadataValidator.js +52 -55
- package/metadata/ReflectMetadataProvider.d.ts +2 -2
- package/metadata/ReflectMetadataProvider.js +8 -12
- package/metadata/index.d.ts +6 -6
- package/metadata/index.js +6 -22
- package/naming-strategy/AbstractNamingStrategy.d.ts +2 -2
- package/naming-strategy/AbstractNamingStrategy.js +4 -8
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +1 -1
- package/naming-strategy/EntityCaseNamingStrategy.js +2 -6
- package/naming-strategy/MongoNamingStrategy.d.ts +1 -1
- package/naming-strategy/MongoNamingStrategy.js +2 -6
- package/naming-strategy/NamingStrategy.d.ts +1 -1
- package/naming-strategy/NamingStrategy.js +1 -2
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +1 -1
- package/naming-strategy/UnderscoreNamingStrategy.js +2 -6
- package/naming-strategy/index.d.ts +5 -5
- package/naming-strategy/index.js +5 -21
- package/package.json +6 -15
- package/platforms/ExceptionConverter.d.ts +2 -2
- package/platforms/ExceptionConverter.js +4 -8
- package/platforms/Platform.d.ts +10 -10
- package/platforms/Platform.js +57 -61
- package/platforms/index.d.ts +2 -2
- package/platforms/index.js +2 -18
- package/serialization/EntitySerializer.d.ts +2 -2
- package/serialization/EntitySerializer.js +36 -41
- package/serialization/EntityTransformer.d.ts +1 -1
- package/serialization/EntityTransformer.js +27 -31
- package/serialization/SerializationContext.d.ts +2 -2
- package/serialization/SerializationContext.js +10 -14
- package/serialization/index.d.ts +3 -3
- package/serialization/index.js +3 -19
- package/types/ArrayType.d.ts +3 -3
- package/types/ArrayType.js +7 -11
- package/types/BigIntType.d.ts +4 -3
- package/types/BigIntType.js +6 -6
- package/types/BlobType.d.ts +3 -3
- package/types/BlobType.js +2 -8
- package/types/BooleanType.d.ts +3 -3
- package/types/BooleanType.js +2 -6
- package/types/CharacterType.d.ts +3 -3
- package/types/CharacterType.js +2 -6
- package/types/DateTimeType.d.ts +3 -3
- package/types/DateTimeType.js +2 -6
- package/types/DateType.d.ts +3 -3
- package/types/DateType.js +2 -6
- package/types/DecimalType.d.ts +3 -3
- package/types/DecimalType.js +4 -7
- package/types/DoubleType.d.ts +3 -3
- package/types/DoubleType.js +3 -6
- package/types/EnumArrayType.d.ts +4 -4
- package/types/EnumArrayType.js +5 -10
- package/types/EnumType.d.ts +3 -3
- package/types/EnumType.js +2 -6
- package/types/FloatType.d.ts +3 -3
- package/types/FloatType.js +2 -6
- package/types/IntegerType.d.ts +3 -3
- package/types/IntegerType.js +2 -6
- package/types/IntervalType.d.ts +3 -3
- package/types/IntervalType.js +2 -6
- package/types/JsonType.d.ts +3 -3
- package/types/JsonType.js +2 -6
- package/types/MediumIntType.d.ts +3 -3
- package/types/MediumIntType.js +2 -6
- package/types/SmallIntType.d.ts +3 -3
- package/types/SmallIntType.js +2 -6
- package/types/StringType.d.ts +3 -3
- package/types/StringType.js +2 -6
- package/types/TextType.d.ts +3 -3
- package/types/TextType.js +2 -6
- package/types/TimeType.d.ts +3 -3
- package/types/TimeType.js +4 -8
- package/types/TinyIntType.d.ts +3 -3
- package/types/TinyIntType.js +3 -6
- package/types/Type.d.ts +2 -2
- package/types/Type.js +5 -9
- package/types/Uint8ArrayType.d.ts +3 -3
- package/types/Uint8ArrayType.js +3 -9
- package/types/UnknownType.d.ts +3 -3
- package/types/UnknownType.js +2 -6
- package/types/UuidType.d.ts +3 -3
- package/types/UuidType.js +2 -6
- package/types/index.d.ts +25 -25
- package/types/index.js +52 -79
- package/typings.d.ts +33 -28
- package/typings.js +37 -38
- package/unit-of-work/ChangeSet.d.ts +1 -1
- package/unit-of-work/ChangeSet.js +13 -17
- package/unit-of-work/ChangeSetComputer.d.ts +8 -7
- package/unit-of-work/ChangeSetComputer.js +26 -30
- package/unit-of-work/ChangeSetPersister.d.ts +7 -6
- package/unit-of-work/ChangeSetPersister.js +51 -48
- package/unit-of-work/CommitOrderCalculator.d.ts +1 -1
- package/unit-of-work/CommitOrderCalculator.js +6 -10
- package/unit-of-work/IdentityMap.d.ts +1 -1
- package/unit-of-work/IdentityMap.js +1 -5
- package/unit-of-work/UnitOfWork.d.ts +8 -7
- package/unit-of-work/UnitOfWork.js +193 -178
- package/unit-of-work/index.d.ts +6 -6
- package/unit-of-work/index.js +6 -22
- package/utils/AbstractSchemaGenerator.d.ts +6 -6
- package/utils/AbstractSchemaGenerator.js +12 -13
- package/utils/Configuration.d.ts +26 -27
- package/utils/Configuration.js +50 -55
- package/utils/ConfigurationLoader.d.ts +9 -8
- package/utils/ConfigurationLoader.js +71 -86
- package/utils/Cursor.d.ts +6 -6
- package/utils/Cursor.js +22 -25
- package/utils/DataloaderUtils.d.ts +4 -4
- package/utils/DataloaderUtils.js +12 -16
- package/utils/EntityComparator.d.ts +2 -2
- package/utils/EntityComparator.js +109 -97
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +1 -5
- package/utils/QueryHelper.d.ts +3 -3
- package/utils/QueryHelper.js +47 -51
- package/utils/RawQueryFragment.d.ts +1 -1
- package/utils/RawQueryFragment.js +22 -25
- package/utils/RequestContext.d.ts +2 -2
- package/utils/RequestContext.js +3 -7
- package/utils/TransactionContext.d.ts +1 -1
- package/utils/TransactionContext.js +4 -8
- package/utils/Utils.d.ts +16 -12
- package/utils/Utils.js +96 -95
- package/utils/clone.js +8 -11
- package/utils/index.d.ts +13 -13
- package/utils/index.js +13 -29
- package/utils/resolveContextProvider.d.ts +3 -3
- package/utils/resolveContextProvider.js +9 -12
- package/utils/upsert-utils.d.ts +3 -3
- package/utils/upsert-utils.js +5 -9
- package/index.mjs +0 -199
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { Collection } from '../entity/Collection.js';
|
|
3
|
+
import { EntityHelper } from '../entity/EntityHelper.js';
|
|
4
|
+
import { helper } from '../entity/wrap.js';
|
|
5
|
+
import { Reference } from '../entity/Reference.js';
|
|
6
|
+
import { EntityIdentifier } from '../entity/EntityIdentifier.js';
|
|
7
|
+
import { ChangeSet, ChangeSetType } from './ChangeSet.js';
|
|
8
|
+
import { ChangeSetComputer } from './ChangeSetComputer.js';
|
|
9
|
+
import { ChangeSetPersister } from './ChangeSetPersister.js';
|
|
10
|
+
import { CommitOrderCalculator } from './CommitOrderCalculator.js';
|
|
11
|
+
import { Utils } from '../utils/Utils.js';
|
|
12
|
+
import { Cascade, DeferMode, EventType, LockMode, ReferenceKind } from '../enums.js';
|
|
13
|
+
import { OptimisticLockError, ValidationError } from '../errors.js';
|
|
14
|
+
import { TransactionEventBroadcaster } from '../events/TransactionEventBroadcaster.js';
|
|
15
|
+
import { IdentityMap } from './IdentityMap.js';
|
|
15
16
|
// to deal with validation for flush inside flush hooks and `Promise.all`
|
|
16
|
-
const insideFlush = new
|
|
17
|
-
class UnitOfWork {
|
|
17
|
+
const insideFlush = new AsyncLocalStorage();
|
|
18
|
+
export class UnitOfWork {
|
|
18
19
|
em;
|
|
19
20
|
/** map of references to managed entities */
|
|
20
21
|
identityMap;
|
|
@@ -38,14 +39,14 @@ class UnitOfWork {
|
|
|
38
39
|
this.em = em;
|
|
39
40
|
this.metadata = this.em.getMetadata();
|
|
40
41
|
this.platform = this.em.getPlatform();
|
|
41
|
-
this.identityMap = new
|
|
42
|
+
this.identityMap = new IdentityMap(this.platform.getDefaultSchemaName());
|
|
42
43
|
this.eventManager = this.em.getEventManager();
|
|
43
44
|
this.comparator = this.em.getComparator();
|
|
44
|
-
this.changeSetComputer = new
|
|
45
|
-
this.changeSetPersister = new
|
|
45
|
+
this.changeSetComputer = new ChangeSetComputer(this.em.getValidator(), 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.getValidator(), this.em.config);
|
|
46
47
|
}
|
|
47
48
|
merge(entity, visited) {
|
|
48
|
-
const wrapped =
|
|
49
|
+
const wrapped = helper(entity);
|
|
49
50
|
wrapped.__em = this.em;
|
|
50
51
|
if (!wrapped.hasPrimaryKey()) {
|
|
51
52
|
return;
|
|
@@ -62,41 +63,41 @@ class UnitOfWork {
|
|
|
62
63
|
wrapped.__originalEntityData = this.comparator.prepareEntity(entity);
|
|
63
64
|
wrapped.__touched = false;
|
|
64
65
|
}
|
|
65
|
-
this.cascade(entity,
|
|
66
|
+
this.cascade(entity, Cascade.MERGE, visited ?? new Set());
|
|
66
67
|
}
|
|
67
68
|
/**
|
|
68
69
|
* @internal
|
|
69
70
|
*/
|
|
70
71
|
register(entity, data, options) {
|
|
71
72
|
this.identityMap.store(entity);
|
|
72
|
-
|
|
73
|
+
EntityHelper.ensurePropagation(entity);
|
|
73
74
|
if (options?.newEntity) {
|
|
74
75
|
return entity;
|
|
75
76
|
}
|
|
76
77
|
const forceUndefined = this.em.config.get('forceUndefined');
|
|
77
|
-
const wrapped =
|
|
78
|
+
const wrapped = helper(entity);
|
|
78
79
|
if (options?.loaded && wrapped.__initialized && !wrapped.__onLoadFired) {
|
|
79
80
|
this.loadedEntities.add(entity);
|
|
80
81
|
}
|
|
81
82
|
wrapped.__em ??= this.em;
|
|
82
83
|
wrapped.__managed = true;
|
|
83
84
|
if (data && (options?.refresh || !wrapped.__originalEntityData)) {
|
|
84
|
-
for (const key of
|
|
85
|
+
for (const key of Utils.keys(data)) {
|
|
85
86
|
const prop = wrapped.__meta.properties[key];
|
|
86
87
|
if (!prop) {
|
|
87
88
|
continue;
|
|
88
89
|
}
|
|
89
90
|
wrapped.__loadedProperties.add(key);
|
|
90
|
-
if ([
|
|
91
|
-
data[prop.name] =
|
|
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);
|
|
92
93
|
}
|
|
93
|
-
else if (prop.kind ===
|
|
94
|
+
else if (prop.kind === ReferenceKind.EMBEDDED && !prop.object && Utils.isPlainObject(data[prop.name])) {
|
|
94
95
|
for (const p of prop.targetMeta.props) {
|
|
95
|
-
/*
|
|
96
|
+
/* v8 ignore next */
|
|
96
97
|
const prefix = prop.prefix === false ? '' : prop.prefix === true ? prop.name + '_' : prop.prefix;
|
|
97
98
|
data[prefix + p.name] = data[prop.name][p.name];
|
|
98
99
|
}
|
|
99
|
-
data[prop.name] =
|
|
100
|
+
data[prop.name] = Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta.primaryKeys, true);
|
|
100
101
|
}
|
|
101
102
|
if (forceUndefined) {
|
|
102
103
|
if (data[key] === null) {
|
|
@@ -114,9 +115,9 @@ class UnitOfWork {
|
|
|
114
115
|
*/
|
|
115
116
|
async dispatchOnLoadEvent() {
|
|
116
117
|
for (const entity of this.loadedEntities) {
|
|
117
|
-
if (this.eventManager.hasListeners(
|
|
118
|
-
await this.eventManager.dispatchEvent(
|
|
119
|
-
|
|
118
|
+
if (this.eventManager.hasListeners(EventType.onLoad, entity.__meta)) {
|
|
119
|
+
await this.eventManager.dispatchEvent(EventType.onLoad, { entity, meta: entity.__meta, em: this.em });
|
|
120
|
+
helper(entity).__onLoadFired = true;
|
|
120
121
|
}
|
|
121
122
|
}
|
|
122
123
|
this.loadedEntities.clear();
|
|
@@ -134,8 +135,8 @@ class UnitOfWork {
|
|
|
134
135
|
hash = '' + id;
|
|
135
136
|
}
|
|
136
137
|
else {
|
|
137
|
-
const keys = Array.isArray(id) ?
|
|
138
|
-
hash =
|
|
138
|
+
const keys = Array.isArray(id) ? Utils.flatten(id) : [id];
|
|
139
|
+
hash = Utils.getPrimaryKeyHash(keys);
|
|
139
140
|
}
|
|
140
141
|
schema ??= meta.schema ?? this.em.config.getSchema();
|
|
141
142
|
if (schema) {
|
|
@@ -144,7 +145,7 @@ class UnitOfWork {
|
|
|
144
145
|
return this.identityMap.getByHash(meta, hash);
|
|
145
146
|
}
|
|
146
147
|
tryGetById(entityName, where, schema, strict = true) {
|
|
147
|
-
const pk =
|
|
148
|
+
const pk = Utils.extractPK(where, this.metadata.find(entityName), strict);
|
|
148
149
|
if (!pk) {
|
|
149
150
|
return null;
|
|
150
151
|
}
|
|
@@ -160,7 +161,7 @@ class UnitOfWork {
|
|
|
160
161
|
* Returns stored snapshot of entity state that is used for change set computation.
|
|
161
162
|
*/
|
|
162
163
|
getOriginalEntityData(entity) {
|
|
163
|
-
return
|
|
164
|
+
return helper(entity).__originalEntityData;
|
|
164
165
|
}
|
|
165
166
|
getPersistStack() {
|
|
166
167
|
return this.persistStack;
|
|
@@ -185,7 +186,7 @@ class UnitOfWork {
|
|
|
185
186
|
return true;
|
|
186
187
|
}
|
|
187
188
|
for (const entity of this.identityMap.getStore(meta).values()) {
|
|
188
|
-
if (
|
|
189
|
+
if (helper(entity).__initialized && helper(entity).isTouched()) {
|
|
189
190
|
return true;
|
|
190
191
|
}
|
|
191
192
|
}
|
|
@@ -195,9 +196,9 @@ class UnitOfWork {
|
|
|
195
196
|
this.queuedActions.clear();
|
|
196
197
|
}
|
|
197
198
|
computeChangeSet(entity, type) {
|
|
198
|
-
const wrapped =
|
|
199
|
+
const wrapped = helper(entity);
|
|
199
200
|
if (type) {
|
|
200
|
-
this.changeSets.set(entity, new
|
|
201
|
+
this.changeSets.set(entity, new ChangeSet(entity, type, {}, wrapped.__meta));
|
|
201
202
|
return;
|
|
202
203
|
}
|
|
203
204
|
const cs = this.changeSetComputer.computeChangeSet(entity);
|
|
@@ -216,19 +217,18 @@ class UnitOfWork {
|
|
|
216
217
|
return;
|
|
217
218
|
}
|
|
218
219
|
const cs = this.changeSetComputer.computeChangeSet(entity);
|
|
219
|
-
/* istanbul ignore else */
|
|
220
220
|
if (cs && !this.checkUniqueProps(cs)) {
|
|
221
221
|
Object.assign(changeSet.payload, cs.payload);
|
|
222
|
-
|
|
223
|
-
|
|
222
|
+
helper(entity).__originalEntityData = this.comparator.prepareEntity(entity);
|
|
223
|
+
helper(entity).__touched = false;
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
226
|
persist(entity, visited, options = {}) {
|
|
227
|
-
|
|
227
|
+
EntityHelper.ensurePropagation(entity);
|
|
228
228
|
if (options.checkRemoveStack && this.removeStack.has(entity)) {
|
|
229
229
|
return;
|
|
230
230
|
}
|
|
231
|
-
const wrapped =
|
|
231
|
+
const wrapped = helper(entity);
|
|
232
232
|
this.persistStack.add(entity);
|
|
233
233
|
this.queuedActions.add(wrapped.__meta.className);
|
|
234
234
|
this.removeStack.delete(entity);
|
|
@@ -236,43 +236,43 @@ class UnitOfWork {
|
|
|
236
236
|
this.identityMap.store(entity);
|
|
237
237
|
}
|
|
238
238
|
if (options.cascade ?? true) {
|
|
239
|
-
this.cascade(entity,
|
|
239
|
+
this.cascade(entity, Cascade.PERSIST, visited, options);
|
|
240
240
|
}
|
|
241
241
|
}
|
|
242
242
|
remove(entity, visited, options = {}) {
|
|
243
243
|
// allow removing not managed entities if they are not part of the persist stack
|
|
244
|
-
if (
|
|
244
|
+
if (helper(entity).__managed || !this.persistStack.has(entity)) {
|
|
245
245
|
this.removeStack.add(entity);
|
|
246
|
-
this.queuedActions.add(
|
|
246
|
+
this.queuedActions.add(helper(entity).__meta.className);
|
|
247
247
|
}
|
|
248
248
|
else {
|
|
249
249
|
this.persistStack.delete(entity);
|
|
250
250
|
this.identityMap.delete(entity);
|
|
251
251
|
}
|
|
252
252
|
// remove from referencing relations that are nullable
|
|
253
|
-
for (const prop of
|
|
253
|
+
for (const prop of helper(entity).__meta.bidirectionalRelations) {
|
|
254
254
|
const inverseProp = prop.mappedBy || prop.inversedBy;
|
|
255
|
-
const relation =
|
|
255
|
+
const relation = Reference.unwrapReference(entity[prop.name]);
|
|
256
256
|
const prop2 = prop.targetMeta.properties[inverseProp];
|
|
257
|
-
if (prop.kind ===
|
|
257
|
+
if (prop.kind === ReferenceKind.ONE_TO_MANY && prop2.nullable && Utils.isCollection(relation)) {
|
|
258
258
|
for (const item of relation.getItems(false)) {
|
|
259
259
|
delete item[inverseProp];
|
|
260
260
|
}
|
|
261
261
|
continue;
|
|
262
262
|
}
|
|
263
263
|
const target = relation && relation[inverseProp];
|
|
264
|
-
if (relation &&
|
|
264
|
+
if (relation && Utils.isCollection(target)) {
|
|
265
265
|
target.removeWithoutPropagation(entity);
|
|
266
266
|
}
|
|
267
267
|
}
|
|
268
268
|
if (options.cascade ?? true) {
|
|
269
|
-
this.cascade(entity,
|
|
269
|
+
this.cascade(entity, Cascade.REMOVE, visited);
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
272
|
async commit() {
|
|
273
273
|
if (this.working) {
|
|
274
274
|
if (insideFlush.getStore()) {
|
|
275
|
-
throw
|
|
275
|
+
throw ValidationError.cannotCommit();
|
|
276
276
|
}
|
|
277
277
|
return new Promise((resolve, reject) => {
|
|
278
278
|
this.flushQueue.push(() => {
|
|
@@ -297,15 +297,15 @@ class UnitOfWork {
|
|
|
297
297
|
async doCommit() {
|
|
298
298
|
const oldTx = this.em.getTransactionContext();
|
|
299
299
|
try {
|
|
300
|
-
await this.eventManager.dispatchEvent(
|
|
300
|
+
await this.eventManager.dispatchEvent(EventType.beforeFlush, { em: this.em, uow: this });
|
|
301
301
|
this.computeChangeSets();
|
|
302
302
|
for (const cs of this.changeSets.values()) {
|
|
303
303
|
cs.entity.__helper.__processing = true;
|
|
304
304
|
}
|
|
305
|
-
await this.eventManager.dispatchEvent(
|
|
305
|
+
await this.eventManager.dispatchEvent(EventType.onFlush, { em: this.em, uow: this });
|
|
306
306
|
// nothing to do, do not start transaction
|
|
307
307
|
if (this.changeSets.size === 0 && this.collectionUpdates.size === 0 && this.extraUpdates.size === 0) {
|
|
308
|
-
return void await this.eventManager.dispatchEvent(
|
|
308
|
+
return void (await this.eventManager.dispatchEvent(EventType.afterFlush, { em: this.em, uow: this }));
|
|
309
309
|
}
|
|
310
310
|
const groups = this.getChangeSetGroups();
|
|
311
311
|
const platform = this.em.getPlatform();
|
|
@@ -313,7 +313,7 @@ class UnitOfWork {
|
|
|
313
313
|
if (runInTransaction) {
|
|
314
314
|
await this.em.getConnection('write').transactional(trx => this.persistToDatabase(groups, trx), {
|
|
315
315
|
ctx: oldTx,
|
|
316
|
-
eventBroadcaster: new
|
|
316
|
+
eventBroadcaster: new TransactionEventBroadcaster(this.em),
|
|
317
317
|
});
|
|
318
318
|
}
|
|
319
319
|
else {
|
|
@@ -323,18 +323,18 @@ class UnitOfWork {
|
|
|
323
323
|
for (const cs of this.changeSets.values()) {
|
|
324
324
|
cs.entity.__helper.__processing = false;
|
|
325
325
|
}
|
|
326
|
-
await this.eventManager.dispatchEvent(
|
|
326
|
+
await this.eventManager.dispatchEvent(EventType.afterFlush, { em: this.em, uow: this });
|
|
327
327
|
}
|
|
328
328
|
finally {
|
|
329
329
|
this.resetTransaction(oldTx);
|
|
330
330
|
}
|
|
331
331
|
}
|
|
332
332
|
async lock(entity, options) {
|
|
333
|
-
if (!this.getById(entity.constructor.name,
|
|
334
|
-
throw
|
|
333
|
+
if (!this.getById(entity.constructor.name, helper(entity).__primaryKeys, helper(entity).__schema)) {
|
|
334
|
+
throw ValidationError.entityNotManaged(entity);
|
|
335
335
|
}
|
|
336
336
|
const meta = this.metadata.find(entity.constructor.name);
|
|
337
|
-
if (options.lockMode ===
|
|
337
|
+
if (options.lockMode === LockMode.OPTIMISTIC) {
|
|
338
338
|
await this.lockOptimistic(entity, meta, options.lockVersion);
|
|
339
339
|
}
|
|
340
340
|
else if (options.lockMode != null) {
|
|
@@ -348,21 +348,21 @@ class UnitOfWork {
|
|
|
348
348
|
}
|
|
349
349
|
unsetIdentity(entity) {
|
|
350
350
|
this.identityMap.delete(entity);
|
|
351
|
-
const wrapped =
|
|
351
|
+
const wrapped = helper(entity);
|
|
352
352
|
const serializedPK = wrapped.getSerializedPrimaryKey();
|
|
353
353
|
// remove references of this entity in all managed entities, otherwise flushing could reinsert the entity
|
|
354
354
|
for (const { meta, prop } of wrapped.__meta.referencingProperties) {
|
|
355
355
|
for (const referrer of this.identityMap.getStore(meta).values()) {
|
|
356
|
-
const rel =
|
|
357
|
-
if (
|
|
356
|
+
const rel = Reference.unwrapReference(referrer[prop.name]);
|
|
357
|
+
if (Utils.isCollection(rel)) {
|
|
358
358
|
rel.removeWithoutPropagation(entity);
|
|
359
359
|
}
|
|
360
|
-
else if (rel && (prop.mapToPk ?
|
|
360
|
+
else if (rel && (prop.mapToPk ? helper(this.em.getReference(prop.type, rel)).getSerializedPrimaryKey() === serializedPK : rel === entity)) {
|
|
361
361
|
if (prop.formula) {
|
|
362
362
|
delete referrer[prop.name];
|
|
363
363
|
}
|
|
364
364
|
else {
|
|
365
|
-
delete
|
|
365
|
+
delete helper(referrer).__data[prop.name];
|
|
366
366
|
}
|
|
367
367
|
}
|
|
368
368
|
}
|
|
@@ -376,15 +376,15 @@ class UnitOfWork {
|
|
|
376
376
|
this.changeSets.clear();
|
|
377
377
|
const visited = new Set();
|
|
378
378
|
for (const entity of this.removeStack) {
|
|
379
|
-
this.cascade(entity,
|
|
379
|
+
this.cascade(entity, Cascade.REMOVE, visited);
|
|
380
380
|
}
|
|
381
381
|
visited.clear();
|
|
382
382
|
for (const entity of this.persistStack) {
|
|
383
|
-
this.cascade(entity,
|
|
383
|
+
this.cascade(entity, Cascade.PERSIST, visited, { checkRemoveStack: true });
|
|
384
384
|
}
|
|
385
385
|
for (const entity of this.identityMap) {
|
|
386
386
|
if (!this.removeStack.has(entity) && !this.persistStack.has(entity) && !this.orphanRemoveStack.has(entity)) {
|
|
387
|
-
this.cascade(entity,
|
|
387
|
+
this.cascade(entity, Cascade.PERSIST, visited, { checkRemoveStack: true });
|
|
388
388
|
}
|
|
389
389
|
}
|
|
390
390
|
visited.clear();
|
|
@@ -392,34 +392,34 @@ class UnitOfWork {
|
|
|
392
392
|
this.findNewEntities(entity, visited);
|
|
393
393
|
}
|
|
394
394
|
for (const entity of this.orphanRemoveStack) {
|
|
395
|
-
if (!
|
|
395
|
+
if (!helper(entity).__processing) {
|
|
396
396
|
this.removeStack.add(entity);
|
|
397
397
|
}
|
|
398
398
|
}
|
|
399
399
|
// Check insert stack if there are any entities matching something from delete stack. This can happen when recreating entities.
|
|
400
400
|
const inserts = {};
|
|
401
401
|
for (const cs of this.changeSets.values()) {
|
|
402
|
-
if (cs.type ===
|
|
402
|
+
if (cs.type === ChangeSetType.CREATE) {
|
|
403
403
|
inserts[cs.meta.className] ??= [];
|
|
404
404
|
inserts[cs.meta.className].push(cs);
|
|
405
405
|
}
|
|
406
406
|
}
|
|
407
407
|
for (const cs of this.changeSets.values()) {
|
|
408
|
-
if (cs.type ===
|
|
408
|
+
if (cs.type === ChangeSetType.UPDATE) {
|
|
409
409
|
this.findEarlyUpdates(cs, inserts[cs.meta.className]);
|
|
410
410
|
}
|
|
411
411
|
}
|
|
412
412
|
for (const entity of this.removeStack) {
|
|
413
|
-
const wrapped =
|
|
414
|
-
/*
|
|
413
|
+
const wrapped = helper(entity);
|
|
414
|
+
/* v8 ignore next 3 */
|
|
415
415
|
if (wrapped.__processing) {
|
|
416
416
|
continue;
|
|
417
417
|
}
|
|
418
418
|
const deletePkHash = [wrapped.getSerializedPrimaryKey(), ...this.expandUniqueProps(entity)];
|
|
419
|
-
let type =
|
|
419
|
+
let type = ChangeSetType.DELETE;
|
|
420
420
|
for (const cs of inserts[wrapped.__meta.className] ?? []) {
|
|
421
421
|
if (deletePkHash.some(hash => hash === cs.getSerializedPrimaryKey() || this.expandUniqueProps(cs.entity).find(child => hash === child))) {
|
|
422
|
-
type =
|
|
422
|
+
type = ChangeSetType.DELETE_EARLY;
|
|
423
423
|
}
|
|
424
424
|
}
|
|
425
425
|
this.computeChangeSet(entity, type);
|
|
@@ -430,7 +430,7 @@ class UnitOfWork {
|
|
|
430
430
|
return;
|
|
431
431
|
}
|
|
432
432
|
let conflicts = false;
|
|
433
|
-
let type =
|
|
433
|
+
let type = ChangeSetType.UPDATE;
|
|
434
434
|
if (!props.some(prop => prop.name in changeSet.payload)) {
|
|
435
435
|
return;
|
|
436
436
|
}
|
|
@@ -439,7 +439,7 @@ class UnitOfWork {
|
|
|
439
439
|
if (prop.name in cs.payload && cs.rootName === changeSet.rootName && cs.type === changeSet.type) {
|
|
440
440
|
conflicts = true;
|
|
441
441
|
if (changeSet.payload[prop.name] == null) {
|
|
442
|
-
type =
|
|
442
|
+
type = ChangeSetType.UPDATE_EARLY;
|
|
443
443
|
}
|
|
444
444
|
}
|
|
445
445
|
}
|
|
@@ -455,15 +455,15 @@ class UnitOfWork {
|
|
|
455
455
|
}
|
|
456
456
|
scheduleOrphanRemoval(entity, visited) {
|
|
457
457
|
if (entity) {
|
|
458
|
-
|
|
458
|
+
helper(entity).__em = this.em;
|
|
459
459
|
this.orphanRemoveStack.add(entity);
|
|
460
460
|
this.queuedActions.add(entity.__meta.className);
|
|
461
|
-
this.cascade(entity,
|
|
461
|
+
this.cascade(entity, Cascade.SCHEDULE_ORPHAN_REMOVAL, visited);
|
|
462
462
|
}
|
|
463
463
|
}
|
|
464
464
|
cancelOrphanRemoval(entity, visited) {
|
|
465
465
|
this.orphanRemoveStack.delete(entity);
|
|
466
|
-
this.cascade(entity,
|
|
466
|
+
this.cascade(entity, Cascade.CANCEL_ORPHAN_REMOVAL, visited);
|
|
467
467
|
}
|
|
468
468
|
getOrphanRemoveStack() {
|
|
469
469
|
return this.orphanRemoveStack;
|
|
@@ -477,7 +477,7 @@ class UnitOfWork {
|
|
|
477
477
|
}
|
|
478
478
|
visited.add(entity);
|
|
479
479
|
processed.add(entity);
|
|
480
|
-
const wrapped =
|
|
480
|
+
const wrapped = helper(entity);
|
|
481
481
|
if (wrapped.__processing || this.removeStack.has(entity) || this.orphanRemoveStack.has(entity)) {
|
|
482
482
|
return;
|
|
483
483
|
}
|
|
@@ -485,9 +485,9 @@ class UnitOfWork {
|
|
|
485
485
|
wrapped.__schema ??= this.em.schema;
|
|
486
486
|
this.initIdentifier(entity);
|
|
487
487
|
for (const prop of wrapped.__meta.relations) {
|
|
488
|
-
const targets =
|
|
488
|
+
const targets = Utils.unwrapProperty(entity, wrapped.__meta, prop);
|
|
489
489
|
for (const [target] of targets) {
|
|
490
|
-
const kind =
|
|
490
|
+
const kind = Reference.unwrapReference(target);
|
|
491
491
|
this.processReference(entity, prop, kind, visited, processed, idx);
|
|
492
492
|
}
|
|
493
493
|
}
|
|
@@ -500,37 +500,37 @@ class UnitOfWork {
|
|
|
500
500
|
* Returns `true` when the change set should be skipped as it will be empty after the extra update.
|
|
501
501
|
*/
|
|
502
502
|
checkUniqueProps(changeSet) {
|
|
503
|
-
if (changeSet.type !==
|
|
503
|
+
if (changeSet.type !== ChangeSetType.UPDATE) {
|
|
504
504
|
return false;
|
|
505
505
|
}
|
|
506
506
|
// when changing a unique nullable property (or a 1:1 relation), we can't do it in a single
|
|
507
507
|
// query as it would cause unique constraint violations
|
|
508
508
|
const uniqueProps = changeSet.meta.uniqueProps.filter(prop => {
|
|
509
|
-
return (prop.nullable || changeSet.type !==
|
|
509
|
+
return (prop.nullable || changeSet.type !== ChangeSetType.CREATE);
|
|
510
510
|
});
|
|
511
511
|
this.scheduleExtraUpdate(changeSet, uniqueProps);
|
|
512
|
-
return changeSet.type ===
|
|
512
|
+
return changeSet.type === ChangeSetType.UPDATE && !Utils.hasObjectKeys(changeSet.payload);
|
|
513
513
|
}
|
|
514
514
|
expandUniqueProps(entity) {
|
|
515
|
-
const wrapped =
|
|
515
|
+
const wrapped = helper(entity);
|
|
516
516
|
if (!wrapped.__meta.hasUniqueProps) {
|
|
517
517
|
return [];
|
|
518
518
|
}
|
|
519
519
|
const simpleUniqueHashes = wrapped.__meta.uniqueProps.map(prop => {
|
|
520
520
|
if (entity[prop.name] != null) {
|
|
521
|
-
return prop.kind ===
|
|
521
|
+
return prop.kind === ReferenceKind.SCALAR || prop.mapToPk ? entity[prop.name] : helper(entity[prop.name]).getSerializedPrimaryKey();
|
|
522
522
|
}
|
|
523
523
|
if (wrapped.__originalEntityData?.[prop.name] != null) {
|
|
524
|
-
return
|
|
524
|
+
return Utils.getPrimaryKeyHash(Utils.asArray(wrapped.__originalEntityData[prop.name]));
|
|
525
525
|
}
|
|
526
526
|
return undefined;
|
|
527
527
|
}).filter(i => i);
|
|
528
528
|
const compoundUniqueHashes = wrapped.__meta.uniques.map(unique => {
|
|
529
|
-
const props =
|
|
529
|
+
const props = Utils.asArray(unique.properties);
|
|
530
530
|
if (props.every(prop => entity[prop] != null)) {
|
|
531
|
-
return
|
|
531
|
+
return Utils.getPrimaryKeyHash(props.map(p => {
|
|
532
532
|
const prop = wrapped.__meta.properties[p];
|
|
533
|
-
return prop.kind ===
|
|
533
|
+
return prop.kind === ReferenceKind.SCALAR || prop.mapToPk ? entity[prop.name] : helper(entity[prop.name]).getSerializedPrimaryKey();
|
|
534
534
|
}));
|
|
535
535
|
}
|
|
536
536
|
return undefined;
|
|
@@ -538,32 +538,41 @@ class UnitOfWork {
|
|
|
538
538
|
return simpleUniqueHashes.concat(compoundUniqueHashes);
|
|
539
539
|
}
|
|
540
540
|
initIdentifier(entity) {
|
|
541
|
-
const wrapped = entity &&
|
|
541
|
+
const wrapped = entity && helper(entity);
|
|
542
542
|
if (!wrapped || wrapped.__identifier || wrapped.hasPrimaryKey()) {
|
|
543
543
|
return;
|
|
544
544
|
}
|
|
545
|
-
const
|
|
546
|
-
|
|
547
|
-
|
|
545
|
+
const pks = wrapped.__meta.getPrimaryProps();
|
|
546
|
+
const idents = [];
|
|
547
|
+
for (const pk of pks) {
|
|
548
|
+
if (pk.kind === ReferenceKind.SCALAR) {
|
|
549
|
+
idents.push(new EntityIdentifier(entity[pk.name]));
|
|
550
|
+
}
|
|
551
|
+
else if (entity[pk.name]) {
|
|
552
|
+
this.initIdentifier(entity[pk.name]);
|
|
553
|
+
idents.push(helper(entity[pk.name])?.__identifier);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
if (pks.length === 1) {
|
|
557
|
+
wrapped.__identifier = idents[0];
|
|
548
558
|
}
|
|
549
|
-
else
|
|
550
|
-
|
|
551
|
-
wrapped.__identifier = (0, entity_1.helper)(entity[pk.name])?.__identifier;
|
|
559
|
+
else {
|
|
560
|
+
wrapped.__identifier = idents;
|
|
552
561
|
}
|
|
553
562
|
}
|
|
554
563
|
processReference(parent, prop, kind, visited, processed, idx) {
|
|
555
|
-
const isToOne = prop.kind ===
|
|
556
|
-
if (isToOne &&
|
|
564
|
+
const isToOne = prop.kind === ReferenceKind.MANY_TO_ONE || prop.kind === ReferenceKind.ONE_TO_ONE;
|
|
565
|
+
if (isToOne && Utils.isEntity(kind)) {
|
|
557
566
|
return this.processToOneReference(kind, visited, processed, idx);
|
|
558
567
|
}
|
|
559
|
-
if (
|
|
568
|
+
if (Utils.isCollection(kind)) {
|
|
560
569
|
kind.getItems(false)
|
|
561
570
|
.filter(item => !item.__helper.__originalEntityData)
|
|
562
571
|
.forEach(item => {
|
|
563
572
|
// propagate schema from parent
|
|
564
|
-
item.__helper.__schema ??=
|
|
573
|
+
item.__helper.__schema ??= helper(parent).__schema;
|
|
565
574
|
});
|
|
566
|
-
if (prop.kind ===
|
|
575
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY && kind.isDirty()) {
|
|
567
576
|
this.processToManyReference(kind, visited, processed, parent, prop);
|
|
568
577
|
}
|
|
569
578
|
}
|
|
@@ -575,8 +584,8 @@ class UnitOfWork {
|
|
|
575
584
|
}
|
|
576
585
|
processToManyReference(collection, visited, processed, parent, prop) {
|
|
577
586
|
if (this.isCollectionSelfReferenced(collection, processed)) {
|
|
578
|
-
this.extraUpdates.add([parent, prop.name, collection, undefined,
|
|
579
|
-
const coll = new
|
|
587
|
+
this.extraUpdates.add([parent, prop.name, collection, undefined, ChangeSetType.UPDATE]);
|
|
588
|
+
const coll = new Collection(parent);
|
|
580
589
|
coll.property = prop;
|
|
581
590
|
parent[prop.name] = coll;
|
|
582
591
|
return;
|
|
@@ -599,14 +608,21 @@ class UnitOfWork {
|
|
|
599
608
|
const current = this.comparator.prepareEntity(changeSet.entity);
|
|
600
609
|
const diff = this.comparator.diffEntities(changeSet.name, copy, current);
|
|
601
610
|
Object.assign(changeSet.payload, diff);
|
|
602
|
-
const wrapped =
|
|
603
|
-
if (wrapped.__identifier
|
|
604
|
-
|
|
611
|
+
const wrapped = helper(changeSet.entity);
|
|
612
|
+
if (wrapped.__identifier) {
|
|
613
|
+
const idents = Utils.asArray(wrapped.__identifier);
|
|
614
|
+
let i = 0;
|
|
615
|
+
for (const pk of wrapped.__meta.primaryKeys) {
|
|
616
|
+
if (diff[pk]) {
|
|
617
|
+
idents[i].setValue(diff[pk]);
|
|
618
|
+
}
|
|
619
|
+
i++;
|
|
620
|
+
}
|
|
605
621
|
}
|
|
606
622
|
}
|
|
607
623
|
postCommitCleanup() {
|
|
608
624
|
for (const cs of this.changeSets.values()) {
|
|
609
|
-
const wrapped =
|
|
625
|
+
const wrapped = helper(cs.entity);
|
|
610
626
|
wrapped.__processing = false;
|
|
611
627
|
delete wrapped.__pk;
|
|
612
628
|
}
|
|
@@ -625,23 +641,23 @@ class UnitOfWork {
|
|
|
625
641
|
}
|
|
626
642
|
visited.add(entity);
|
|
627
643
|
switch (type) {
|
|
628
|
-
case
|
|
644
|
+
case Cascade.PERSIST:
|
|
629
645
|
this.persist(entity, visited, options);
|
|
630
646
|
break;
|
|
631
|
-
case
|
|
647
|
+
case Cascade.MERGE:
|
|
632
648
|
this.merge(entity, visited);
|
|
633
649
|
break;
|
|
634
|
-
case
|
|
650
|
+
case Cascade.REMOVE:
|
|
635
651
|
this.remove(entity, visited, options);
|
|
636
652
|
break;
|
|
637
|
-
case
|
|
653
|
+
case Cascade.SCHEDULE_ORPHAN_REMOVAL:
|
|
638
654
|
this.scheduleOrphanRemoval(entity, visited);
|
|
639
655
|
break;
|
|
640
|
-
case
|
|
656
|
+
case Cascade.CANCEL_ORPHAN_REMOVAL:
|
|
641
657
|
this.cancelOrphanRemoval(entity, visited);
|
|
642
658
|
break;
|
|
643
659
|
}
|
|
644
|
-
for (const prop of
|
|
660
|
+
for (const prop of helper(entity).__meta.relations) {
|
|
645
661
|
this.cascadeReference(entity, prop, type, visited, options);
|
|
646
662
|
}
|
|
647
663
|
}
|
|
@@ -650,72 +666,72 @@ class UnitOfWork {
|
|
|
650
666
|
if (!this.shouldCascade(prop, type)) {
|
|
651
667
|
return;
|
|
652
668
|
}
|
|
653
|
-
const kind =
|
|
654
|
-
if ([
|
|
669
|
+
const kind = Reference.unwrapReference(entity[prop.name]);
|
|
670
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils.isEntity(kind)) {
|
|
655
671
|
return this.cascade(kind, type, visited, options);
|
|
656
672
|
}
|
|
657
673
|
const collection = kind;
|
|
658
|
-
if ([
|
|
674
|
+
if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind) && collection) {
|
|
659
675
|
for (const item of collection.getItems(false)) {
|
|
660
676
|
this.cascade(item, type, visited, options);
|
|
661
677
|
}
|
|
662
678
|
}
|
|
663
679
|
}
|
|
664
680
|
isCollectionSelfReferenced(collection, processed) {
|
|
665
|
-
const filtered = collection.getItems(false).filter(item => !
|
|
681
|
+
const filtered = collection.getItems(false).filter(item => !helper(item).__originalEntityData);
|
|
666
682
|
return filtered.some(items => processed.has(items));
|
|
667
683
|
}
|
|
668
684
|
shouldCascade(prop, type) {
|
|
669
|
-
if ([
|
|
685
|
+
if ([Cascade.REMOVE, Cascade.SCHEDULE_ORPHAN_REMOVAL, Cascade.CANCEL_ORPHAN_REMOVAL, Cascade.ALL].includes(type) && prop.orphanRemoval) {
|
|
670
686
|
return true;
|
|
671
687
|
}
|
|
672
688
|
// ignore user settings for merge, it is kept only for back compatibility, this should have never been configurable
|
|
673
|
-
if (type ===
|
|
689
|
+
if (type === Cascade.MERGE) {
|
|
674
690
|
return true;
|
|
675
691
|
}
|
|
676
|
-
return prop.cascade && (prop.cascade.includes(type) || prop.cascade.includes(
|
|
692
|
+
return prop.cascade && (prop.cascade.includes(type) || prop.cascade.includes(Cascade.ALL));
|
|
677
693
|
}
|
|
678
694
|
async lockPessimistic(entity, options) {
|
|
679
695
|
if (!this.em.isInTransaction()) {
|
|
680
|
-
throw
|
|
696
|
+
throw ValidationError.transactionRequired();
|
|
681
697
|
}
|
|
682
698
|
await this.em.getDriver().lockPessimistic(entity, { ctx: this.em.getTransactionContext(), ...options });
|
|
683
699
|
}
|
|
684
700
|
async lockOptimistic(entity, meta, version) {
|
|
685
701
|
if (!meta.versionProperty) {
|
|
686
|
-
throw
|
|
702
|
+
throw OptimisticLockError.notVersioned(meta);
|
|
687
703
|
}
|
|
688
|
-
if (!
|
|
704
|
+
if (!Utils.isDefined(version)) {
|
|
689
705
|
return;
|
|
690
706
|
}
|
|
691
|
-
const wrapped =
|
|
707
|
+
const wrapped = helper(entity);
|
|
692
708
|
if (!wrapped.__initialized) {
|
|
693
709
|
await wrapped.init();
|
|
694
710
|
}
|
|
695
711
|
const previousVersion = entity[meta.versionProperty];
|
|
696
712
|
if (previousVersion !== version) {
|
|
697
|
-
throw
|
|
713
|
+
throw OptimisticLockError.lockFailedVersionMismatch(entity, version, previousVersion);
|
|
698
714
|
}
|
|
699
715
|
}
|
|
700
716
|
fixMissingReference(entity, prop) {
|
|
701
717
|
const reference = entity[prop.name];
|
|
702
|
-
const kind =
|
|
703
|
-
if ([
|
|
704
|
-
if (!
|
|
718
|
+
const kind = Reference.unwrapReference(reference);
|
|
719
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && kind && !prop.mapToPk) {
|
|
720
|
+
if (!Utils.isEntity(kind)) {
|
|
705
721
|
entity[prop.name] = this.em.getReference(prop.type, kind, { wrapped: !!prop.ref });
|
|
706
722
|
}
|
|
707
|
-
else if (!
|
|
708
|
-
const pk =
|
|
723
|
+
else if (!helper(kind).__initialized && !helper(kind).__em) {
|
|
724
|
+
const pk = helper(kind).getPrimaryKey();
|
|
709
725
|
entity[prop.name] = this.em.getReference(prop.type, pk, { wrapped: !!prop.ref });
|
|
710
726
|
}
|
|
711
727
|
}
|
|
712
728
|
// perf: set the `Collection._property` to skip the getter, as it can be slow when there is a lot of relations
|
|
713
|
-
if (
|
|
729
|
+
if (Utils.isCollection(kind)) {
|
|
714
730
|
kind.property = prop;
|
|
715
731
|
}
|
|
716
|
-
const isCollection = [
|
|
732
|
+
const isCollection = [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind);
|
|
717
733
|
if (isCollection && Array.isArray(kind)) {
|
|
718
|
-
const collection = new
|
|
734
|
+
const collection = new Collection(entity);
|
|
719
735
|
collection.property = prop;
|
|
720
736
|
entity[prop.name] = collection;
|
|
721
737
|
collection.set(kind);
|
|
@@ -729,29 +745,29 @@ class UnitOfWork {
|
|
|
729
745
|
const commitOrderReversed = [...commitOrder].reverse();
|
|
730
746
|
// early delete - when we recreate entity in the same UoW, we need to issue those delete queries before inserts
|
|
731
747
|
for (const name of commitOrderReversed) {
|
|
732
|
-
await this.commitDeleteChangeSets(groups[
|
|
748
|
+
await this.commitDeleteChangeSets(groups[ChangeSetType.DELETE_EARLY].get(name) ?? [], ctx);
|
|
733
749
|
}
|
|
734
750
|
// early update - when we recreate entity in the same UoW, we need to issue those delete queries before inserts
|
|
735
751
|
for (const name of commitOrder) {
|
|
736
|
-
await this.commitUpdateChangeSets(groups[
|
|
752
|
+
await this.commitUpdateChangeSets(groups[ChangeSetType.UPDATE_EARLY].get(name) ?? [], ctx);
|
|
737
753
|
}
|
|
738
754
|
// extra updates
|
|
739
|
-
await this.commitExtraUpdates(
|
|
755
|
+
await this.commitExtraUpdates(ChangeSetType.UPDATE_EARLY, ctx);
|
|
740
756
|
// create
|
|
741
757
|
for (const name of commitOrder) {
|
|
742
|
-
await this.commitCreateChangeSets(groups[
|
|
758
|
+
await this.commitCreateChangeSets(groups[ChangeSetType.CREATE].get(name) ?? [], ctx);
|
|
743
759
|
}
|
|
744
760
|
// update
|
|
745
761
|
for (const name of commitOrder) {
|
|
746
|
-
await this.commitUpdateChangeSets(groups[
|
|
762
|
+
await this.commitUpdateChangeSets(groups[ChangeSetType.UPDATE].get(name) ?? [], ctx);
|
|
747
763
|
}
|
|
748
764
|
// extra updates
|
|
749
|
-
await this.commitExtraUpdates(
|
|
765
|
+
await this.commitExtraUpdates(ChangeSetType.UPDATE, ctx);
|
|
750
766
|
// collection updates
|
|
751
767
|
await this.commitCollectionUpdates(ctx);
|
|
752
768
|
// delete - entity deletions need to be in reverse commit order
|
|
753
769
|
for (const name of commitOrderReversed) {
|
|
754
|
-
await this.commitDeleteChangeSets(groups[
|
|
770
|
+
await this.commitDeleteChangeSets(groups[ChangeSetType.DELETE].get(name) ?? [], ctx);
|
|
755
771
|
}
|
|
756
772
|
// take snapshots of all persisted collections
|
|
757
773
|
const visited = new Set();
|
|
@@ -764,30 +780,30 @@ class UnitOfWork {
|
|
|
764
780
|
return;
|
|
765
781
|
}
|
|
766
782
|
const props = changeSets[0].meta.root.relations.filter(prop => {
|
|
767
|
-
return (prop.kind ===
|
|
768
|
-
|| prop.kind ===
|
|
769
|
-
|| (prop.kind ===
|
|
783
|
+
return (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner)
|
|
784
|
+
|| prop.kind === ReferenceKind.MANY_TO_ONE
|
|
785
|
+
|| (prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner && !this.platform.usesPivotTable());
|
|
770
786
|
});
|
|
771
787
|
for (const changeSet of changeSets) {
|
|
772
788
|
this.findExtraUpdates(changeSet, props);
|
|
773
|
-
await this.runHooks(
|
|
789
|
+
await this.runHooks(EventType.beforeCreate, changeSet, true);
|
|
774
790
|
}
|
|
775
791
|
await this.changeSetPersister.executeInserts(changeSets, { ctx });
|
|
776
792
|
for (const changeSet of changeSets) {
|
|
777
793
|
this.register(changeSet.entity, changeSet.payload, { refresh: true });
|
|
778
|
-
await this.runHooks(
|
|
794
|
+
await this.runHooks(EventType.afterCreate, changeSet);
|
|
779
795
|
}
|
|
780
796
|
}
|
|
781
797
|
findExtraUpdates(changeSet, props) {
|
|
782
798
|
for (const prop of props) {
|
|
783
799
|
const ref = changeSet.entity[prop.name];
|
|
784
|
-
if (!ref || prop.deferMode ===
|
|
800
|
+
if (!ref || prop.deferMode === DeferMode.INITIALLY_DEFERRED) {
|
|
785
801
|
continue;
|
|
786
802
|
}
|
|
787
|
-
if (
|
|
803
|
+
if (Utils.isCollection(ref)) {
|
|
788
804
|
ref.getItems(false).some(item => {
|
|
789
|
-
const cs = this.changeSets.get(
|
|
790
|
-
const isScheduledForInsert = cs && cs.type ===
|
|
805
|
+
const cs = this.changeSets.get(Reference.unwrapReference(item));
|
|
806
|
+
const isScheduledForInsert = cs && cs.type === ChangeSetType.CREATE && !cs.persisted;
|
|
791
807
|
if (isScheduledForInsert) {
|
|
792
808
|
this.scheduleExtraUpdate(changeSet, [prop]);
|
|
793
809
|
return true;
|
|
@@ -795,8 +811,8 @@ class UnitOfWork {
|
|
|
795
811
|
return false;
|
|
796
812
|
});
|
|
797
813
|
}
|
|
798
|
-
const cs = this.changeSets.get(
|
|
799
|
-
const isScheduledForInsert = cs && cs.type ===
|
|
814
|
+
const cs = this.changeSets.get(Reference.unwrapReference(ref));
|
|
815
|
+
const isScheduledForInsert = cs && cs.type === ChangeSetType.CREATE && !cs.persisted;
|
|
800
816
|
if (isScheduledForInsert) {
|
|
801
817
|
this.scheduleExtraUpdate(changeSet, [prop]);
|
|
802
818
|
}
|
|
@@ -805,14 +821,14 @@ class UnitOfWork {
|
|
|
805
821
|
findEarlyUpdates(changeSet, inserts = []) {
|
|
806
822
|
const props = changeSet.meta.uniqueProps;
|
|
807
823
|
for (const prop of props) {
|
|
808
|
-
const insert = inserts.find(c =>
|
|
824
|
+
const insert = inserts.find(c => Utils.equals(c.payload[prop.name], changeSet.originalEntity[prop.name]));
|
|
809
825
|
const propEmpty = changeSet.payload[prop.name] === null || changeSet.payload[prop.name] === undefined;
|
|
810
826
|
if (prop.name in changeSet.payload &&
|
|
811
827
|
insert &&
|
|
812
828
|
// We only want to update early if the unique property on the changeset is going to be empty, so that
|
|
813
829
|
// the previous unique value can be set on a different entity without constraint issues
|
|
814
830
|
propEmpty) {
|
|
815
|
-
changeSet.type =
|
|
831
|
+
changeSet.type = ChangeSetType.UPDATE_EARLY;
|
|
816
832
|
}
|
|
817
833
|
}
|
|
818
834
|
}
|
|
@@ -821,14 +837,14 @@ class UnitOfWork {
|
|
|
821
837
|
return;
|
|
822
838
|
}
|
|
823
839
|
for (const changeSet of changeSets) {
|
|
824
|
-
await this.runHooks(
|
|
840
|
+
await this.runHooks(EventType.beforeUpdate, changeSet, true);
|
|
825
841
|
}
|
|
826
842
|
await this.changeSetPersister.executeUpdates(changeSets, batched, { ctx });
|
|
827
843
|
for (const changeSet of changeSets) {
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
await this.runHooks(
|
|
844
|
+
helper(changeSet.entity).__originalEntityData = this.comparator.prepareEntity(changeSet.entity);
|
|
845
|
+
helper(changeSet.entity).__touched = false;
|
|
846
|
+
helper(changeSet.entity).__initialized = true;
|
|
847
|
+
await this.runHooks(EventType.afterUpdate, changeSet);
|
|
832
848
|
}
|
|
833
849
|
}
|
|
834
850
|
async commitDeleteChangeSets(changeSets, ctx) {
|
|
@@ -836,12 +852,12 @@ class UnitOfWork {
|
|
|
836
852
|
return;
|
|
837
853
|
}
|
|
838
854
|
for (const changeSet of changeSets) {
|
|
839
|
-
await this.runHooks(
|
|
855
|
+
await this.runHooks(EventType.beforeDelete, changeSet, true);
|
|
840
856
|
}
|
|
841
857
|
await this.changeSetPersister.executeDeletes(changeSets, { ctx });
|
|
842
858
|
for (const changeSet of changeSets) {
|
|
843
859
|
this.unsetIdentity(changeSet.entity);
|
|
844
|
-
await this.runHooks(
|
|
860
|
+
await this.runHooks(EventType.afterDelete, changeSet);
|
|
845
861
|
}
|
|
846
862
|
}
|
|
847
863
|
async commitExtraUpdates(type, ctx) {
|
|
@@ -877,14 +893,14 @@ class UnitOfWork {
|
|
|
877
893
|
collectionUpdates.push(coll);
|
|
878
894
|
}
|
|
879
895
|
}
|
|
880
|
-
else if (coll.property.kind ===
|
|
896
|
+
else if (coll.property.kind === ReferenceKind.ONE_TO_MANY && coll.getSnapshot() === undefined) {
|
|
881
897
|
collectionUpdates.push(coll);
|
|
882
898
|
}
|
|
883
|
-
else if (coll.property.kind ===
|
|
899
|
+
else if (coll.property.kind === ReferenceKind.MANY_TO_MANY && !coll.property.owner) {
|
|
884
900
|
collectionUpdates.push(coll);
|
|
885
901
|
}
|
|
886
902
|
}
|
|
887
|
-
await this.em.getDriver().syncCollections(collectionUpdates, { ctx });
|
|
903
|
+
await this.em.getDriver().syncCollections(collectionUpdates, { ctx, schema: this.em.schema });
|
|
888
904
|
for (const coll of this.collectionUpdates) {
|
|
889
905
|
coll.takeSnapshot();
|
|
890
906
|
}
|
|
@@ -894,11 +910,11 @@ class UnitOfWork {
|
|
|
894
910
|
*/
|
|
895
911
|
getChangeSetGroups() {
|
|
896
912
|
const groups = {
|
|
897
|
-
[
|
|
898
|
-
[
|
|
899
|
-
[
|
|
900
|
-
[
|
|
901
|
-
[
|
|
913
|
+
[ChangeSetType.CREATE]: new Map(),
|
|
914
|
+
[ChangeSetType.UPDATE]: new Map(),
|
|
915
|
+
[ChangeSetType.DELETE]: new Map(),
|
|
916
|
+
[ChangeSetType.UPDATE_EARLY]: new Map(),
|
|
917
|
+
[ChangeSetType.DELETE_EARLY]: new Map(),
|
|
902
918
|
};
|
|
903
919
|
for (const cs of this.changeSets.values()) {
|
|
904
920
|
const group = groups[cs.type];
|
|
@@ -911,7 +927,7 @@ class UnitOfWork {
|
|
|
911
927
|
return groups;
|
|
912
928
|
}
|
|
913
929
|
getCommitOrder() {
|
|
914
|
-
const calc = new
|
|
930
|
+
const calc = new CommitOrderCalculator();
|
|
915
931
|
const set = new Set();
|
|
916
932
|
this.changeSets.forEach(cs => set.add(cs.rootName));
|
|
917
933
|
set.forEach(entityName => calc.addNode(entityName));
|
|
@@ -938,16 +954,15 @@ class UnitOfWork {
|
|
|
938
954
|
return;
|
|
939
955
|
}
|
|
940
956
|
visited.add(entity);
|
|
941
|
-
|
|
957
|
+
helper(entity)?.__meta.relations.forEach(prop => {
|
|
942
958
|
const value = entity[prop.name];
|
|
943
|
-
if (
|
|
959
|
+
if (Utils.isCollection(value)) {
|
|
944
960
|
value.takeSnapshot();
|
|
945
961
|
}
|
|
946
962
|
// cascade to m:1 relations as we need to snapshot the 1:m inverse side (for `removeAll()` with orphan removal)
|
|
947
|
-
if (prop.kind ===
|
|
948
|
-
this.takeCollectionSnapshots(
|
|
963
|
+
if (prop.kind === ReferenceKind.MANY_TO_ONE && value) {
|
|
964
|
+
this.takeCollectionSnapshots(Reference.unwrapReference(value), visited);
|
|
949
965
|
}
|
|
950
966
|
});
|
|
951
967
|
}
|
|
952
968
|
}
|
|
953
|
-
exports.UnitOfWork = UnitOfWork;
|