@mikro-orm/core 7.0.0-dev.0 → 7.0.0-dev.2
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 +18 -13
- package/EntityManager.js +202 -202
- 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 +1 -1
- package/decorators/Check.js +5 -8
- package/decorators/CreateRequestContext.d.ts +1 -1
- package/decorators/CreateRequestContext.js +9 -13
- package/decorators/Embeddable.d.ts +1 -1
- package/decorators/Embeddable.js +3 -6
- package/decorators/Embedded.d.ts +1 -1
- package/decorators/Embedded.js +10 -12
- package/decorators/Entity.d.ts +2 -2
- package/decorators/Entity.js +5 -8
- package/decorators/Enum.d.ts +2 -2
- 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 -2
- package/decorators/Formula.js +7 -10
- package/decorators/Indexed.d.ts +2 -2
- package/decorators/Indexed.js +6 -10
- package/decorators/ManyToMany.d.ts +3 -3
- package/decorators/ManyToMany.js +10 -12
- package/decorators/ManyToOne.d.ts +3 -3
- package/decorators/ManyToOne.js +10 -12
- package/decorators/OneToMany.d.ts +3 -3
- package/decorators/OneToMany.js +11 -14
- package/decorators/OneToOne.d.ts +4 -4
- package/decorators/OneToOne.js +4 -7
- package/decorators/PrimaryKey.d.ts +2 -2
- package/decorators/PrimaryKey.js +10 -13
- package/decorators/Property.d.ts +5 -5
- 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 +12 -12
- package/drivers/IDatabaseDriver.js +1 -4
- package/drivers/index.d.ts +2 -2
- package/drivers/index.js +2 -18
- package/entity/ArrayCollection.d.ts +2 -2
- package/entity/ArrayCollection.js +29 -33
- package/entity/BaseEntity.d.ts +6 -6
- package/entity/BaseEntity.js +17 -21
- package/entity/Collection.d.ts +6 -6
- package/entity/Collection.js +40 -44
- package/entity/EntityAssigner.d.ts +2 -2
- package/entity/EntityAssigner.js +58 -63
- package/entity/EntityFactory.d.ts +3 -3
- package/entity/EntityFactory.js +58 -62
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +41 -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 +92 -96
- 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 +2 -2
- 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 +5 -4
- 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 +25 -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 +12 -4
- package/metadata/EntitySchema.js +41 -45
- package/metadata/MetadataDiscovery.d.ts +7 -7
- package/metadata/MetadataDiscovery.js +174 -177
- 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 +49 -53
- 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 +5 -14
- 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 +3 -3
- package/types/BigIntType.js +3 -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 +22 -20
- package/typings.js +34 -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 +44 -47
- 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 +171 -172
- 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 +7 -11
- 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 +3 -3
- 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 +48 -52
- 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 +16 -24
- 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 +89 -93
- 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,32 @@ 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
545
|
const pk = wrapped.__meta.getPrimaryProps()[0];
|
|
546
|
-
if (pk.kind ===
|
|
547
|
-
wrapped.__identifier = new
|
|
546
|
+
if (pk.kind === ReferenceKind.SCALAR) {
|
|
547
|
+
wrapped.__identifier = new EntityIdentifier();
|
|
548
548
|
}
|
|
549
549
|
else if (entity[pk.name]) {
|
|
550
550
|
this.initIdentifier(entity[pk.name]);
|
|
551
|
-
wrapped.__identifier =
|
|
551
|
+
wrapped.__identifier = helper(entity[pk.name])?.__identifier;
|
|
552
552
|
}
|
|
553
553
|
}
|
|
554
554
|
processReference(parent, prop, kind, visited, processed, idx) {
|
|
555
|
-
const isToOne = prop.kind ===
|
|
556
|
-
if (isToOne &&
|
|
555
|
+
const isToOne = prop.kind === ReferenceKind.MANY_TO_ONE || prop.kind === ReferenceKind.ONE_TO_ONE;
|
|
556
|
+
if (isToOne && Utils.isEntity(kind)) {
|
|
557
557
|
return this.processToOneReference(kind, visited, processed, idx);
|
|
558
558
|
}
|
|
559
|
-
if (
|
|
559
|
+
if (Utils.isCollection(kind)) {
|
|
560
560
|
kind.getItems(false)
|
|
561
561
|
.filter(item => !item.__helper.__originalEntityData)
|
|
562
562
|
.forEach(item => {
|
|
563
563
|
// propagate schema from parent
|
|
564
|
-
item.__helper.__schema ??=
|
|
564
|
+
item.__helper.__schema ??= helper(parent).__schema;
|
|
565
565
|
});
|
|
566
|
-
if (prop.kind ===
|
|
566
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY && kind.isDirty()) {
|
|
567
567
|
this.processToManyReference(kind, visited, processed, parent, prop);
|
|
568
568
|
}
|
|
569
569
|
}
|
|
@@ -575,8 +575,8 @@ class UnitOfWork {
|
|
|
575
575
|
}
|
|
576
576
|
processToManyReference(collection, visited, processed, parent, prop) {
|
|
577
577
|
if (this.isCollectionSelfReferenced(collection, processed)) {
|
|
578
|
-
this.extraUpdates.add([parent, prop.name, collection, undefined,
|
|
579
|
-
const coll = new
|
|
578
|
+
this.extraUpdates.add([parent, prop.name, collection, undefined, ChangeSetType.UPDATE]);
|
|
579
|
+
const coll = new Collection(parent);
|
|
580
580
|
coll.property = prop;
|
|
581
581
|
parent[prop.name] = coll;
|
|
582
582
|
return;
|
|
@@ -599,14 +599,14 @@ class UnitOfWork {
|
|
|
599
599
|
const current = this.comparator.prepareEntity(changeSet.entity);
|
|
600
600
|
const diff = this.comparator.diffEntities(changeSet.name, copy, current);
|
|
601
601
|
Object.assign(changeSet.payload, diff);
|
|
602
|
-
const wrapped =
|
|
602
|
+
const wrapped = helper(changeSet.entity);
|
|
603
603
|
if (wrapped.__identifier && diff[wrapped.__meta.primaryKeys[0]]) {
|
|
604
604
|
wrapped.__identifier.setValue(diff[wrapped.__meta.primaryKeys[0]]);
|
|
605
605
|
}
|
|
606
606
|
}
|
|
607
607
|
postCommitCleanup() {
|
|
608
608
|
for (const cs of this.changeSets.values()) {
|
|
609
|
-
const wrapped =
|
|
609
|
+
const wrapped = helper(cs.entity);
|
|
610
610
|
wrapped.__processing = false;
|
|
611
611
|
delete wrapped.__pk;
|
|
612
612
|
}
|
|
@@ -625,23 +625,23 @@ class UnitOfWork {
|
|
|
625
625
|
}
|
|
626
626
|
visited.add(entity);
|
|
627
627
|
switch (type) {
|
|
628
|
-
case
|
|
628
|
+
case Cascade.PERSIST:
|
|
629
629
|
this.persist(entity, visited, options);
|
|
630
630
|
break;
|
|
631
|
-
case
|
|
631
|
+
case Cascade.MERGE:
|
|
632
632
|
this.merge(entity, visited);
|
|
633
633
|
break;
|
|
634
|
-
case
|
|
634
|
+
case Cascade.REMOVE:
|
|
635
635
|
this.remove(entity, visited, options);
|
|
636
636
|
break;
|
|
637
|
-
case
|
|
637
|
+
case Cascade.SCHEDULE_ORPHAN_REMOVAL:
|
|
638
638
|
this.scheduleOrphanRemoval(entity, visited);
|
|
639
639
|
break;
|
|
640
|
-
case
|
|
640
|
+
case Cascade.CANCEL_ORPHAN_REMOVAL:
|
|
641
641
|
this.cancelOrphanRemoval(entity, visited);
|
|
642
642
|
break;
|
|
643
643
|
}
|
|
644
|
-
for (const prop of
|
|
644
|
+
for (const prop of helper(entity).__meta.relations) {
|
|
645
645
|
this.cascadeReference(entity, prop, type, visited, options);
|
|
646
646
|
}
|
|
647
647
|
}
|
|
@@ -650,72 +650,72 @@ class UnitOfWork {
|
|
|
650
650
|
if (!this.shouldCascade(prop, type)) {
|
|
651
651
|
return;
|
|
652
652
|
}
|
|
653
|
-
const kind =
|
|
654
|
-
if ([
|
|
653
|
+
const kind = Reference.unwrapReference(entity[prop.name]);
|
|
654
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils.isEntity(kind)) {
|
|
655
655
|
return this.cascade(kind, type, visited, options);
|
|
656
656
|
}
|
|
657
657
|
const collection = kind;
|
|
658
|
-
if ([
|
|
658
|
+
if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind) && collection) {
|
|
659
659
|
for (const item of collection.getItems(false)) {
|
|
660
660
|
this.cascade(item, type, visited, options);
|
|
661
661
|
}
|
|
662
662
|
}
|
|
663
663
|
}
|
|
664
664
|
isCollectionSelfReferenced(collection, processed) {
|
|
665
|
-
const filtered = collection.getItems(false).filter(item => !
|
|
665
|
+
const filtered = collection.getItems(false).filter(item => !helper(item).__originalEntityData);
|
|
666
666
|
return filtered.some(items => processed.has(items));
|
|
667
667
|
}
|
|
668
668
|
shouldCascade(prop, type) {
|
|
669
|
-
if ([
|
|
669
|
+
if ([Cascade.REMOVE, Cascade.SCHEDULE_ORPHAN_REMOVAL, Cascade.CANCEL_ORPHAN_REMOVAL, Cascade.ALL].includes(type) && prop.orphanRemoval) {
|
|
670
670
|
return true;
|
|
671
671
|
}
|
|
672
672
|
// ignore user settings for merge, it is kept only for back compatibility, this should have never been configurable
|
|
673
|
-
if (type ===
|
|
673
|
+
if (type === Cascade.MERGE) {
|
|
674
674
|
return true;
|
|
675
675
|
}
|
|
676
|
-
return prop.cascade && (prop.cascade.includes(type) || prop.cascade.includes(
|
|
676
|
+
return prop.cascade && (prop.cascade.includes(type) || prop.cascade.includes(Cascade.ALL));
|
|
677
677
|
}
|
|
678
678
|
async lockPessimistic(entity, options) {
|
|
679
679
|
if (!this.em.isInTransaction()) {
|
|
680
|
-
throw
|
|
680
|
+
throw ValidationError.transactionRequired();
|
|
681
681
|
}
|
|
682
682
|
await this.em.getDriver().lockPessimistic(entity, { ctx: this.em.getTransactionContext(), ...options });
|
|
683
683
|
}
|
|
684
684
|
async lockOptimistic(entity, meta, version) {
|
|
685
685
|
if (!meta.versionProperty) {
|
|
686
|
-
throw
|
|
686
|
+
throw OptimisticLockError.notVersioned(meta);
|
|
687
687
|
}
|
|
688
|
-
if (!
|
|
688
|
+
if (!Utils.isDefined(version)) {
|
|
689
689
|
return;
|
|
690
690
|
}
|
|
691
|
-
const wrapped =
|
|
691
|
+
const wrapped = helper(entity);
|
|
692
692
|
if (!wrapped.__initialized) {
|
|
693
693
|
await wrapped.init();
|
|
694
694
|
}
|
|
695
695
|
const previousVersion = entity[meta.versionProperty];
|
|
696
696
|
if (previousVersion !== version) {
|
|
697
|
-
throw
|
|
697
|
+
throw OptimisticLockError.lockFailedVersionMismatch(entity, version, previousVersion);
|
|
698
698
|
}
|
|
699
699
|
}
|
|
700
700
|
fixMissingReference(entity, prop) {
|
|
701
701
|
const reference = entity[prop.name];
|
|
702
|
-
const kind =
|
|
703
|
-
if ([
|
|
704
|
-
if (!
|
|
702
|
+
const kind = Reference.unwrapReference(reference);
|
|
703
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && kind && !prop.mapToPk) {
|
|
704
|
+
if (!Utils.isEntity(kind)) {
|
|
705
705
|
entity[prop.name] = this.em.getReference(prop.type, kind, { wrapped: !!prop.ref });
|
|
706
706
|
}
|
|
707
|
-
else if (!
|
|
708
|
-
const pk =
|
|
707
|
+
else if (!helper(kind).__initialized && !helper(kind).__em) {
|
|
708
|
+
const pk = helper(kind).getPrimaryKey();
|
|
709
709
|
entity[prop.name] = this.em.getReference(prop.type, pk, { wrapped: !!prop.ref });
|
|
710
710
|
}
|
|
711
711
|
}
|
|
712
712
|
// perf: set the `Collection._property` to skip the getter, as it can be slow when there is a lot of relations
|
|
713
|
-
if (
|
|
713
|
+
if (Utils.isCollection(kind)) {
|
|
714
714
|
kind.property = prop;
|
|
715
715
|
}
|
|
716
|
-
const isCollection = [
|
|
716
|
+
const isCollection = [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind);
|
|
717
717
|
if (isCollection && Array.isArray(kind)) {
|
|
718
|
-
const collection = new
|
|
718
|
+
const collection = new Collection(entity);
|
|
719
719
|
collection.property = prop;
|
|
720
720
|
entity[prop.name] = collection;
|
|
721
721
|
collection.set(kind);
|
|
@@ -729,29 +729,29 @@ class UnitOfWork {
|
|
|
729
729
|
const commitOrderReversed = [...commitOrder].reverse();
|
|
730
730
|
// early delete - when we recreate entity in the same UoW, we need to issue those delete queries before inserts
|
|
731
731
|
for (const name of commitOrderReversed) {
|
|
732
|
-
await this.commitDeleteChangeSets(groups[
|
|
732
|
+
await this.commitDeleteChangeSets(groups[ChangeSetType.DELETE_EARLY].get(name) ?? [], ctx);
|
|
733
733
|
}
|
|
734
734
|
// early update - when we recreate entity in the same UoW, we need to issue those delete queries before inserts
|
|
735
735
|
for (const name of commitOrder) {
|
|
736
|
-
await this.commitUpdateChangeSets(groups[
|
|
736
|
+
await this.commitUpdateChangeSets(groups[ChangeSetType.UPDATE_EARLY].get(name) ?? [], ctx);
|
|
737
737
|
}
|
|
738
738
|
// extra updates
|
|
739
|
-
await this.commitExtraUpdates(
|
|
739
|
+
await this.commitExtraUpdates(ChangeSetType.UPDATE_EARLY, ctx);
|
|
740
740
|
// create
|
|
741
741
|
for (const name of commitOrder) {
|
|
742
|
-
await this.commitCreateChangeSets(groups[
|
|
742
|
+
await this.commitCreateChangeSets(groups[ChangeSetType.CREATE].get(name) ?? [], ctx);
|
|
743
743
|
}
|
|
744
744
|
// update
|
|
745
745
|
for (const name of commitOrder) {
|
|
746
|
-
await this.commitUpdateChangeSets(groups[
|
|
746
|
+
await this.commitUpdateChangeSets(groups[ChangeSetType.UPDATE].get(name) ?? [], ctx);
|
|
747
747
|
}
|
|
748
748
|
// extra updates
|
|
749
|
-
await this.commitExtraUpdates(
|
|
749
|
+
await this.commitExtraUpdates(ChangeSetType.UPDATE, ctx);
|
|
750
750
|
// collection updates
|
|
751
751
|
await this.commitCollectionUpdates(ctx);
|
|
752
752
|
// delete - entity deletions need to be in reverse commit order
|
|
753
753
|
for (const name of commitOrderReversed) {
|
|
754
|
-
await this.commitDeleteChangeSets(groups[
|
|
754
|
+
await this.commitDeleteChangeSets(groups[ChangeSetType.DELETE].get(name) ?? [], ctx);
|
|
755
755
|
}
|
|
756
756
|
// take snapshots of all persisted collections
|
|
757
757
|
const visited = new Set();
|
|
@@ -764,30 +764,30 @@ class UnitOfWork {
|
|
|
764
764
|
return;
|
|
765
765
|
}
|
|
766
766
|
const props = changeSets[0].meta.root.relations.filter(prop => {
|
|
767
|
-
return (prop.kind ===
|
|
768
|
-
|| prop.kind ===
|
|
769
|
-
|| (prop.kind ===
|
|
767
|
+
return (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner)
|
|
768
|
+
|| prop.kind === ReferenceKind.MANY_TO_ONE
|
|
769
|
+
|| (prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner && !this.platform.usesPivotTable());
|
|
770
770
|
});
|
|
771
771
|
for (const changeSet of changeSets) {
|
|
772
772
|
this.findExtraUpdates(changeSet, props);
|
|
773
|
-
await this.runHooks(
|
|
773
|
+
await this.runHooks(EventType.beforeCreate, changeSet, true);
|
|
774
774
|
}
|
|
775
775
|
await this.changeSetPersister.executeInserts(changeSets, { ctx });
|
|
776
776
|
for (const changeSet of changeSets) {
|
|
777
777
|
this.register(changeSet.entity, changeSet.payload, { refresh: true });
|
|
778
|
-
await this.runHooks(
|
|
778
|
+
await this.runHooks(EventType.afterCreate, changeSet);
|
|
779
779
|
}
|
|
780
780
|
}
|
|
781
781
|
findExtraUpdates(changeSet, props) {
|
|
782
782
|
for (const prop of props) {
|
|
783
783
|
const ref = changeSet.entity[prop.name];
|
|
784
|
-
if (!ref || prop.deferMode ===
|
|
784
|
+
if (!ref || prop.deferMode === DeferMode.INITIALLY_DEFERRED) {
|
|
785
785
|
continue;
|
|
786
786
|
}
|
|
787
|
-
if (
|
|
787
|
+
if (Utils.isCollection(ref)) {
|
|
788
788
|
ref.getItems(false).some(item => {
|
|
789
|
-
const cs = this.changeSets.get(
|
|
790
|
-
const isScheduledForInsert = cs && cs.type ===
|
|
789
|
+
const cs = this.changeSets.get(Reference.unwrapReference(item));
|
|
790
|
+
const isScheduledForInsert = cs && cs.type === ChangeSetType.CREATE && !cs.persisted;
|
|
791
791
|
if (isScheduledForInsert) {
|
|
792
792
|
this.scheduleExtraUpdate(changeSet, [prop]);
|
|
793
793
|
return true;
|
|
@@ -795,8 +795,8 @@ class UnitOfWork {
|
|
|
795
795
|
return false;
|
|
796
796
|
});
|
|
797
797
|
}
|
|
798
|
-
const cs = this.changeSets.get(
|
|
799
|
-
const isScheduledForInsert = cs && cs.type ===
|
|
798
|
+
const cs = this.changeSets.get(Reference.unwrapReference(ref));
|
|
799
|
+
const isScheduledForInsert = cs && cs.type === ChangeSetType.CREATE && !cs.persisted;
|
|
800
800
|
if (isScheduledForInsert) {
|
|
801
801
|
this.scheduleExtraUpdate(changeSet, [prop]);
|
|
802
802
|
}
|
|
@@ -805,14 +805,14 @@ class UnitOfWork {
|
|
|
805
805
|
findEarlyUpdates(changeSet, inserts = []) {
|
|
806
806
|
const props = changeSet.meta.uniqueProps;
|
|
807
807
|
for (const prop of props) {
|
|
808
|
-
const insert = inserts.find(c =>
|
|
808
|
+
const insert = inserts.find(c => Utils.equals(c.payload[prop.name], changeSet.originalEntity[prop.name]));
|
|
809
809
|
const propEmpty = changeSet.payload[prop.name] === null || changeSet.payload[prop.name] === undefined;
|
|
810
810
|
if (prop.name in changeSet.payload &&
|
|
811
811
|
insert &&
|
|
812
812
|
// We only want to update early if the unique property on the changeset is going to be empty, so that
|
|
813
813
|
// the previous unique value can be set on a different entity without constraint issues
|
|
814
814
|
propEmpty) {
|
|
815
|
-
changeSet.type =
|
|
815
|
+
changeSet.type = ChangeSetType.UPDATE_EARLY;
|
|
816
816
|
}
|
|
817
817
|
}
|
|
818
818
|
}
|
|
@@ -821,14 +821,14 @@ class UnitOfWork {
|
|
|
821
821
|
return;
|
|
822
822
|
}
|
|
823
823
|
for (const changeSet of changeSets) {
|
|
824
|
-
await this.runHooks(
|
|
824
|
+
await this.runHooks(EventType.beforeUpdate, changeSet, true);
|
|
825
825
|
}
|
|
826
826
|
await this.changeSetPersister.executeUpdates(changeSets, batched, { ctx });
|
|
827
827
|
for (const changeSet of changeSets) {
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
await this.runHooks(
|
|
828
|
+
helper(changeSet.entity).__originalEntityData = this.comparator.prepareEntity(changeSet.entity);
|
|
829
|
+
helper(changeSet.entity).__touched = false;
|
|
830
|
+
helper(changeSet.entity).__initialized = true;
|
|
831
|
+
await this.runHooks(EventType.afterUpdate, changeSet);
|
|
832
832
|
}
|
|
833
833
|
}
|
|
834
834
|
async commitDeleteChangeSets(changeSets, ctx) {
|
|
@@ -836,12 +836,12 @@ class UnitOfWork {
|
|
|
836
836
|
return;
|
|
837
837
|
}
|
|
838
838
|
for (const changeSet of changeSets) {
|
|
839
|
-
await this.runHooks(
|
|
839
|
+
await this.runHooks(EventType.beforeDelete, changeSet, true);
|
|
840
840
|
}
|
|
841
841
|
await this.changeSetPersister.executeDeletes(changeSets, { ctx });
|
|
842
842
|
for (const changeSet of changeSets) {
|
|
843
843
|
this.unsetIdentity(changeSet.entity);
|
|
844
|
-
await this.runHooks(
|
|
844
|
+
await this.runHooks(EventType.afterDelete, changeSet);
|
|
845
845
|
}
|
|
846
846
|
}
|
|
847
847
|
async commitExtraUpdates(type, ctx) {
|
|
@@ -877,10 +877,10 @@ class UnitOfWork {
|
|
|
877
877
|
collectionUpdates.push(coll);
|
|
878
878
|
}
|
|
879
879
|
}
|
|
880
|
-
else if (coll.property.kind ===
|
|
880
|
+
else if (coll.property.kind === ReferenceKind.ONE_TO_MANY && coll.getSnapshot() === undefined) {
|
|
881
881
|
collectionUpdates.push(coll);
|
|
882
882
|
}
|
|
883
|
-
else if (coll.property.kind ===
|
|
883
|
+
else if (coll.property.kind === ReferenceKind.MANY_TO_MANY && !coll.property.owner) {
|
|
884
884
|
collectionUpdates.push(coll);
|
|
885
885
|
}
|
|
886
886
|
}
|
|
@@ -894,11 +894,11 @@ class UnitOfWork {
|
|
|
894
894
|
*/
|
|
895
895
|
getChangeSetGroups() {
|
|
896
896
|
const groups = {
|
|
897
|
-
[
|
|
898
|
-
[
|
|
899
|
-
[
|
|
900
|
-
[
|
|
901
|
-
[
|
|
897
|
+
[ChangeSetType.CREATE]: new Map(),
|
|
898
|
+
[ChangeSetType.UPDATE]: new Map(),
|
|
899
|
+
[ChangeSetType.DELETE]: new Map(),
|
|
900
|
+
[ChangeSetType.UPDATE_EARLY]: new Map(),
|
|
901
|
+
[ChangeSetType.DELETE_EARLY]: new Map(),
|
|
902
902
|
};
|
|
903
903
|
for (const cs of this.changeSets.values()) {
|
|
904
904
|
const group = groups[cs.type];
|
|
@@ -911,7 +911,7 @@ class UnitOfWork {
|
|
|
911
911
|
return groups;
|
|
912
912
|
}
|
|
913
913
|
getCommitOrder() {
|
|
914
|
-
const calc = new
|
|
914
|
+
const calc = new CommitOrderCalculator();
|
|
915
915
|
const set = new Set();
|
|
916
916
|
this.changeSets.forEach(cs => set.add(cs.rootName));
|
|
917
917
|
set.forEach(entityName => calc.addNode(entityName));
|
|
@@ -938,16 +938,15 @@ class UnitOfWork {
|
|
|
938
938
|
return;
|
|
939
939
|
}
|
|
940
940
|
visited.add(entity);
|
|
941
|
-
|
|
941
|
+
helper(entity)?.__meta.relations.forEach(prop => {
|
|
942
942
|
const value = entity[prop.name];
|
|
943
|
-
if (
|
|
943
|
+
if (Utils.isCollection(value)) {
|
|
944
944
|
value.takeSnapshot();
|
|
945
945
|
}
|
|
946
946
|
// 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(
|
|
947
|
+
if (prop.kind === ReferenceKind.MANY_TO_ONE && value) {
|
|
948
|
+
this.takeCollectionSnapshots(Reference.unwrapReference(value), visited);
|
|
949
949
|
}
|
|
950
950
|
});
|
|
951
951
|
}
|
|
952
952
|
}
|
|
953
|
-
exports.UnitOfWork = UnitOfWork;
|