@mikro-orm/core 7.0.10 → 7.0.11-dev.1
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 +584 -885
- package/EntityManager.js +1900 -1927
- package/MikroORM.d.ts +74 -103
- package/MikroORM.js +178 -177
- package/README.md +1 -1
- package/cache/CacheAdapter.d.ts +36 -36
- package/cache/FileCacheAdapter.d.ts +24 -30
- package/cache/FileCacheAdapter.js +78 -80
- package/cache/GeneratedCacheAdapter.d.ts +20 -18
- package/cache/GeneratedCacheAdapter.js +30 -30
- package/cache/MemoryCacheAdapter.d.ts +20 -18
- package/cache/MemoryCacheAdapter.js +36 -35
- package/cache/NullCacheAdapter.d.ts +16 -16
- package/cache/NullCacheAdapter.js +24 -24
- package/connections/Connection.d.ts +84 -95
- package/connections/Connection.js +168 -165
- package/drivers/DatabaseDriver.d.ts +80 -186
- package/drivers/DatabaseDriver.js +443 -450
- package/drivers/IDatabaseDriver.d.ts +301 -440
- package/entity/BaseEntity.d.ts +83 -120
- package/entity/BaseEntity.js +43 -43
- package/entity/Collection.d.ts +181 -215
- package/entity/Collection.js +724 -730
- package/entity/EntityAssigner.d.ts +77 -88
- package/entity/EntityAssigner.js +230 -231
- package/entity/EntityFactory.d.ts +55 -67
- package/entity/EntityFactory.js +416 -457
- package/entity/EntityHelper.d.ts +23 -35
- package/entity/EntityHelper.js +290 -302
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +72 -98
- package/entity/EntityLoader.js +776 -805
- package/entity/EntityRepository.d.ts +201 -316
- package/entity/EntityRepository.js +213 -213
- package/entity/PolymorphicRef.d.ts +5 -5
- package/entity/PolymorphicRef.js +10 -10
- package/entity/Reference.d.ts +83 -127
- package/entity/Reference.js +277 -281
- package/entity/WrappedEntity.d.ts +72 -115
- package/entity/WrappedEntity.js +166 -168
- package/entity/defineEntity.d.ts +654 -1359
- package/entity/defineEntity.js +518 -527
- package/entity/utils.d.ts +3 -13
- package/entity/utils.js +73 -71
- package/entity/validators.js +43 -43
- package/entity/wrap.js +8 -8
- package/enums.d.ts +253 -258
- package/enums.js +252 -251
- package/errors.d.ts +72 -114
- package/errors.js +253 -350
- package/events/EventManager.d.ts +14 -26
- package/events/EventManager.js +77 -79
- package/events/EventSubscriber.d.ts +29 -29
- package/events/TransactionEventBroadcaster.d.ts +8 -15
- package/events/TransactionEventBroadcaster.js +14 -14
- package/exceptions.d.ts +40 -23
- package/exceptions.js +52 -35
- package/hydration/Hydrator.d.ts +17 -42
- package/hydration/Hydrator.js +43 -43
- package/hydration/ObjectHydrator.d.ts +17 -50
- package/hydration/ObjectHydrator.js +418 -483
- package/index.d.ts +2 -116
- package/index.js +1 -10
- package/logging/DefaultLogger.d.ts +32 -34
- package/logging/DefaultLogger.js +86 -86
- package/logging/Logger.d.ts +41 -41
- package/logging/SimpleLogger.d.ts +11 -13
- package/logging/SimpleLogger.js +22 -22
- package/logging/colors.d.ts +6 -6
- package/logging/colors.js +10 -11
- package/logging/inspect.js +7 -7
- package/metadata/EntitySchema.d.ts +130 -214
- package/metadata/EntitySchema.js +412 -411
- package/metadata/MetadataDiscovery.d.ts +114 -114
- package/metadata/MetadataDiscovery.js +1884 -1962
- package/metadata/MetadataProvider.d.ts +26 -29
- package/metadata/MetadataProvider.js +97 -95
- package/metadata/MetadataStorage.d.ts +32 -38
- package/metadata/MetadataStorage.js +118 -118
- package/metadata/MetadataValidator.d.ts +39 -39
- package/metadata/MetadataValidator.js +338 -381
- package/metadata/discover-entities.d.ts +2 -5
- package/metadata/discover-entities.js +37 -35
- package/metadata/types.d.ts +531 -615
- package/naming-strategy/AbstractNamingStrategy.d.ts +39 -54
- package/naming-strategy/AbstractNamingStrategy.js +85 -90
- 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 +99 -109
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
- package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
- package/not-supported.js +4 -7
- package/package.json +1 -1
- package/platforms/ExceptionConverter.d.ts +1 -1
- package/platforms/ExceptionConverter.js +4 -4
- package/platforms/Platform.d.ts +303 -312
- package/platforms/Platform.js +675 -695
- package/serialization/EntitySerializer.d.ts +26 -49
- package/serialization/EntitySerializer.js +218 -224
- package/serialization/EntityTransformer.d.ts +6 -10
- package/serialization/EntityTransformer.js +217 -219
- package/serialization/SerializationContext.d.ts +23 -27
- package/serialization/SerializationContext.js +105 -105
- package/types/ArrayType.d.ts +8 -8
- package/types/ArrayType.js +33 -33
- package/types/BigIntType.d.ts +10 -17
- 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 +79 -83
- 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 +49 -75
- package/types/index.js +26 -52
- package/typings.d.ts +741 -1254
- package/typings.js +233 -244
- 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 +179 -187
- package/unit-of-work/ChangeSetPersister.d.ts +50 -69
- package/unit-of-work/ChangeSetPersister.js +442 -465
- package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
- package/unit-of-work/CommitOrderCalculator.js +88 -89
- package/unit-of-work/IdentityMap.d.ts +31 -31
- package/unit-of-work/IdentityMap.js +105 -105
- package/unit-of-work/UnitOfWork.d.ts +141 -181
- package/unit-of-work/UnitOfWork.js +1223 -1237
- package/utils/AbstractMigrator.d.ts +91 -111
- package/utils/AbstractMigrator.js +275 -275
- package/utils/AbstractSchemaGenerator.d.ts +34 -43
- package/utils/AbstractSchemaGenerator.js +122 -121
- package/utils/AsyncContext.d.ts +3 -3
- package/utils/AsyncContext.js +35 -34
- package/utils/Configuration.d.ts +808 -852
- package/utils/Configuration.js +344 -359
- package/utils/Cursor.d.ts +22 -40
- package/utils/Cursor.js +127 -135
- package/utils/DataloaderUtils.d.ts +43 -58
- package/utils/DataloaderUtils.js +198 -203
- package/utils/EntityComparator.d.ts +82 -99
- package/utils/EntityComparator.js +737 -829
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +3 -3
- package/utils/QueryHelper.d.ts +51 -79
- package/utils/QueryHelper.js +364 -375
- package/utils/RawQueryFragment.d.ts +34 -50
- package/utils/RawQueryFragment.js +121 -122
- package/utils/RequestContext.d.ts +32 -32
- package/utils/RequestContext.js +53 -52
- package/utils/TransactionContext.d.ts +16 -16
- package/utils/TransactionContext.js +27 -27
- package/utils/TransactionManager.d.ts +58 -58
- package/utils/TransactionManager.js +200 -202
- package/utils/Utils.d.ts +145 -204
- package/utils/Utils.js +826 -826
- package/utils/clone.js +114 -105
- package/utils/env-vars.js +88 -90
- package/utils/fs-utils.d.ts +15 -15
- package/utils/fs-utils.js +182 -181
- package/utils/upsert-utils.d.ts +5 -20
- package/utils/upsert-utils.js +116 -114
|
@@ -3,67 +3,67 @@ import { Utils } from '../utils/Utils.js';
|
|
|
3
3
|
import { inspect } from '../logging/inspect.js';
|
|
4
4
|
/** Represents a pending change (create, update, or delete) for a single entity. */
|
|
5
5
|
export class ChangeSet {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
/** Returns the primary key of the entity, optionally as an object for composite keys. */
|
|
22
|
-
getPrimaryKey(object = false) {
|
|
23
|
-
if (!this.originalEntity) {
|
|
24
|
-
this.primaryKey ??= helper(this.entity).getPrimaryKey(true);
|
|
25
|
-
} else if (this.meta.compositePK) {
|
|
26
|
-
this.primaryKey = this.meta.primaryKeys.map(pk => this.originalEntity[pk]);
|
|
27
|
-
} else {
|
|
28
|
-
this.primaryKey = this.originalEntity[this.meta.primaryKeys[0]];
|
|
6
|
+
entity;
|
|
7
|
+
type;
|
|
8
|
+
payload;
|
|
9
|
+
meta;
|
|
10
|
+
primaryKey;
|
|
11
|
+
serializedPrimaryKey;
|
|
12
|
+
constructor(entity, type, payload, meta) {
|
|
13
|
+
this.entity = entity;
|
|
14
|
+
this.type = type;
|
|
15
|
+
this.payload = payload;
|
|
16
|
+
this.meta = meta;
|
|
17
|
+
this.meta = meta;
|
|
18
|
+
this.rootMeta = meta.root;
|
|
19
|
+
this.schema = helper(entity).__schema ?? meta.root.schema;
|
|
29
20
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
21
|
+
/** Returns the primary key of the entity, optionally as an object for composite keys. */
|
|
22
|
+
getPrimaryKey(object = false) {
|
|
23
|
+
if (!this.originalEntity) {
|
|
24
|
+
this.primaryKey ??= helper(this.entity).getPrimaryKey(true);
|
|
25
|
+
}
|
|
26
|
+
else if (this.meta.compositePK) {
|
|
27
|
+
this.primaryKey = this.meta.primaryKeys.map(pk => this.originalEntity[pk]);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.primaryKey = this.originalEntity[this.meta.primaryKeys[0]];
|
|
31
|
+
}
|
|
32
|
+
if (!this.meta.compositePK &&
|
|
33
|
+
this.meta.getPrimaryProp().targetMeta?.compositePK &&
|
|
34
|
+
typeof this.primaryKey === 'object' &&
|
|
35
|
+
this.primaryKey !== null) {
|
|
36
|
+
this.primaryKey = this.meta.getPrimaryProp().targetMeta.primaryKeys.map(childPK => {
|
|
37
|
+
return this.primaryKey[childPK];
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (object && this.primaryKey != null) {
|
|
41
|
+
return Utils.primaryKeyToObject(this.meta, this.primaryKey);
|
|
42
|
+
}
|
|
43
|
+
return this.primaryKey ?? null;
|
|
39
44
|
}
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
/** Returns the serialized (string) form of the primary key. */
|
|
46
|
+
getSerializedPrimaryKey() {
|
|
47
|
+
this.serializedPrimaryKey ??= helper(this.entity).getSerializedPrimaryKey();
|
|
48
|
+
return this.serializedPrimaryKey;
|
|
49
|
+
}
|
|
50
|
+
/** @ignore */
|
|
51
|
+
[Symbol.for('nodejs.util.inspect.custom')](depth = 2) {
|
|
52
|
+
const object = { ...this };
|
|
53
|
+
const hidden = ['meta', 'serializedPrimaryKey'];
|
|
54
|
+
hidden.forEach(k => delete object[k]);
|
|
55
|
+
const ret = inspect(object, { depth });
|
|
56
|
+
const name = `${this.constructor.name}<${this.meta.className}>`;
|
|
57
|
+
/* v8 ignore next */
|
|
58
|
+
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
42
59
|
}
|
|
43
|
-
return this.primaryKey ?? null;
|
|
44
|
-
}
|
|
45
|
-
/** Returns the serialized (string) form of the primary key. */
|
|
46
|
-
getSerializedPrimaryKey() {
|
|
47
|
-
this.serializedPrimaryKey ??= helper(this.entity).getSerializedPrimaryKey();
|
|
48
|
-
return this.serializedPrimaryKey;
|
|
49
|
-
}
|
|
50
|
-
/** @ignore */
|
|
51
|
-
[Symbol.for('nodejs.util.inspect.custom')](depth = 2) {
|
|
52
|
-
const object = { ...this };
|
|
53
|
-
const hidden = ['meta', 'serializedPrimaryKey'];
|
|
54
|
-
hidden.forEach(k => delete object[k]);
|
|
55
|
-
const ret = inspect(object, { depth });
|
|
56
|
-
const name = `${this.constructor.name}<${this.meta.className}>`;
|
|
57
|
-
/* v8 ignore next */
|
|
58
|
-
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
59
|
-
}
|
|
60
60
|
}
|
|
61
61
|
/** Enumeration of change set operation types. */
|
|
62
62
|
export var ChangeSetType;
|
|
63
63
|
(function (ChangeSetType) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
ChangeSetType["CREATE"] = "create";
|
|
65
|
+
ChangeSetType["UPDATE"] = "update";
|
|
66
|
+
ChangeSetType["DELETE"] = "delete";
|
|
67
|
+
ChangeSetType["UPDATE_EARLY"] = "update_early";
|
|
68
|
+
ChangeSetType["DELETE_EARLY"] = "delete_early";
|
|
69
69
|
})(ChangeSetType || (ChangeSetType = {}));
|
|
@@ -4,16 +4,16 @@ import { type Collection } from '../entity/Collection.js';
|
|
|
4
4
|
import type { EntityManager } from '../EntityManager.js';
|
|
5
5
|
/** @internal Computes change sets by comparing entity state against original snapshots. */
|
|
6
6
|
export declare class ChangeSetComputer {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
7
|
+
#private;
|
|
8
|
+
constructor(em: EntityManager, collectionUpdates: Set<Collection<AnyEntity>>);
|
|
9
|
+
/** Computes a change set for the given entity by diffing against its original state. */
|
|
10
|
+
computeChangeSet<T extends object>(entity: T): ChangeSet<T> | null;
|
|
11
|
+
/**
|
|
12
|
+
* Traverses entity graph and executes `onCreate` and `onUpdate` methods, assigning the values to given properties.
|
|
13
|
+
*/
|
|
14
|
+
private processPropertyInitializers;
|
|
15
|
+
private computePayload;
|
|
16
|
+
private processProperty;
|
|
17
|
+
private processToOne;
|
|
18
|
+
private processToMany;
|
|
19
19
|
}
|
|
@@ -9,196 +9,188 @@ import { ReferenceKind } from '../enums.js';
|
|
|
9
9
|
import { isRaw } from '../utils/RawQueryFragment.js';
|
|
10
10
|
/** @internal Computes change sets by comparing entity state against original snapshots. */
|
|
11
11
|
export class ChangeSetComputer {
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
12
|
+
#comparator;
|
|
13
|
+
#metadata;
|
|
14
|
+
#platform;
|
|
15
|
+
#config;
|
|
16
|
+
#em;
|
|
17
|
+
#collectionUpdates;
|
|
18
|
+
constructor(em, collectionUpdates) {
|
|
19
|
+
this.#em = em;
|
|
20
|
+
this.#collectionUpdates = collectionUpdates;
|
|
21
|
+
this.#config = this.#em.config;
|
|
22
|
+
this.#metadata = this.#em.getMetadata();
|
|
23
|
+
this.#platform = this.#em.getPlatform();
|
|
24
|
+
this.#comparator = this.#config.getComparator(this.#metadata);
|
|
25
|
+
}
|
|
26
|
+
/** Computes a change set for the given entity by diffing against its original state. */
|
|
27
|
+
computeChangeSet(entity) {
|
|
28
|
+
const meta = this.#metadata.get(entity.constructor);
|
|
29
|
+
if (meta.readonly) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const wrapped = helper(entity);
|
|
33
|
+
const type = wrapped.__originalEntityData ? ChangeSetType.UPDATE : ChangeSetType.CREATE;
|
|
34
|
+
const map = new Map();
|
|
35
|
+
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
36
|
+
// to the `map` as we want to apply those only if something else changed.
|
|
37
|
+
if (type === ChangeSetType.CREATE) {
|
|
38
|
+
// run update hooks only after we know there are other changes
|
|
39
|
+
for (const prop of meta.hydrateProps) {
|
|
40
|
+
this.processPropertyInitializers(entity, prop, type, map);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (type === ChangeSetType.UPDATE && !wrapped.__initialized) {
|
|
44
|
+
const data = this.#comparator.prepareEntity(entity);
|
|
45
|
+
if (meta.root.discriminatorColumn) {
|
|
46
|
+
delete data[meta.root.discriminatorColumn];
|
|
47
|
+
}
|
|
48
|
+
if (Utils.equals(data, wrapped.__originalEntityData)) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const changeSet = new ChangeSet(entity, type, this.computePayload(entity), meta);
|
|
53
|
+
changeSet.originalEntity = wrapped.__originalEntityData;
|
|
54
|
+
for (const prop of meta.relations.filter(prop => prop.persist !== false || prop.userDefined === false)) {
|
|
55
|
+
this.processProperty(changeSet, prop);
|
|
56
|
+
}
|
|
57
|
+
if (changeSet.type === ChangeSetType.UPDATE && !Utils.hasObjectKeys(changeSet.payload)) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
validateEntity(changeSet.entity, meta);
|
|
61
|
+
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
62
|
+
// to the `map` as we want to apply those only if something else changed.
|
|
63
|
+
if (type === ChangeSetType.UPDATE) {
|
|
64
|
+
for (const prop of meta.hydrateProps) {
|
|
65
|
+
this.processPropertyInitializers(entity, prop, type, map);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (map.size > 0) {
|
|
69
|
+
for (const [entity, pairs] of map) {
|
|
70
|
+
for (const [prop, value] of pairs) {
|
|
71
|
+
entity[prop] = value;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Recompute the changeset, we need to merge this as here we ignore relations.
|
|
75
|
+
const diff = this.computePayload(entity, true);
|
|
76
|
+
Utils.merge(changeSet.payload, diff);
|
|
77
|
+
}
|
|
78
|
+
return changeSet;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Traverses entity graph and executes `onCreate` and `onUpdate` methods, assigning the values to given properties.
|
|
82
|
+
*/
|
|
83
|
+
processPropertyInitializers(entity, prop, type, map, nested) {
|
|
84
|
+
if (prop.onCreate &&
|
|
85
|
+
type === ChangeSetType.CREATE &&
|
|
86
|
+
(entity[prop.name] == null ||
|
|
87
|
+
(Utils.isScalarReference(entity[prop.name]) && entity[prop.name].unwrap() == null))) {
|
|
88
|
+
entity[prop.name] = prop.onCreate(entity, this.#em);
|
|
89
|
+
}
|
|
90
|
+
else if (prop.default != null &&
|
|
91
|
+
!isRaw(prop.default) &&
|
|
92
|
+
![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
|
|
93
|
+
type === ChangeSetType.CREATE &&
|
|
94
|
+
entity[prop.name] === undefined) {
|
|
95
|
+
entity[prop.name] = prop.default;
|
|
96
|
+
}
|
|
97
|
+
if (prop.onUpdate && type === ChangeSetType.UPDATE) {
|
|
98
|
+
const pairs = map.get(entity) ?? [];
|
|
99
|
+
pairs.push([prop.name, prop.onUpdate(entity, this.#em)]);
|
|
100
|
+
map.set(entity, pairs);
|
|
101
|
+
}
|
|
102
|
+
if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
|
|
103
|
+
const items = prop.array ? entity[prop.name] : [entity[prop.name]];
|
|
104
|
+
for (const item of items) {
|
|
105
|
+
for (const embeddedProp of prop.targetMeta.hydrateProps) {
|
|
106
|
+
this.processPropertyInitializers(item, embeddedProp, type, map, nested || prop.object);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
109
|
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
computePayload(entity, ignoreUndefined = false) {
|
|
114
|
-
const data = this.#comparator.prepareEntity(entity);
|
|
115
|
-
const wrapped = helper(entity);
|
|
116
|
-
const entityName = wrapped.__meta.class;
|
|
117
|
-
const originalEntityData = wrapped.__originalEntityData;
|
|
118
|
-
if (!wrapped.__initialized) {
|
|
119
|
-
for (const prop of wrapped.__meta.primaryKeys) {
|
|
120
|
-
delete data[prop];
|
|
121
|
-
}
|
|
122
|
-
// strip discriminator column for STI entities — it's always hardcoded
|
|
123
|
-
// in the snapshot generator and not present in __originalEntityData,
|
|
124
|
-
// causing false dirty detection on uninitialized reference proxies
|
|
125
|
-
if (wrapped.__meta.root.discriminatorColumn) {
|
|
126
|
-
delete data[wrapped.__meta.root.discriminatorColumn];
|
|
127
|
-
}
|
|
128
|
-
return data;
|
|
129
|
-
}
|
|
130
|
-
if (originalEntityData) {
|
|
131
|
-
const comparator = this.#comparator.getEntityComparator(entityName);
|
|
132
|
-
const diff = comparator(originalEntityData, data);
|
|
133
|
-
if (ignoreUndefined) {
|
|
134
|
-
Utils.keys(diff)
|
|
135
|
-
.filter(k => diff[k] === undefined)
|
|
136
|
-
.forEach(k => delete diff[k]);
|
|
137
|
-
}
|
|
138
|
-
return diff;
|
|
139
|
-
}
|
|
140
|
-
return data;
|
|
141
|
-
}
|
|
142
|
-
processProperty(changeSet, prop, target) {
|
|
143
|
-
if (!target) {
|
|
144
|
-
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
145
|
-
targets.forEach(([t]) => this.processProperty(changeSet, prop, t));
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
if (Utils.isCollection(target)) {
|
|
149
|
-
// m:n or 1:m
|
|
150
|
-
this.processToMany(prop, changeSet);
|
|
151
|
-
}
|
|
152
|
-
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
153
|
-
this.processToOne(prop, changeSet);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
processToOne(prop, changeSet) {
|
|
157
|
-
const isToOneOwner =
|
|
158
|
-
prop.kind === ReferenceKind.MANY_TO_ONE || (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner);
|
|
159
|
-
if (!isToOneOwner || prop.mapToPk) {
|
|
160
|
-
return;
|
|
161
110
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
processToMany(prop, changeSet) {
|
|
191
|
-
const target = changeSet.entity[prop.name];
|
|
192
|
-
if (!target.isDirty() && changeSet.type !== ChangeSetType.CREATE) {
|
|
193
|
-
return;
|
|
111
|
+
computePayload(entity, ignoreUndefined = false) {
|
|
112
|
+
const data = this.#comparator.prepareEntity(entity);
|
|
113
|
+
const wrapped = helper(entity);
|
|
114
|
+
const entityName = wrapped.__meta.class;
|
|
115
|
+
const originalEntityData = wrapped.__originalEntityData;
|
|
116
|
+
if (!wrapped.__initialized) {
|
|
117
|
+
for (const prop of wrapped.__meta.primaryKeys) {
|
|
118
|
+
delete data[prop];
|
|
119
|
+
}
|
|
120
|
+
// strip discriminator column for STI entities — it's always hardcoded
|
|
121
|
+
// in the snapshot generator and not present in __originalEntityData,
|
|
122
|
+
// causing false dirty detection on uninitialized reference proxies
|
|
123
|
+
if (wrapped.__meta.root.discriminatorColumn) {
|
|
124
|
+
delete data[wrapped.__meta.root.discriminatorColumn];
|
|
125
|
+
}
|
|
126
|
+
return data;
|
|
127
|
+
}
|
|
128
|
+
if (originalEntityData) {
|
|
129
|
+
const comparator = this.#comparator.getEntityComparator(entityName);
|
|
130
|
+
const diff = comparator(originalEntityData, data);
|
|
131
|
+
if (ignoreUndefined) {
|
|
132
|
+
Utils.keys(diff)
|
|
133
|
+
.filter(k => diff[k] === undefined)
|
|
134
|
+
.forEach(k => delete diff[k]);
|
|
135
|
+
}
|
|
136
|
+
return diff;
|
|
137
|
+
}
|
|
138
|
+
return data;
|
|
194
139
|
}
|
|
195
|
-
|
|
196
|
-
|
|
140
|
+
processProperty(changeSet, prop, target) {
|
|
141
|
+
if (!target) {
|
|
142
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
143
|
+
targets.forEach(([t]) => this.processProperty(changeSet, prop, t));
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (Utils.isCollection(target)) {
|
|
147
|
+
// m:n or 1:m
|
|
148
|
+
this.processToMany(prop, changeSet);
|
|
149
|
+
}
|
|
150
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
151
|
+
this.processToOne(prop, changeSet);
|
|
152
|
+
}
|
|
197
153
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
154
|
+
processToOne(prop, changeSet) {
|
|
155
|
+
const isToOneOwner = prop.kind === ReferenceKind.MANY_TO_ONE || (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner);
|
|
156
|
+
if (!isToOneOwner || prop.mapToPk) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
160
|
+
targets.forEach(([rawTarget, idx]) => {
|
|
161
|
+
const target = Reference.unwrapReference(rawTarget);
|
|
162
|
+
const needsProcessing = target != null && (prop.targetKey != null || !target.__helper.hasPrimaryKey());
|
|
163
|
+
if (needsProcessing) {
|
|
164
|
+
let value = prop.targetKey ? target[prop.targetKey] : target.__helper.__identifier;
|
|
165
|
+
/* v8 ignore next */
|
|
166
|
+
if (prop.targetKey && prop.targetMeta) {
|
|
167
|
+
const targetProp = prop.targetMeta.properties[prop.targetKey];
|
|
168
|
+
if (targetProp?.customType) {
|
|
169
|
+
value = targetProp.customType.convertToDatabaseValue(value, this.#platform, { mode: 'serialization' });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (prop.polymorphic) {
|
|
173
|
+
const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, target.constructor);
|
|
174
|
+
Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, new PolymorphicRef(discriminator, value), idx);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, value, idx);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
processToMany(prop, changeSet) {
|
|
183
|
+
const target = changeSet.entity[prop.name];
|
|
184
|
+
if (!target.isDirty() && changeSet.type !== ChangeSetType.CREATE) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (target.isDirty()) {
|
|
188
|
+
this.#collectionUpdates.add(target);
|
|
189
|
+
}
|
|
190
|
+
if (prop.owner && !this.#platform.usesPivotTable()) {
|
|
191
|
+
changeSet.payload[prop.name] = target.getItems(false).map((item) => {
|
|
192
|
+
return item.__helper.__identifier ?? item.__helper.getPrimaryKey();
|
|
193
|
+
});
|
|
194
|
+
}
|
|
202
195
|
}
|
|
203
|
-
}
|
|
204
196
|
}
|