@mikro-orm/core 7.0.4-dev.8 → 7.0.4
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 +884 -583
- package/EntityManager.js +1922 -1895
- package/MikroORM.d.ts +103 -74
- package/MikroORM.js +178 -179
- package/README.md +1 -1
- package/cache/CacheAdapter.d.ts +36 -36
- package/cache/FileCacheAdapter.d.ts +30 -24
- package/cache/FileCacheAdapter.js +80 -78
- package/cache/GeneratedCacheAdapter.d.ts +18 -20
- package/cache/GeneratedCacheAdapter.js +30 -30
- package/cache/MemoryCacheAdapter.d.ts +18 -20
- package/cache/MemoryCacheAdapter.js +35 -36
- package/cache/NullCacheAdapter.d.ts +16 -16
- package/cache/NullCacheAdapter.js +24 -24
- package/connections/Connection.d.ts +95 -84
- package/connections/Connection.js +165 -168
- package/drivers/DatabaseDriver.d.ts +186 -80
- package/drivers/DatabaseDriver.js +450 -443
- package/drivers/IDatabaseDriver.d.ts +440 -301
- package/entity/BaseEntity.d.ts +120 -83
- package/entity/BaseEntity.js +43 -43
- package/entity/Collection.d.ts +212 -179
- package/entity/Collection.js +727 -721
- package/entity/EntityAssigner.d.ts +88 -77
- package/entity/EntityAssigner.js +231 -230
- package/entity/EntityFactory.d.ts +66 -54
- package/entity/EntityFactory.js +425 -383
- package/entity/EntityHelper.d.ts +34 -22
- package/entity/EntityHelper.js +280 -267
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +98 -72
- package/entity/EntityLoader.js +753 -723
- package/entity/EntityRepository.d.ts +316 -201
- package/entity/EntityRepository.js +213 -213
- package/entity/PolymorphicRef.d.ts +5 -5
- package/entity/PolymorphicRef.js +10 -10
- package/entity/Reference.d.ts +126 -82
- package/entity/Reference.js +278 -274
- package/entity/WrappedEntity.d.ts +115 -72
- package/entity/WrappedEntity.js +168 -166
- package/entity/defineEntity.d.ts +1315 -636
- package/entity/defineEntity.js +527 -518
- package/entity/utils.d.ts +13 -3
- package/entity/utils.js +71 -73
- package/entity/validators.js +43 -43
- package/entity/wrap.js +8 -8
- package/enums.d.ts +258 -253
- package/enums.js +251 -252
- package/errors.d.ts +114 -72
- package/errors.js +350 -253
- package/events/EventManager.d.ts +26 -14
- package/events/EventManager.js +79 -77
- package/events/EventSubscriber.d.ts +29 -29
- package/events/TransactionEventBroadcaster.d.ts +15 -8
- package/events/TransactionEventBroadcaster.js +14 -14
- package/exceptions.d.ts +23 -40
- package/exceptions.js +35 -52
- package/hydration/Hydrator.d.ts +42 -17
- package/hydration/Hydrator.js +43 -43
- package/hydration/ObjectHydrator.d.ts +50 -17
- package/hydration/ObjectHydrator.js +481 -416
- package/index.d.ts +116 -2
- package/index.js +10 -1
- package/logging/DefaultLogger.d.ts +34 -32
- package/logging/DefaultLogger.js +86 -86
- package/logging/Logger.d.ts +41 -41
- package/logging/SimpleLogger.d.ts +13 -11
- package/logging/SimpleLogger.js +22 -22
- package/logging/colors.d.ts +6 -6
- package/logging/colors.js +11 -10
- package/logging/inspect.js +7 -7
- package/metadata/EntitySchema.d.ts +211 -127
- package/metadata/EntitySchema.js +397 -398
- package/metadata/MetadataDiscovery.d.ts +114 -114
- package/metadata/MetadataDiscovery.js +1951 -1863
- package/metadata/MetadataProvider.d.ts +24 -21
- package/metadata/MetadataProvider.js +82 -84
- package/metadata/MetadataStorage.d.ts +38 -32
- package/metadata/MetadataStorage.js +118 -118
- package/metadata/MetadataValidator.d.ts +39 -39
- package/metadata/MetadataValidator.js +381 -338
- package/metadata/discover-entities.d.ts +5 -2
- package/metadata/discover-entities.js +35 -27
- package/metadata/types.d.ts +615 -531
- package/naming-strategy/AbstractNamingStrategy.d.ts +54 -39
- package/naming-strategy/AbstractNamingStrategy.js +90 -85
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +6 -6
- package/naming-strategy/EntityCaseNamingStrategy.js +22 -22
- package/naming-strategy/MongoNamingStrategy.d.ts +6 -6
- package/naming-strategy/MongoNamingStrategy.js +18 -18
- package/naming-strategy/NamingStrategy.d.ts +109 -99
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
- package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
- package/not-supported.js +7 -4
- package/package.json +1 -1
- package/platforms/ExceptionConverter.d.ts +1 -1
- package/platforms/ExceptionConverter.js +4 -4
- package/platforms/Platform.d.ts +310 -299
- package/platforms/Platform.js +663 -636
- package/serialization/EntitySerializer.d.ts +49 -26
- package/serialization/EntitySerializer.js +224 -218
- package/serialization/EntityTransformer.d.ts +10 -6
- package/serialization/EntityTransformer.js +219 -217
- package/serialization/SerializationContext.d.ts +27 -23
- package/serialization/SerializationContext.js +105 -105
- package/types/ArrayType.d.ts +8 -8
- package/types/ArrayType.js +33 -33
- package/types/BigIntType.d.ts +17 -10
- package/types/BigIntType.js +37 -37
- package/types/BlobType.d.ts +3 -3
- package/types/BlobType.js +13 -13
- package/types/BooleanType.d.ts +4 -4
- package/types/BooleanType.js +12 -12
- package/types/CharacterType.d.ts +2 -2
- package/types/CharacterType.js +6 -6
- package/types/DateTimeType.d.ts +5 -5
- package/types/DateTimeType.js +15 -15
- package/types/DateType.d.ts +5 -5
- package/types/DateType.js +15 -15
- package/types/DecimalType.d.ts +7 -7
- package/types/DecimalType.js +26 -26
- package/types/DoubleType.d.ts +3 -3
- package/types/DoubleType.js +12 -12
- package/types/EnumArrayType.d.ts +5 -5
- package/types/EnumArrayType.js +24 -24
- package/types/EnumType.d.ts +3 -3
- package/types/EnumType.js +11 -11
- package/types/FloatType.d.ts +3 -3
- package/types/FloatType.js +9 -9
- package/types/IntegerType.d.ts +3 -3
- package/types/IntegerType.js +9 -9
- package/types/IntervalType.d.ts +4 -4
- package/types/IntervalType.js +12 -12
- package/types/JsonType.d.ts +8 -8
- package/types/JsonType.js +32 -32
- package/types/MediumIntType.d.ts +1 -1
- package/types/MediumIntType.js +3 -3
- package/types/SmallIntType.d.ts +3 -3
- package/types/SmallIntType.js +9 -9
- package/types/StringType.d.ts +4 -4
- package/types/StringType.js +12 -12
- package/types/TextType.d.ts +3 -3
- package/types/TextType.js +9 -9
- package/types/TimeType.d.ts +5 -5
- package/types/TimeType.js +17 -17
- package/types/TinyIntType.d.ts +3 -3
- package/types/TinyIntType.js +10 -10
- package/types/Type.d.ts +83 -79
- package/types/Type.js +82 -82
- package/types/Uint8ArrayType.d.ts +4 -4
- package/types/Uint8ArrayType.js +21 -21
- package/types/UnknownType.d.ts +4 -4
- package/types/UnknownType.js +12 -12
- package/types/UuidType.d.ts +5 -5
- package/types/UuidType.js +19 -19
- package/types/index.d.ts +75 -49
- package/types/index.js +52 -26
- package/typings.d.ts +1250 -737
- package/typings.js +244 -231
- package/unit-of-work/ChangeSet.d.ts +26 -26
- package/unit-of-work/ChangeSet.js +56 -56
- package/unit-of-work/ChangeSetComputer.d.ts +12 -12
- package/unit-of-work/ChangeSetComputer.js +178 -170
- package/unit-of-work/ChangeSetPersister.d.ts +63 -44
- package/unit-of-work/ChangeSetPersister.js +442 -421
- package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
- package/unit-of-work/CommitOrderCalculator.js +89 -88
- package/unit-of-work/IdentityMap.d.ts +31 -31
- package/unit-of-work/IdentityMap.js +105 -105
- package/unit-of-work/UnitOfWork.d.ts +181 -141
- package/unit-of-work/UnitOfWork.js +1200 -1183
- package/utils/AbstractMigrator.d.ts +111 -91
- package/utils/AbstractMigrator.js +275 -275
- package/utils/AbstractSchemaGenerator.d.ts +43 -34
- package/utils/AbstractSchemaGenerator.js +121 -122
- package/utils/AsyncContext.d.ts +3 -3
- package/utils/AsyncContext.js +34 -35
- package/utils/Configuration.d.ts +852 -808
- package/utils/Configuration.js +359 -344
- package/utils/Cursor.d.ts +40 -22
- package/utils/Cursor.js +135 -127
- package/utils/DataloaderUtils.d.ts +58 -43
- package/utils/DataloaderUtils.js +203 -198
- package/utils/EntityComparator.d.ts +98 -81
- package/utils/EntityComparator.js +828 -728
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +3 -3
- package/utils/QueryHelper.d.ts +79 -51
- package/utils/QueryHelper.js +372 -361
- package/utils/RawQueryFragment.d.ts +50 -34
- package/utils/RawQueryFragment.js +107 -105
- package/utils/RequestContext.d.ts +32 -32
- package/utils/RequestContext.js +52 -53
- package/utils/TransactionContext.d.ts +16 -16
- package/utils/TransactionContext.js +27 -27
- package/utils/TransactionManager.d.ts +58 -58
- package/utils/TransactionManager.js +199 -197
- package/utils/Utils.d.ts +204 -145
- package/utils/Utils.js +812 -810
- package/utils/clone.js +104 -113
- package/utils/env-vars.js +90 -88
- package/utils/fs-utils.d.ts +15 -15
- package/utils/fs-utils.js +180 -181
- package/utils/upsert-utils.d.ts +20 -5
- package/utils/upsert-utils.js +114 -116
|
@@ -8,447 +8,468 @@ import { OptimisticLockError, ValidationError } from '../errors.js';
|
|
|
8
8
|
import { ReferenceKind } from '../enums.js';
|
|
9
9
|
/** @internal Executes change sets against the database, handling inserts, updates, and deletes. */
|
|
10
10
|
export class ChangeSetPersister {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
const meta = changeSets[0].meta;
|
|
37
|
-
changeSets.forEach(changeSet => this.processProperties(changeSet));
|
|
38
|
-
if (changeSets.length > 1 && this.#config.get('useBatchInserts', this.#platform.usesBatchInserts())) {
|
|
39
|
-
return this.persistNewEntities(meta, changeSets, options);
|
|
40
|
-
}
|
|
41
|
-
for (const changeSet of changeSets) {
|
|
42
|
-
await this.persistNewEntity(meta, changeSet, options);
|
|
43
|
-
}
|
|
11
|
+
#platform;
|
|
12
|
+
#comparator;
|
|
13
|
+
#usesReturningStatement;
|
|
14
|
+
#driver;
|
|
15
|
+
#metadata;
|
|
16
|
+
#hydrator;
|
|
17
|
+
#factory;
|
|
18
|
+
#config;
|
|
19
|
+
#em;
|
|
20
|
+
constructor(em) {
|
|
21
|
+
this.#em = em;
|
|
22
|
+
this.#driver = this.#em.getDriver();
|
|
23
|
+
this.#config = this.#em.config;
|
|
24
|
+
this.#metadata = this.#em.getMetadata();
|
|
25
|
+
this.#factory = this.#em.getEntityFactory();
|
|
26
|
+
this.#platform = this.#driver.getPlatform();
|
|
27
|
+
this.#hydrator = this.#config.getHydrator(this.#metadata);
|
|
28
|
+
this.#comparator = this.#config.getComparator(this.#metadata);
|
|
29
|
+
this.#usesReturningStatement = this.#platform.usesReturningStatement() || this.#platform.usesOutputStatement();
|
|
30
|
+
}
|
|
31
|
+
/** Executes all pending INSERT change sets, using batch inserts when possible. */
|
|
32
|
+
async executeInserts(changeSets, options, withSchema) {
|
|
33
|
+
if (!withSchema) {
|
|
34
|
+
return this.runForEachSchema(changeSets, 'executeInserts', options);
|
|
44
35
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
const meta = changeSets[0].meta;
|
|
51
|
-
changeSets.forEach(changeSet => this.processProperties(changeSet));
|
|
52
|
-
// For STI with conflicting fieldNames (renamedFrom properties), we can't batch mixed child types
|
|
53
|
-
const hasSTIConflicts = meta.root.props.some(p => p.renamedFrom);
|
|
54
|
-
const hasMixedTypes = hasSTIConflicts && changeSets.some(cs => cs.meta.class !== meta.class);
|
|
55
|
-
if (batched &&
|
|
56
|
-
changeSets.length > 1 &&
|
|
57
|
-
!hasMixedTypes &&
|
|
58
|
-
this.#config.get('useBatchUpdates', this.#platform.usesBatchUpdates())) {
|
|
59
|
-
return this.persistManagedEntities(meta, changeSets, options);
|
|
60
|
-
}
|
|
61
|
-
for (const changeSet of changeSets) {
|
|
62
|
-
await this.persistManagedEntity(changeSet, options);
|
|
63
|
-
}
|
|
36
|
+
const meta = changeSets[0].meta;
|
|
37
|
+
changeSets.forEach(changeSet => this.processProperties(changeSet));
|
|
38
|
+
if (changeSets.length > 1 && this.#config.get('useBatchInserts', this.#platform.usesBatchInserts())) {
|
|
39
|
+
return this.persistNewEntities(meta, changeSets, options);
|
|
64
40
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (!withSchema) {
|
|
68
|
-
return this.runForEachSchema(changeSets, 'executeDeletes', options);
|
|
69
|
-
}
|
|
70
|
-
const size = this.#config.get('batchSize');
|
|
71
|
-
const meta = changeSets[0].meta;
|
|
72
|
-
const pk = Utils.getPrimaryKeyHash(meta.primaryKeys);
|
|
73
|
-
for (let i = 0; i < changeSets.length; i += size) {
|
|
74
|
-
const chunk = changeSets.slice(i, i + size);
|
|
75
|
-
const pks = chunk.map(cs => cs.getPrimaryKey());
|
|
76
|
-
options = this.prepareOptions(meta, options);
|
|
77
|
-
await this.#driver.nativeDelete(meta.root.class, { [pk]: { $in: pks } }, options);
|
|
78
|
-
}
|
|
41
|
+
for (const changeSet of changeSets) {
|
|
42
|
+
await this.persistNewEntity(meta, changeSet, options);
|
|
79
43
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
groups.set(cs.schema, group);
|
|
86
|
-
});
|
|
87
|
-
for (const [key, group] of groups.entries()) {
|
|
88
|
-
options = { ...options, schema: key };
|
|
89
|
-
// @ts-ignore
|
|
90
|
-
await this[method](group, ...args, options, true);
|
|
91
|
-
}
|
|
44
|
+
}
|
|
45
|
+
/** Executes all pending UPDATE change sets, using batch updates when possible. */
|
|
46
|
+
async executeUpdates(changeSets, batched, options, withSchema) {
|
|
47
|
+
if (!withSchema) {
|
|
48
|
+
return this.runForEachSchema(changeSets, 'executeUpdates', options, batched);
|
|
92
49
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
prop.type !== 'ObjectId' &&
|
|
106
|
-
prop.persist !== false &&
|
|
107
|
-
entity[prop.name] == null) {
|
|
108
|
-
throw ValidationError.propertyRequired(entity, prop);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
50
|
+
const meta = changeSets[0].meta;
|
|
51
|
+
changeSets.forEach(changeSet => this.processProperties(changeSet));
|
|
52
|
+
// For STI with conflicting fieldNames (renamedFrom properties), we can't batch mixed child types
|
|
53
|
+
const hasSTIConflicts = meta.root.props.some(p => p.renamedFrom);
|
|
54
|
+
const hasMixedTypes = hasSTIConflicts && changeSets.some(cs => cs.meta.class !== meta.class);
|
|
55
|
+
if (
|
|
56
|
+
batched &&
|
|
57
|
+
changeSets.length > 1 &&
|
|
58
|
+
!hasMixedTypes &&
|
|
59
|
+
this.#config.get('useBatchUpdates', this.#platform.usesBatchUpdates())
|
|
60
|
+
) {
|
|
61
|
+
return this.persistManagedEntities(meta, changeSets, options);
|
|
111
62
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
for (const prop of meta.relations) {
|
|
115
|
-
this.processProperty(changeSet, prop);
|
|
116
|
-
}
|
|
117
|
-
if (changeSet.type === ChangeSetType.CREATE && this.#config.get('validateRequired')) {
|
|
118
|
-
this.validateRequired(changeSet.entity);
|
|
119
|
-
}
|
|
63
|
+
for (const changeSet of changeSets) {
|
|
64
|
+
await this.persistManagedEntity(changeSet, options);
|
|
120
65
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
this.resolveTPTIdentifiers(changeSet);
|
|
127
|
-
// Use changeSet's own meta for STI entities to get correct field mappings
|
|
128
|
-
const res = await this.#driver.nativeInsertMany(changeSet.meta.class, [changeSet.payload], options);
|
|
129
|
-
if (!wrapped.hasPrimaryKey()) {
|
|
130
|
-
this.mapPrimaryKey(meta, res.insertId ?? res.row?.[meta.primaryKeys[0]], changeSet);
|
|
131
|
-
}
|
|
132
|
-
this.mapReturnedValues(changeSet.entity, changeSet.payload, res.row, meta);
|
|
133
|
-
this.markAsPopulated(changeSet, meta);
|
|
134
|
-
wrapped.__initialized = true;
|
|
135
|
-
wrapped.__managed = true;
|
|
136
|
-
if (!this.#usesReturningStatement) {
|
|
137
|
-
await this.reloadVersionValues(meta, [changeSet], options);
|
|
138
|
-
}
|
|
139
|
-
changeSet.persisted = true;
|
|
140
|
-
}
|
|
141
|
-
async persistNewEntities(meta, changeSets, options) {
|
|
142
|
-
const size = this.#config.get('batchSize');
|
|
143
|
-
for (let i = 0; i < changeSets.length; i += size) {
|
|
144
|
-
const chunk = changeSets.slice(i, i + size);
|
|
145
|
-
await this.persistNewEntitiesBatch(meta, chunk, options);
|
|
146
|
-
if (!this.#usesReturningStatement) {
|
|
147
|
-
await this.reloadVersionValues(meta, chunk, options);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
66
|
+
}
|
|
67
|
+
/** Executes all pending DELETE change sets in batches. */
|
|
68
|
+
async executeDeletes(changeSets, options, withSchema) {
|
|
69
|
+
if (!withSchema) {
|
|
70
|
+
return this.runForEachSchema(changeSets, 'executeDeletes', options);
|
|
150
71
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
async persistNewEntitiesBatch(meta, changeSets, options) {
|
|
161
|
-
options = this.prepareOptions(meta, options, {
|
|
162
|
-
convertCustomTypes: false,
|
|
163
|
-
processCollections: false,
|
|
164
|
-
});
|
|
165
|
-
for (const changeSet of changeSets) {
|
|
166
|
-
this.resolveTPTIdentifiers(changeSet);
|
|
167
|
-
}
|
|
168
|
-
const res = await this.#driver.nativeInsertMany(meta.class, changeSets.map(cs => cs.payload), options);
|
|
169
|
-
for (let i = 0; i < changeSets.length; i++) {
|
|
170
|
-
const changeSet = changeSets[i];
|
|
171
|
-
const wrapped = helper(changeSet.entity);
|
|
172
|
-
if (!wrapped.hasPrimaryKey()) {
|
|
173
|
-
const field = meta.getPrimaryProps()[0].fieldNames[0];
|
|
174
|
-
this.mapPrimaryKey(meta, res.rows[i][field], changeSet);
|
|
175
|
-
}
|
|
176
|
-
if (res.rows) {
|
|
177
|
-
this.mapReturnedValues(changeSet.entity, changeSet.payload, res.rows[i], meta);
|
|
178
|
-
}
|
|
179
|
-
this.markAsPopulated(changeSet, meta);
|
|
180
|
-
wrapped.__initialized = true;
|
|
181
|
-
wrapped.__managed = true;
|
|
182
|
-
changeSet.persisted = true;
|
|
183
|
-
}
|
|
72
|
+
const size = this.#config.get('batchSize');
|
|
73
|
+
const meta = changeSets[0].meta;
|
|
74
|
+
const pk = Utils.getPrimaryKeyHash(meta.primaryKeys);
|
|
75
|
+
for (let i = 0; i < changeSets.length; i += size) {
|
|
76
|
+
const chunk = changeSets.slice(i, i + size);
|
|
77
|
+
const pks = chunk.map(cs => cs.getPrimaryKey());
|
|
78
|
+
options = this.prepareOptions(meta, options);
|
|
79
|
+
await this.#driver.nativeDelete(meta.root.class, { [pk]: { $in: pks } }, options);
|
|
184
80
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
await this.persistManagedEntitiesBatch(meta, chunk, options);
|
|
198
|
-
await this.reloadVersionValues(meta, chunk, options);
|
|
199
|
-
}
|
|
81
|
+
}
|
|
82
|
+
async runForEachSchema(changeSets, method, options, ...args) {
|
|
83
|
+
const groups = new Map();
|
|
84
|
+
changeSets.forEach(cs => {
|
|
85
|
+
const group = groups.get(cs.schema) ?? [];
|
|
86
|
+
group.push(cs);
|
|
87
|
+
groups.set(cs.schema, group);
|
|
88
|
+
});
|
|
89
|
+
for (const [key, group] of groups.entries()) {
|
|
90
|
+
options = { ...options, schema: key };
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
await this[method](group, ...args, options, true);
|
|
200
93
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
94
|
+
}
|
|
95
|
+
validateRequired(entity) {
|
|
96
|
+
const wrapped = helper(entity);
|
|
97
|
+
for (const prop of wrapped.__meta.props) {
|
|
98
|
+
if (
|
|
99
|
+
!prop.nullable &&
|
|
100
|
+
!prop.autoincrement &&
|
|
101
|
+
!prop.default &&
|
|
102
|
+
!prop.defaultRaw &&
|
|
103
|
+
!prop.onCreate &&
|
|
104
|
+
!prop.generated &&
|
|
105
|
+
!prop.embedded &&
|
|
106
|
+
![ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind) &&
|
|
107
|
+
prop.name !== wrapped.__meta.root.discriminatorColumn &&
|
|
108
|
+
prop.type !== 'ObjectId' &&
|
|
109
|
+
prop.persist !== false &&
|
|
110
|
+
entity[prop.name] == null
|
|
111
|
+
) {
|
|
112
|
+
throw ValidationError.propertyRequired(entity, prop);
|
|
113
|
+
}
|
|
212
114
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
});
|
|
219
|
-
const cond = [];
|
|
220
|
-
const payload = [];
|
|
221
|
-
for (const changeSet of changeSets) {
|
|
222
|
-
const where = changeSet.getPrimaryKey(true);
|
|
223
|
-
this.checkConcurrencyKeys(meta, changeSet, where);
|
|
224
|
-
cond.push(where);
|
|
225
|
-
payload.push(changeSet.payload);
|
|
226
|
-
}
|
|
227
|
-
const res = await this.#driver.nativeUpdateMany(meta.class, cond, payload, options);
|
|
228
|
-
const map = new Map();
|
|
229
|
-
res.rows?.forEach(item => map.set(Utils.getCompositeKeyHash(item, meta, true, this.#platform, true), item));
|
|
230
|
-
for (const changeSet of changeSets) {
|
|
231
|
-
if (res.rows) {
|
|
232
|
-
const row = map.get(helper(changeSet.entity).getSerializedPrimaryKey());
|
|
233
|
-
this.mapReturnedValues(changeSet.entity, changeSet.payload, row, meta);
|
|
234
|
-
}
|
|
235
|
-
changeSet.persisted = true;
|
|
236
|
-
}
|
|
115
|
+
}
|
|
116
|
+
processProperties(changeSet) {
|
|
117
|
+
const meta = changeSet.meta;
|
|
118
|
+
for (const prop of meta.relations) {
|
|
119
|
+
this.processProperty(changeSet, prop);
|
|
237
120
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const insertId = prop.customType ? prop.customType.convertToJSValue(value, this.#platform) : value;
|
|
241
|
-
const wrapped = helper(changeSet.entity);
|
|
242
|
-
if (!wrapped.hasPrimaryKey()) {
|
|
243
|
-
wrapped.setPrimaryKey(insertId);
|
|
244
|
-
}
|
|
245
|
-
// some drivers might be returning bigint PKs as numbers when the number is small enough,
|
|
246
|
-
// but we need to have it as string so comparison works in change set tracking, so instead
|
|
247
|
-
// of using the raw value from db, we convert it back to the db value explicitly
|
|
248
|
-
value = prop.customType
|
|
249
|
-
? prop.customType.convertToDatabaseValue(insertId, this.#platform, { mode: 'serialization' })
|
|
250
|
-
: value;
|
|
251
|
-
changeSet.payload[wrapped.__meta.primaryKeys[0]] = value;
|
|
252
|
-
if (wrapped.__identifier && !Array.isArray(wrapped.__identifier)) {
|
|
253
|
-
wrapped.__identifier.setValue(value);
|
|
254
|
-
}
|
|
121
|
+
if (changeSet.type === ChangeSetType.CREATE && this.#config.get('validateRequired')) {
|
|
122
|
+
this.validateRequired(changeSet.entity);
|
|
255
123
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
if (Utils.isEntity(value, true)) {
|
|
268
|
-
value.__helper.populated();
|
|
269
|
-
}
|
|
270
|
-
else if (Utils.isCollection(value)) {
|
|
271
|
-
value.populated();
|
|
272
|
-
}
|
|
273
|
-
});
|
|
124
|
+
}
|
|
125
|
+
async persistNewEntity(meta, changeSet, options) {
|
|
126
|
+
const wrapped = helper(changeSet.entity);
|
|
127
|
+
options = this.prepareOptions(meta, options, {
|
|
128
|
+
convertCustomTypes: false,
|
|
129
|
+
});
|
|
130
|
+
this.resolveTPTIdentifiers(changeSet);
|
|
131
|
+
// Use changeSet's own meta for STI entities to get correct field mappings
|
|
132
|
+
const res = await this.#driver.nativeInsertMany(changeSet.meta.class, [changeSet.payload], options);
|
|
133
|
+
if (!wrapped.hasPrimaryKey()) {
|
|
134
|
+
this.mapPrimaryKey(meta, res.insertId ?? res.row?.[meta.primaryKeys[0]], changeSet);
|
|
274
135
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
(!meta.versionProperty || changeSet.entity[meta.versionProperty] == null)) {
|
|
282
|
-
return this.#driver.nativeUpdate(changeSet.meta.class, cond, changeSet.payload, options);
|
|
283
|
-
}
|
|
284
|
-
if (meta.versionProperty) {
|
|
285
|
-
cond[meta.versionProperty] = this.#platform.convertVersionValue(changeSet.entity[meta.versionProperty], meta.properties[meta.versionProperty]);
|
|
286
|
-
}
|
|
287
|
-
this.checkConcurrencyKeys(meta, changeSet, cond);
|
|
288
|
-
return this.#driver.nativeUpdate(changeSet.meta.class, cond, changeSet.payload, options);
|
|
136
|
+
this.mapReturnedValues(changeSet.entity, changeSet.payload, res.row, meta);
|
|
137
|
+
this.markAsPopulated(changeSet, meta);
|
|
138
|
+
wrapped.__initialized = true;
|
|
139
|
+
wrapped.__managed = true;
|
|
140
|
+
if (!this.#usesReturningStatement) {
|
|
141
|
+
await this.reloadVersionValues(meta, [changeSet], options);
|
|
289
142
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
// @ts-ignore
|
|
301
|
-
cond[meta.versionProperty] = this.#platform.convertVersionValue(cs.entity[meta.versionProperty], meta.properties[meta.versionProperty]);
|
|
302
|
-
}
|
|
303
|
-
return cond;
|
|
304
|
-
});
|
|
305
|
-
const primaryKeys = meta.primaryKeys.concat(...meta.concurrencyCheckKeys);
|
|
306
|
-
options = this.prepareOptions(meta, options, {
|
|
307
|
-
fields: primaryKeys,
|
|
308
|
-
});
|
|
309
|
-
const res = await this.#driver.find(meta.root.class, { $or }, options);
|
|
310
|
-
if (res.length !== changeSets.length) {
|
|
311
|
-
const compare = (a, b, keys) => keys.every(k => a[k] === b[k]);
|
|
312
|
-
const entity = changeSets.find(cs => {
|
|
313
|
-
return !res.some(row => compare(Utils.getPrimaryKeyCond(cs.entity, primaryKeys), row, primaryKeys));
|
|
314
|
-
}).entity;
|
|
315
|
-
throw OptimisticLockError.lockFailed(entity);
|
|
316
|
-
}
|
|
143
|
+
changeSet.persisted = true;
|
|
144
|
+
}
|
|
145
|
+
async persistNewEntities(meta, changeSets, options) {
|
|
146
|
+
const size = this.#config.get('batchSize');
|
|
147
|
+
for (let i = 0; i < changeSets.length; i += size) {
|
|
148
|
+
const chunk = changeSets.slice(i, i + size);
|
|
149
|
+
await this.persistNewEntitiesBatch(meta, chunk, options);
|
|
150
|
+
if (!this.#usesReturningStatement) {
|
|
151
|
+
await this.reloadVersionValues(meta, chunk, options);
|
|
152
|
+
}
|
|
317
153
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
154
|
+
}
|
|
155
|
+
prepareOptions(meta, options, additionalOptions) {
|
|
156
|
+
const loggerContext = Utils.merge(
|
|
157
|
+
{ id: this.#em._id },
|
|
158
|
+
this.#em.getLoggerContext({ disableContextResolution: true }),
|
|
159
|
+
);
|
|
160
|
+
return {
|
|
161
|
+
...options,
|
|
162
|
+
...additionalOptions,
|
|
163
|
+
schema: options?.schema ?? meta.schema,
|
|
164
|
+
loggerContext,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
async persistNewEntitiesBatch(meta, changeSets, options) {
|
|
168
|
+
options = this.prepareOptions(meta, options, {
|
|
169
|
+
convertCustomTypes: false,
|
|
170
|
+
processCollections: false,
|
|
171
|
+
});
|
|
172
|
+
for (const changeSet of changeSets) {
|
|
173
|
+
this.resolveTPTIdentifiers(changeSet);
|
|
322
174
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
if (prop.autoincrement || prop.generated || prop.defaultRaw) {
|
|
343
|
-
reloadProps.push(prop);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
if (changeSets[0].type === ChangeSetType.UPDATE) {
|
|
348
|
-
const returning = new Set();
|
|
349
|
-
changeSets.forEach(cs => {
|
|
350
|
-
Utils.keys(cs.payload).forEach(k => {
|
|
351
|
-
if (isRaw(cs.payload[k]) && isRaw(cs.entity[k])) {
|
|
352
|
-
returning.add(meta.properties[k]);
|
|
353
|
-
}
|
|
354
|
-
});
|
|
355
|
-
});
|
|
356
|
-
// reload generated columns
|
|
357
|
-
if (!this.#usesReturningStatement) {
|
|
358
|
-
meta.props.filter(prop => prop.generated && !prop.primary).forEach(prop => reloadProps.push(prop));
|
|
359
|
-
reloadProps.push(...returning);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
if (reloadProps.length === 0) {
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
reloadProps.unshift(...meta.getPrimaryProps());
|
|
366
|
-
const pk = Utils.getPrimaryKeyHash(meta.primaryKeys);
|
|
367
|
-
const pks = changeSets.map(cs => {
|
|
368
|
-
const val = helper(cs.entity).getPrimaryKey(true);
|
|
369
|
-
if (Utils.isPlainObject(val)) {
|
|
370
|
-
return Utils.getCompositeKeyValue(val, meta, false, this.#platform);
|
|
371
|
-
}
|
|
372
|
-
return val;
|
|
373
|
-
});
|
|
374
|
-
options = this.prepareOptions(meta, options, {
|
|
375
|
-
fields: Utils.unique(reloadProps.map(prop => prop.name)),
|
|
376
|
-
});
|
|
377
|
-
const data = await this.#driver.find(meta.class, { [pk]: { $in: pks } }, options);
|
|
378
|
-
const map = new Map();
|
|
379
|
-
data.forEach(item => map.set(Utils.getCompositeKeyHash(item, meta, false, this.#platform, true), item));
|
|
380
|
-
for (const changeSet of changeSets) {
|
|
381
|
-
const data = map.get(helper(changeSet.entity).getSerializedPrimaryKey());
|
|
382
|
-
this.#hydrator.hydrate(changeSet.entity, meta, data, this.#factory, 'full', false, true);
|
|
383
|
-
Object.assign(changeSet.payload, data); // merge to the changeset payload, so it gets saved to the entity snapshot
|
|
384
|
-
}
|
|
175
|
+
const res = await this.#driver.nativeInsertMany(
|
|
176
|
+
meta.class,
|
|
177
|
+
changeSets.map(cs => cs.payload),
|
|
178
|
+
options,
|
|
179
|
+
);
|
|
180
|
+
for (let i = 0; i < changeSets.length; i++) {
|
|
181
|
+
const changeSet = changeSets[i];
|
|
182
|
+
const wrapped = helper(changeSet.entity);
|
|
183
|
+
if (!wrapped.hasPrimaryKey()) {
|
|
184
|
+
const field = meta.getPrimaryProps()[0].fieldNames[0];
|
|
185
|
+
this.mapPrimaryKey(meta, res.rows[i][field], changeSet);
|
|
186
|
+
}
|
|
187
|
+
if (res.rows) {
|
|
188
|
+
this.mapReturnedValues(changeSet.entity, changeSet.payload, res.rows[i], meta);
|
|
189
|
+
}
|
|
190
|
+
this.markAsPopulated(changeSet, meta);
|
|
191
|
+
wrapped.__initialized = true;
|
|
192
|
+
wrapped.__managed = true;
|
|
193
|
+
changeSet.persisted = true;
|
|
385
194
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
195
|
+
}
|
|
196
|
+
async persistManagedEntity(changeSet, options) {
|
|
197
|
+
const meta = changeSet.meta;
|
|
198
|
+
const res = await this.updateEntity(meta, changeSet, options);
|
|
199
|
+
this.checkOptimisticLock(meta, changeSet, res);
|
|
200
|
+
this.mapReturnedValues(changeSet.entity, changeSet.payload, res.row, meta);
|
|
201
|
+
await this.reloadVersionValues(meta, [changeSet], options);
|
|
202
|
+
changeSet.persisted = true;
|
|
203
|
+
}
|
|
204
|
+
async persistManagedEntities(meta, changeSets, options) {
|
|
205
|
+
const size = this.#config.get('batchSize');
|
|
206
|
+
for (let i = 0; i < changeSets.length; i += size) {
|
|
207
|
+
const chunk = changeSets.slice(i, i + size);
|
|
208
|
+
await this.persistManagedEntitiesBatch(meta, chunk, options);
|
|
209
|
+
await this.reloadVersionValues(meta, chunk, options);
|
|
400
210
|
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
211
|
+
}
|
|
212
|
+
checkConcurrencyKeys(meta, changeSet, cond) {
|
|
213
|
+
const tmp = [];
|
|
214
|
+
for (const key of meta.concurrencyCheckKeys) {
|
|
215
|
+
cond[key] = changeSet.originalEntity[key];
|
|
216
|
+
if (changeSet.payload[key]) {
|
|
217
|
+
tmp.push(key);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (tmp.length === 0 && meta.concurrencyCheckKeys.size > 0) {
|
|
221
|
+
throw OptimisticLockError.lockFailed(changeSet.entity);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async persistManagedEntitiesBatch(meta, changeSets, options) {
|
|
225
|
+
await this.checkOptimisticLocks(meta, changeSets, options);
|
|
226
|
+
options = this.prepareOptions(meta, options, {
|
|
227
|
+
convertCustomTypes: false,
|
|
228
|
+
processCollections: false,
|
|
229
|
+
});
|
|
230
|
+
const cond = [];
|
|
231
|
+
const payload = [];
|
|
232
|
+
for (const changeSet of changeSets) {
|
|
233
|
+
const where = changeSet.getPrimaryKey(true);
|
|
234
|
+
this.checkConcurrencyKeys(meta, changeSet, where);
|
|
235
|
+
cond.push(where);
|
|
236
|
+
payload.push(changeSet.payload);
|
|
237
|
+
}
|
|
238
|
+
const res = await this.#driver.nativeUpdateMany(meta.class, cond, payload, options);
|
|
239
|
+
const map = new Map();
|
|
240
|
+
res.rows?.forEach(item => map.set(Utils.getCompositeKeyHash(item, meta, true, this.#platform, true), item));
|
|
241
|
+
for (const changeSet of changeSets) {
|
|
242
|
+
if (res.rows) {
|
|
243
|
+
const row = map.get(helper(changeSet.entity).getSerializedPrimaryKey());
|
|
244
|
+
this.mapReturnedValues(changeSet.entity, changeSet.payload, row, meta);
|
|
245
|
+
}
|
|
246
|
+
changeSet.persisted = true;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
mapPrimaryKey(meta, value, changeSet) {
|
|
250
|
+
const prop = meta.properties[meta.primaryKeys[0]];
|
|
251
|
+
const insertId = prop.customType ? prop.customType.convertToJSValue(value, this.#platform) : value;
|
|
252
|
+
const wrapped = helper(changeSet.entity);
|
|
253
|
+
if (!wrapped.hasPrimaryKey()) {
|
|
254
|
+
wrapped.setPrimaryKey(insertId);
|
|
255
|
+
}
|
|
256
|
+
// some drivers might be returning bigint PKs as numbers when the number is small enough,
|
|
257
|
+
// but we need to have it as string so comparison works in change set tracking, so instead
|
|
258
|
+
// of using the raw value from db, we convert it back to the db value explicitly
|
|
259
|
+
value = prop.customType
|
|
260
|
+
? prop.customType.convertToDatabaseValue(insertId, this.#platform, { mode: 'serialization' })
|
|
261
|
+
: value;
|
|
262
|
+
changeSet.payload[wrapped.__meta.primaryKeys[0]] = value;
|
|
263
|
+
if (wrapped.__identifier && !Array.isArray(wrapped.__identifier)) {
|
|
264
|
+
wrapped.__identifier.setValue(value);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Sets populate flag to new entities so they are serialized like if they were loaded from the db
|
|
269
|
+
*/
|
|
270
|
+
markAsPopulated(changeSet, meta) {
|
|
271
|
+
helper(changeSet.entity).__schema = this.#driver.getSchemaName(meta, changeSet);
|
|
272
|
+
if (!this.#config.get('populateAfterFlush')) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
helper(changeSet.entity).populated();
|
|
276
|
+
meta.relations.forEach(prop => {
|
|
277
|
+
const value = changeSet.entity[prop.name];
|
|
278
|
+
if (Utils.isEntity(value, true)) {
|
|
279
|
+
value.__helper.populated();
|
|
280
|
+
} else if (Utils.isCollection(value)) {
|
|
281
|
+
value.populated();
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
async updateEntity(meta, changeSet, options) {
|
|
286
|
+
const cond = changeSet.getPrimaryKey(true);
|
|
287
|
+
options = this.prepareOptions(meta, options, {
|
|
288
|
+
convertCustomTypes: false,
|
|
289
|
+
});
|
|
290
|
+
if (
|
|
291
|
+
meta.concurrencyCheckKeys.size === 0 &&
|
|
292
|
+
(!meta.versionProperty || changeSet.entity[meta.versionProperty] == null)
|
|
293
|
+
) {
|
|
294
|
+
return this.#driver.nativeUpdate(changeSet.meta.class, cond, changeSet.payload, options);
|
|
295
|
+
}
|
|
296
|
+
if (meta.versionProperty) {
|
|
297
|
+
cond[meta.versionProperty] = this.#platform.convertVersionValue(
|
|
298
|
+
changeSet.entity[meta.versionProperty],
|
|
299
|
+
meta.properties[meta.versionProperty],
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
this.checkConcurrencyKeys(meta, changeSet, cond);
|
|
303
|
+
return this.#driver.nativeUpdate(changeSet.meta.class, cond, changeSet.payload, options);
|
|
304
|
+
}
|
|
305
|
+
async checkOptimisticLocks(meta, changeSets, options) {
|
|
306
|
+
if (
|
|
307
|
+
meta.concurrencyCheckKeys.size === 0 &&
|
|
308
|
+
(!meta.versionProperty || changeSets.every(cs => cs.entity[meta.versionProperty] == null))
|
|
309
|
+
) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
// skip entity references as they don't have version values loaded
|
|
313
|
+
changeSets = changeSets.filter(cs => helper(cs.entity).__initialized);
|
|
314
|
+
const $or = changeSets.map(cs => {
|
|
315
|
+
const cond = Utils.getPrimaryKeyCond(cs.originalEntity, meta.primaryKeys.concat(...meta.concurrencyCheckKeys));
|
|
316
|
+
if (meta.versionProperty) {
|
|
317
|
+
// @ts-ignore
|
|
318
|
+
cond[meta.versionProperty] = this.#platform.convertVersionValue(
|
|
319
|
+
cs.entity[meta.versionProperty],
|
|
320
|
+
meta.properties[meta.versionProperty],
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
return cond;
|
|
324
|
+
});
|
|
325
|
+
const primaryKeys = meta.primaryKeys.concat(...meta.concurrencyCheckKeys);
|
|
326
|
+
options = this.prepareOptions(meta, options, {
|
|
327
|
+
fields: primaryKeys,
|
|
328
|
+
});
|
|
329
|
+
const res = await this.#driver.find(meta.root.class, { $or }, options);
|
|
330
|
+
if (res.length !== changeSets.length) {
|
|
331
|
+
const compare = (a, b, keys) => keys.every(k => a[k] === b[k]);
|
|
332
|
+
const entity = changeSets.find(cs => {
|
|
333
|
+
return !res.some(row => compare(Utils.getPrimaryKeyCond(cs.entity, primaryKeys), row, primaryKeys));
|
|
334
|
+
}).entity;
|
|
335
|
+
throw OptimisticLockError.lockFailed(entity);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
checkOptimisticLock(meta, changeSet, res) {
|
|
339
|
+
if ((meta.versionProperty || meta.concurrencyCheckKeys.size > 0) && res && !res.affectedRows) {
|
|
340
|
+
throw OptimisticLockError.lockFailed(changeSet.entity);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* This method also handles reloading of database default values for inserts and raw property updates,
|
|
345
|
+
* so we use a single query in case of both versioning and default values is used.
|
|
346
|
+
*/
|
|
347
|
+
async reloadVersionValues(meta, changeSets, options) {
|
|
348
|
+
const reloadProps =
|
|
349
|
+
meta.versionProperty && !this.#usesReturningStatement ? [meta.properties[meta.versionProperty]] : [];
|
|
350
|
+
if (changeSets[0].type === ChangeSetType.CREATE) {
|
|
351
|
+
for (const prop of meta.props) {
|
|
352
|
+
if (prop.persist === false) {
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
if (isRaw(changeSets[0].entity[prop.name])) {
|
|
356
|
+
reloadProps.push(prop);
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
// do not reload things that already had a runtime value
|
|
360
|
+
if (changeSets[0].entity[prop.name] != null || prop.defaultRaw === 'null') {
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (prop.autoincrement || prop.generated || prop.defaultRaw) {
|
|
364
|
+
reloadProps.push(prop);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (changeSets[0].type === ChangeSetType.UPDATE) {
|
|
369
|
+
const returning = new Set();
|
|
370
|
+
changeSets.forEach(cs => {
|
|
371
|
+
Utils.keys(cs.payload).forEach(k => {
|
|
372
|
+
if (isRaw(cs.payload[k]) && isRaw(cs.entity[k])) {
|
|
373
|
+
returning.add(meta.properties[k]);
|
|
374
|
+
}
|
|
430
375
|
});
|
|
376
|
+
});
|
|
377
|
+
// reload generated columns
|
|
378
|
+
if (!this.#usesReturningStatement) {
|
|
379
|
+
meta.props.filter(prop => prop.generated && !prop.primary).forEach(prop => reloadProps.push(prop));
|
|
380
|
+
reloadProps.push(...returning);
|
|
381
|
+
}
|
|
431
382
|
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
383
|
+
if (reloadProps.length === 0) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
reloadProps.unshift(...meta.getPrimaryProps());
|
|
387
|
+
const pk = Utils.getPrimaryKeyHash(meta.primaryKeys);
|
|
388
|
+
const pks = changeSets.map(cs => {
|
|
389
|
+
const val = helper(cs.entity).getPrimaryKey(true);
|
|
390
|
+
if (Utils.isPlainObject(val)) {
|
|
391
|
+
return Utils.getCompositeKeyValue(val, meta, false, this.#platform);
|
|
392
|
+
}
|
|
393
|
+
return val;
|
|
394
|
+
});
|
|
395
|
+
options = this.prepareOptions(meta, options, {
|
|
396
|
+
fields: Utils.unique(reloadProps.map(prop => prop.name)),
|
|
397
|
+
});
|
|
398
|
+
const data = await this.#driver.find(meta.class, { [pk]: { $in: pks } }, options);
|
|
399
|
+
const map = new Map();
|
|
400
|
+
data.forEach(item => map.set(Utils.getCompositeKeyHash(item, meta, false, this.#platform, true), item));
|
|
401
|
+
for (const changeSet of changeSets) {
|
|
402
|
+
const data = map.get(helper(changeSet.entity).getSerializedPrimaryKey());
|
|
403
|
+
this.#hydrator.hydrate(changeSet.entity, meta, data, this.#factory, 'full', false, true);
|
|
404
|
+
Object.assign(changeSet.payload, data); // merge to the changeset payload, so it gets saved to the entity snapshot
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* For TPT child tables, resolve EntityIdentifier values in PK fields.
|
|
409
|
+
* The parent table insert assigns the actual PK value, which the child table references.
|
|
410
|
+
*/
|
|
411
|
+
resolveTPTIdentifiers(changeSet) {
|
|
412
|
+
if (changeSet.meta.inheritanceType !== 'tpt' || !changeSet.meta.tptParent) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
for (const pk of changeSet.meta.primaryKeys) {
|
|
416
|
+
const value = changeSet.payload[pk];
|
|
417
|
+
if (value instanceof EntityIdentifier) {
|
|
418
|
+
changeSet.payload[pk] = value.getValue();
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
processProperty(changeSet, prop) {
|
|
423
|
+
const meta = changeSet.meta;
|
|
424
|
+
const value = changeSet.payload[prop.name]; // for inline embeddables
|
|
425
|
+
if (value instanceof EntityIdentifier) {
|
|
426
|
+
changeSet.payload[prop.name] = value.getValue();
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (value instanceof PolymorphicRef) {
|
|
430
|
+
if (value.id instanceof EntityIdentifier) {
|
|
431
|
+
value.id = value.id.getValue();
|
|
432
|
+
}
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (Array.isArray(value) && value.every(item => item instanceof EntityIdentifier)) {
|
|
436
|
+
changeSet.payload[prop.name] = value.map(item => item.getValue());
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY && Array.isArray(value)) {
|
|
440
|
+
changeSet.payload[prop.name] = value.map(val => (val instanceof EntityIdentifier ? val.getValue() : val));
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
if (prop.name in changeSet.payload) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
const values = Utils.unwrapProperty(changeSet.payload, meta, prop, true); // for object embeddables
|
|
447
|
+
values.forEach(([value, indexes]) => {
|
|
448
|
+
if (value instanceof EntityIdentifier) {
|
|
449
|
+
Utils.setPayloadProperty(changeSet.payload, meta, prop, value.getValue(), indexes);
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Maps values returned via `returning` statement (postgres) or the inserted id (other sql drivers).
|
|
455
|
+
* No need to handle composite keys here as they need to be set upfront.
|
|
456
|
+
* We do need to map to the change set payload too, as it will be used in the originalEntityData for new entities.
|
|
457
|
+
*/
|
|
458
|
+
mapReturnedValues(entity, payload, row, meta, upsert = false) {
|
|
459
|
+
if ((!this.#usesReturningStatement && !upsert) || !row || !Utils.hasObjectKeys(row)) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
const mapped = this.#comparator.mapResult(meta, row);
|
|
463
|
+
if (entity) {
|
|
464
|
+
this.#hydrator.hydrate(entity, meta, mapped, this.#factory, 'full', false, true);
|
|
465
|
+
}
|
|
466
|
+
if (upsert) {
|
|
467
|
+
for (const prop of meta.props) {
|
|
468
|
+
if (prop.customType && prop.name in mapped) {
|
|
469
|
+
mapped[prop.name] = prop.customType.convertToJSValue(mapped[prop.name], this.#platform);
|
|
451
470
|
}
|
|
452
|
-
|
|
471
|
+
}
|
|
453
472
|
}
|
|
473
|
+
Object.assign(payload, mapped);
|
|
474
|
+
}
|
|
454
475
|
}
|