@mikro-orm/core 7.0.2 → 7.0.3-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 (205) hide show
  1. package/EntityManager.d.ts +583 -883
  2. package/EntityManager.js +1869 -1897
  3. package/MikroORM.d.ts +74 -103
  4. package/MikroORM.js +179 -178
  5. package/cache/CacheAdapter.d.ts +36 -36
  6. package/cache/FileCacheAdapter.d.ts +24 -30
  7. package/cache/FileCacheAdapter.js +78 -80
  8. package/cache/GeneratedCacheAdapter.d.ts +20 -18
  9. package/cache/GeneratedCacheAdapter.js +30 -30
  10. package/cache/MemoryCacheAdapter.d.ts +20 -18
  11. package/cache/MemoryCacheAdapter.js +36 -35
  12. package/cache/NullCacheAdapter.d.ts +16 -16
  13. package/cache/NullCacheAdapter.js +24 -24
  14. package/connections/Connection.d.ts +84 -95
  15. package/connections/Connection.js +168 -165
  16. package/drivers/DatabaseDriver.d.ts +80 -186
  17. package/drivers/DatabaseDriver.js +443 -450
  18. package/drivers/IDatabaseDriver.d.ts +301 -440
  19. package/entity/BaseEntity.d.ts +83 -120
  20. package/entity/BaseEntity.js +43 -43
  21. package/entity/Collection.d.ts +179 -212
  22. package/entity/Collection.js +721 -727
  23. package/entity/EntityAssigner.d.ts +77 -88
  24. package/entity/EntityAssigner.js +230 -231
  25. package/entity/EntityFactory.d.ts +54 -66
  26. package/entity/EntityFactory.js +383 -425
  27. package/entity/EntityHelper.d.ts +22 -34
  28. package/entity/EntityHelper.js +267 -280
  29. package/entity/EntityIdentifier.d.ts +4 -4
  30. package/entity/EntityIdentifier.js +10 -10
  31. package/entity/EntityLoader.d.ts +73 -103
  32. package/entity/EntityLoader.js +723 -753
  33. package/entity/EntityRepository.d.ts +201 -316
  34. package/entity/EntityRepository.js +213 -213
  35. package/entity/PolymorphicRef.d.ts +5 -5
  36. package/entity/PolymorphicRef.js +10 -10
  37. package/entity/Reference.d.ts +82 -126
  38. package/entity/Reference.js +274 -278
  39. package/entity/WrappedEntity.d.ts +72 -115
  40. package/entity/WrappedEntity.js +166 -168
  41. package/entity/defineEntity.d.ts +614 -1280
  42. package/entity/defineEntity.js +511 -520
  43. package/entity/utils.d.ts +3 -13
  44. package/entity/utils.js +73 -71
  45. package/entity/validators.js +43 -43
  46. package/entity/wrap.js +8 -8
  47. package/enums.d.ts +253 -258
  48. package/enums.js +252 -251
  49. package/errors.d.ts +72 -114
  50. package/errors.js +253 -350
  51. package/events/EventManager.d.ts +14 -26
  52. package/events/EventManager.js +77 -79
  53. package/events/EventSubscriber.d.ts +29 -29
  54. package/events/TransactionEventBroadcaster.d.ts +8 -15
  55. package/events/TransactionEventBroadcaster.js +14 -14
  56. package/exceptions.d.ts +40 -23
  57. package/exceptions.js +52 -35
  58. package/hydration/Hydrator.d.ts +17 -42
  59. package/hydration/Hydrator.js +43 -43
  60. package/hydration/ObjectHydrator.d.ts +17 -50
  61. package/hydration/ObjectHydrator.js +416 -479
  62. package/index.d.ts +2 -116
  63. package/index.js +1 -10
  64. package/logging/DefaultLogger.d.ts +32 -34
  65. package/logging/DefaultLogger.js +86 -86
  66. package/logging/Logger.d.ts +41 -41
  67. package/logging/SimpleLogger.d.ts +11 -13
  68. package/logging/SimpleLogger.js +22 -22
  69. package/logging/colors.d.ts +6 -6
  70. package/logging/colors.js +10 -11
  71. package/logging/inspect.js +7 -7
  72. package/metadata/EntitySchema.d.ts +127 -211
  73. package/metadata/EntitySchema.js +398 -397
  74. package/metadata/MetadataDiscovery.d.ts +114 -114
  75. package/metadata/MetadataDiscovery.js +1863 -1947
  76. package/metadata/MetadataProvider.d.ts +21 -24
  77. package/metadata/MetadataProvider.js +84 -82
  78. package/metadata/MetadataStorage.d.ts +32 -38
  79. package/metadata/MetadataStorage.js +118 -118
  80. package/metadata/MetadataValidator.d.ts +39 -39
  81. package/metadata/MetadataValidator.js +338 -381
  82. package/metadata/discover-entities.d.ts +2 -5
  83. package/metadata/discover-entities.js +27 -27
  84. package/metadata/types.d.ts +531 -615
  85. package/naming-strategy/AbstractNamingStrategy.d.ts +39 -54
  86. package/naming-strategy/AbstractNamingStrategy.js +85 -90
  87. package/naming-strategy/EntityCaseNamingStrategy.d.ts +6 -6
  88. package/naming-strategy/EntityCaseNamingStrategy.js +22 -22
  89. package/naming-strategy/MongoNamingStrategy.d.ts +6 -6
  90. package/naming-strategy/MongoNamingStrategy.js +18 -18
  91. package/naming-strategy/NamingStrategy.d.ts +99 -109
  92. package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
  93. package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
  94. package/not-supported.js +4 -7
  95. package/package.json +1 -1
  96. package/platforms/ExceptionConverter.d.ts +1 -1
  97. package/platforms/ExceptionConverter.js +4 -4
  98. package/platforms/Platform.d.ts +299 -308
  99. package/platforms/Platform.js +636 -659
  100. package/serialization/EntitySerializer.d.ts +26 -48
  101. package/serialization/EntitySerializer.js +218 -224
  102. package/serialization/EntityTransformer.d.ts +6 -10
  103. package/serialization/EntityTransformer.js +217 -219
  104. package/serialization/SerializationContext.d.ts +23 -27
  105. package/serialization/SerializationContext.js +105 -105
  106. package/types/ArrayType.d.ts +8 -8
  107. package/types/ArrayType.js +33 -33
  108. package/types/BigIntType.d.ts +10 -17
  109. package/types/BigIntType.js +37 -37
  110. package/types/BlobType.d.ts +3 -3
  111. package/types/BlobType.js +13 -13
  112. package/types/BooleanType.d.ts +4 -4
  113. package/types/BooleanType.js +12 -12
  114. package/types/CharacterType.d.ts +2 -2
  115. package/types/CharacterType.js +6 -6
  116. package/types/DateTimeType.d.ts +5 -5
  117. package/types/DateTimeType.js +15 -15
  118. package/types/DateType.d.ts +5 -5
  119. package/types/DateType.js +15 -15
  120. package/types/DecimalType.d.ts +7 -7
  121. package/types/DecimalType.js +26 -26
  122. package/types/DoubleType.d.ts +3 -3
  123. package/types/DoubleType.js +12 -12
  124. package/types/EnumArrayType.d.ts +5 -5
  125. package/types/EnumArrayType.js +24 -24
  126. package/types/EnumType.d.ts +3 -3
  127. package/types/EnumType.js +11 -11
  128. package/types/FloatType.d.ts +3 -3
  129. package/types/FloatType.js +9 -9
  130. package/types/IntegerType.d.ts +3 -3
  131. package/types/IntegerType.js +9 -9
  132. package/types/IntervalType.d.ts +4 -4
  133. package/types/IntervalType.js +12 -12
  134. package/types/JsonType.d.ts +8 -8
  135. package/types/JsonType.js +32 -32
  136. package/types/MediumIntType.d.ts +1 -1
  137. package/types/MediumIntType.js +3 -3
  138. package/types/SmallIntType.d.ts +3 -3
  139. package/types/SmallIntType.js +9 -9
  140. package/types/StringType.d.ts +4 -4
  141. package/types/StringType.js +12 -12
  142. package/types/TextType.d.ts +3 -3
  143. package/types/TextType.js +9 -9
  144. package/types/TimeType.d.ts +5 -5
  145. package/types/TimeType.js +17 -17
  146. package/types/TinyIntType.d.ts +3 -3
  147. package/types/TinyIntType.js +10 -10
  148. package/types/Type.d.ts +79 -83
  149. package/types/Type.js +82 -82
  150. package/types/Uint8ArrayType.d.ts +4 -4
  151. package/types/Uint8ArrayType.js +21 -21
  152. package/types/UnknownType.d.ts +4 -4
  153. package/types/UnknownType.js +12 -12
  154. package/types/UuidType.d.ts +5 -5
  155. package/types/UuidType.js +19 -19
  156. package/types/index.d.ts +49 -75
  157. package/types/index.js +26 -52
  158. package/typings.d.ts +729 -1211
  159. package/typings.js +231 -244
  160. package/unit-of-work/ChangeSet.d.ts +26 -26
  161. package/unit-of-work/ChangeSet.js +56 -56
  162. package/unit-of-work/ChangeSetComputer.d.ts +12 -12
  163. package/unit-of-work/ChangeSetComputer.js +170 -178
  164. package/unit-of-work/ChangeSetPersister.d.ts +44 -63
  165. package/unit-of-work/ChangeSetPersister.js +421 -442
  166. package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
  167. package/unit-of-work/CommitOrderCalculator.js +88 -89
  168. package/unit-of-work/IdentityMap.d.ts +31 -31
  169. package/unit-of-work/IdentityMap.js +105 -105
  170. package/unit-of-work/UnitOfWork.d.ts +141 -181
  171. package/unit-of-work/UnitOfWork.js +1183 -1200
  172. package/utils/AbstractMigrator.d.ts +91 -111
  173. package/utils/AbstractMigrator.js +275 -275
  174. package/utils/AbstractSchemaGenerator.d.ts +34 -43
  175. package/utils/AbstractSchemaGenerator.js +122 -121
  176. package/utils/AsyncContext.d.ts +3 -3
  177. package/utils/AsyncContext.js +35 -34
  178. package/utils/Configuration.d.ts +808 -852
  179. package/utils/Configuration.js +344 -359
  180. package/utils/Cursor.d.ts +22 -40
  181. package/utils/Cursor.js +127 -135
  182. package/utils/DataloaderUtils.d.ts +43 -58
  183. package/utils/DataloaderUtils.js +198 -203
  184. package/utils/EntityComparator.d.ts +81 -98
  185. package/utils/EntityComparator.js +728 -824
  186. package/utils/NullHighlighter.d.ts +1 -1
  187. package/utils/NullHighlighter.js +3 -3
  188. package/utils/QueryHelper.d.ts +51 -79
  189. package/utils/QueryHelper.js +361 -372
  190. package/utils/RawQueryFragment.d.ts +34 -50
  191. package/utils/RawQueryFragment.js +105 -107
  192. package/utils/RequestContext.d.ts +32 -32
  193. package/utils/RequestContext.js +53 -52
  194. package/utils/TransactionContext.d.ts +16 -16
  195. package/utils/TransactionContext.js +27 -27
  196. package/utils/TransactionManager.d.ts +58 -58
  197. package/utils/TransactionManager.js +197 -199
  198. package/utils/Utils.d.ts +145 -204
  199. package/utils/Utils.js +813 -814
  200. package/utils/clone.js +113 -104
  201. package/utils/env-vars.js +88 -90
  202. package/utils/fs-utils.d.ts +15 -15
  203. package/utils/fs-utils.js +181 -180
  204. package/utils/upsert-utils.d.ts +5 -20
  205. 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,187 +9,179 @@ 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 (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 (
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);
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 (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
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
107
  }
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;
108
+ computePayload(entity, ignoreUndefined = false) {
109
+ const data = this.#comparator.prepareEntity(entity);
110
+ const wrapped = helper(entity);
111
+ const entityName = wrapped.__meta.class;
112
+ const originalEntityData = wrapped.__originalEntityData;
113
+ if (!wrapped.__initialized) {
114
+ for (const prop of wrapped.__meta.primaryKeys) {
115
+ delete data[prop];
116
+ }
117
+ return data;
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;
185
130
  }
186
- if (target.isDirty()) {
187
- this.#collectionUpdates.add(target);
131
+ processProperty(changeSet, prop, target) {
132
+ if (!target) {
133
+ const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
134
+ targets.forEach(([t]) => this.processProperty(changeSet, prop, t));
135
+ return;
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
+ }
188
144
  }
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
- });
145
+ processToOne(prop, changeSet) {
146
+ const isToOneOwner = prop.kind === ReferenceKind.MANY_TO_ONE || (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner);
147
+ if (!isToOneOwner || prop.mapToPk) {
148
+ return;
149
+ }
150
+ const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
151
+ targets.forEach(([rawTarget, idx]) => {
152
+ const target = Reference.unwrapReference(rawTarget);
153
+ const needsProcessing = target != null && (prop.targetKey != null || !target.__helper.hasPrimaryKey());
154
+ if (needsProcessing) {
155
+ let value = prop.targetKey ? target[prop.targetKey] : target.__helper.__identifier;
156
+ /* v8 ignore next */
157
+ if (prop.targetKey && prop.targetMeta) {
158
+ const targetProp = prop.targetMeta.properties[prop.targetKey];
159
+ if (targetProp?.customType) {
160
+ value = targetProp.customType.convertToDatabaseValue(value, this.#platform, { mode: 'serialization' });
161
+ }
162
+ }
163
+ if (prop.polymorphic) {
164
+ const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, target.constructor);
165
+ Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, new PolymorphicRef(discriminator, value), idx);
166
+ }
167
+ else {
168
+ Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, value, idx);
169
+ }
170
+ }
171
+ });
172
+ }
173
+ processToMany(prop, changeSet) {
174
+ const target = changeSet.entity[prop.name];
175
+ if (!target.isDirty() && changeSet.type !== ChangeSetType.CREATE) {
176
+ return;
177
+ }
178
+ if (target.isDirty()) {
179
+ this.#collectionUpdates.add(target);
180
+ }
181
+ if (prop.owner && !this.#platform.usesPivotTable()) {
182
+ changeSet.payload[prop.name] = target.getItems(false).map((item) => {
183
+ return item.__helper.__identifier ?? item.__helper.getPrimaryKey();
184
+ });
185
+ }
193
186
  }
