@mikro-orm/core 7.0.0-dev.1 → 7.0.0-dev.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/EntityManager.d.ts +25 -16
- package/EntityManager.js +219 -209
- package/MikroORM.d.ts +7 -6
- package/MikroORM.js +33 -45
- package/cache/CacheAdapter.js +1 -2
- package/cache/FileCacheAdapter.d.ts +1 -1
- package/cache/FileCacheAdapter.js +18 -26
- package/cache/GeneratedCacheAdapter.d.ts +2 -2
- package/cache/GeneratedCacheAdapter.js +1 -5
- package/cache/MemoryCacheAdapter.d.ts +1 -1
- package/cache/MemoryCacheAdapter.js +1 -5
- package/cache/NullCacheAdapter.d.ts +1 -1
- package/cache/NullCacheAdapter.js +1 -5
- package/cache/index.d.ts +5 -5
- package/cache/index.js +5 -21
- package/connections/Connection.d.ts +7 -7
- package/connections/Connection.js +8 -12
- package/connections/index.d.ts +1 -1
- package/connections/index.js +1 -17
- package/decorators/Check.d.ts +2 -2
- package/decorators/Check.js +5 -8
- package/decorators/CreateRequestContext.d.ts +1 -1
- package/decorators/CreateRequestContext.js +13 -14
- package/decorators/Embeddable.d.ts +5 -5
- package/decorators/Embeddable.js +4 -7
- package/decorators/Embedded.d.ts +3 -3
- package/decorators/Embedded.js +10 -12
- package/decorators/Entity.d.ts +6 -6
- package/decorators/Entity.js +5 -9
- package/decorators/Enum.d.ts +3 -3
- package/decorators/Enum.js +7 -10
- package/decorators/Filter.d.ts +1 -1
- package/decorators/Filter.js +3 -6
- package/decorators/Formula.d.ts +2 -3
- package/decorators/Formula.js +7 -10
- package/decorators/Indexed.d.ts +10 -8
- package/decorators/Indexed.js +7 -11
- package/decorators/ManyToMany.d.ts +4 -4
- package/decorators/ManyToMany.js +10 -12
- package/decorators/ManyToOne.d.ts +4 -4
- package/decorators/ManyToOne.js +10 -12
- package/decorators/OneToMany.d.ts +6 -6
- package/decorators/OneToMany.js +11 -14
- package/decorators/OneToOne.d.ts +4 -4
- package/decorators/OneToOne.js +4 -7
- package/decorators/PrimaryKey.d.ts +3 -4
- package/decorators/PrimaryKey.js +10 -13
- package/decorators/Property.d.ts +6 -6
- package/decorators/Property.js +10 -12
- package/decorators/Transactional.d.ts +2 -2
- package/decorators/Transactional.js +7 -10
- package/decorators/hooks.js +23 -35
- package/decorators/index.d.ts +17 -17
- package/decorators/index.js +17 -36
- package/drivers/DatabaseDriver.d.ts +13 -12
- package/drivers/DatabaseDriver.js +60 -64
- package/drivers/IDatabaseDriver.d.ts +16 -13
- package/drivers/IDatabaseDriver.js +1 -4
- package/drivers/index.d.ts +2 -2
- package/drivers/index.js +2 -18
- package/entity/ArrayCollection.d.ts +3 -3
- package/entity/ArrayCollection.js +38 -35
- package/entity/BaseEntity.d.ts +6 -6
- package/entity/BaseEntity.js +17 -21
- package/entity/Collection.d.ts +6 -7
- package/entity/Collection.js +47 -51
- package/entity/EntityAssigner.d.ts +2 -2
- package/entity/EntityAssigner.js +58 -63
- package/entity/EntityFactory.d.ts +3 -3
- package/entity/EntityFactory.js +62 -63
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +44 -45
- package/entity/EntityIdentifier.d.ts +1 -1
- package/entity/EntityIdentifier.js +1 -5
- package/entity/EntityLoader.d.ts +5 -5
- package/entity/EntityLoader.js +106 -98
- package/entity/EntityRepository.d.ts +7 -7
- package/entity/EntityRepository.js +7 -11
- package/entity/EntityValidator.d.ts +1 -1
- package/entity/EntityValidator.js +25 -29
- package/entity/Reference.d.ts +4 -8
- package/entity/Reference.js +35 -42
- package/entity/WrappedEntity.d.ts +12 -12
- package/entity/WrappedEntity.js +23 -27
- package/entity/index.d.ts +13 -13
- package/entity/index.js +13 -29
- package/entity/utils.d.ts +1 -1
- package/entity/utils.js +9 -12
- package/entity/wrap.d.ts +1 -1
- package/entity/wrap.js +2 -6
- package/enums.d.ts +3 -3
- package/enums.js +37 -41
- package/errors.d.ts +1 -1
- package/errors.js +15 -24
- package/events/EventManager.d.ts +3 -3
- package/events/EventManager.js +8 -12
- package/events/EventSubscriber.d.ts +8 -5
- package/events/EventSubscriber.js +1 -2
- package/events/TransactionEventBroadcaster.d.ts +3 -3
- package/events/TransactionEventBroadcaster.js +1 -5
- package/events/index.d.ts +3 -3
- package/events/index.js +3 -19
- package/exceptions.js +18 -39
- package/hydration/Hydrator.d.ts +5 -5
- package/hydration/Hydrator.js +3 -6
- package/hydration/ObjectHydrator.d.ts +3 -3
- package/hydration/ObjectHydrator.js +26 -28
- package/hydration/index.d.ts +2 -2
- package/hydration/index.js +2 -18
- package/index.d.ts +21 -21
- package/index.js +21 -46
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/DefaultLogger.js +9 -13
- package/logging/Logger.d.ts +1 -1
- package/logging/Logger.js +1 -2
- package/logging/SimpleLogger.d.ts +2 -2
- package/logging/SimpleLogger.js +2 -6
- package/logging/colors.js +1 -5
- package/logging/index.d.ts +4 -4
- package/logging/index.js +4 -20
- package/metadata/EntitySchema.d.ts +14 -6
- package/metadata/EntitySchema.js +41 -45
- package/metadata/MetadataDiscovery.d.ts +7 -7
- package/metadata/MetadataDiscovery.js +181 -180
- package/metadata/MetadataProvider.d.ts +2 -2
- package/metadata/MetadataProvider.js +4 -7
- package/metadata/MetadataStorage.d.ts +2 -2
- package/metadata/MetadataStorage.js +15 -19
- package/metadata/MetadataValidator.d.ts +4 -4
- package/metadata/MetadataValidator.js +52 -55
- package/metadata/ReflectMetadataProvider.d.ts +2 -2
- package/metadata/ReflectMetadataProvider.js +8 -12
- package/metadata/index.d.ts +6 -6
- package/metadata/index.js +6 -22
- package/naming-strategy/AbstractNamingStrategy.d.ts +2 -2
- package/naming-strategy/AbstractNamingStrategy.js +4 -8
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +1 -1
- package/naming-strategy/EntityCaseNamingStrategy.js +2 -6
- package/naming-strategy/MongoNamingStrategy.d.ts +1 -1
- package/naming-strategy/MongoNamingStrategy.js +2 -6
- package/naming-strategy/NamingStrategy.d.ts +1 -1
- package/naming-strategy/NamingStrategy.js +1 -2
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +1 -1
- package/naming-strategy/UnderscoreNamingStrategy.js +2 -6
- package/naming-strategy/index.d.ts +5 -5
- package/naming-strategy/index.js +5 -21
- package/package.json +6 -15
- package/platforms/ExceptionConverter.d.ts +2 -2
- package/platforms/ExceptionConverter.js +4 -8
- package/platforms/Platform.d.ts +10 -10
- package/platforms/Platform.js +57 -61
- package/platforms/index.d.ts +2 -2
- package/platforms/index.js +2 -18
- package/serialization/EntitySerializer.d.ts +2 -2
- package/serialization/EntitySerializer.js +36 -41
- package/serialization/EntityTransformer.d.ts +1 -1
- package/serialization/EntityTransformer.js +27 -31
- package/serialization/SerializationContext.d.ts +2 -2
- package/serialization/SerializationContext.js +10 -14
- package/serialization/index.d.ts +3 -3
- package/serialization/index.js +3 -19
- package/types/ArrayType.d.ts +3 -3
- package/types/ArrayType.js +7 -11
- package/types/BigIntType.d.ts +4 -3
- package/types/BigIntType.js +6 -6
- package/types/BlobType.d.ts +3 -3
- package/types/BlobType.js +2 -8
- package/types/BooleanType.d.ts +3 -3
- package/types/BooleanType.js +2 -6
- package/types/CharacterType.d.ts +3 -3
- package/types/CharacterType.js +2 -6
- package/types/DateTimeType.d.ts +3 -3
- package/types/DateTimeType.js +2 -6
- package/types/DateType.d.ts +3 -3
- package/types/DateType.js +2 -6
- package/types/DecimalType.d.ts +3 -3
- package/types/DecimalType.js +4 -7
- package/types/DoubleType.d.ts +3 -3
- package/types/DoubleType.js +3 -6
- package/types/EnumArrayType.d.ts +4 -4
- package/types/EnumArrayType.js +5 -10
- package/types/EnumType.d.ts +3 -3
- package/types/EnumType.js +2 -6
- package/types/FloatType.d.ts +3 -3
- package/types/FloatType.js +2 -6
- package/types/IntegerType.d.ts +3 -3
- package/types/IntegerType.js +2 -6
- package/types/IntervalType.d.ts +3 -3
- package/types/IntervalType.js +2 -6
- package/types/JsonType.d.ts +3 -3
- package/types/JsonType.js +2 -6
- package/types/MediumIntType.d.ts +3 -3
- package/types/MediumIntType.js +2 -6
- package/types/SmallIntType.d.ts +3 -3
- package/types/SmallIntType.js +2 -6
- package/types/StringType.d.ts +3 -3
- package/types/StringType.js +2 -6
- package/types/TextType.d.ts +3 -3
- package/types/TextType.js +2 -6
- package/types/TimeType.d.ts +3 -3
- package/types/TimeType.js +4 -8
- package/types/TinyIntType.d.ts +3 -3
- package/types/TinyIntType.js +3 -6
- package/types/Type.d.ts +2 -2
- package/types/Type.js +5 -9
- package/types/Uint8ArrayType.d.ts +3 -3
- package/types/Uint8ArrayType.js +3 -9
- package/types/UnknownType.d.ts +3 -3
- package/types/UnknownType.js +2 -6
- package/types/UuidType.d.ts +3 -3
- package/types/UuidType.js +2 -6
- package/types/index.d.ts +25 -25
- package/types/index.js +52 -79
- package/typings.d.ts +33 -28
- package/typings.js +37 -38
- package/unit-of-work/ChangeSet.d.ts +1 -1
- package/unit-of-work/ChangeSet.js +13 -17
- package/unit-of-work/ChangeSetComputer.d.ts +8 -7
- package/unit-of-work/ChangeSetComputer.js +26 -30
- package/unit-of-work/ChangeSetPersister.d.ts +7 -6
- package/unit-of-work/ChangeSetPersister.js +51 -48
- package/unit-of-work/CommitOrderCalculator.d.ts +1 -1
- package/unit-of-work/CommitOrderCalculator.js +6 -10
- package/unit-of-work/IdentityMap.d.ts +1 -1
- package/unit-of-work/IdentityMap.js +1 -5
- package/unit-of-work/UnitOfWork.d.ts +8 -7
- package/unit-of-work/UnitOfWork.js +193 -178
- package/unit-of-work/index.d.ts +6 -6
- package/unit-of-work/index.js +6 -22
- package/utils/AbstractSchemaGenerator.d.ts +6 -6
- package/utils/AbstractSchemaGenerator.js +12 -13
- package/utils/Configuration.d.ts +26 -27
- package/utils/Configuration.js +50 -55
- package/utils/ConfigurationLoader.d.ts +9 -8
- package/utils/ConfigurationLoader.js +71 -86
- package/utils/Cursor.d.ts +6 -6
- package/utils/Cursor.js +22 -25
- package/utils/DataloaderUtils.d.ts +4 -4
- package/utils/DataloaderUtils.js +12 -16
- package/utils/EntityComparator.d.ts +2 -2
- package/utils/EntityComparator.js +109 -97
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +1 -5
- package/utils/QueryHelper.d.ts +3 -3
- package/utils/QueryHelper.js +47 -51
- package/utils/RawQueryFragment.d.ts +1 -1
- package/utils/RawQueryFragment.js +22 -25
- package/utils/RequestContext.d.ts +2 -2
- package/utils/RequestContext.js +3 -7
- package/utils/TransactionContext.d.ts +1 -1
- package/utils/TransactionContext.js +4 -8
- package/utils/Utils.d.ts +16 -12
- package/utils/Utils.js +96 -95
- package/utils/clone.js +8 -11
- package/utils/index.d.ts +13 -13
- package/utils/index.js +13 -29
- package/utils/resolveContextProvider.d.ts +3 -3
- package/utils/resolveContextProvider.js +9 -12
- package/utils/upsert-utils.d.ts +3 -3
- package/utils/upsert-utils.js +5 -9
- package/index.mjs +0 -199
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const entity_1 = require("../entity");
|
|
7
|
-
const enums_1 = require("../enums");
|
|
8
|
-
class ChangeSetComputer {
|
|
1
|
+
import { Utils } from '../utils/Utils.js';
|
|
2
|
+
import { ChangeSet, ChangeSetType } from './ChangeSet.js';
|
|
3
|
+
import { helper } from '../entity/wrap.js';
|
|
4
|
+
import { ReferenceKind } from '../enums.js';
|
|
5
|
+
export class ChangeSetComputer {
|
|
9
6
|
validator;
|
|
10
7
|
collectionUpdates;
|
|
11
8
|
metadata;
|
|
@@ -27,20 +24,20 @@ class ChangeSetComputer {
|
|
|
27
24
|
if (meta.readonly) {
|
|
28
25
|
return null;
|
|
29
26
|
}
|
|
30
|
-
const wrapped =
|
|
31
|
-
const type = wrapped.__originalEntityData ?
|
|
27
|
+
const wrapped = helper(entity);
|
|
28
|
+
const type = wrapped.__originalEntityData ? ChangeSetType.UPDATE : ChangeSetType.CREATE;
|
|
32
29
|
const map = new Map();
|
|
33
30
|
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
34
31
|
// to the `map` as we want to apply those only if something else changed.
|
|
35
|
-
if (type ===
|
|
32
|
+
if (type === ChangeSetType.CREATE) { // run update hooks only after we know there are other changes
|
|
36
33
|
for (const prop of meta.hydrateProps) {
|
|
37
34
|
this.processPropertyInitializers(entity, prop, type, map);
|
|
38
35
|
}
|
|
39
36
|
}
|
|
40
|
-
if (type ===
|
|
37
|
+
if (type === ChangeSetType.UPDATE && !wrapped.__initialized && !wrapped.isTouched()) {
|
|
41
38
|
return null;
|
|
42
39
|
}
|
|
43
|
-
const changeSet = new
|
|
40
|
+
const changeSet = new ChangeSet(entity, type, this.computePayload(entity), meta);
|
|
44
41
|
changeSet.originalEntity = wrapped.__originalEntityData;
|
|
45
42
|
if (this.config.get('validate')) {
|
|
46
43
|
this.validator.validate(changeSet.entity, changeSet.payload, meta);
|
|
@@ -48,12 +45,12 @@ class ChangeSetComputer {
|
|
|
48
45
|
for (const prop of meta.relations.filter(prop => prop.persist !== false || prop.userDefined === false)) {
|
|
49
46
|
this.processProperty(changeSet, prop);
|
|
50
47
|
}
|
|
51
|
-
if (changeSet.type ===
|
|
48
|
+
if (changeSet.type === ChangeSetType.UPDATE && !Utils.hasObjectKeys(changeSet.payload)) {
|
|
52
49
|
return null;
|
|
53
50
|
}
|
|
54
51
|
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
55
52
|
// to the `map` as we want to apply those only if something else changed.
|
|
56
|
-
if (type ===
|
|
53
|
+
if (type === ChangeSetType.UPDATE) {
|
|
57
54
|
for (const prop of meta.hydrateProps) {
|
|
58
55
|
this.processPropertyInitializers(entity, prop, type, map);
|
|
59
56
|
}
|
|
@@ -66,7 +63,7 @@ class ChangeSetComputer {
|
|
|
66
63
|
}
|
|
67
64
|
// Recompute the changeset, we need to merge this as here we ignore relations.
|
|
68
65
|
const diff = this.computePayload(entity, true);
|
|
69
|
-
|
|
66
|
+
Utils.merge(changeSet.payload, diff);
|
|
70
67
|
}
|
|
71
68
|
return changeSet;
|
|
72
69
|
}
|
|
@@ -75,17 +72,17 @@ class ChangeSetComputer {
|
|
|
75
72
|
*/
|
|
76
73
|
processPropertyInitializers(entity, prop, type, map, nested) {
|
|
77
74
|
if (prop.onCreate
|
|
78
|
-
&& type ===
|
|
75
|
+
&& type === ChangeSetType.CREATE
|
|
79
76
|
&& (entity[prop.name] == null
|
|
80
|
-
|| (
|
|
77
|
+
|| (Utils.isScalarReference(entity[prop.name]) && entity[prop.name].unwrap() == null))) {
|
|
81
78
|
entity[prop.name] = prop.onCreate(entity, this.em);
|
|
82
79
|
}
|
|
83
|
-
if (prop.onUpdate && type ===
|
|
80
|
+
if (prop.onUpdate && type === ChangeSetType.UPDATE) {
|
|
84
81
|
const pairs = map.get(entity) ?? [];
|
|
85
82
|
pairs.push([prop.name, prop.onUpdate(entity, this.em)]);
|
|
86
83
|
map.set(entity, pairs);
|
|
87
84
|
}
|
|
88
|
-
if (prop.kind ===
|
|
85
|
+
if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
|
|
89
86
|
for (const embeddedProp of prop.targetMeta.hydrateProps) {
|
|
90
87
|
this.processPropertyInitializers(entity[prop.name], embeddedProp, type, map, nested || prop.object);
|
|
91
88
|
}
|
|
@@ -93,7 +90,7 @@ class ChangeSetComputer {
|
|
|
93
90
|
}
|
|
94
91
|
computePayload(entity, ignoreUndefined = false) {
|
|
95
92
|
const data = this.comparator.prepareEntity(entity);
|
|
96
|
-
const wrapped =
|
|
93
|
+
const wrapped = helper(entity);
|
|
97
94
|
const entityName = wrapped.__meta.className;
|
|
98
95
|
const originalEntityData = wrapped.__originalEntityData;
|
|
99
96
|
if (!wrapped.__initialized) {
|
|
@@ -106,7 +103,7 @@ class ChangeSetComputer {
|
|
|
106
103
|
const comparator = this.comparator.getEntityComparator(entityName);
|
|
107
104
|
const diff = comparator(originalEntityData, data);
|
|
108
105
|
if (ignoreUndefined) {
|
|
109
|
-
|
|
106
|
+
Utils.keys(diff)
|
|
110
107
|
.filter(k => diff[k] === undefined)
|
|
111
108
|
.forEach(k => delete diff[k]);
|
|
112
109
|
}
|
|
@@ -116,32 +113,32 @@ class ChangeSetComputer {
|
|
|
116
113
|
}
|
|
117
114
|
processProperty(changeSet, prop, target) {
|
|
118
115
|
if (!target) {
|
|
119
|
-
const targets =
|
|
116
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
120
117
|
targets.forEach(([t]) => this.processProperty(changeSet, prop, t));
|
|
121
118
|
return;
|
|
122
119
|
}
|
|
123
|
-
if (
|
|
120
|
+
if (Utils.isCollection(target)) { // m:n or 1:m
|
|
124
121
|
this.processToMany(prop, changeSet);
|
|
125
122
|
}
|
|
126
|
-
if ([
|
|
123
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
127
124
|
this.processToOne(prop, changeSet);
|
|
128
125
|
}
|
|
129
126
|
}
|
|
130
127
|
processToOne(prop, changeSet) {
|
|
131
|
-
const isToOneOwner = prop.kind ===
|
|
128
|
+
const isToOneOwner = prop.kind === ReferenceKind.MANY_TO_ONE || (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner);
|
|
132
129
|
if (!isToOneOwner || prop.mapToPk) {
|
|
133
130
|
return;
|
|
134
131
|
}
|
|
135
|
-
const targets =
|
|
132
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
136
133
|
targets.forEach(([target, idx]) => {
|
|
137
134
|
if (!target.__helper.hasPrimaryKey()) {
|
|
138
|
-
|
|
135
|
+
Utils.setPayloadProperty(changeSet.payload, this.metadata.find(changeSet.name), prop, target.__helper.__identifier, idx);
|
|
139
136
|
}
|
|
140
137
|
});
|
|
141
138
|
}
|
|
142
139
|
processToMany(prop, changeSet) {
|
|
143
140
|
const target = changeSet.entity[prop.name];
|
|
144
|
-
if (!target.isDirty() && changeSet.type !==
|
|
141
|
+
if (!target.isDirty() && changeSet.type !== ChangeSetType.CREATE) {
|
|
145
142
|
return;
|
|
146
143
|
}
|
|
147
144
|
this.collectionUpdates.add(target);
|
|
@@ -150,4 +147,3 @@ class ChangeSetComputer {
|
|
|
150
147
|
}
|
|
151
148
|
}
|
|
152
149
|
}
|
|
153
|
-
exports.ChangeSetComputer = ChangeSetComputer;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { MetadataStorage } from '../metadata';
|
|
2
|
-
import type { Dictionary, EntityDictionary, EntityMetadata, IHydrator } from '../typings';
|
|
3
|
-
import { type EntityFactory
|
|
4
|
-
import { type
|
|
5
|
-
import { type
|
|
6
|
-
import
|
|
1
|
+
import type { MetadataStorage } from '../metadata/MetadataStorage.js';
|
|
2
|
+
import type { Dictionary, EntityDictionary, EntityMetadata, IHydrator } from '../typings.js';
|
|
3
|
+
import { type EntityFactory } from '../entity/EntityFactory.js';
|
|
4
|
+
import { type EntityValidator } from '../entity/EntityValidator.js';
|
|
5
|
+
import { type ChangeSet } from './ChangeSet.js';
|
|
6
|
+
import { type Configuration } from '../utils/Configuration.js';
|
|
7
|
+
import type { DriverMethodOptions, IDatabaseDriver } from '../drivers/IDatabaseDriver.js';
|
|
7
8
|
export declare class ChangeSetPersister {
|
|
8
9
|
private readonly driver;
|
|
9
10
|
private readonly metadata;
|
|
@@ -1,13 +1,11 @@
|
|
|
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 } from '../errors.js';
|
|
7
|
+
import { ReferenceKind } from '../enums.js';
|
|
8
|
+
export class ChangeSetPersister {
|
|
11
9
|
driver;
|
|
12
10
|
metadata;
|
|
13
11
|
hydrator;
|
|
@@ -60,7 +58,7 @@ 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());
|
|
@@ -86,12 +84,12 @@ class ChangeSetPersister {
|
|
|
86
84
|
for (const prop of meta.relations) {
|
|
87
85
|
this.processProperty(changeSet, prop);
|
|
88
86
|
}
|
|
89
|
-
if (changeSet.type ===
|
|
87
|
+
if (changeSet.type === ChangeSetType.CREATE && this.config.get('validateRequired')) {
|
|
90
88
|
this.validator.validateRequired(changeSet.entity);
|
|
91
89
|
}
|
|
92
90
|
}
|
|
93
91
|
async persistNewEntity(meta, changeSet, options) {
|
|
94
|
-
const wrapped =
|
|
92
|
+
const wrapped = helper(changeSet.entity);
|
|
95
93
|
options = this.propagateSchemaFromMetadata(meta, options, {
|
|
96
94
|
convertCustomTypes: false,
|
|
97
95
|
});
|
|
@@ -133,7 +131,7 @@ class ChangeSetPersister {
|
|
|
133
131
|
const res = await this.driver.nativeInsertMany(meta.className, changeSets.map(cs => cs.payload), options);
|
|
134
132
|
for (let i = 0; i < changeSets.length; i++) {
|
|
135
133
|
const changeSet = changeSets[i];
|
|
136
|
-
const wrapped =
|
|
134
|
+
const wrapped = helper(changeSet.entity);
|
|
137
135
|
if (!wrapped.hasPrimaryKey()) {
|
|
138
136
|
const field = meta.getPrimaryProps()[0].fieldNames[0];
|
|
139
137
|
this.mapPrimaryKey(meta, res.rows[i][field], changeSet);
|
|
@@ -172,7 +170,7 @@ class ChangeSetPersister {
|
|
|
172
170
|
}
|
|
173
171
|
}
|
|
174
172
|
if (tmp.length === 0 && meta.concurrencyCheckKeys.size > 0) {
|
|
175
|
-
throw
|
|
173
|
+
throw OptimisticLockError.lockFailed(changeSet.entity);
|
|
176
174
|
}
|
|
177
175
|
}
|
|
178
176
|
async persistManagedEntitiesBatch(meta, changeSets, options) {
|
|
@@ -191,10 +189,10 @@ class ChangeSetPersister {
|
|
|
191
189
|
}
|
|
192
190
|
const res = await this.driver.nativeUpdateMany(meta.className, cond, payload, options);
|
|
193
191
|
const map = new Map();
|
|
194
|
-
res.rows?.forEach(item => map.set(
|
|
192
|
+
res.rows?.forEach(item => map.set(Utils.getCompositeKeyHash(item, meta, true, this.platform, true), item));
|
|
195
193
|
for (const changeSet of changeSets) {
|
|
196
194
|
if (res.rows) {
|
|
197
|
-
const row = map.get(
|
|
195
|
+
const row = map.get(helper(changeSet.entity).getSerializedPrimaryKey());
|
|
198
196
|
this.mapReturnedValues(changeSet.entity, changeSet.payload, row, meta);
|
|
199
197
|
}
|
|
200
198
|
changeSet.persisted = true;
|
|
@@ -203,7 +201,7 @@ class ChangeSetPersister {
|
|
|
203
201
|
mapPrimaryKey(meta, value, changeSet) {
|
|
204
202
|
const prop = meta.properties[meta.primaryKeys[0]];
|
|
205
203
|
const insertId = prop.customType ? prop.customType.convertToJSValue(value, this.platform) : value;
|
|
206
|
-
const wrapped =
|
|
204
|
+
const wrapped = helper(changeSet.entity);
|
|
207
205
|
if (!wrapped.hasPrimaryKey()) {
|
|
208
206
|
wrapped.setPrimaryKey(insertId);
|
|
209
207
|
}
|
|
@@ -212,23 +210,25 @@ class ChangeSetPersister {
|
|
|
212
210
|
// of using the raw value from db, we convert it back to the db value explicitly
|
|
213
211
|
value = prop.customType ? prop.customType.convertToDatabaseValue(insertId, this.platform, { mode: 'serialization' }) : value;
|
|
214
212
|
changeSet.payload[wrapped.__meta.primaryKeys[0]] = value;
|
|
215
|
-
wrapped.__identifier
|
|
213
|
+
if (wrapped.__identifier && !Array.isArray(wrapped.__identifier)) {
|
|
214
|
+
wrapped.__identifier.setValue(value);
|
|
215
|
+
}
|
|
216
216
|
}
|
|
217
217
|
/**
|
|
218
218
|
* Sets populate flag to new entities so they are serialized like if they were loaded from the db
|
|
219
219
|
*/
|
|
220
220
|
markAsPopulated(changeSet, meta) {
|
|
221
|
-
|
|
221
|
+
helper(changeSet.entity).__schema = this.driver.getSchemaName(meta, changeSet);
|
|
222
222
|
if (!this.config.get('populateAfterFlush')) {
|
|
223
223
|
return;
|
|
224
224
|
}
|
|
225
|
-
|
|
225
|
+
helper(changeSet.entity).populated();
|
|
226
226
|
meta.relations.forEach(prop => {
|
|
227
227
|
const value = changeSet.entity[prop.name];
|
|
228
|
-
if (
|
|
228
|
+
if (Utils.isEntity(value, true)) {
|
|
229
229
|
value.__helper.populated();
|
|
230
230
|
}
|
|
231
|
-
else if (
|
|
231
|
+
else if (Utils.isCollection(value)) {
|
|
232
232
|
value.populated();
|
|
233
233
|
}
|
|
234
234
|
});
|
|
@@ -252,9 +252,9 @@ class ChangeSetPersister {
|
|
|
252
252
|
return;
|
|
253
253
|
}
|
|
254
254
|
// skip entity references as they don't have version values loaded
|
|
255
|
-
changeSets = changeSets.filter(cs =>
|
|
255
|
+
changeSets = changeSets.filter(cs => helper(cs.entity).__initialized);
|
|
256
256
|
const $or = changeSets.map(cs => {
|
|
257
|
-
const cond =
|
|
257
|
+
const cond = Utils.getPrimaryKeyCond(cs.originalEntity, meta.primaryKeys.concat(...meta.concurrencyCheckKeys));
|
|
258
258
|
if (meta.versionProperty) {
|
|
259
259
|
// @ts-ignore
|
|
260
260
|
cond[meta.versionProperty] = this.platform.quoteVersionValue(cs.entity[meta.versionProperty], meta.properties[meta.versionProperty]);
|
|
@@ -269,14 +269,14 @@ class ChangeSetPersister {
|
|
|
269
269
|
if (res.length !== changeSets.length) {
|
|
270
270
|
const compare = (a, b, keys) => keys.every(k => a[k] === b[k]);
|
|
271
271
|
const entity = changeSets.find(cs => {
|
|
272
|
-
return !res.some(row => compare(
|
|
272
|
+
return !res.some(row => compare(Utils.getPrimaryKeyCond(cs.entity, primaryKeys), row, primaryKeys));
|
|
273
273
|
}).entity;
|
|
274
|
-
throw
|
|
274
|
+
throw OptimisticLockError.lockFailed(entity);
|
|
275
275
|
}
|
|
276
276
|
}
|
|
277
277
|
checkOptimisticLock(meta, changeSet, res) {
|
|
278
278
|
if ((meta.versionProperty || meta.concurrencyCheckKeys.size > 0) && res && !res.affectedRows) {
|
|
279
|
-
throw
|
|
279
|
+
throw OptimisticLockError.lockFailed(changeSet.entity);
|
|
280
280
|
}
|
|
281
281
|
}
|
|
282
282
|
/**
|
|
@@ -285,18 +285,18 @@ class ChangeSetPersister {
|
|
|
285
285
|
*/
|
|
286
286
|
async reloadVersionValues(meta, changeSets, options) {
|
|
287
287
|
const reloadProps = meta.versionProperty && !this.usesReturningStatement ? [meta.properties[meta.versionProperty]] : [];
|
|
288
|
-
if (changeSets[0].type ===
|
|
288
|
+
if (changeSets[0].type === ChangeSetType.CREATE) {
|
|
289
289
|
// do not reload things that already had a runtime value
|
|
290
290
|
meta.props
|
|
291
291
|
.filter(prop => prop.persist !== false && (prop.autoincrement || prop.generated || prop.defaultRaw))
|
|
292
|
-
.filter(prop => (changeSets[0].entity[prop.name] == null && prop.defaultRaw !== 'null') ||
|
|
292
|
+
.filter(prop => (changeSets[0].entity[prop.name] == null && prop.defaultRaw !== 'null') || isRaw(changeSets[0].entity[prop.name]))
|
|
293
293
|
.forEach(prop => reloadProps.push(prop));
|
|
294
294
|
}
|
|
295
|
-
if (changeSets[0].type ===
|
|
295
|
+
if (changeSets[0].type === ChangeSetType.UPDATE) {
|
|
296
296
|
const returning = new Set();
|
|
297
297
|
changeSets.forEach(cs => {
|
|
298
|
-
|
|
299
|
-
if (
|
|
298
|
+
Utils.keys(cs.payload).forEach(k => {
|
|
299
|
+
if (isRaw(cs.payload[k]) && isRaw(cs.entity[k])) {
|
|
300
300
|
returning.add(meta.properties[k]);
|
|
301
301
|
}
|
|
302
302
|
});
|
|
@@ -313,22 +313,22 @@ class ChangeSetPersister {
|
|
|
313
313
|
return;
|
|
314
314
|
}
|
|
315
315
|
reloadProps.unshift(...meta.getPrimaryProps());
|
|
316
|
-
const pk =
|
|
316
|
+
const pk = Utils.getPrimaryKeyHash(meta.primaryKeys);
|
|
317
317
|
const pks = changeSets.map(cs => {
|
|
318
|
-
const val =
|
|
319
|
-
if (
|
|
320
|
-
return
|
|
318
|
+
const val = helper(cs.entity).getPrimaryKey(true);
|
|
319
|
+
if (Utils.isPlainObject(val)) {
|
|
320
|
+
return Utils.getCompositeKeyValue(val, meta, false, this.platform);
|
|
321
321
|
}
|
|
322
322
|
return val;
|
|
323
323
|
});
|
|
324
324
|
options = this.propagateSchemaFromMetadata(meta, options, {
|
|
325
|
-
fields:
|
|
325
|
+
fields: Utils.unique(reloadProps.map(prop => prop.name)),
|
|
326
326
|
});
|
|
327
327
|
const data = await this.driver.find(meta.className, { [pk]: { $in: pks } }, options);
|
|
328
328
|
const map = new Map();
|
|
329
|
-
data.forEach(item => map.set(
|
|
329
|
+
data.forEach(item => map.set(Utils.getCompositeKeyHash(item, meta, true, this.platform, true), item));
|
|
330
330
|
for (const changeSet of changeSets) {
|
|
331
|
-
const data = map.get(
|
|
331
|
+
const data = map.get(helper(changeSet.entity).getSerializedPrimaryKey());
|
|
332
332
|
this.hydrator.hydrate(changeSet.entity, meta, data, this.factory, 'full', false, true);
|
|
333
333
|
Object.assign(changeSet.payload, data); // merge to the changeset payload, so it gets saved to the entity snapshot
|
|
334
334
|
}
|
|
@@ -336,21 +336,25 @@ class ChangeSetPersister {
|
|
|
336
336
|
processProperty(changeSet, prop) {
|
|
337
337
|
const meta = this.metadata.find(changeSet.name);
|
|
338
338
|
const value = changeSet.payload[prop.name]; // for inline embeddables
|
|
339
|
-
if (value instanceof
|
|
339
|
+
if (value instanceof EntityIdentifier) {
|
|
340
340
|
changeSet.payload[prop.name] = value.getValue();
|
|
341
341
|
return;
|
|
342
342
|
}
|
|
343
|
-
if (
|
|
344
|
-
changeSet.payload[prop.name] = value.map(
|
|
343
|
+
if (Array.isArray(value) && value.every(item => item instanceof EntityIdentifier)) {
|
|
344
|
+
changeSet.payload[prop.name] = value.map(item => item.getValue());
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY && Array.isArray(value)) {
|
|
348
|
+
changeSet.payload[prop.name] = value.map(val => val instanceof EntityIdentifier ? val.getValue() : val);
|
|
345
349
|
return;
|
|
346
350
|
}
|
|
347
351
|
if (prop.name in changeSet.payload) {
|
|
348
352
|
return;
|
|
349
353
|
}
|
|
350
|
-
const values =
|
|
354
|
+
const values = Utils.unwrapProperty(changeSet.payload, meta, prop, true); // for object embeddables
|
|
351
355
|
values.forEach(([value, indexes]) => {
|
|
352
|
-
if (value instanceof
|
|
353
|
-
|
|
356
|
+
if (value instanceof EntityIdentifier) {
|
|
357
|
+
Utils.setPayloadProperty(changeSet.payload, meta, prop, value.getValue(), indexes);
|
|
354
358
|
}
|
|
355
359
|
});
|
|
356
360
|
}
|
|
@@ -360,7 +364,7 @@ class ChangeSetPersister {
|
|
|
360
364
|
* We do need to map to the change set payload too, as it will be used in the originalEntityData for new entities.
|
|
361
365
|
*/
|
|
362
366
|
mapReturnedValues(entity, payload, row, meta, upsert = false) {
|
|
363
|
-
if ((!this.usesReturningStatement && !upsert) || !row || !
|
|
367
|
+
if ((!this.usesReturningStatement && !upsert) || !row || !Utils.hasObjectKeys(row)) {
|
|
364
368
|
return;
|
|
365
369
|
}
|
|
366
370
|
const mapped = this.comparator.mapResult(meta.className, row);
|
|
@@ -377,4 +381,3 @@ class ChangeSetPersister {
|
|
|
377
381
|
Object.assign(payload, mapped);
|
|
378
382
|
}
|
|
379
383
|
}
|
|
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 */
|