@mikro-orm/core 7.0.4-dev.9 → 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
|
@@ -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
|
-
|
|
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;
|
|
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]];
|
|
20
29
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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;
|
|
30
|
+
if (
|
|
31
|
+
!this.meta.compositePK &&
|
|
32
|
+
this.meta.getPrimaryProp().targetMeta?.compositePK &&
|
|
33
|
+
typeof this.primaryKey === 'object' &&
|
|
34
|
+
this.primaryKey !== null
|
|
35
|
+
) {
|
|
36
|
+
this.primaryKey = this.meta.getPrimaryProp().targetMeta.primaryKeys.map(childPK => {
|
|
37
|
+
return this.primaryKey[childPK];
|
|
38
|
+
});
|
|
44
39
|
}
|
|
45
|
-
|
|
46
|
-
|
|
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;
|
|
40
|
+
if (object && this.primaryKey != null) {
|
|
41
|
+
return Utils.primaryKeyToObject(this.meta, this.primaryKey);
|
|
59
42
|
}
|
|
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,179 +9,187 @@ 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
|
-
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 (Utils.equals(data, wrapped.__originalEntityData)) {
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
const changeSet = new ChangeSet(entity, type, this.computePayload(entity), meta);
|
|
50
|
-
changeSet.originalEntity = wrapped.__originalEntityData;
|
|
51
|
-
for (const prop of meta.relations.filter(prop => prop.persist !== false || prop.userDefined === false)) {
|
|
52
|
-
this.processProperty(changeSet, prop);
|
|
53
|
-
}
|
|
54
|
-
if (changeSet.type === ChangeSetType.UPDATE && !Utils.hasObjectKeys(changeSet.payload)) {
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
validateEntity(changeSet.entity, meta);
|
|
58
|
-
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
59
|
-
// to the `map` as we want to apply those only if something else changed.
|
|
60
|
-
if (type === ChangeSetType.UPDATE) {
|
|
61
|
-
for (const prop of meta.hydrateProps) {
|
|
62
|
-
this.processPropertyInitializers(entity, prop, type, map);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
if (map.size > 0) {
|
|
66
|
-
for (const [entity, pairs] of map) {
|
|
67
|
-
for (const [prop, value] of pairs) {
|
|
68
|
-
entity[prop] = value;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
// Recompute the changeset, we need to merge this as here we ignore relations.
|
|
72
|
-
const diff = this.computePayload(entity, true);
|
|
73
|
-
Utils.merge(changeSet.payload, diff);
|
|
74
|
-
}
|
|
75
|
-
return changeSet;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Traverses entity graph and executes `onCreate` and `onUpdate` methods, assigning the values to given properties.
|
|
79
|
-
*/
|
|
80
|
-
processPropertyInitializers(entity, prop, type, map, nested) {
|
|
81
|
-
if (prop.onCreate &&
|
|
82
|
-
type === ChangeSetType.CREATE &&
|
|
83
|
-
(entity[prop.name] == null ||
|
|
84
|
-
(Utils.isScalarReference(entity[prop.name]) && entity[prop.name].unwrap() == null))) {
|
|
85
|
-
entity[prop.name] = prop.onCreate(entity, this.#em);
|
|
86
|
-
}
|
|
87
|
-
else if (prop.default != null &&
|
|
88
|
-
!isRaw(prop.default) &&
|
|
89
|
-
![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
|
|
90
|
-
type === ChangeSetType.CREATE &&
|
|
91
|
-
entity[prop.name] === undefined) {
|
|
92
|
-
entity[prop.name] = prop.default;
|
|
93
|
-
}
|
|
94
|
-
if (prop.onUpdate && type === ChangeSetType.UPDATE) {
|
|
95
|
-
const pairs = map.get(entity) ?? [];
|
|
96
|
-
pairs.push([prop.name, prop.onUpdate(entity, this.#em)]);
|
|
97
|
-
map.set(entity, pairs);
|
|
98
|
-
}
|
|
99
|
-
if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
|
|
100
|
-
const items = prop.array ? entity[prop.name] : [entity[prop.name]];
|
|
101
|
-
for (const item of items) {
|
|
102
|
-
for (const embeddedProp of prop.targetMeta.hydrateProps) {
|
|
103
|
-
this.processPropertyInitializers(item, embeddedProp, type, map, nested || prop.object);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
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;
|
|
107
31
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
if (originalEntityData) {
|
|
120
|
-
const comparator = this.#comparator.getEntityComparator(entityName);
|
|
121
|
-
const diff = comparator(originalEntityData, data);
|
|
122
|
-
if (ignoreUndefined) {
|
|
123
|
-
Utils.keys(diff)
|
|
124
|
-
.filter(k => diff[k] === undefined)
|
|
125
|
-
.forEach(k => delete diff[k]);
|
|
126
|
-
}
|
|
127
|
-
return diff;
|
|
128
|
-
}
|
|
129
|
-
return data;
|
|
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
|
+
}
|
|
130
42
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
if (Utils.isCollection(target)) {
|
|
138
|
-
// m:n or 1:m
|
|
139
|
-
this.processToMany(prop, changeSet);
|
|
140
|
-
}
|
|
141
|
-
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
142
|
-
this.processToOne(prop, changeSet);
|
|
143
|
-
}
|
|
43
|
+
if (type === ChangeSetType.UPDATE && !wrapped.__initialized) {
|
|
44
|
+
const data = this.#comparator.prepareEntity(entity);
|
|
45
|
+
if (Utils.equals(data, wrapped.__originalEntityData)) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
144
48
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
49
|
+
const changeSet = new ChangeSet(entity, type, this.computePayload(entity), meta);
|
|
50
|
+
changeSet.originalEntity = wrapped.__originalEntityData;
|
|
51
|
+
for (const prop of meta.relations.filter(prop => prop.persist !== false || prop.userDefined === false)) {
|
|
52
|
+
this.processProperty(changeSet, prop);
|
|
53
|
+
}
|
|
54
|
+
if (changeSet.type === ChangeSetType.UPDATE && !Utils.hasObjectKeys(changeSet.payload)) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
validateEntity(changeSet.entity, meta);
|
|
58
|
+
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
59
|
+
// to the `map` as we want to apply those only if something else changed.
|
|
60
|
+
if (type === ChangeSetType.UPDATE) {
|
|
61
|
+
for (const prop of meta.hydrateProps) {
|
|
62
|
+
this.processPropertyInitializers(entity, prop, type, map);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (map.size > 0) {
|
|
66
|
+
for (const [entity, pairs] of map) {
|
|
67
|
+
for (const [prop, value] of pairs) {
|
|
68
|
+
entity[prop] = value;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Recompute the changeset, we need to merge this as here we ignore relations.
|
|
72
|
+
const diff = this.computePayload(entity, true);
|
|
73
|
+
Utils.merge(changeSet.payload, diff);
|
|
74
|
+
}
|
|
75
|
+
return changeSet;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Traverses entity graph and executes `onCreate` and `onUpdate` methods, assigning the values to given properties.
|
|
79
|
+
*/
|
|
80
|
+
processPropertyInitializers(entity, prop, type, map, nested) {
|
|
81
|
+
if (
|
|
82
|
+
prop.onCreate &&
|
|
83
|
+
type === ChangeSetType.CREATE &&
|
|
84
|
+
(entity[prop.name] == null || (Utils.isScalarReference(entity[prop.name]) && entity[prop.name].unwrap() == null))
|
|
85
|
+
) {
|
|
86
|
+
entity[prop.name] = prop.onCreate(entity, this.#em);
|
|
87
|
+
} else if (
|
|
88
|
+
prop.default != null &&
|
|
89
|
+
!isRaw(prop.default) &&
|
|
90
|
+
![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
|
|
91
|
+
type === ChangeSetType.CREATE &&
|
|
92
|
+
entity[prop.name] === undefined
|
|
93
|
+
) {
|
|
94
|
+
entity[prop.name] = prop.default;
|
|
95
|
+
}
|
|
96
|
+
if (prop.onUpdate && type === ChangeSetType.UPDATE) {
|
|
97
|
+
const pairs = map.get(entity) ?? [];
|
|
98
|
+
pairs.push([prop.name, prop.onUpdate(entity, this.#em)]);
|
|
99
|
+
map.set(entity, pairs);
|
|
100
|
+
}
|
|
101
|
+
if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
|
|
102
|
+
const items = prop.array ? entity[prop.name] : [entity[prop.name]];
|
|
103
|
+
for (const item of items) {
|
|
104
|
+
for (const embeddedProp of prop.targetMeta.hydrateProps) {
|
|
105
|
+
this.processPropertyInitializers(item, embeddedProp, type, map, nested || prop.object);
|
|
185
106
|
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
computePayload(entity, ignoreUndefined = false) {
|
|
111
|
+
const data = this.#comparator.prepareEntity(entity);
|
|
112
|
+
const wrapped = helper(entity);
|
|
113
|
+
const entityName = wrapped.__meta.class;
|
|
114
|
+
const originalEntityData = wrapped.__originalEntityData;
|
|
115
|
+
if (!wrapped.__initialized) {
|
|
116
|
+
for (const prop of wrapped.__meta.primaryKeys) {
|
|
117
|
+
delete data[prop];
|
|
118
|
+
}
|
|
119
|
+
return data;
|
|
120
|
+
}
|
|
121
|
+
if (originalEntityData) {
|
|
122
|
+
const comparator = this.#comparator.getEntityComparator(entityName);
|
|
123
|
+
const diff = comparator(originalEntityData, data);
|
|
124
|
+
if (ignoreUndefined) {
|
|
125
|
+
Utils.keys(diff)
|
|
126
|
+
.filter(k => diff[k] === undefined)
|
|
127
|
+
.forEach(k => delete diff[k]);
|
|
128
|
+
}
|
|
129
|
+
return diff;
|
|
130
|
+
}
|
|
131
|
+
return data;
|
|
132
|
+
}
|
|
133
|
+
processProperty(changeSet, prop, target) {
|
|
134
|
+
if (!target) {
|
|
135
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
136
|
+
targets.forEach(([t]) => this.processProperty(changeSet, prop, t));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (Utils.isCollection(target)) {
|
|
140
|
+
// m:n or 1:m
|
|
141
|
+
this.processToMany(prop, changeSet);
|
|
142
|
+
}
|
|
143
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
144
|
+
this.processToOne(prop, changeSet);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
processToOne(prop, changeSet) {
|
|
148
|
+
const isToOneOwner =
|
|
149
|
+
prop.kind === ReferenceKind.MANY_TO_ONE || (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner);
|
|
150
|
+
if (!isToOneOwner || prop.mapToPk) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
154
|
+
targets.forEach(([rawTarget, idx]) => {
|
|
155
|
+
const target = Reference.unwrapReference(rawTarget);
|
|
156
|
+
const needsProcessing = target != null && (prop.targetKey != null || !target.__helper.hasPrimaryKey());
|
|
157
|
+
if (needsProcessing) {
|
|
158
|
+
let value = prop.targetKey ? target[prop.targetKey] : target.__helper.__identifier;
|
|
159
|
+
/* v8 ignore next */
|
|
160
|
+
if (prop.targetKey && prop.targetMeta) {
|
|
161
|
+
const targetProp = prop.targetMeta.properties[prop.targetKey];
|
|
162
|
+
if (targetProp?.customType) {
|
|
163
|
+
value = targetProp.customType.convertToDatabaseValue(value, this.#platform, { mode: 'serialization' });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (prop.polymorphic) {
|
|
167
|
+
const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, target.constructor);
|
|
168
|
+
Utils.setPayloadProperty(
|
|
169
|
+
changeSet.payload,
|
|
170
|
+
changeSet.meta,
|
|
171
|
+
prop,
|
|
172
|
+
new PolymorphicRef(discriminator, value),
|
|
173
|
+
idx,
|
|
174
|
+
);
|
|
175
|
+
} else {
|
|
176
|
+
Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, value, idx);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
processToMany(prop, changeSet) {
|
|
182
|
+
const target = changeSet.entity[prop.name];
|
|
183
|
+
if (!target.isDirty() && changeSet.type !== ChangeSetType.CREATE) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (target.isDirty()) {
|
|
187
|
+
this.#collectionUpdates.add(target);
|
|
188
|
+
}
|
|
189
|
+
if (prop.owner && !this.#platform.usesPivotTable()) {
|
|
190
|
+
changeSet.payload[prop.name] = target.getItems(false).map(item => {
|
|
191
|
+
return item.__helper.__identifier ?? item.__helper.getPrimaryKey();
|
|
192
|
+
});
|
|
186
193
|
}
|
|
194
|
+
}
|
|
187
195
|
}
|
|
@@ -4,48 +4,67 @@ import type { DriverMethodOptions } from '../drivers/IDatabaseDriver.js';
|
|
|
4
4
|
import type { EntityManager } from '../EntityManager.js';
|
|
5
5
|
/** @internal Executes change sets against the database, handling inserts, updates, and deletes. */
|
|
6
6
|
export declare class ChangeSetPersister {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
7
|
+
#private;
|
|
8
|
+
constructor(em: EntityManager);
|
|
9
|
+
/** Executes all pending INSERT change sets, using batch inserts when possible. */
|
|
10
|
+
executeInserts<T extends object>(
|
|
11
|
+
changeSets: ChangeSet<T>[],
|
|
12
|
+
options?: DriverMethodOptions,
|
|
13
|
+
withSchema?: boolean,
|
|
14
|
+
): Promise<void>;
|
|
15
|
+
/** Executes all pending UPDATE change sets, using batch updates when possible. */
|
|
16
|
+
executeUpdates<T extends object>(
|
|
17
|
+
changeSets: ChangeSet<T>[],
|
|
18
|
+
batched: boolean,
|
|
19
|
+
options?: DriverMethodOptions,
|
|
20
|
+
withSchema?: boolean,
|
|
21
|
+
): Promise<void>;
|
|
22
|
+
/** Executes all pending DELETE change sets in batches. */
|
|
23
|
+
executeDeletes<T extends object>(
|
|
24
|
+
changeSets: ChangeSet<T>[],
|
|
25
|
+
options?: DriverMethodOptions,
|
|
26
|
+
withSchema?: boolean,
|
|
27
|
+
): Promise<void>;
|
|
28
|
+
private runForEachSchema;
|
|
29
|
+
private validateRequired;
|
|
30
|
+
private processProperties;
|
|
31
|
+
private persistNewEntity;
|
|
32
|
+
private persistNewEntities;
|
|
33
|
+
private prepareOptions;
|
|
34
|
+
private persistNewEntitiesBatch;
|
|
35
|
+
private persistManagedEntity;
|
|
36
|
+
private persistManagedEntities;
|
|
37
|
+
private checkConcurrencyKeys;
|
|
38
|
+
private persistManagedEntitiesBatch;
|
|
39
|
+
private mapPrimaryKey;
|
|
40
|
+
/**
|
|
41
|
+
* Sets populate flag to new entities so they are serialized like if they were loaded from the db
|
|
42
|
+
*/
|
|
43
|
+
private markAsPopulated;
|
|
44
|
+
private updateEntity;
|
|
45
|
+
private checkOptimisticLocks;
|
|
46
|
+
private checkOptimisticLock;
|
|
47
|
+
/**
|
|
48
|
+
* This method also handles reloading of database default values for inserts and raw property updates,
|
|
49
|
+
* so we use a single query in case of both versioning and default values is used.
|
|
50
|
+
*/
|
|
51
|
+
private reloadVersionValues;
|
|
52
|
+
/**
|
|
53
|
+
* For TPT child tables, resolve EntityIdentifier values in PK fields.
|
|
54
|
+
* The parent table insert assigns the actual PK value, which the child table references.
|
|
55
|
+
*/
|
|
56
|
+
private resolveTPTIdentifiers;
|
|
57
|
+
private processProperty;
|
|
58
|
+
/**
|
|
59
|
+
* Maps values returned via `returning` statement (postgres) or the inserted id (other sql drivers).
|
|
60
|
+
* No need to handle composite keys here as they need to be set upfront.
|
|
61
|
+
* We do need to map to the change set payload too, as it will be used in the originalEntityData for new entities.
|
|
62
|
+
*/
|
|
63
|
+
mapReturnedValues<T extends object>(
|
|
64
|
+
entity: T | null | undefined,
|
|
65
|
+
payload: EntityDictionary<T>,
|
|
66
|
+
row: Dictionary | undefined,
|
|
67
|
+
meta: EntityMetadata<T>,
|
|
68
|
+
upsert?: boolean,
|
|
69
|
+
): void;
|
|
51
70
|
}
|