@mikro-orm/core 7.0.9-dev.9 → 7.0.9

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 +884 -583
  2. package/EntityManager.js +1926 -1899
  3. package/MikroORM.d.ts +103 -74
  4. package/MikroORM.js +177 -179
  5. package/README.md +1 -1
  6. package/cache/CacheAdapter.d.ts +36 -36
  7. package/cache/FileCacheAdapter.d.ts +30 -24
  8. package/cache/FileCacheAdapter.js +80 -78
  9. package/cache/GeneratedCacheAdapter.d.ts +18 -20
  10. package/cache/GeneratedCacheAdapter.js +30 -30
  11. package/cache/MemoryCacheAdapter.d.ts +18 -20
  12. package/cache/MemoryCacheAdapter.js +35 -36
  13. package/cache/NullCacheAdapter.d.ts +16 -16
  14. package/cache/NullCacheAdapter.js +24 -24
  15. package/connections/Connection.d.ts +95 -84
  16. package/connections/Connection.js +165 -168
  17. package/drivers/DatabaseDriver.d.ts +187 -81
  18. package/drivers/DatabaseDriver.js +450 -444
  19. package/drivers/IDatabaseDriver.d.ts +440 -301
  20. package/entity/BaseEntity.d.ts +120 -83
  21. package/entity/BaseEntity.js +43 -43
  22. package/entity/Collection.d.ts +215 -181
  23. package/entity/Collection.js +730 -724
  24. package/entity/EntityAssigner.d.ts +88 -77
  25. package/entity/EntityAssigner.js +231 -230
  26. package/entity/EntityFactory.d.ts +67 -55
  27. package/entity/EntityFactory.js +457 -414
  28. package/entity/EntityHelper.d.ts +35 -23
  29. package/entity/EntityHelper.js +291 -279
  30. package/entity/EntityIdentifier.d.ts +4 -4
  31. package/entity/EntityIdentifier.js +10 -10
  32. package/entity/EntityLoader.d.ts +98 -72
  33. package/entity/EntityLoader.js +792 -761
  34. package/entity/EntityRepository.d.ts +316 -201
  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 +127 -83
  39. package/entity/Reference.js +281 -277
  40. package/entity/WrappedEntity.d.ts +115 -72
  41. package/entity/WrappedEntity.js +168 -166
  42. package/entity/defineEntity.d.ts +1359 -654
  43. package/entity/defineEntity.js +527 -518
  44. package/entity/utils.d.ts +13 -3
  45. package/entity/utils.js +71 -73
  46. package/entity/validators.js +43 -43
  47. package/entity/wrap.js +8 -8
  48. package/enums.d.ts +258 -253
  49. package/enums.js +251 -252
  50. package/errors.d.ts +114 -72
  51. package/errors.js +350 -253
  52. package/events/EventManager.d.ts +26 -14
  53. package/events/EventManager.js +79 -77
  54. package/events/EventSubscriber.d.ts +29 -29
  55. package/events/TransactionEventBroadcaster.d.ts +15 -8
  56. package/events/TransactionEventBroadcaster.js +14 -14
  57. package/exceptions.d.ts +23 -40
  58. package/exceptions.js +35 -52
  59. package/hydration/Hydrator.d.ts +42 -17
  60. package/hydration/Hydrator.js +43 -43
  61. package/hydration/ObjectHydrator.d.ts +50 -17
  62. package/hydration/ObjectHydrator.js +483 -418
  63. package/index.d.ts +116 -2
  64. package/index.js +10 -1
  65. package/logging/DefaultLogger.d.ts +34 -32
  66. package/logging/DefaultLogger.js +86 -86
  67. package/logging/Logger.d.ts +41 -41
  68. package/logging/SimpleLogger.d.ts +13 -11
  69. package/logging/SimpleLogger.js +22 -22
  70. package/logging/colors.d.ts +6 -6
  71. package/logging/colors.js +11 -10
  72. package/logging/inspect.js +7 -7
  73. package/metadata/EntitySchema.d.ts +214 -130
  74. package/metadata/EntitySchema.js +411 -412
  75. package/metadata/MetadataDiscovery.d.ts +114 -114
  76. package/metadata/MetadataDiscovery.js +1957 -1879
  77. package/metadata/MetadataProvider.d.ts +29 -26
  78. package/metadata/MetadataProvider.js +95 -97
  79. package/metadata/MetadataStorage.d.ts +38 -32
  80. package/metadata/MetadataStorage.js +118 -118
  81. package/metadata/MetadataValidator.d.ts +39 -39
  82. package/metadata/MetadataValidator.js +381 -338
  83. package/metadata/discover-entities.d.ts +5 -2
  84. package/metadata/discover-entities.js +35 -37
  85. package/metadata/types.d.ts +615 -531
  86. package/naming-strategy/AbstractNamingStrategy.d.ts +54 -39
  87. package/naming-strategy/AbstractNamingStrategy.js +90 -85
  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 +109 -99
  93. package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
  94. package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
  95. package/not-supported.js +7 -4
  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 +312 -303
  100. package/platforms/Platform.js +695 -642
  101. package/serialization/EntitySerializer.d.ts +49 -26
  102. package/serialization/EntitySerializer.js +224 -218
  103. package/serialization/EntityTransformer.d.ts +10 -6
  104. package/serialization/EntityTransformer.js +219 -217
  105. package/serialization/SerializationContext.d.ts +27 -23
  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 +17 -10
  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 +83 -79
  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 +75 -49
  158. package/types/index.js +52 -26
  159. package/typings.d.ts +1254 -741
  160. package/typings.js +244 -233
  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 +187 -179
  165. package/unit-of-work/ChangeSetPersister.d.ts +69 -50
  166. package/unit-of-work/ChangeSetPersister.js +465 -442
  167. package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
  168. package/unit-of-work/CommitOrderCalculator.js +89 -88
  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 +181 -141
  172. package/unit-of-work/UnitOfWork.js +1236 -1222
  173. package/utils/AbstractMigrator.d.ts +111 -91
  174. package/utils/AbstractMigrator.js +275 -275
  175. package/utils/AbstractSchemaGenerator.d.ts +43 -34
  176. package/utils/AbstractSchemaGenerator.js +121 -122
  177. package/utils/AsyncContext.d.ts +3 -3
  178. package/utils/AsyncContext.js +34 -35
  179. package/utils/Configuration.d.ts +852 -808
  180. package/utils/Configuration.js +359 -344
  181. package/utils/Cursor.d.ts +40 -22
  182. package/utils/Cursor.js +135 -127
  183. package/utils/DataloaderUtils.d.ts +58 -43
  184. package/utils/DataloaderUtils.js +203 -198
  185. package/utils/EntityComparator.d.ts +99 -82
  186. package/utils/EntityComparator.js +829 -737
  187. package/utils/NullHighlighter.d.ts +1 -1
  188. package/utils/NullHighlighter.js +3 -3
  189. package/utils/QueryHelper.d.ts +79 -51
  190. package/utils/QueryHelper.js +372 -361
  191. package/utils/RawQueryFragment.d.ts +50 -34
  192. package/utils/RawQueryFragment.js +107 -105
  193. package/utils/RequestContext.d.ts +32 -32
  194. package/utils/RequestContext.js +52 -53
  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 +199 -197
  199. package/utils/Utils.d.ts +204 -145
  200. package/utils/Utils.js +815 -815
  201. package/utils/clone.js +105 -114
  202. package/utils/env-vars.js +90 -88
  203. package/utils/fs-utils.d.ts +15 -15
  204. package/utils/fs-utils.js +180 -181
  205. package/utils/upsert-utils.d.ts +20 -5
  206. package/utils/upsert-utils.js +114 -116