194
- }
195
187
  }
@@ -4,67 +4,48 @@ 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
- #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;
7
+ #private;
8
+ constructor(em: EntityManager);
9
+ /** Executes all pending INSERT change sets, using batch inserts when possible. */
10
+ executeInserts<T extends object>(changeSets: ChangeSet<T>[], options?: DriverMethodOptions, withSchema?: boolean): Promise<void>;
11
+ /** Executes all pending UPDATE change sets, using batch updates when possible. */
12
+ executeUpdates<T extends object>(changeSets: ChangeSet<T>[], batched: boolean, options?: DriverMethodOptions, withSchema?: boolean): Promise<void>;
13
+ /** Executes all pending DELETE change sets in batches. */
14
+ executeDeletes<T extends object>(changeSets: ChangeSet<T>[], options?: DriverMethodOptions, withSchema?: boolean): Promise<void>;
15
+ private runForEachSchema;
16
+ private validateRequired;
17
+ private processProperties;
18
+ private persistNewEntity;
19
+ private persistNewEntities;
20
+ private prepareOptions;
21
+ private persistNewEntitiesBatch;
22
+ private persistManagedEntity;
23
+ private persistManagedEntities;
24
+ private checkConcurrencyKeys;
25
+ private persistManagedEntitiesBatch;
26
+ private mapPrimaryKey;
27
+ /**
28
+ * Sets populate flag to new entities so they are serialized like if they were loaded from the db
29
+ */
30
+ private markAsPopulated;
31
+ private updateEntity;
32
+ private checkOptimisticLocks;
33
+ private checkOptimisticLock;
34
+ /**
35
+ * This method also handles reloading of database default values for inserts and raw property updates,
36
+ * so we use a single query in case of both versioning and default values is used.
37
+ */
38
+ private reloadVersionValues;
39
+ /**
40
+ * For TPT child tables, resolve EntityIdentifier values in PK fields.
41
+ * The parent table insert assigns the actual PK value, which the child table references.
42
+ */
43
+ private resolveTPTIdentifiers;
44
+ private processProperty;
45
+ /**
46
+ * Maps values returned via `returning` statement (postgres) or the inserted id (other sql drivers).
47
+ * No need to handle composite keys here as they need to be set upfront.
48
+ * We do need to map to the change set payload too, as it will be used in the originalEntityData for new entities.
49
+ */
50
+ mapReturnedValues<T extends object>(entity: T | null | undefined, payload: EntityDictionary<T>, row: Dictionary | undefined, meta: EntityMetadata<T>, upsert?: boolean): void;
70
51
  }