@mikro-orm/core 7.0.0-dev.1 → 7.0.0-dev.100
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 +96 -58
- package/EntityManager.js +465 -395
- package/MikroORM.d.ts +45 -35
- package/MikroORM.js +109 -160
- package/README.md +3 -2
- package/cache/CacheAdapter.js +1 -2
- package/cache/FileCacheAdapter.d.ts +2 -2
- package/cache/FileCacheAdapter.js +20 -27
- package/cache/GeneratedCacheAdapter.d.ts +2 -3
- package/cache/GeneratedCacheAdapter.js +1 -7
- 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 +4 -5
- package/cache/index.js +4 -21
- package/connections/Connection.d.ts +22 -14
- package/connections/Connection.js +27 -23
- package/connections/index.d.ts +1 -1
- package/connections/index.js +1 -17
- package/drivers/DatabaseDriver.d.ts +25 -15
- package/drivers/DatabaseDriver.js +77 -64
- package/drivers/IDatabaseDriver.d.ts +40 -16
- package/drivers/IDatabaseDriver.js +1 -4
- package/drivers/index.d.ts +2 -2
- package/drivers/index.js +2 -18
- package/entity/BaseEntity.d.ts +6 -7
- package/entity/BaseEntity.js +16 -23
- package/entity/Collection.d.ts +98 -34
- package/entity/Collection.js +466 -131
- package/entity/EntityAssigner.d.ts +3 -3
- package/entity/EntityAssigner.js +67 -64
- package/entity/EntityFactory.d.ts +10 -3
- package/entity/EntityFactory.js +112 -91
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +66 -53
- package/entity/EntityIdentifier.d.ts +1 -1
- package/entity/EntityIdentifier.js +1 -5
- package/entity/EntityLoader.d.ts +8 -7
- package/entity/EntityLoader.js +161 -128
- package/entity/EntityRepository.d.ts +8 -8
- package/entity/EntityRepository.js +7 -11
- package/entity/Reference.d.ts +10 -13
- package/entity/Reference.js +64 -46
- package/entity/WrappedEntity.d.ts +12 -17
- package/entity/WrappedEntity.js +22 -31
- package/entity/defineEntity.d.ts +568 -0
- package/entity/defineEntity.js +529 -0
- package/entity/index.d.ts +14 -13
- package/entity/index.js +14 -29
- package/entity/utils.d.ts +8 -1
- package/entity/utils.js +22 -13
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +65 -0
- package/entity/wrap.d.ts +1 -1
- package/entity/wrap.js +2 -6
- package/enums.d.ts +24 -9
- package/enums.js +50 -41
- package/errors.d.ts +11 -3
- package/errors.js +42 -32
- package/events/EventManager.d.ts +5 -4
- package/events/EventManager.js +26 -22
- 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 +2 -6
- package/hydration/ObjectHydrator.d.ts +7 -7
- package/hydration/ObjectHydrator.js +58 -50
- package/hydration/index.d.ts +2 -2
- package/hydration/index.js +2 -18
- package/index.d.ts +21 -21
- package/index.js +20 -46
- package/logging/DefaultLogger.d.ts +2 -2
- package/logging/DefaultLogger.js +10 -13
- package/logging/Logger.d.ts +1 -1
- package/logging/Logger.js +1 -2
- package/logging/SimpleLogger.d.ts +3 -3
- package/logging/SimpleLogger.js +2 -6
- package/logging/colors.js +1 -5
- package/logging/index.d.ts +5 -4
- package/logging/index.js +5 -20
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +16 -0
- package/metadata/EntitySchema.d.ts +14 -10
- package/metadata/EntitySchema.js +78 -64
- package/metadata/MetadataDiscovery.d.ts +11 -14
- package/metadata/MetadataDiscovery.js +278 -317
- package/metadata/MetadataProvider.d.ts +13 -4
- package/metadata/MetadataProvider.js +47 -8
- package/metadata/MetadataStorage.d.ts +2 -7
- package/metadata/MetadataStorage.js +19 -35
- package/metadata/MetadataValidator.d.ts +3 -10
- package/metadata/MetadataValidator.js +51 -64
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +40 -0
- package/metadata/index.d.ts +6 -6
- package/metadata/index.js +6 -22
- package/metadata/types.d.ts +480 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +7 -3
- package/naming-strategy/AbstractNamingStrategy.js +11 -9
- 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 +12 -2
- 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/not-supported.d.ts +2 -0
- package/not-supported.js +4 -0
- package/package.json +19 -20
- package/platforms/ExceptionConverter.d.ts +2 -2
- package/platforms/ExceptionConverter.js +4 -8
- package/platforms/Platform.d.ts +15 -22
- package/platforms/Platform.js +58 -88
- package/platforms/index.d.ts +2 -2
- package/platforms/index.js +2 -18
- package/serialization/EntitySerializer.d.ts +4 -2
- package/serialization/EntitySerializer.js +64 -51
- package/serialization/EntityTransformer.d.ts +1 -1
- package/serialization/EntityTransformer.js +48 -42
- package/serialization/SerializationContext.d.ts +2 -2
- package/serialization/SerializationContext.js +24 -25
- package/serialization/index.d.ts +3 -3
- package/serialization/index.js +3 -19
- package/types/ArrayType.d.ts +3 -3
- package/types/ArrayType.js +6 -11
- package/types/BigIntType.d.ts +12 -9
- package/types/BigIntType.js +6 -6
- package/types/BlobType.d.ts +3 -4
- package/types/BlobType.js +2 -11
- package/types/BooleanType.d.ts +5 -4
- package/types/BooleanType.js +5 -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 +9 -7
- package/types/DecimalType.js +5 -8
- package/types/DoubleType.d.ts +3 -3
- package/types/DoubleType.js +4 -7
- package/types/EnumArrayType.d.ts +4 -4
- package/types/EnumArrayType.js +4 -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 +4 -4
- package/types/JsonType.js +9 -8
- 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 +4 -6
- package/types/Type.js +6 -10
- package/types/Uint8ArrayType.d.ts +3 -4
- package/types/Uint8ArrayType.js +3 -12
- 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 +134 -93
- package/typings.js +67 -65
- package/unit-of-work/ChangeSet.d.ts +1 -4
- package/unit-of-work/ChangeSet.js +13 -17
- package/unit-of-work/ChangeSetComputer.d.ts +8 -9
- package/unit-of-work/ChangeSetComputer.js +36 -38
- package/unit-of-work/ChangeSetPersister.d.ts +11 -9
- package/unit-of-work/ChangeSetPersister.js +100 -65
- 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 +16 -8
- package/unit-of-work/UnitOfWork.js +266 -209
- package/unit-of-work/index.d.ts +6 -6
- package/unit-of-work/index.js +6 -22
- package/utils/AbstractSchemaGenerator.d.ts +11 -11
- package/utils/AbstractSchemaGenerator.js +21 -20
- package/utils/Configuration.d.ts +774 -224
- package/utils/Configuration.js +166 -216
- package/utils/ConfigurationLoader.d.ts +1 -53
- package/utils/ConfigurationLoader.js +1 -367
- package/utils/Cursor.d.ts +6 -9
- package/utils/Cursor.js +25 -25
- package/utils/DataloaderUtils.d.ts +18 -8
- package/utils/DataloaderUtils.js +63 -21
- package/utils/EntityComparator.d.ts +9 -5
- package/utils/EntityComparator.js +155 -108
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +1 -5
- package/utils/QueryHelper.d.ts +12 -4
- package/utils/QueryHelper.js +110 -53
- package/utils/RawQueryFragment.d.ts +37 -14
- package/utils/RawQueryFragment.js +50 -33
- 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/TransactionManager.d.ts +65 -0
- package/utils/TransactionManager.js +223 -0
- package/utils/Utils.d.ts +16 -100
- package/utils/Utils.js +114 -332
- package/utils/clone.js +7 -11
- package/utils/env-vars.d.ts +3 -0
- package/utils/env-vars.js +87 -0
- package/utils/fs-utils.d.ts +12 -0
- package/utils/fs-utils.js +97 -0
- package/utils/index.d.ts +14 -13
- package/utils/index.js +14 -29
- package/utils/upsert-utils.d.ts +8 -3
- package/utils/upsert-utils.js +57 -10
- package/decorators/Check.d.ts +0 -3
- package/decorators/Check.js +0 -16
- package/decorators/CreateRequestContext.d.ts +0 -3
- package/decorators/CreateRequestContext.js +0 -33
- package/decorators/Embeddable.d.ts +0 -8
- package/decorators/Embeddable.js +0 -14
- package/decorators/Embedded.d.ts +0 -18
- package/decorators/Embedded.js +0 -20
- package/decorators/Entity.d.ts +0 -18
- package/decorators/Entity.js +0 -16
- package/decorators/Enum.d.ts +0 -9
- package/decorators/Enum.js +0 -19
- package/decorators/Filter.d.ts +0 -2
- package/decorators/Filter.js +0 -11
- package/decorators/Formula.d.ts +0 -5
- package/decorators/Formula.js +0 -18
- package/decorators/Indexed.d.ts +0 -17
- package/decorators/Indexed.js +0 -24
- package/decorators/ManyToMany.d.ts +0 -40
- package/decorators/ManyToMany.js +0 -16
- package/decorators/ManyToOne.d.ts +0 -30
- package/decorators/ManyToOne.js +0 -16
- package/decorators/OneToMany.d.ts +0 -28
- package/decorators/OneToMany.js +0 -20
- package/decorators/OneToOne.d.ts +0 -24
- package/decorators/OneToOne.js +0 -10
- package/decorators/PrimaryKey.d.ts +0 -9
- package/decorators/PrimaryKey.js +0 -23
- package/decorators/Property.d.ts +0 -250
- package/decorators/Property.js +0 -34
- package/decorators/Transactional.d.ts +0 -13
- package/decorators/Transactional.js +0 -31
- package/decorators/hooks.d.ts +0 -16
- package/decorators/hooks.js +0 -59
- package/decorators/index.d.ts +0 -17
- package/decorators/index.js +0 -36
- package/entity/ArrayCollection.d.ts +0 -116
- package/entity/ArrayCollection.js +0 -399
- package/entity/EntityValidator.d.ts +0 -19
- package/entity/EntityValidator.js +0 -154
- package/index.mjs +0 -199
- package/metadata/ReflectMetadataProvider.d.ts +0 -8
- package/metadata/ReflectMetadataProvider.js +0 -48
- package/utils/resolveContextProvider.d.ts +0 -10
- package/utils/resolveContextProvider.js +0 -31
|
@@ -1,29 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const enums_1 = require("../enums");
|
|
10
|
-
class ChangeSetPersister {
|
|
1
|
+
import { EntityIdentifier } from '../entity/EntityIdentifier.js';
|
|
2
|
+
import { helper } from '../entity/wrap.js';
|
|
3
|
+
import { ChangeSetType } from './ChangeSet.js';
|
|
4
|
+
import { isRaw } from '../utils/RawQueryFragment.js';
|
|
5
|
+
import { Utils } from '../utils/Utils.js';
|
|
6
|
+
import { OptimisticLockError, ValidationError } from '../errors.js';
|
|
7
|
+
import { ReferenceKind } from '../enums.js';
|
|
8
|
+
export class ChangeSetPersister {
|
|
11
9
|
driver;
|
|
12
10
|
metadata;
|
|
13
11
|
hydrator;
|
|
14
12
|
factory;
|
|
15
|
-
validator;
|
|
16
13
|
config;
|
|
14
|
+
em;
|
|
17
15
|
platform;
|
|
18
16
|
comparator;
|
|
19
17
|
usesReturningStatement;
|
|
20
|
-
constructor(driver, metadata, hydrator, factory,
|
|
18
|
+
constructor(driver, metadata, hydrator, factory, config, em) {
|
|
21
19
|
this.driver = driver;
|
|
22
20
|
this.metadata = metadata;
|
|
23
21
|
this.hydrator = hydrator;
|
|
24
22
|
this.factory = factory;
|
|
25
|
-
this.validator = validator;
|
|
26
23
|
this.config = config;
|
|
24
|
+
this.em = em;
|
|
27
25
|
this.platform = this.driver.getPlatform();
|
|
28
26
|
this.comparator = this.config.getComparator(this.metadata);
|
|
29
27
|
this.usesReturningStatement = this.platform.usesReturningStatement() || this.platform.usesOutputStatement();
|
|
@@ -60,11 +58,11 @@ class ChangeSetPersister {
|
|
|
60
58
|
}
|
|
61
59
|
const size = this.config.get('batchSize');
|
|
62
60
|
const meta = changeSets[0].meta;
|
|
63
|
-
const pk =
|
|
61
|
+
const pk = Utils.getPrimaryKeyHash(meta.primaryKeys);
|
|
64
62
|
for (let i = 0; i < changeSets.length; i += size) {
|
|
65
63
|
const chunk = changeSets.slice(i, i + size);
|
|
66
64
|
const pks = chunk.map(cs => cs.getPrimaryKey());
|
|
67
|
-
options = this.
|
|
65
|
+
options = this.prepareOptions(meta, options);
|
|
68
66
|
await this.driver.nativeDelete(meta.root.className, { [pk]: { $in: pks } }, options);
|
|
69
67
|
}
|
|
70
68
|
}
|
|
@@ -81,18 +79,37 @@ class ChangeSetPersister {
|
|
|
81
79
|
await this[method](group, ...args, options, true);
|
|
82
80
|
}
|
|
83
81
|
}
|
|
82
|
+
validateRequired(entity) {
|
|
83
|
+
const wrapped = helper(entity);
|
|
84
|
+
for (const prop of wrapped.__meta.props) {
|
|
85
|
+
if (!prop.nullable &&
|
|
86
|
+
!prop.autoincrement &&
|
|
87
|
+
!prop.default &&
|
|
88
|
+
!prop.defaultRaw &&
|
|
89
|
+
!prop.onCreate &&
|
|
90
|
+
!prop.generated &&
|
|
91
|
+
!prop.embedded &&
|
|
92
|
+
![ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind) &&
|
|
93
|
+
prop.name !== wrapped.__meta.root.discriminatorColumn &&
|
|
94
|
+
prop.type !== 'ObjectId' &&
|
|
95
|
+
prop.persist !== false &&
|
|
96
|
+
entity[prop.name] == null) {
|
|
97
|
+
throw ValidationError.propertyRequired(entity, prop);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
84
101
|
processProperties(changeSet) {
|
|
85
102
|
const meta = this.metadata.find(changeSet.name);
|
|
86
103
|
for (const prop of meta.relations) {
|
|
87
104
|
this.processProperty(changeSet, prop);
|
|
88
105
|
}
|
|
89
|
-
if (changeSet.type ===
|
|
90
|
-
this.
|
|
106
|
+
if (changeSet.type === ChangeSetType.CREATE && this.config.get('validateRequired')) {
|
|
107
|
+
this.validateRequired(changeSet.entity);
|
|
91
108
|
}
|
|
92
109
|
}
|
|
93
110
|
async persistNewEntity(meta, changeSet, options) {
|
|
94
|
-
const wrapped =
|
|
95
|
-
options = this.
|
|
111
|
+
const wrapped = helper(changeSet.entity);
|
|
112
|
+
options = this.prepareOptions(meta, options, {
|
|
96
113
|
convertCustomTypes: false,
|
|
97
114
|
});
|
|
98
115
|
const res = await this.driver.nativeInsertMany(meta.className, [changeSet.payload], options);
|
|
@@ -118,22 +135,24 @@ class ChangeSetPersister {
|
|
|
118
135
|
}
|
|
119
136
|
}
|
|
120
137
|
}
|
|
121
|
-
|
|
138
|
+
prepareOptions(meta, options, additionalOptions) {
|
|
139
|
+
const loggerContext = Utils.merge({ id: this.em._id }, this.em.getLoggerContext({ disableContextResolution: true }));
|
|
122
140
|
return {
|
|
123
141
|
...options,
|
|
124
142
|
...additionalOptions,
|
|
125
143
|
schema: options?.schema ?? meta.schema,
|
|
144
|
+
loggerContext,
|
|
126
145
|
};
|
|
127
146
|
}
|
|
128
147
|
async persistNewEntitiesBatch(meta, changeSets, options) {
|
|
129
|
-
options = this.
|
|
148
|
+
options = this.prepareOptions(meta, options, {
|
|
130
149
|
convertCustomTypes: false,
|
|
131
150
|
processCollections: false,
|
|
132
151
|
});
|
|
133
152
|
const res = await this.driver.nativeInsertMany(meta.className, changeSets.map(cs => cs.payload), options);
|
|
134
153
|
for (let i = 0; i < changeSets.length; i++) {
|
|
135
154
|
const changeSet = changeSets[i];
|
|
136
|
-
const wrapped =
|
|
155
|
+
const wrapped = helper(changeSet.entity);
|
|
137
156
|
if (!wrapped.hasPrimaryKey()) {
|
|
138
157
|
const field = meta.getPrimaryProps()[0].fieldNames[0];
|
|
139
158
|
this.mapPrimaryKey(meta, res.rows[i][field], changeSet);
|
|
@@ -172,12 +191,12 @@ class ChangeSetPersister {
|
|
|
172
191
|
}
|
|
173
192
|
}
|
|
174
193
|
if (tmp.length === 0 && meta.concurrencyCheckKeys.size > 0) {
|
|
175
|
-
throw
|
|
194
|
+
throw OptimisticLockError.lockFailed(changeSet.entity);
|
|
176
195
|
}
|
|
177
196
|
}
|
|
178
197
|
async persistManagedEntitiesBatch(meta, changeSets, options) {
|
|
179
198
|
await this.checkOptimisticLocks(meta, changeSets, options);
|
|
180
|
-
options = this.
|
|
199
|
+
options = this.prepareOptions(meta, options, {
|
|
181
200
|
convertCustomTypes: false,
|
|
182
201
|
processCollections: false,
|
|
183
202
|
});
|
|
@@ -191,10 +210,10 @@ class ChangeSetPersister {
|
|
|
191
210
|
}
|
|
192
211
|
const res = await this.driver.nativeUpdateMany(meta.className, cond, payload, options);
|
|
193
212
|
const map = new Map();
|
|
194
|
-
res.rows?.forEach(item => map.set(
|
|
213
|
+
res.rows?.forEach(item => map.set(Utils.getCompositeKeyHash(item, meta, true, this.platform, true), item));
|
|
195
214
|
for (const changeSet of changeSets) {
|
|
196
215
|
if (res.rows) {
|
|
197
|
-
const row = map.get(
|
|
216
|
+
const row = map.get(helper(changeSet.entity).getSerializedPrimaryKey());
|
|
198
217
|
this.mapReturnedValues(changeSet.entity, changeSet.payload, row, meta);
|
|
199
218
|
}
|
|
200
219
|
changeSet.persisted = true;
|
|
@@ -203,7 +222,7 @@ class ChangeSetPersister {
|
|
|
203
222
|
mapPrimaryKey(meta, value, changeSet) {
|
|
204
223
|
const prop = meta.properties[meta.primaryKeys[0]];
|
|
205
224
|
const insertId = prop.customType ? prop.customType.convertToJSValue(value, this.platform) : value;
|
|
206
|
-
const wrapped =
|
|
225
|
+
const wrapped = helper(changeSet.entity);
|
|
207
226
|
if (!wrapped.hasPrimaryKey()) {
|
|
208
227
|
wrapped.setPrimaryKey(insertId);
|
|
209
228
|
}
|
|
@@ -212,30 +231,32 @@ class ChangeSetPersister {
|
|
|
212
231
|
// of using the raw value from db, we convert it back to the db value explicitly
|
|
213
232
|
value = prop.customType ? prop.customType.convertToDatabaseValue(insertId, this.platform, { mode: 'serialization' }) : value;
|
|
214
233
|
changeSet.payload[wrapped.__meta.primaryKeys[0]] = value;
|
|
215
|
-
wrapped.__identifier
|
|
234
|
+
if (wrapped.__identifier && !Array.isArray(wrapped.__identifier)) {
|
|
235
|
+
wrapped.__identifier.setValue(value);
|
|
236
|
+
}
|
|
216
237
|
}
|
|
217
238
|
/**
|
|
218
239
|
* Sets populate flag to new entities so they are serialized like if they were loaded from the db
|
|
219
240
|
*/
|
|
220
241
|
markAsPopulated(changeSet, meta) {
|
|
221
|
-
|
|
242
|
+
helper(changeSet.entity).__schema = this.driver.getSchemaName(meta, changeSet);
|
|
222
243
|
if (!this.config.get('populateAfterFlush')) {
|
|
223
244
|
return;
|
|
224
245
|
}
|
|
225
|
-
|
|
246
|
+
helper(changeSet.entity).populated();
|
|
226
247
|
meta.relations.forEach(prop => {
|
|
227
248
|
const value = changeSet.entity[prop.name];
|
|
228
|
-
if (
|
|
249
|
+
if (Utils.isEntity(value, true)) {
|
|
229
250
|
value.__helper.populated();
|
|
230
251
|
}
|
|
231
|
-
else if (
|
|
252
|
+
else if (Utils.isCollection(value)) {
|
|
232
253
|
value.populated();
|
|
233
254
|
}
|
|
234
255
|
});
|
|
235
256
|
}
|
|
236
257
|
async updateEntity(meta, changeSet, options) {
|
|
237
258
|
const cond = changeSet.getPrimaryKey(true);
|
|
238
|
-
options = this.
|
|
259
|
+
options = this.prepareOptions(meta, options, {
|
|
239
260
|
convertCustomTypes: false,
|
|
240
261
|
});
|
|
241
262
|
if (meta.concurrencyCheckKeys.size === 0 && (!meta.versionProperty || changeSet.entity[meta.versionProperty] == null)) {
|
|
@@ -252,9 +273,9 @@ class ChangeSetPersister {
|
|
|
252
273
|
return;
|
|
253
274
|
}
|
|
254
275
|
// skip entity references as they don't have version values loaded
|
|
255
|
-
changeSets = changeSets.filter(cs =>
|
|
276
|
+
changeSets = changeSets.filter(cs => helper(cs.entity).__initialized);
|
|
256
277
|
const $or = changeSets.map(cs => {
|
|
257
|
-
const cond =
|
|
278
|
+
const cond = Utils.getPrimaryKeyCond(cs.originalEntity, meta.primaryKeys.concat(...meta.concurrencyCheckKeys));
|
|
258
279
|
if (meta.versionProperty) {
|
|
259
280
|
// @ts-ignore
|
|
260
281
|
cond[meta.versionProperty] = this.platform.quoteVersionValue(cs.entity[meta.versionProperty], meta.properties[meta.versionProperty]);
|
|
@@ -262,21 +283,21 @@ class ChangeSetPersister {
|
|
|
262
283
|
return cond;
|
|
263
284
|
});
|
|
264
285
|
const primaryKeys = meta.primaryKeys.concat(...meta.concurrencyCheckKeys);
|
|
265
|
-
options = this.
|
|
286
|
+
options = this.prepareOptions(meta, options, {
|
|
266
287
|
fields: primaryKeys,
|
|
267
288
|
});
|
|
268
289
|
const res = await this.driver.find(meta.root.className, { $or }, options);
|
|
269
290
|
if (res.length !== changeSets.length) {
|
|
270
291
|
const compare = (a, b, keys) => keys.every(k => a[k] === b[k]);
|
|
271
292
|
const entity = changeSets.find(cs => {
|
|
272
|
-
return !res.some(row => compare(
|
|
293
|
+
return !res.some(row => compare(Utils.getPrimaryKeyCond(cs.entity, primaryKeys), row, primaryKeys));
|
|
273
294
|
}).entity;
|
|
274
|
-
throw
|
|
295
|
+
throw OptimisticLockError.lockFailed(entity);
|
|
275
296
|
}
|
|
276
297
|
}
|
|
277
298
|
checkOptimisticLock(meta, changeSet, res) {
|
|
278
299
|
if ((meta.versionProperty || meta.concurrencyCheckKeys.size > 0) && res && !res.affectedRows) {
|
|
279
|
-
throw
|
|
300
|
+
throw OptimisticLockError.lockFailed(changeSet.entity);
|
|
280
301
|
}
|
|
281
302
|
}
|
|
282
303
|
/**
|
|
@@ -285,18 +306,29 @@ class ChangeSetPersister {
|
|
|
285
306
|
*/
|
|
286
307
|
async reloadVersionValues(meta, changeSets, options) {
|
|
287
308
|
const reloadProps = meta.versionProperty && !this.usesReturningStatement ? [meta.properties[meta.versionProperty]] : [];
|
|
288
|
-
if (changeSets[0].type ===
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
.
|
|
294
|
-
|
|
295
|
-
|
|
309
|
+
if (changeSets[0].type === ChangeSetType.CREATE) {
|
|
310
|
+
for (const prop of meta.props) {
|
|
311
|
+
if (prop.persist === false) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (isRaw(changeSets[0].entity[prop.name])) {
|
|
315
|
+
reloadProps.push(prop);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
// do not reload things that already had a runtime value
|
|
319
|
+
if (changeSets[0].entity[prop.name] != null || prop.defaultRaw === 'null') {
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
if (prop.autoincrement || prop.generated || prop.defaultRaw) {
|
|
323
|
+
reloadProps.push(prop);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (changeSets[0].type === ChangeSetType.UPDATE) {
|
|
296
328
|
const returning = new Set();
|
|
297
329
|
changeSets.forEach(cs => {
|
|
298
|
-
|
|
299
|
-
if (
|
|
330
|
+
Utils.keys(cs.payload).forEach(k => {
|
|
331
|
+
if (isRaw(cs.payload[k]) && isRaw(cs.entity[k])) {
|
|
300
332
|
returning.add(meta.properties[k]);
|
|
301
333
|
}
|
|
302
334
|
});
|
|
@@ -313,22 +345,22 @@ class ChangeSetPersister {
|
|
|
313
345
|
return;
|
|
314
346
|
}
|
|
315
347
|
reloadProps.unshift(...meta.getPrimaryProps());
|
|
316
|
-
const pk =
|
|
348
|
+
const pk = Utils.getPrimaryKeyHash(meta.primaryKeys);
|
|
317
349
|
const pks = changeSets.map(cs => {
|
|
318
|
-
const val =
|
|
319
|
-
if (
|
|
320
|
-
return
|
|
350
|
+
const val = helper(cs.entity).getPrimaryKey(true);
|
|
351
|
+
if (Utils.isPlainObject(val)) {
|
|
352
|
+
return Utils.getCompositeKeyValue(val, meta, false, this.platform);
|
|
321
353
|
}
|
|
322
354
|
return val;
|
|
323
355
|
});
|
|
324
|
-
options = this.
|
|
325
|
-
fields:
|
|
356
|
+
options = this.prepareOptions(meta, options, {
|
|
357
|
+
fields: Utils.unique(reloadProps.map(prop => prop.name)),
|
|
326
358
|
});
|
|
327
359
|
const data = await this.driver.find(meta.className, { [pk]: { $in: pks } }, options);
|
|
328
360
|
const map = new Map();
|
|
329
|
-
data.forEach(item => map.set(
|
|
361
|
+
data.forEach(item => map.set(Utils.getCompositeKeyHash(item, meta, false, this.platform, true), item));
|
|
330
362
|
for (const changeSet of changeSets) {
|
|
331
|
-
const data = map.get(
|
|
363
|
+
const data = map.get(helper(changeSet.entity).getSerializedPrimaryKey());
|
|
332
364
|
this.hydrator.hydrate(changeSet.entity, meta, data, this.factory, 'full', false, true);
|
|
333
365
|
Object.assign(changeSet.payload, data); // merge to the changeset payload, so it gets saved to the entity snapshot
|
|
334
366
|
}
|
|
@@ -336,21 +368,25 @@ class ChangeSetPersister {
|
|
|
336
368
|
processProperty(changeSet, prop) {
|
|
337
369
|
const meta = this.metadata.find(changeSet.name);
|
|
338
370
|
const value = changeSet.payload[prop.name]; // for inline embeddables
|
|
339
|
-
if (value instanceof
|
|
371
|
+
if (value instanceof EntityIdentifier) {
|
|
340
372
|
changeSet.payload[prop.name] = value.getValue();
|
|
341
373
|
return;
|
|
342
374
|
}
|
|
343
|
-
if (
|
|
344
|
-
changeSet.payload[prop.name] = value.map(
|
|
375
|
+
if (Array.isArray(value) && value.every(item => item instanceof EntityIdentifier)) {
|
|
376
|
+
changeSet.payload[prop.name] = value.map(item => item.getValue());
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY && Array.isArray(value)) {
|
|
380
|
+
changeSet.payload[prop.name] = value.map(val => val instanceof EntityIdentifier ? val.getValue() : val);
|
|
345
381
|
return;
|
|
346
382
|
}
|
|
347
383
|
if (prop.name in changeSet.payload) {
|
|
348
384
|
return;
|
|
349
385
|
}
|
|
350
|
-
const values =
|
|
386
|
+
const values = Utils.unwrapProperty(changeSet.payload, meta, prop, true); // for object embeddables
|
|
351
387
|
values.forEach(([value, indexes]) => {
|
|
352
|
-
if (value instanceof
|
|
353
|
-
|
|
388
|
+
if (value instanceof EntityIdentifier) {
|
|
389
|
+
Utils.setPayloadProperty(changeSet.payload, meta, prop, value.getValue(), indexes);
|
|
354
390
|
}
|
|
355
391
|
});
|
|
356
392
|
}
|
|
@@ -360,7 +396,7 @@ class ChangeSetPersister {
|
|
|
360
396
|
* We do need to map to the change set payload too, as it will be used in the originalEntityData for new entities.
|
|
361
397
|
*/
|
|
362
398
|
mapReturnedValues(entity, payload, row, meta, upsert = false) {
|
|
363
|
-
if ((!this.usesReturningStatement && !upsert) || !row || !
|
|
399
|
+
if ((!this.usesReturningStatement && !upsert) || !row || !Utils.hasObjectKeys(row)) {
|
|
364
400
|
return;
|
|
365
401
|
}
|
|
366
402
|
const mapped = this.comparator.mapResult(meta.className, row);
|
|
@@ -377,4 +413,3 @@ class ChangeSetPersister {
|
|
|
377
413
|
Object.assign(payload, mapped);
|
|
378
414
|
}
|
|
379
415
|
}
|
|
380
|
-
exports.ChangeSetPersister = ChangeSetPersister;
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.CommitOrderCalculator = exports.NodeState = void 0;
|
|
4
|
-
const enums_1 = require("../enums");
|
|
5
|
-
var NodeState;
|
|
1
|
+
import { ReferenceKind } from '../enums.js';
|
|
2
|
+
export var NodeState;
|
|
6
3
|
(function (NodeState) {
|
|
7
4
|
NodeState[NodeState["NOT_VISITED"] = 0] = "NOT_VISITED";
|
|
8
5
|
NodeState[NodeState["IN_PROGRESS"] = 1] = "IN_PROGRESS";
|
|
9
6
|
NodeState[NodeState["VISITED"] = 2] = "VISITED";
|
|
10
|
-
})(NodeState || (
|
|
7
|
+
})(NodeState || (NodeState = {}));
|
|
11
8
|
/**
|
|
12
9
|
* CommitOrderCalculator implements topological sorting, which is an ordering
|
|
13
10
|
* algorithm for directed graphs (DG) and/or directed acyclic graphs (DAG) by
|
|
@@ -18,7 +15,7 @@ var NodeState;
|
|
|
18
15
|
* Based on https://github.com/doctrine/orm/blob/master/lib/Doctrine/ORM/Internal/CommitOrderCalculator.php
|
|
19
16
|
* @internal
|
|
20
17
|
*/
|
|
21
|
-
class CommitOrderCalculator {
|
|
18
|
+
export class CommitOrderCalculator {
|
|
22
19
|
/** Matrix of nodes, keys are provided hashes and values are the node definition objects. */
|
|
23
20
|
nodes = {};
|
|
24
21
|
/** Volatile variable holding calculated nodes during sorting process. */
|
|
@@ -42,8 +39,8 @@ class CommitOrderCalculator {
|
|
|
42
39
|
this.nodes[from].dependencies[to] = { from, to, weight };
|
|
43
40
|
}
|
|
44
41
|
discoverProperty(prop, entityName) {
|
|
45
|
-
const toOneOwner = (prop.kind ===
|
|
46
|
-
const toManyOwner = prop.kind ===
|
|
42
|
+
const toOneOwner = (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner) || prop.kind === ReferenceKind.MANY_TO_ONE;
|
|
43
|
+
const toManyOwner = prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner && !prop.pivotEntity;
|
|
47
44
|
if (!toOneOwner && !toManyOwner) {
|
|
48
45
|
return;
|
|
49
46
|
}
|
|
@@ -110,4 +107,3 @@ class CommitOrderCalculator {
|
|
|
110
107
|
this.sortedNodeList.push(target.hash);
|
|
111
108
|
}
|
|
112
109
|
}
|
|
113
|
-
exports.CommitOrderCalculator = CommitOrderCalculator;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IdentityMap = void 0;
|
|
4
|
-
class IdentityMap {
|
|
1
|
+
export class IdentityMap {
|
|
5
2
|
defaultSchema;
|
|
6
3
|
constructor(defaultSchema) {
|
|
7
4
|
this.defaultSchema = defaultSchema;
|
|
@@ -73,4 +70,3 @@ class IdentityMap {
|
|
|
73
70
|
return hash;
|
|
74
71
|
}
|
|
75
72
|
}
|
|
76
|
-
exports.IdentityMap = IdentityMap;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { AnyEntity, EntityData, EntityMetadata, EntityProperty, FilterQuery, Primary } from '../typings';
|
|
2
|
-
import { Collection
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
1
|
+
import type { AnyEntity, EntityData, EntityMetadata, EntityProperty, FilterQuery, Primary } from '../typings.js';
|
|
2
|
+
import { Collection } from '../entity/Collection.js';
|
|
3
|
+
import { Reference } from '../entity/Reference.js';
|
|
4
|
+
import { ChangeSet, ChangeSetType } from './ChangeSet.js';
|
|
5
|
+
import { ChangeSetPersister } from './ChangeSetPersister.js';
|
|
6
|
+
import type { EntityManager } from '../EntityManager.js';
|
|
7
|
+
import { IdentityMap } from './IdentityMap.js';
|
|
8
|
+
import type { LockOptions } from '../drivers/IDatabaseDriver.js';
|
|
8
9
|
export declare class UnitOfWork {
|
|
9
10
|
private readonly em;
|
|
10
11
|
/** map of references to managed entities */
|
|
@@ -27,6 +28,12 @@ export declare class UnitOfWork {
|
|
|
27
28
|
private working;
|
|
28
29
|
constructor(em: EntityManager);
|
|
29
30
|
merge<T extends object>(entity: T, visited?: Set<AnyEntity>): void;
|
|
31
|
+
/**
|
|
32
|
+
* Entity data can wary in its shape, e.g. we might get a deep relation graph with joined strategy, but for diffing,
|
|
33
|
+
* we need to normalize the shape, so relation values are only raw FKs. This method handles that.
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
normalizeEntityData<T extends object>(meta: EntityMetadata<T>, data: EntityData<T>): void;
|
|
30
37
|
/**
|
|
31
38
|
* @internal
|
|
32
39
|
*/
|
|
@@ -38,7 +45,7 @@ export declare class UnitOfWork {
|
|
|
38
45
|
/**
|
|
39
46
|
* Returns entity from the identity map. For composite keys, you need to pass an array of PKs in the same order as they are defined in `meta.primaryKeys`.
|
|
40
47
|
*/
|
|
41
|
-
getById<T extends object>(entityName: string, id: Primary<T> | Primary<T>[], schema?: string): T | undefined;
|
|
48
|
+
getById<T extends object>(entityName: string, id: Primary<T> | Primary<T>[], schema?: string, convertCustomTypes?: boolean): T | undefined;
|
|
42
49
|
tryGetById<T extends object>(entityName: string, where: FilterQuery<T>, schema?: string, strict?: boolean): T | null;
|
|
43
50
|
/**
|
|
44
51
|
* Returns map of all managed entities.
|
|
@@ -102,6 +109,7 @@ export declare class UnitOfWork {
|
|
|
102
109
|
private commitDeleteChangeSets;
|
|
103
110
|
private commitExtraUpdates;
|
|
104
111
|
private commitCollectionUpdates;
|
|
112
|
+
private filterCollectionUpdates;
|
|
105
113
|
/**
|
|
106
114
|
* Orders change sets so FK constrains are maintained, ensures stable order (needed for node < 11)
|
|
107
115
|
*/
|