@@ -8,442 +8,485 @@ import { JsonType } from '../types/JsonType.js';
8
8
  import { isRaw } from '../utils/RawQueryFragment.js';
9
9
  /** @internal Factory responsible for creating, merging, and hydrating entity instances. */
10
10
  export class EntityFactory {
11
- #driver;
12
- #platform;
13
- #config;
14
- #metadata;
15
- #hydrator;
16
- #eventManager;
17
- #comparator;
18
- #em;
19
- constructor(em) {
20
- this.#em = em;
21
- this.#driver = this.#em.getDriver();
22
- this.#platform = this.#driver.getPlatform();
23
- this.#config = this.#em.config;
24
- this.#metadata = this.#em.getMetadata();
25
- this.#hydrator = this.#config.getHydrator(this.#metadata);
26
- this.#eventManager = this.#em.getEventManager();
27
- this.#comparator = this.#em.getComparator();
11
+ #driver;
12
+ #platform;
13
+ #config;
14
+ #metadata;
15
+ #hydrator;
16
+ #eventManager;
17
+ #comparator;
18
+ #em;
19
+ constructor(em) {
20
+ this.#em = em;
21
+ this.#driver = this.#em.getDriver();
22
+ this.#platform = this.#driver.getPlatform();
23
+ this.#config = this.#em.config;
24
+ this.#metadata = this.#em.getMetadata();
25
+ this.#hydrator = this.#config.getHydrator(this.#metadata);
26
+ this.#eventManager = this.#em.getEventManager();
27
+ this.#comparator = this.#em.getComparator();
28
+ }
29
+ /** Creates a new entity instance or returns an existing one from the identity map, hydrating it with the provided data. */
30
+ create(entityName, data, options = {}) {
31
+ data = Reference.unwrapReference(data);
32
+ options.initialized ??= true;
33
+ if (EntityHelper.isEntity(data)) {
34
+ return data;
28
35
  }
29
- /** Creates a new entity instance or returns an existing one from the identity map, hydrating it with the provided data. */
30
- create(entityName, data, options = {}) {
31
- data = Reference.unwrapReference(data);
32
- options.initialized ??= true;
33
- if (EntityHelper.isEntity(data)) {
34
- return data;
35
- }
36
- const meta = this.#metadata.get(entityName);
37
- if (meta.virtual) {
38
- data = { ...data };
39
- const entity = this.createEntity(data, meta, options);
40
- this.hydrate(entity, meta, data, options);
41
- return entity;
42
- }
43
- if (meta.serializedPrimaryKey) {
44
- this.denormalizePrimaryKey(meta, data);
45
- }
46
- const meta2 = this.processDiscriminatorColumn(meta, data);
47
- const exists = this.findEntity(data, meta2, options);
48
- let wrapped = exists && helper(exists);
49
- if (wrapped && !options.refresh) {
50
- const wasInitialized = wrapped.isInitialized();
51
- wrapped.__processing = true;
52
- Utils.dropUndefinedProperties(data);
53
- this.mergeData(meta2, exists, data, options);
54
- wrapped.__processing = false;
55
- wrapped.__initialized ||= !!options.initialized;
56
- if (wrapped.isInitialized()) {
57
- if (!wasInitialized) {
58
- if (meta.root.inheritanceType && !(exists instanceof meta2.class)) {
59
- Object.setPrototypeOf(exists, meta2.prototype);
60
- }
61
- if (options.merge && wrapped.hasPrimaryKey()) {
62
- this.unitOfWork.register(exists, data, { loaded: options.initialized });
63
- // ensure all data keys are tracked as loaded for shouldRefresh checks,
64
- // but don't overwrite __originalEntityData — mergeData already set it
65
- // with DB values for non-user-modified keys, leaving user changes detectable
66
- for (const key of Utils.keys(data)) {
67
- if (meta2.properties[key]) {
68
- wrapped.__loadedProperties.add(key);
69
- }
70
- }
71
- }
72
- if (this.#eventManager.hasListeners(EventType.onInit, meta2)) {
73
- this.#eventManager.dispatchEvent(EventType.onInit, { entity: exists, meta: meta2, em: this.#em });
74
- }
75
- }
76
- return exists;
77
- }
78
- }
79
- data = { ...data };
80
- const entity = exists ?? this.createEntity(data, meta2, options);
81
- wrapped = helper(entity);
82
- wrapped.__processing = true;
83
- wrapped.__initialized = options.initialized;
84
- if (options.newEntity || meta.forceConstructor || meta.virtual) {
85
- const tmp = { ...data };
86
- meta.constructorParams?.forEach(prop => delete tmp[prop]);
87
- this.hydrate(entity, meta2, tmp, options);
88
- // since we now process only a copy of the `data` via hydrator, but later we register the state with the full snapshot,
89
- // we need to go through all props with custom types that have `ensureComparable: true` and ensure they are comparable
90
- // even if they are not part of constructor parameters (as this is otherwise normalized during hydration, here only in `tmp`)
91
- if (options.convertCustomTypes) {
92
- for (const prop of meta.props) {
93
- if (prop.customType?.ensureComparable(meta, prop) && data[prop.name]) {
94
- if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind)) {
95
- continue;
96
- }
97
- if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
98
- Utils.isPlainObject(data[prop.name])) {
99
- data[prop.name] = Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta, true);
100
- }
101
- if (prop.customType instanceof JsonType && this.#platform.convertsJsonAutomatically()) {
102
- data[prop.name] = prop.customType.convertToDatabaseValue(data[prop.name], this.#platform, {
103
- key: prop.name,
104
- mode: 'hydration',
105
- });
106
- }
107
- }
108
- }
109
- }
110
- }
111
- else {
112
- this.hydrate(entity, meta2, data, options);
113
- }
114
- if (exists && meta.root.inheritanceType && !(entity instanceof meta2.class)) {
115
- Object.setPrototypeOf(entity, meta2.prototype);
116
- }
117
- if (options.merge && wrapped.hasPrimaryKey()) {
118
- this.unitOfWork.register(entity, data, {
119
- // Always refresh to ensure the payload is in correct shape for joined strategy. When loading nested relations,
120
- // they will be created early without `Type.ensureComparable` being properly handled, resulting in extra updates.
121
- refresh: options.initialized,
122
- newEntity: options.newEntity,
123
- loaded: options.initialized,
124
- });
125
- if (options.recomputeSnapshot) {
126
- wrapped.__originalEntityData = this.#comparator.prepareEntity(entity);
36
+ const meta = this.#metadata.get(entityName);
37
+ if (meta.virtual) {
38
+ data = { ...data };
39
+ const entity = this.createEntity(data, meta, options);
40
+ this.hydrate(entity, meta, data, options);
41
+ return entity;
42
+ }
43
+ if (meta.serializedPrimaryKey) {
44
+ this.denormalizePrimaryKey(meta, data);
45
+ }
46
+ const meta2 = this.processDiscriminatorColumn(meta, data);
47
+ const exists = this.findEntity(data, meta2, options);
48
+ let wrapped = exists && helper(exists);
49
+ if (wrapped && !options.refresh) {
50
+ const wasInitialized = wrapped.isInitialized();
51
+ wrapped.__processing = true;
52
+ Utils.dropUndefinedProperties(data);
53
+ this.mergeData(meta2, exists, data, options);
54
+ wrapped.__processing = false;
55
+ wrapped.__initialized ||= !!options.initialized;
56
+ if (wrapped.isInitialized()) {
57
+ if (!wasInitialized) {
58
+ if (meta.root.inheritanceType && !(exists instanceof meta2.class)) {
59
+ Object.setPrototypeOf(exists, meta2.prototype);
60
+ }
61
+ if (options.merge && wrapped.hasPrimaryKey()) {
62
+ this.unitOfWork.register(exists, data, { loaded: options.initialized });
63
+ // ensure all data keys are tracked as loaded for shouldRefresh checks,
64
+ // but don't overwrite __originalEntityData — mergeData already set it
65
+ // with DB values for non-user-modified keys, leaving user changes detectable
66
+ for (const key of Utils.keys(data)) {
67
+ if (meta2.properties[key]) {
68
+ wrapped.__loadedProperties.add(key);
69
+ }
127
70
  }
71
+ }
72
+ if (this.#eventManager.hasListeners(EventType.onInit, meta2)) {
73
+ this.#eventManager.dispatchEvent(EventType.onInit, { entity: exists, meta: meta2, em: this.#em });
74
+ }
128
75
  }
129
- if (this.#eventManager.hasListeners(EventType.onInit, meta2)) {
130
- this.#eventManager.dispatchEvent(EventType.onInit, { entity, meta: meta2, em: this.#em });
131
- }
132
- wrapped.__processing = false;
133
- return entity;
76
+ return exists;
77
+ }
134
78
  }
135
- /** Merges new data into an existing entity, preserving user-modified properties. */
136
- mergeData(meta, entity, data, options = {}) {
137
- // merge unchanged properties automatically
138
- data = QueryHelper.processParams(data);
139
- const existsData = this.#comparator.prepareEntity(entity);
140
- const originalEntityData = helper(entity).__originalEntityData ?? {};
141
- const diff = this.#comparator.diffEntities(meta.class, originalEntityData, existsData);
142
- // version properties are not part of entity snapshots
143
- if (meta.versionProperty &&
144
- data[meta.versionProperty] &&
145
- data[meta.versionProperty] !== originalEntityData[meta.versionProperty]) {
146
- diff[meta.versionProperty] = data[meta.versionProperty];
147
- }
148
- const diff2 = this.#comparator.diffEntities(meta.class, existsData, data, { includeInverseSides: true });
149
- // do not override values changed by user; for uninitialized entities,
150
- // the diff may include stale snapshot entries (value is undefined) — skip those
151
- if (helper(entity).__initialized) {
152
- Utils.keys(diff).forEach(key => delete diff2[key]);
153
- }
154
- else {
155
- Utils.keys(diff).forEach(key => {
156
- if (diff[key] !== undefined) {
157
- delete diff2[key];
158
- }
159
- });
160
- }
161
- Utils.keys(diff2)
162
- .filter(key => {
163
- // ignore null values if there is already present non-null value
164
- if (existsData[key] != null) {
165
- return diff2[key] == null;
166
- }
167
- return diff2[key] === undefined;
168
- })
169
- .forEach(key => delete diff2[key]);
170
- // but always add collection properties and formulas if they are part of the `data`
171
- Utils.keys(data)
172
- .filter(key => meta.properties[key]?.formula ||
173
- [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(meta.properties[key]?.kind))
174
- .forEach(key => (diff2[key] = data[key]));
175
- // rehydrated with the new values, skip those changed by user
176
- // use full hydration if the entity is already initialized, even if the caller used `initialized: false`
177
- // (e.g. from createReference), otherwise scalar properties in diff2 won't be applied
178
- const initialized = options.initialized || helper(entity).__initialized;
179
- this.hydrate(entity, meta, diff2, initialized ? { ...options, initialized } : options);
180
- // we need to update the entity data only with keys that were not present before
181
- const nullVal = this.#config.get('forceUndefined') ? undefined : null;
182
- Utils.keys(diff2).forEach(key => {
183
- const prop = meta.properties[key];
184
- if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
185
- Utils.isPlainObject(data[prop.name])) {
186
- // oxfmt-ignore
187
- diff2[key] = entity[prop.name] ? helper(entity[prop.name]).getPrimaryKey(options.convertCustomTypes) : null;
188
- }
189
- if (!options.convertCustomTypes &&
190
- [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE, ReferenceKind.SCALAR].includes(prop.kind) &&
191
- prop.customType?.ensureComparable(meta, prop) &&
192
- diff2[key] != null) {
193
- const converted = prop.customType.convertToJSValue(diff2[key], this.#platform, { force: true });
194
- diff2[key] = prop.customType.convertToDatabaseValue(converted, this.#platform, { fromQuery: true });
79
+ data = { ...data };
80
+ const entity = exists ?? this.createEntity(data, meta2, options);
81
+ wrapped = helper(entity);
82
+ wrapped.__processing = true;
83
+ wrapped.__initialized = options.initialized;
84
+ if (options.newEntity || meta.forceConstructor || meta.virtual) {
85
+ const tmp = { ...data };
86
+ meta.constructorParams?.forEach(prop => delete tmp[prop]);
87
+ this.hydrate(entity, meta2, tmp, options);
88
+ // since we now process only a copy of the `data` via hydrator, but later we register the state with the full snapshot,
89
+ // we need to go through all props with custom types that have `ensureComparable: true` and ensure they are comparable
90
+ // even if they are not part of constructor parameters (as this is otherwise normalized during hydration, here only in `tmp`)
91
+ if (options.convertCustomTypes) {
92
+ for (const prop of meta.props) {
93
+ if (prop.customType?.ensureComparable(meta, prop) && data[prop.name]) {
94
+ if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind)) {
95
+ continue;
195
96
  }
196
- originalEntityData[key] = diff2[key] === null ? nullVal : diff2[key];
197
- helper(entity).__loadedProperties.add(key);
198
- });
199
- // in case of joined loading strategy, we need to cascade the merging to possibly loaded relations manually
200
- meta.relations.forEach(prop => {
201
- if ([ReferenceKind.MANY_TO_MANY, ReferenceKind.ONE_TO_MANY].includes(prop.kind) &&
202
- Array.isArray(data[prop.name])) {
203
- // instead of trying to match the collection items (which could easily fail if the collection was loaded with different ordering),
204
- // we just create the entity from scratch, which will automatically pick the right one from the identity map and call `mergeData` on it
205
- data[prop.name]
206
- .filter(child => Utils.isPlainObject(child)) // objects with prototype can be PKs (e.g. `ObjectId`)
207
- .forEach(child => this.create(prop.targetMeta.class, child, options)); // we can ignore the value, we just care about the `mergeData` call
208
- return;
97
+ if (
98
+ [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
99
+ Utils.isPlainObject(data[prop.name])
100
+ ) {
101
+ data[prop.name] = Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta, true);
209
102
  }
210
- if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
211
- Utils.isPlainObject(data[prop.name]) &&
212
- entity[prop.name] &&
213
- helper(entity[prop.name]).__initialized) {
214
- this.create(prop.targetMeta.class, data[prop.name], options); // we can ignore the value, we just care about the `mergeData` call
103
+ if (prop.customType instanceof JsonType && this.#platform.convertsJsonAutomatically()) {
104
+ data[prop.name] = prop.customType.convertToDatabaseValue(data[prop.name], this.#platform, {
105
+ key: prop.name,
106
+ mode: 'hydration',
107
+ });
215
108
  }
216
- });
217
- this.unitOfWork.normalizeEntityData(meta, originalEntityData);
218
- }
219
- /** Creates or retrieves an uninitialized entity reference by its primary key or alternate key. */
220
- createReference(entityName, id, options = {}) {
221
- options.convertCustomTypes ??= true;
222
- const meta = this.#metadata.get(entityName);
223
- const schema = this.#driver.getSchemaName(meta, options);
224
- // Handle alternate key lookup
225
- if (options.key) {
226
- const value = '' + (Array.isArray(id) ? id[0] : Utils.isPlainObject(id) ? id[options.key] : id);
227
- const exists = this.unitOfWork.getByKey(entityName, options.key, value, schema, options.convertCustomTypes);
228
- if (exists) {
229
- return exists;
230
- }
231
- // Create entity stub - storeByKey will set the alternate key property and store in identity map
232
- const entity = this.create(entityName, {}, { ...options, initialized: false });
233
- this.unitOfWork.storeByKey(entity, options.key, value, schema, options.convertCustomTypes);
234
- return entity;
235
- }
236
- if (meta.simplePK) {
237
- const exists = this.unitOfWork.getById(entityName, id, schema);
238
- if (exists) {
239
- return exists;
240
- }
241
- const data = Utils.isPlainObject(id) ? id : { [meta.primaryKeys[0]]: Array.isArray(id) ? id[0] : id };
242
- return this.create(entityName, data, { ...options, initialized: false });
243
- }
244
- if (Array.isArray(id)) {
245
- id = Utils.getPrimaryKeyCondFromArray(id, meta);
246
- }
247
- const pks = Utils.getOrderedPrimaryKeys(id, meta, this.#platform);
248
- const exists = this.unitOfWork.getById(entityName, pks, schema, options.convertCustomTypes);
249
- if (exists) {
250
- return exists;
251
- }
252
- if (Utils.isPrimaryKey(id)) {
253
- id = { [meta.primaryKeys[0]]: id };
109
+ }
254
110
  }
255
- return this.create(entityName, id, { ...options, initialized: false });
111
+ }
112
+ } else {
113
+ this.hydrate(entity, meta2, data, options);
256
114
  }
257
- /** Creates an embeddable entity instance from the provided data. */
258
- createEmbeddable(entityName, data, options = {}) {
259
- data = { ...data };
260
- const meta = this.#metadata.get(entityName);
261
- const meta2 = this.processDiscriminatorColumn(meta, data);
262
- return this.createEntity(data, meta2, options);
115
+ if (exists && meta.root.inheritanceType && !(entity instanceof meta2.class)) {
116
+ Object.setPrototypeOf(entity, meta2.prototype);
263
117
  }
264
- /** Returns the EntityComparator instance used for diffing entities. */
265
- getComparator() {
266
- return this.#comparator;
118
+ if (options.merge && wrapped.hasPrimaryKey()) {
119
+ this.unitOfWork.register(entity, data, {
120
+ // Always refresh to ensure the payload is in correct shape for joined strategy. When loading nested relations,
121
+ // they will be created early without `Type.ensureComparable` being properly handled, resulting in extra updates.
122
+ refresh: options.initialized,
123
+ newEntity: options.newEntity,
124
+ loaded: options.initialized,
125
+ });
126
+ if (options.recomputeSnapshot) {
127
+ wrapped.__originalEntityData = this.#comparator.prepareEntity(entity);
128
+ }
267
129
  }
268
- createEntity(data, meta, options) {
269
- const schema = this.#driver.getSchemaName(meta, options);
270
- if (options.newEntity || meta.forceConstructor || meta.virtual) {
271
- if (meta.polymorphs) {
272
- throw new Error(`Cannot create entity ${meta.className}, class prototype is unknown`);
273
- }
274
- const params = this.extractConstructorParams(meta, data, options);
275
- const Entity = meta.class;
276
- // creates new instance via constructor as this is the new entity
277
- const entity = new Entity(...params);
278
- // creating managed entity instance when `forceEntityConstructor` is enabled,
279
- // we need to wipe all the values as they would cause update queries on next flush
280
- if (!options.newEntity && (meta.forceConstructor || this.#config.get('forceEntityConstructor'))) {
281
- meta.props
282
- .filter(prop => prop.persist !== false && !prop.primary && data[prop.name] === undefined)
283
- .forEach(prop => delete entity[prop.name]);
284
- }
285
- if (meta.virtual) {
286
- return entity;
287
- }
288
- helper(entity).__schema = schema;
289
- if (options.initialized) {
290
- EntityHelper.ensurePropagation(entity);
291
- }
292
- return entity;
293
- }
294
- // creates new entity instance, bypassing constructor call as its already persisted entity
295
- const entity = Object.create(meta.class.prototype);
296
- helper(entity).__managed = true;
297
- helper(entity).__processing = !meta.embeddable && !meta.virtual;
298
- helper(entity).__schema = schema;
299
- if (options.merge && !options.newEntity) {
300
- this.#hydrator.hydrateReference(entity, meta, data, this, options.convertCustomTypes, options.schema, options.parentSchema);
301
- this.unitOfWork.register(entity);
130
+ if (this.#eventManager.hasListeners(EventType.onInit, meta2)) {
131
+ this.#eventManager.dispatchEvent(EventType.onInit, { entity, meta: meta2, em: this.#em });
132
+ }
133
+ wrapped.__processing = false;
134
+ return entity;
135
+ }
136
+ /** Merges new data into an existing entity, preserving user-modified properties. */
137
+ mergeData(meta, entity, data, options = {}) {
138
+ // merge unchanged properties automatically
139
+ data = QueryHelper.processParams(data);
140
+ const existsData = this.#comparator.prepareEntity(entity);
141
+ const originalEntityData = helper(entity).__originalEntityData ?? {};
142
+ const diff = this.#comparator.diffEntities(meta.class, originalEntityData, existsData);
143
+ // version properties are not part of entity snapshots
144
+ if (
145
+ meta.versionProperty &&
146
+ data[meta.versionProperty] &&
147
+ data[meta.versionProperty] !== originalEntityData[meta.versionProperty]
148
+ ) {
149
+ diff[meta.versionProperty] = data[meta.versionProperty];
150
+ }
151
+ const diff2 = this.#comparator.diffEntities(meta.class, existsData, data, { includeInverseSides: true });
152
+ // do not override values changed by user; for uninitialized entities,
153
+ // the diff may include stale snapshot entries (value is undefined) — skip those
154
+ if (helper(entity).__initialized) {
155
+ Utils.keys(diff).forEach(key => delete diff2[key]);
156
+ } else {
157
+ Utils.keys(diff).forEach(key => {
158
+ if (diff[key] !== undefined) {
159
+ delete diff2[key];
302
160
  }
303
- if (options.initialized) {
304
- EntityHelper.ensurePropagation(entity);
161
+ });
162
+ }
163
+ Utils.keys(diff2)
164
+ .filter(key => {
165
+ // ignore null values if there is already present non-null value
166
+ if (existsData[key] != null) {
167
+ return diff2[key] == null;
305
168
  }
169
+ return diff2[key] === undefined;
170
+ })
171
+ .forEach(key => delete diff2[key]);
172
+ // but always add collection properties, formulas, and generated columns if they are part of the `data`,
173
+ // as these are excluded from `comparableProps` and won't appear in the diff
174
+ Utils.keys(data)
175
+ .filter(
176
+ key =>
177
+ meta.properties[key]?.formula ||
178
+ (meta.properties[key]?.generated && !meta.properties[key]?.primary) ||
179
+ [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(meta.properties[key]?.kind),
180
+ )
181
+ .forEach(key => (diff2[key] = data[key]));
182
+ // rehydrated with the new values, skip those changed by user
183
+ // use full hydration if the entity is already initialized, even if the caller used `initialized: false`
184
+ // (e.g. from createReference), otherwise scalar properties in diff2 won't be applied
185
+ const initialized = options.initialized || helper(entity).__initialized;
186
+ this.hydrate(entity, meta, diff2, initialized ? { ...options, initialized } : options);
187
+ // we need to update the entity data only with keys that were not present before
188
+ const nullVal = this.#config.get('forceUndefined') ? undefined : null;
189
+ Utils.keys(diff2).forEach(key => {
190
+ const prop = meta.properties[key];
191
+ if (
192
+ [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
193
+ Utils.isPlainObject(data[prop.name])
194
+ ) {
195
+ // oxfmt-ignore
196
+ diff2[key] = entity[prop.name] ? helper(entity[prop.name]).getPrimaryKey(options.convertCustomTypes) : null;
197
+ }
198
+ if (
199
+ !options.convertCustomTypes &&
200
+ [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE, ReferenceKind.SCALAR].includes(prop.kind) &&
201
+ prop.customType?.ensureComparable(meta, prop) &&
202
+ diff2[key] != null
203
+ ) {
204
+ const converted = prop.customType.convertToJSValue(diff2[key], this.#platform, { force: true });
205
+ diff2[key] = prop.customType.convertToDatabaseValue(converted, this.#platform, { fromQuery: true });
206
+ }
207
+ originalEntityData[key] = diff2[key] === null ? nullVal : diff2[key];
208
+ helper(entity).__loadedProperties.add(key);
209
+ });
210
+ // in case of joined loading strategy, we need to cascade the merging to possibly loaded relations manually
211
+ meta.relations.forEach(prop => {
212
+ if (
213
+ [ReferenceKind.MANY_TO_MANY, ReferenceKind.ONE_TO_MANY].includes(prop.kind) &&
214
+ Array.isArray(data[prop.name])
215
+ ) {
216
+ // instead of trying to match the collection items (which could easily fail if the collection was loaded with different ordering),
217
+ // we just create the entity from scratch, which will automatically pick the right one from the identity map and call `mergeData` on it
218
+ data[prop.name]
219
+ .filter(child => Utils.isPlainObject(child)) // objects with prototype can be PKs (e.g. `ObjectId`)
220
+ .forEach(child => this.create(prop.targetMeta.class, child, options)); // we can ignore the value, we just care about the `mergeData` call
221
+ return;
222
+ }
223
+ if (
224
+ [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
225
+ Utils.isPlainObject(data[prop.name]) &&
226
+ entity[prop.name] &&
227
+ helper(entity[prop.name]).__initialized
228
+ ) {
229
+ this.create(prop.targetMeta.class, data[prop.name], options); // we can ignore the value, we just care about the `mergeData` call
230
+ }
231
+ });
232
+ this.unitOfWork.normalizeEntityData(meta, originalEntityData);
233
+ }
234
+ /** Creates or retrieves an uninitialized entity reference by its primary key or alternate key. */
235
+ createReference(entityName, id, options = {}) {
236
+ options.convertCustomTypes ??= true;
237
+ const meta = this.#metadata.get(entityName);
238
+ const schema = this.#driver.getSchemaName(meta, options);
239
+ // Handle alternate key lookup
240
+ if (options.key) {
241
+ const value = '' + (Array.isArray(id) ? id[0] : Utils.isPlainObject(id) ? id[options.key] : id);
242
+ const exists = this.unitOfWork.getByKey(entityName, options.key, value, schema, options.convertCustomTypes);
243
+ if (exists) {
244
+ return exists;
245
+ }
246
+ // Create entity stub - storeByKey will set the alternate key property and store in identity map
247
+ const entity = this.create(entityName, {}, { ...options, initialized: false });
248
+ this.unitOfWork.storeByKey(entity, options.key, value, schema, options.convertCustomTypes);
249
+ return entity;
250
+ }
251
+ if (meta.simplePK) {
252
+ const exists = this.unitOfWork.getById(entityName, id, schema);
253
+ if (exists) {
254
+ return exists;
255
+ }
256
+ const data = Utils.isPlainObject(id) ? id : { [meta.primaryKeys[0]]: Array.isArray(id) ? id[0] : id };
257
+ return this.create(entityName, data, { ...options, initialized: false });
258
+ }
259
+ if (Array.isArray(id)) {
260
+ id = Utils.getPrimaryKeyCondFromArray(id, meta);
261
+ }
262
+ const pks = Utils.getOrderedPrimaryKeys(id, meta, this.#platform);
263
+ const exists = this.unitOfWork.getById(entityName, pks, schema, options.convertCustomTypes);
264
+ if (exists) {
265
+ return exists;
266
+ }
267
+ if (Utils.isPrimaryKey(id)) {
268
+ id = { [meta.primaryKeys[0]]: id };
269
+ }
270
+ return this.create(entityName, id, { ...options, initialized: false });
271
+ }
272
+ /** Creates an embeddable entity instance from the provided data. */
273
+ createEmbeddable(entityName, data, options = {}) {
274
+ data = { ...data };
275
+ const meta = this.#metadata.get(entityName);
276
+ const meta2 = this.processDiscriminatorColumn(meta, data);
277
+ return this.createEntity(data, meta2, options);
278
+ }
279
+ /** Returns the EntityComparator instance used for diffing entities. */
280
+ getComparator() {
281
+ return this.#comparator;
282
+ }
283
+ createEntity(data, meta, options) {
284
+ const schema = this.#driver.getSchemaName(meta, options);
285
+ if (options.newEntity || meta.forceConstructor || meta.virtual) {
286
+ if (meta.polymorphs) {
287
+ throw new Error(`Cannot create entity ${meta.className}, class prototype is unknown`);
288
+ }
289
+ const params = this.extractConstructorParams(meta, data, options);
290
+ const Entity = meta.class;
291
+ // creates new instance via constructor as this is the new entity
292
+ const entity = new Entity(...params);
293
+ // creating managed entity instance when `forceEntityConstructor` is enabled,
294
+ // we need to wipe all the values as they would cause update queries on next flush
295
+ if (!options.newEntity && (meta.forceConstructor || this.#config.get('forceEntityConstructor'))) {
296
+ meta.props
297
+ .filter(prop => prop.persist !== false && !prop.primary && data[prop.name] === undefined)
298
+ .forEach(prop => delete entity[prop.name]);
299
+ }
300
+ if (meta.virtual) {
306
301
  return entity;
302
+ }
303
+ helper(entity).__schema = schema;
304
+ if (options.initialized) {
305
+ EntityHelper.ensurePropagation(entity);
306
+ }
307
+ return entity;
307
308
  }
308
- /** @internal */
309
- assignDefaultValues(entity, meta, onCreateOnly) {
310
- for (const prop of meta.props) {
311
- if (prop.embedded || [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
312
- continue;
313
- }
314
- if (prop.onCreate) {
315
- entity[prop.name] ??= prop.onCreate(entity, this.#em);
316
- }
317
- else if (!onCreateOnly && prop.default != null && !isRaw(prop.default) && entity[prop.name] === undefined) {
318
- entity[prop.name] = prop.default;
319
- }
320
- if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
321
- const items = prop.array ? entity[prop.name] : [entity[prop.name]];
322
- for (const item of items) {
323
- // Embedded sub-properties need all defaults since the DB can't apply them within JSON columns.
324
- this.assignDefaultValues(item, prop.targetMeta);
325
- }
326
- }
327
- }
309
+ // creates new entity instance, bypassing constructor call as its already persisted entity
310
+ const entity = Object.create(meta.class.prototype);
311
+ helper(entity).__managed = true;
312
+ helper(entity).__processing = !meta.embeddable && !meta.virtual;
313
+ helper(entity).__schema = schema;
314
+ if (options.merge && !options.newEntity) {
315
+ this.#hydrator.hydrateReference(
316
+ entity,
317
+ meta,
318
+ data,
319
+ this,
320
+ options.convertCustomTypes,
321
+ options.schema,
322
+ options.parentSchema,
323
+ );
324
+ this.unitOfWork.register(entity);
328
325
  }
329
- hydrate(entity, meta, data, options) {
330
- if (options.initialized) {
331
- this.#hydrator.hydrate(entity, meta, data, this, 'full', options.newEntity, options.convertCustomTypes, options.schema, this.#driver.getSchemaName(meta, options), options.normalizeAccessors);
332
- }
333
- else {
334
- this.#hydrator.hydrateReference(entity, meta, data, this, options.convertCustomTypes, options.schema, this.#driver.getSchemaName(meta, options), options.normalizeAccessors);
335
- }
336
- Utils.keys(data).forEach(key => {
337
- helper(entity)?.__loadedProperties.add(key);
338
- helper(entity)?.__serializationContext.fields?.add(key);
339
- });
340
- const processOnCreateHooksEarly = options.processOnCreateHooksEarly ?? this.#config.get('processOnCreateHooksEarly');
341
- if (options.newEntity && processOnCreateHooksEarly) {
342
- this.assignDefaultValues(entity, meta);
343
- }
326
+ if (options.initialized) {
327
+ EntityHelper.ensurePropagation(entity);
344
328
  }
345
- findEntity(data, meta, options) {
346
- const schema = this.#driver.getSchemaName(meta, options);
347
- if (meta.simplePK) {
348
- return this.unitOfWork.getById(meta.class, data[meta.primaryKeys[0]], schema);
349
- }
350
- if (!Array.isArray(data) && meta.primaryKeys.some(pk => data[pk] == null)) {
351
- return undefined;
329
+ return entity;
330
+ }
331
+ /** @internal */
332
+ assignDefaultValues(entity, meta, onCreateOnly) {
333
+ for (const prop of meta.props) {
334
+ if (prop.embedded || [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
335
+ continue;
336
+ }
337
+ if (prop.onCreate) {
338
+ entity[prop.name] ??= prop.onCreate(entity, this.#em);
339
+ } else if (!onCreateOnly && prop.default != null && !isRaw(prop.default) && entity[prop.name] === undefined) {
340
+ entity[prop.name] = prop.default;
341
+ }
342
+ if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
343
+ const items = prop.array ? entity[prop.name] : [entity[prop.name]];
344
+ for (const item of items) {
345
+ // Embedded sub-properties need all defaults since the DB can't apply them within JSON columns.
346
+ this.assignDefaultValues(item, prop.targetMeta);
352
347
  }
353
- const pks = Utils.getOrderedPrimaryKeys(data, meta, this.#platform, options.convertCustomTypes);
354
- return this.unitOfWork.getById(meta.class, pks, schema);
348
+ }
349
+ }
350
+ }
351
+ hydrate(entity, meta, data, options) {
352
+ if (options.initialized) {
353
+ this.#hydrator.hydrate(
354
+ entity,
355
+ meta,
356
+ data,
357
+ this,
358
+ 'full',
359
+ options.newEntity,
360
+ options.convertCustomTypes,
361
+ options.schema,
362
+ this.#driver.getSchemaName(meta, options),
363
+ options.normalizeAccessors,
364
+ );
365
+ } else {
366
+ this.#hydrator.hydrateReference(
367
+ entity,
368
+ meta,
369
+ data,
370
+ this,
371
+ options.convertCustomTypes,
372
+ options.schema,
373
+ this.#driver.getSchemaName(meta, options),
374
+ options.normalizeAccessors,
375
+ );
376
+ }
377
+ Utils.keys(data).forEach(key => {
378
+ helper(entity)?.__loadedProperties.add(key);
379
+ helper(entity)?.__serializationContext.fields?.add(key);
380
+ });
381
+ const processOnCreateHooksEarly =
382
+ options.processOnCreateHooksEarly ?? this.#config.get('processOnCreateHooksEarly');
383
+ if (options.newEntity && processOnCreateHooksEarly) {
384
+ this.assignDefaultValues(entity, meta);
355
385
  }
356
- processDiscriminatorColumn(meta, data) {
357
- // Handle STI discriminator (persisted column)
358
- if (meta.root.inheritanceType === 'sti') {
359
- const prop = meta.properties[meta.root.discriminatorColumn];
360
- const value = data[prop.name];
361
- const type = meta.root.discriminatorMap[value];
362
- meta = type ? this.#metadata.get(type) : meta;
363
- return meta;
386
+ }
387
+ findEntity(data, meta, options) {
388
+ const schema = this.#driver.getSchemaName(meta, options);
389
+ if (meta.simplePK) {
390
+ return this.unitOfWork.getById(meta.class, data[meta.primaryKeys[0]], schema);
391
+ }
392
+ if (!Array.isArray(data) && meta.primaryKeys.some(pk => data[pk] == null)) {
393
+ return undefined;
394
+ }
395
+ const pks = Utils.getOrderedPrimaryKeys(data, meta, this.#platform, options.convertCustomTypes);
396
+ return this.unitOfWork.getById(meta.class, pks, schema);
397
+ }
398
+ processDiscriminatorColumn(meta, data) {
399
+ // Handle STI discriminator (persisted column)
400
+ if (meta.root.inheritanceType === 'sti') {
401
+ const prop = meta.properties[meta.root.discriminatorColumn];
402
+ const value = data[prop.name];
403
+ const type = meta.root.discriminatorMap[value];
404
+ meta = type ? this.#metadata.get(type) : meta;
405
+ return meta;
406
+ }
407
+ // Handle TPT discriminator (computed at query time)
408
+ if (meta.root.inheritanceType === 'tpt' && meta.root.discriminatorMap) {
409
+ const value = data[meta.root.tptDiscriminatorColumn];
410
+ if (value) {
411
+ const type = meta.root.discriminatorMap[value];
412
+ meta = type ? this.#metadata.get(type) : meta;
413
+ }
414
+ }
415
+ return meta;
416
+ }
417
+ /**
418
+ * denormalize PK to value required by driver (e.g. ObjectId)
419
+ */
420
+ denormalizePrimaryKey(meta, data) {
421
+ const pk = meta.getPrimaryProp();
422
+ const spk = meta.properties[meta.serializedPrimaryKey];
423
+ if (!spk?.serializedPrimaryKey) {
424
+ return;
425
+ }
426
+ if (pk.type === 'ObjectId' && (data[pk.name] != null || data[spk.name] != null)) {
427
+ data[pk.name] = this.#platform.denormalizePrimaryKey(data[spk.name] || data[pk.name]);
428
+ delete data[spk.name];
429
+ }
430
+ }
431
+ /**
432
+ * returns parameters for entity constructor, creating references from plain ids
433
+ */
434
+ extractConstructorParams(meta, data, options) {
435
+ if (!meta.constructorParams) {
436
+ return [data];
437
+ }
438
+ return meta.constructorParams.map(k => {
439
+ const prop = meta.properties[k];
440
+ const value = data[k];
441
+ if (prop && [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && value) {
442
+ const pk = Reference.unwrapReference(value);
443
+ const entity = this.unitOfWork.getById(prop.targetMeta.class, pk, options.schema, true);
444
+ if (entity) {
445
+ return entity;
364
446
  }
365
- // Handle TPT discriminator (computed at query time)
366
- if (meta.root.inheritanceType === 'tpt' && meta.root.discriminatorMap) {
367
- const value = data[meta.root.tptDiscriminatorColumn];
368
- if (value) {
369
- const type = meta.root.discriminatorMap[value];
370
- meta = type ? this.#metadata.get(type) : meta;
371
- }
447
+ if (Utils.isEntity(value)) {
448
+ return value;
372
449
  }
373
- return meta;
374
- }
375
- /**
376
- * denormalize PK to value required by driver (e.g. ObjectId)
377
- */
378
- denormalizePrimaryKey(meta, data) {
379
- const pk = meta.getPrimaryProp();
380
- const spk = meta.properties[meta.serializedPrimaryKey];
381
- if (!spk?.serializedPrimaryKey) {
382
- return;
450
+ const nakedPk = Utils.extractPK(value, prop.targetMeta, true);
451
+ if (Utils.isObject(value) && !nakedPk) {
452
+ return this.create(prop.targetMeta.class, value, options);
383
453
  }
384
- if (pk.type === 'ObjectId' && (data[pk.name] != null || data[spk.name] != null)) {
385
- data[pk.name] = this.#platform.denormalizePrimaryKey((data[spk.name] || data[pk.name]));
386
- delete data[spk.name];
454
+ const { newEntity, initialized, ...rest } = options;
455
+ const target = this.createReference(prop.targetMeta.class, nakedPk, rest);
456
+ return Reference.wrapReference(target, prop);
457
+ }
458
+ if (prop?.kind === ReferenceKind.EMBEDDED && value) {
459
+ /* v8 ignore next */
460
+ if (Utils.isEntity(value)) {
461
+ return value;
387
462
  }
388
- }
389
- /**
390
- * returns parameters for entity constructor, creating references from plain ids
391
- */
392
- extractConstructorParams(meta, data, options) {
393
- if (!meta.constructorParams) {
394
- return [data];
463
+ return this.createEmbeddable(prop.targetMeta.class, value, options);
464
+ }
465
+ if (!prop) {
466
+ const tmp = { ...data };
467
+ for (const prop of meta.props) {
468
+ if (!options.convertCustomTypes || !prop.customType || tmp[prop.name] == null) {
469
+ continue;
470
+ }
471
+ if (
472
+ [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
473
+ Utils.isPlainObject(tmp[prop.name]) &&
474
+ !Utils.extractPK(tmp[prop.name], prop.targetMeta, true)
475
+ ) {
476
+ tmp[prop.name] = Reference.wrapReference(this.create(prop.targetMeta.class, tmp[prop.name], options), prop);
477
+ } else if (prop.kind === ReferenceKind.SCALAR) {
478
+ tmp[prop.name] = prop.customType.convertToJSValue(tmp[prop.name], this.#platform);
479
+ }
395
480
  }
396
- return meta.constructorParams.map(k => {
397
- const prop = meta.properties[k];
398
- const value = data[k];
399
- if (prop && [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && value) {
400
- const pk = Reference.unwrapReference(value);
401
- const entity = this.unitOfWork.getById(prop.targetMeta.class, pk, options.schema, true);
402
- if (entity) {
403
- return entity;
404
- }
405
- if (Utils.isEntity(value)) {
406
- return value;
407
- }
408
- const nakedPk = Utils.extractPK(value, prop.targetMeta, true);
409
- if (Utils.isObject(value) && !nakedPk) {
410
- return this.create(prop.targetMeta.class, value, options);
411
- }
412
- const { newEntity, initialized, ...rest } = options;
413
- const target = this.createReference(prop.targetMeta.class, nakedPk, rest);
414
- return Reference.wrapReference(target, prop);
415
- }
416
- if (prop?.kind === ReferenceKind.EMBEDDED && value) {
417
- /* v8 ignore next */
418
- if (Utils.isEntity(value)) {
419
- return value;
420
- }
421
- return this.createEmbeddable(prop.targetMeta.class, value, options);
422
- }
423
- if (!prop) {
424
- const tmp = { ...data };
425
- for (const prop of meta.props) {
426
- if (!options.convertCustomTypes || !prop.customType || tmp[prop.name] == null) {
427
- continue;
428
- }
429
- if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
430
- Utils.isPlainObject(tmp[prop.name]) &&
431
- !Utils.extractPK(tmp[prop.name], prop.targetMeta, true)) {
432
- tmp[prop.name] = Reference.wrapReference(this.create(prop.targetMeta.class, tmp[prop.name], options), prop);
433
- }
434
- else if (prop.kind === ReferenceKind.SCALAR) {
435
- tmp[prop.name] = prop.customType.convertToJSValue(tmp[prop.name], this.#platform);
436
- }
437
- }
438
- return tmp;
439
- }
440
- if (options.convertCustomTypes && prop.customType && value != null) {
441
- return prop.customType.convertToJSValue(value, this.#platform);
442
- }
443
- return value;
444
- });
445
- }
446
- get unitOfWork() {
447
- return this.#em.getUnitOfWork(false);
448
- }
481
+ return tmp;
482
+ }
483
+ if (options.convertCustomTypes && prop.customType && value != null) {
484
+ return prop.customType.convertToJSValue(value, this.#platform);
485
+ }
486
+ return value;
487
+ });
488
+ }
489
+ get unitOfWork() {
490
+ return this.#em.getUnitOfWork(false);
491
+ }
449
492
  }