@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.
Files changed (206) hide show
  1. package/EntityManager.d.ts +584 -885
  2. package/EntityManager.js +1900 -1927
  3. package/MikroORM.d.ts +74 -103
  4. package/MikroORM.js +178 -177
  5. package/README.md +1 -1
  6. package/cache/CacheAdapter.d.ts +36 -36
  7. package/cache/FileCacheAdapter.d.ts +24 -30
  8. package/cache/FileCacheAdapter.js +78 -80
  9. package/cache/GeneratedCacheAdapter.d.ts +20 -18
  10. package/cache/GeneratedCacheAdapter.js +30 -30
  11. package/cache/MemoryCacheAdapter.d.ts +20 -18
  12. package/cache/MemoryCacheAdapter.js +36 -35
  13. package/cache/NullCacheAdapter.d.ts +16 -16
  14. package/cache/NullCacheAdapter.js +24 -24
  15. package/connections/Connection.d.ts +84 -95
  16. package/connections/Connection.js +168 -165
  17. package/drivers/DatabaseDriver.d.ts +80 -186
  18. package/drivers/DatabaseDriver.js +443 -450
  19. package/drivers/IDatabaseDriver.d.ts +301 -440
  20. package/entity/BaseEntity.d.ts +83 -120
  21. package/entity/BaseEntity.js +43 -43
  22. package/entity/Collection.d.ts +181 -215
  23. package/entity/Collection.js +724 -730
  24. package/entity/EntityAssigner.d.ts +77 -88
  25. package/entity/EntityAssigner.js +230 -231
  26. package/entity/EntityFactory.d.ts +55 -67
  27. package/entity/EntityFactory.js +416 -457
  28. package/entity/EntityHelper.d.ts +23 -35
  29. package/entity/EntityHelper.js +290 -302
  30. package/entity/EntityIdentifier.d.ts +4 -4
  31. package/entity/EntityIdentifier.js +10 -10
  32. package/entity/EntityLoader.d.ts +72 -98
  33. package/entity/EntityLoader.js +776 -805
  34. package/entity/EntityRepository.d.ts +201 -316
  35. package/entity/EntityRepository.js +213 -213
  36. package/entity/PolymorphicRef.d.ts +5 -5
  37. package/entity/PolymorphicRef.js +10 -10
  38. package/entity/Reference.d.ts +83 -127
  39. package/entity/Reference.js +277 -281
  40. package/entity/WrappedEntity.d.ts +72 -115
  41. package/entity/WrappedEntity.js +166 -168
  42. package/entity/defineEntity.d.ts +654 -1359
  43. package/entity/defineEntity.js +518 -527
  44. package/entity/utils.d.ts +3 -13
  45. package/entity/utils.js +73 -71
  46. package/entity/validators.js +43 -43
  47. package/entity/wrap.js +8 -8
  48. package/enums.d.ts +253 -258
  49. package/enums.js +252 -251
  50. package/errors.d.ts +72 -114
  51. package/errors.js +253 -350
  52. package/events/EventManager.d.ts +14 -26
  53. package/events/EventManager.js +77 -79
  54. package/events/EventSubscriber.d.ts +29 -29
  55. package/events/TransactionEventBroadcaster.d.ts +8 -15
  56. package/events/TransactionEventBroadcaster.js +14 -14
  57. package/exceptions.d.ts +40 -23
  58. package/exceptions.js +52 -35
  59. package/hydration/Hydrator.d.ts +17 -42
  60. package/hydration/Hydrator.js +43 -43
  61. package/hydration/ObjectHydrator.d.ts +17 -50
  62. package/hydration/ObjectHydrator.js +418 -483
  63. package/index.d.ts +2 -116
  64. package/index.js +1 -10
  65. package/logging/DefaultLogger.d.ts +32 -34
  66. package/logging/DefaultLogger.js +86 -86
  67. package/logging/Logger.d.ts +41 -41
  68. package/logging/SimpleLogger.d.ts +11 -13
  69. package/logging/SimpleLogger.js +22 -22
  70. package/logging/colors.d.ts +6 -6
  71. package/logging/colors.js +10 -11
  72. package/logging/inspect.js +7 -7
  73. package/metadata/EntitySchema.d.ts +130 -214
  74. package/metadata/EntitySchema.js +412 -411
  75. package/metadata/MetadataDiscovery.d.ts +114 -114
  76. package/metadata/MetadataDiscovery.js +1884 -1962
  77. package/metadata/MetadataProvider.d.ts +26 -29
  78. package/metadata/MetadataProvider.js +97 -95
  79. package/metadata/MetadataStorage.d.ts +32 -38
  80. package/metadata/MetadataStorage.js +118 -118
  81. package/metadata/MetadataValidator.d.ts +39 -39
  82. package/metadata/MetadataValidator.js +338 -381
  83. package/metadata/discover-entities.d.ts +2 -5
  84. package/metadata/discover-entities.js +37 -35
  85. package/metadata/types.d.ts +531 -615
  86. package/naming-strategy/AbstractNamingStrategy.d.ts +39 -54
  87. package/naming-strategy/AbstractNamingStrategy.js +85 -90
  88. package/naming-strategy/EntityCaseNamingStrategy.d.ts +6 -6
  89. package/naming-strategy/EntityCaseNamingStrategy.js +22 -22
  90. package/naming-strategy/MongoNamingStrategy.d.ts +6 -6
  91. package/naming-strategy/MongoNamingStrategy.js +18 -18
  92. package/naming-strategy/NamingStrategy.d.ts +99 -109
  93. package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
  94. package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
  95. package/not-supported.js +4 -7
  96. package/package.json +1 -1
  97. package/platforms/ExceptionConverter.d.ts +1 -1
  98. package/platforms/ExceptionConverter.js +4 -4
  99. package/platforms/Platform.d.ts +303 -312
  100. package/platforms/Platform.js +675 -695
  101. package/serialization/EntitySerializer.d.ts +26 -49
  102. package/serialization/EntitySerializer.js +218 -224
  103. package/serialization/EntityTransformer.d.ts +6 -10
  104. package/serialization/EntityTransformer.js +217 -219
  105. package/serialization/SerializationContext.d.ts +23 -27
  106. package/serialization/SerializationContext.js +105 -105
  107. package/types/ArrayType.d.ts +8 -8
  108. package/types/ArrayType.js +33 -33
  109. package/types/BigIntType.d.ts +10 -17
  110. package/types/BigIntType.js +37 -37
  111. package/types/BlobType.d.ts +3 -3
  112. package/types/BlobType.js +13 -13
  113. package/types/BooleanType.d.ts +4 -4
  114. package/types/BooleanType.js +12 -12
  115. package/types/CharacterType.d.ts +2 -2
  116. package/types/CharacterType.js +6 -6
  117. package/types/DateTimeType.d.ts +5 -5
  118. package/types/DateTimeType.js +15 -15
  119. package/types/DateType.d.ts +5 -5
  120. package/types/DateType.js +15 -15
  121. package/types/DecimalType.d.ts +7 -7
  122. package/types/DecimalType.js +26 -26
  123. package/types/DoubleType.d.ts +3 -3
  124. package/types/DoubleType.js +12 -12
  125. package/types/EnumArrayType.d.ts +5 -5
  126. package/types/EnumArrayType.js +24 -24
  127. package/types/EnumType.d.ts +3 -3
  128. package/types/EnumType.js +11 -11
  129. package/types/FloatType.d.ts +3 -3
  130. package/types/FloatType.js +9 -9
  131. package/types/IntegerType.d.ts +3 -3
  132. package/types/IntegerType.js +9 -9
  133. package/types/IntervalType.d.ts +4 -4
  134. package/types/IntervalType.js +12 -12
  135. package/types/JsonType.d.ts +8 -8
  136. package/types/JsonType.js +32 -32
  137. package/types/MediumIntType.d.ts +1 -1
  138. package/types/MediumIntType.js +3 -3
  139. package/types/SmallIntType.d.ts +3 -3
  140. package/types/SmallIntType.js +9 -9
  141. package/types/StringType.d.ts +4 -4
  142. package/types/StringType.js +12 -12
  143. package/types/TextType.d.ts +3 -3
  144. package/types/TextType.js +9 -9
  145. package/types/TimeType.d.ts +5 -5
  146. package/types/TimeType.js +17 -17
  147. package/types/TinyIntType.d.ts +3 -3
  148. package/types/TinyIntType.js +10 -10
  149. package/types/Type.d.ts +79 -83
  150. package/types/Type.js +82 -82
  151. package/types/Uint8ArrayType.d.ts +4 -4
  152. package/types/Uint8ArrayType.js +21 -21
  153. package/types/UnknownType.d.ts +4 -4
  154. package/types/UnknownType.js +12 -12
  155. package/types/UuidType.d.ts +5 -5
  156. package/types/UuidType.js +19 -19
  157. package/types/index.d.ts +49 -75
  158. package/types/index.js +26 -52
  159. package/typings.d.ts +741 -1254
  160. package/typings.js +233 -244
  161. package/unit-of-work/ChangeSet.d.ts +26 -26
  162. package/unit-of-work/ChangeSet.js +56 -56
  163. package/unit-of-work/ChangeSetComputer.d.ts +12 -12
  164. package/unit-of-work/ChangeSetComputer.js +179 -187
  165. package/unit-of-work/ChangeSetPersister.d.ts +50 -69
  166. package/unit-of-work/ChangeSetPersister.js +442 -465
  167. package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
  168. package/unit-of-work/CommitOrderCalculator.js +88 -89
  169. package/unit-of-work/IdentityMap.d.ts +31 -31
  170. package/unit-of-work/IdentityMap.js +105 -105
  171. package/unit-of-work/UnitOfWork.d.ts +141 -181
  172. package/unit-of-work/UnitOfWork.js +1223 -1237
  173. package/utils/AbstractMigrator.d.ts +91 -111
  174. package/utils/AbstractMigrator.js +275 -275
  175. package/utils/AbstractSchemaGenerator.d.ts +34 -43
  176. package/utils/AbstractSchemaGenerator.js +122 -121
  177. package/utils/AsyncContext.d.ts +3 -3
  178. package/utils/AsyncContext.js +35 -34
  179. package/utils/Configuration.d.ts +808 -852
  180. package/utils/Configuration.js +344 -359
  181. package/utils/Cursor.d.ts +22 -40
  182. package/utils/Cursor.js +127 -135
  183. package/utils/DataloaderUtils.d.ts +43 -58
  184. package/utils/DataloaderUtils.js +198 -203
  185. package/utils/EntityComparator.d.ts +82 -99
  186. package/utils/EntityComparator.js +737 -829
  187. package/utils/NullHighlighter.d.ts +1 -1
  188. package/utils/NullHighlighter.js +3 -3
  189. package/utils/QueryHelper.d.ts +51 -79
  190. package/utils/QueryHelper.js +364 -375
  191. package/utils/RawQueryFragment.d.ts +34 -50
  192. package/utils/RawQueryFragment.js +121 -122
  193. package/utils/RequestContext.d.ts +32 -32
  194. package/utils/RequestContext.js +53 -52
  195. package/utils/TransactionContext.d.ts +16 -16
  196. package/utils/TransactionContext.js +27 -27
  197. package/utils/TransactionManager.d.ts +58 -58
  198. package/utils/TransactionManager.js +200 -202
  199. package/utils/Utils.d.ts +145 -204
  200. package/utils/Utils.js +826 -826
  201. package/utils/clone.js +114 -105
  202. package/utils/env-vars.js +88 -90
  203. package/utils/fs-utils.d.ts +15 -15
  204. package/utils/fs-utils.js +182 -181
  205. package/utils/upsert-utils.d.ts +5 -20
  206. 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
- 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]];
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
- 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
- });
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
- if (object && this.primaryKey != null) {
41
- return Utils.primaryKeyToObject(this.meta, this.primaryKey);
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
- ChangeSetType['CREATE'] = 'create';
65
- ChangeSetType['UPDATE'] = 'update';
66
- ChangeSetType['DELETE'] = 'delete';
67
- ChangeSetType['UPDATE_EARLY'] = 'update_early';
68
- ChangeSetType['DELETE_EARLY'] = 'delete_early';
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
- #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;
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
- #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 (
85
- prop.onCreate &&
86
- type === ChangeSetType.CREATE &&
87
- (entity[prop.name] == null || (Utils.isScalarReference(entity[prop.name]) && entity[prop.name].unwrap() == null))
88
- ) {
89
- entity[prop.name] = prop.onCreate(entity, this.#em);
90
- } else if (
91
- prop.default != null &&
92
- !isRaw(prop.default) &&
93
- ![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
94
- type === ChangeSetType.CREATE &&
95
- entity[prop.name] === undefined
96
- ) {
97
- entity[prop.name] = prop.default;
98
- }
99
- if (prop.onUpdate && type === ChangeSetType.UPDATE) {
100
- const pairs = map.get(entity) ?? [];
101
- pairs.push([prop.name, prop.onUpdate(entity, this.#em)]);
102
- map.set(entity, pairs);
103
- }
104
- if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
105
- const items = prop.array ? entity[prop.name] : [entity[prop.name]];
106
- for (const item of items) {
107
- for (const embeddedProp of prop.targetMeta.hydrateProps) {
108
- this.processPropertyInitializers(item, embeddedProp, type, map, nested || prop.object);
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
- const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
163
- targets.forEach(([rawTarget, idx]) => {
164
- const target = Reference.unwrapReference(rawTarget);
165
- const needsProcessing = target != null && (prop.targetKey != null || !target.__helper.hasPrimaryKey());
166
- if (needsProcessing) {
167
- let value = prop.targetKey ? target[prop.targetKey] : target.__helper.__identifier;
168
- /* v8 ignore next */
169
- if (prop.targetKey && prop.targetMeta) {
170
- const targetProp = prop.targetMeta.properties[prop.targetKey];
171
- if (targetProp?.customType) {
172
- value = targetProp.customType.convertToDatabaseValue(value, this.#platform, { mode: 'serialization' });
173
- }
174
- }
175
- if (prop.polymorphic) {
176
- const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, target.constructor);
177
- Utils.setPayloadProperty(
178
- changeSet.payload,
179
- changeSet.meta,
180
- prop,
181
- new PolymorphicRef(discriminator, value),
182
- idx,
183
- );
184
- } else {
185
- Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, value, idx);
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
- if (target.isDirty()) {
196
- this.#collectionUpdates.add(target);
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
- if (prop.owner && !this.#platform.usesPivotTable()) {
199
- changeSet.payload[prop.name] = target.getItems(false).map(item => {
200
- return item.__helper.__identifier ?? item.__helper.getPrimaryKey();
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
  }