@mikro-orm/core 7.0.9 → 7.0.10-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 +583 -884
  2. package/EntityManager.js +1899 -1926
  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 +279 -291
  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 +761 -792
  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 +1879 -1957
  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 +1222 -1236
  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 +361 -372
  191. package/utils/RawQueryFragment.d.ts +34 -50
  192. package/utils/RawQueryFragment.js +105 -107
  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 +197 -199
  199. package/utils/Utils.d.ts +145 -204
  200. package/utils/Utils.js +815 -815
  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 +181 -180
  205. package/utils/upsert-utils.d.ts +5 -20
  206. package/utils/upsert-utils.js +116 -114
@@ -1,11 +1,4 @@
1
- import {
2
- EagerProps,
3
- EntityName,
4
- EntityRepositoryType,
5
- HiddenProps,
6
- OptionalProps,
7
- PrimaryKeyProp,
8
- } from '../typings.js';
1
+ import { EagerProps, EntityName, EntityRepositoryType, HiddenProps, OptionalProps, PrimaryKeyProp, } from '../typings.js';
9
2
  import { EntityTransformer } from '../serialization/EntityTransformer.js';
10
3
  import { Reference } from './Reference.js';
11
4
  import { Utils } from '../utils/Utils.js';
@@ -19,308 +12,303 @@ const entitySymbol = Symbol('Entity');
19
12
  * @internal
20
13
  */
21
14
  export class EntityHelper {
22
- static isEntity(data) {
23
- return data != null && typeof data === 'object' && !!data[entitySymbol];
24
- }
25
- static decorate(meta, em) {
26
- const fork = em.fork(); // use fork so we can access `EntityFactory`
27
- const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
28
- if (serializedPrimaryKey) {
29
- Object.defineProperty(meta.prototype, serializedPrimaryKey.name, {
30
- get() {
31
- return this._id ? em.getPlatform().normalizePrimaryKey(this._id) : null;
32
- },
33
- set(id) {
34
- this._id = id ? em.getPlatform().denormalizePrimaryKey(id) : null;
35
- },
36
- configurable: true,
37
- });
15
+ static isEntity(data) {
16
+ return data != null && typeof data === 'object' && !!data[entitySymbol];
38
17
  }
39
- EntityHelper.defineBaseProperties(meta, meta.prototype, fork);
40
- EntityHelper.defineCustomInspect(meta);
41
- if (em.config.get('propagationOnPrototype') && !meta.embeddable && !meta.virtual) {
42
- EntityHelper.defineProperties(meta, fork);
18
+ static decorate(meta, em) {
19
+ const fork = em.fork(); // use fork so we can access `EntityFactory`
20
+ const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
21
+ if (serializedPrimaryKey) {
22
+ Object.defineProperty(meta.prototype, serializedPrimaryKey.name, {
23
+ get() {
24
+ return this._id ? em.getPlatform().normalizePrimaryKey(this._id) : null;
25
+ },
26
+ set(id) {
27
+ this._id = id ? em.getPlatform().denormalizePrimaryKey(id) : null;
28
+ },
29
+ configurable: true,
30
+ });
31
+ }
32
+ EntityHelper.defineBaseProperties(meta, meta.prototype, fork);
33
+ EntityHelper.defineCustomInspect(meta);
34
+ if (em.config.get('propagationOnPrototype') && !meta.embeddable && !meta.virtual) {
35
+ EntityHelper.defineProperties(meta, fork);
36
+ }
37
+ const prototype = meta.prototype;
38
+ if (!prototype.toJSON) {
39
+ // toJSON can be overridden
40
+ Object.defineProperty(prototype, 'toJSON', {
41
+ value: function (...args) {
42
+ // Guard against being called on the prototype itself (e.g. by serializers
43
+ // walking the object graph and calling toJSON on prototype objects)
44
+ if (this === prototype) {
45
+ return {};
46
+ }
47
+ return EntityTransformer.toObject(this, ...args);
48
+ },
49
+ writable: true,
50
+ configurable: true,
51
+ enumerable: false,
52
+ });
53
+ }
43
54
  }
44
- const prototype = meta.prototype;
45
- if (!prototype.toJSON) {
46
- // toJSON can be overridden
47
- Object.defineProperty(prototype, 'toJSON', {
48
- value: function (...args) {
49
- // Guard against being called on the prototype itself (e.g. by serializers
50
- // walking the object graph and calling toJSON on prototype objects)
51
- if (this === prototype) {
52
- return {};
53
- }
54
- return EntityTransformer.toObject(this, ...args);
55
- },
56
- writable: true,
57
- configurable: true,
58
- enumerable: false,
59
- });
55
+ /**
56
+ * As a performance optimization, we create entity state methods lazily. We first add
57
+ * the `null` value to the prototype to reserve space in memory. Then we define a setter on the
58
+ * prototype that will be executed exactly once per entity instance. There we redefine the given
59
+ * property on the entity instance, so shadowing the prototype setter.
60
+ */
61
+ static defineBaseProperties(meta, prototype, em) {
62
+ // oxfmt-ignore
63
+ const helperParams = meta.embeddable || meta.virtual ? [] : [em.getComparator().getPkGetter(meta), em.getComparator().getPkSerializer(meta), em.getComparator().getPkGetterConverted(meta)];
64
+ Object.defineProperties(prototype, {
65
+ [entitySymbol]: { value: !meta.embeddable, enumerable: false, configurable: true },
66
+ __meta: { value: meta, configurable: true },
67
+ __config: { value: em.config, configurable: true },
68
+ __platform: { value: em.getPlatform(), configurable: true },
69
+ __factory: { value: em.getEntityFactory(), configurable: true },
70
+ __helper: {
71
+ get() {
72
+ Object.defineProperty(this, '__helper', {
73
+ value: new WrappedEntity(this, em.getHydrator(), ...helperParams),
74
+ enumerable: false,
75
+ configurable: true,
76
+ });
77
+ return this.__helper;
78
+ },
79
+ configurable: true, // otherwise jest fails when trying to compare entities ¯\_(ツ)_/¯
80
+ },
81
+ });
60
82
  }
61
- }
62
- /**
63
- * As a performance optimization, we create entity state methods lazily. We first add
64
- * the `null` value to the prototype to reserve space in memory. Then we define a setter on the
65
- * prototype that will be executed exactly once per entity instance. There we redefine the given
66
- * property on the entity instance, so shadowing the prototype setter.
67
- */
68
- static defineBaseProperties(meta, prototype, em) {
69
- // oxfmt-ignore
70
- const helperParams = meta.embeddable || meta.virtual ? [] : [em.getComparator().getPkGetter(meta), em.getComparator().getPkSerializer(meta), em.getComparator().getPkGetterConverted(meta)];
71
- Object.defineProperties(prototype, {
72
- [entitySymbol]: { value: !meta.embeddable, enumerable: false, configurable: true },
73
- __meta: { value: meta, configurable: true },
74
- __config: { value: em.config, configurable: true },
75
- __platform: { value: em.getPlatform(), configurable: true },
76
- __factory: { value: em.getEntityFactory(), configurable: true },
77
- __helper: {
78
- get() {
79
- Object.defineProperty(this, '__helper', {
80
- value: new WrappedEntity(this, em.getHydrator(), ...helperParams),
81
- enumerable: false,
82
- configurable: true,
83
- });
84
- return this.__helper;
85
- },
86
- configurable: true, // otherwise jest fails when trying to compare entities ¯\_(ツ)_/¯
87
- },
88
- });
89
- }
90
- /**
91
- * Defines getter and setter for every owning side of m:1 and 1:1 relation. This is then used for propagation of
92
- * changes to the inverse side of bi-directional relations. Rest of the properties are also defined this way to
93
- * achieve dirtiness, which is then used for fast checks whether we need to auto-flush because of managed entities.
94
- *
95
- * First defines a setter on the prototype, once called, actual get/set handlers are registered on the instance rather
96
- * than on its prototype. Thanks to this we still have those properties enumerable (e.g. part of `Object.keys(entity)`).
97
- */
98
- static defineProperties(meta, em) {
99
- Object.values(meta.properties).forEach(prop => {
100
- const isCollection = [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind);
101
- // oxfmt-ignore
102
- const isReference = [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk;
103
- if (isReference) {
104
- Object.defineProperty(meta.prototype, prop.name, {
105
- set(val) {
106
- EntityHelper.defineReferenceProperty(meta, prop, this, em.getHydrator());
107
- this[prop.name] = val;
108
- },
109
- configurable: true,
83
+ /**
84
+ * Defines getter and setter for every owning side of m:1 and 1:1 relation. This is then used for propagation of
85
+ * changes to the inverse side of bi-directional relations. Rest of the properties are also defined this way to
86
+ * achieve dirtiness, which is then used for fast checks whether we need to auto-flush because of managed entities.
87
+ *
88
+ * First defines a setter on the prototype, once called, actual get/set handlers are registered on the instance rather
89
+ * than on its prototype. Thanks to this we still have those properties enumerable (e.g. part of `Object.keys(entity)`).
90
+ */
91
+ static defineProperties(meta, em) {
92
+ Object.values(meta.properties).forEach(prop => {
93
+ const isCollection = [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind);
94
+ // oxfmt-ignore
95
+ const isReference = [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk;
96
+ if (isReference) {
97
+ Object.defineProperty(meta.prototype, prop.name, {
98
+ set(val) {
99
+ EntityHelper.defineReferenceProperty(meta, prop, this, em.getHydrator());
100
+ this[prop.name] = val;
101
+ },
102
+ configurable: true,
103
+ });
104
+ return;
105
+ }
106
+ if (prop.inherited || prop.primary || prop.accessor || prop.persist === false || prop.embedded || isCollection) {
107
+ return;
108
+ }
109
+ Object.defineProperty(meta.prototype, prop.name, {
110
+ set(val) {
111
+ Object.defineProperty(this, prop.name, {
112
+ get() {
113
+ return this.__helper?.__data[prop.name];
114
+ },
115
+ set(val) {
116
+ this.__helper.__data[prop.name] = val;
117
+ },
118
+ enumerable: true,
119
+ configurable: true,
120
+ });
121
+ this.__helper.__data[prop.name] = val;
122
+ },
123
+ configurable: true,
124
+ });
110
125
  });
111
- return;
112
- }
113
- if (prop.inherited || prop.primary || prop.accessor || prop.persist === false || prop.embedded || isCollection) {
114
- return;
115
- }
116
- Object.defineProperty(meta.prototype, prop.name, {
117
- set(val) {
118
- Object.defineProperty(this, prop.name, {
126
+ }
127
+ static defineCustomInspect(meta) {
128
+ // @ts-ignore
129
+ meta.prototype[Symbol.for('nodejs.util.inspect.custom')] ??= function (depth = 2) {
130
+ const object = {};
131
+ const keys = new Set(Utils.keys(this));
132
+ for (const prop of meta.props) {
133
+ if (keys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
134
+ object[prop.name] = this[prop.name];
135
+ }
136
+ }
137
+ for (const key of keys) {
138
+ if (!meta.properties[key]) {
139
+ object[key] = this[key];
140
+ }
141
+ }
142
+ // ensure we dont have internal symbols in the POJO
143
+ [OptionalProps, EntityRepositoryType, PrimaryKeyProp, EagerProps, HiddenProps, EntityName].forEach(sym => delete object[sym]);
144
+ meta.props.filter(prop => object[prop.name] === undefined).forEach(prop => delete object[prop.name]);
145
+ const ret = inspect(object, { depth });
146
+ let name = this.constructor.name;
147
+ const showEM = ['true', 't', '1'].includes(getEnv('MIKRO_ORM_LOG_EM_ID')?.toLowerCase() ?? '');
148
+ if (showEM) {
149
+ if (helper(this).__em) {
150
+ name += ` [managed by ${helper(this).__em.id}]`;
151
+ }
152
+ else {
153
+ name += ` [not managed]`;
154
+ }
155
+ }
156
+ // distinguish not initialized entities
157
+ if (!helper(this).__initialized) {
158
+ name = `(${name})`;
159
+ }
160
+ return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
161
+ };
162
+ }
163
+ static defineReferenceProperty(meta, prop, ref, hydrator) {
164
+ const wrapped = helper(ref);
165
+ Object.defineProperty(ref, prop.name, {
119
166
  get() {
120
- return this.__helper?.__data[prop.name];
167
+ return helper(ref).__data[prop.name];
121
168
  },
122
169
  set(val) {
123
- this.__helper.__data[prop.name] = val;
170
+ const entity = Reference.unwrapReference(val ?? wrapped.__data[prop.name]);
171
+ const old = Reference.unwrapReference(wrapped.__data[prop.name]);
172
+ // oxfmt-ignore
173
+ if (old && old !== entity && prop.kind === ReferenceKind.MANY_TO_ONE && prop.inversedBy && old[prop.inversedBy]) {
174
+ old[prop.inversedBy].removeWithoutPropagation(this);
175
+ }
176
+ wrapped.__data[prop.name] = Reference.wrapReference(val, prop);
177
+ // when propagation from inside hydration, we set the FK to the entity data immediately
178
+ if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
179
+ wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(wrapped.__data[prop.name], prop.targetMeta, true);
180
+ }
181
+ EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
124
182
  },
125
183
  enumerable: true,
126
184
  configurable: true,
127
- });
128
- this.__helper.__data[prop.name] = val;
129
- },
130
- configurable: true,
131
- });
132
- });
133
- }
134
- static defineCustomInspect(meta) {
135
- // @ts-ignore
136
- meta.prototype[Symbol.for('nodejs.util.inspect.custom')] ??= function (depth = 2) {
137
- const object = {};
138
- const keys = new Set(Utils.keys(this));
139
- for (const prop of meta.props) {
140
- if (keys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
141
- object[prop.name] = this[prop.name];
142
- }
143
- }
144
- for (const key of keys) {
145
- if (!meta.properties[key]) {
146
- object[key] = this[key];
185
+ });
186
+ }
187
+ static propagate(meta, entity, owner, prop, value, old) {
188
+ // For polymorphic relations, get bidirectional relations from the actual entity's metadata
189
+ let bidirectionalRelations;
190
+ if (prop.polymorphic && prop.polymorphTargets?.length) {
191
+ // For polymorphic relations, we need to get the bidirectional relations from the actual value's metadata
192
+ if (!value) {
193
+ return; // No value means no propagation needed
194
+ }
195
+ bidirectionalRelations = helper(value).__meta.bidirectionalRelations;
147
196
  }
148
- }
149
- // ensure we dont have internal symbols in the POJO
150
- [OptionalProps, EntityRepositoryType, PrimaryKeyProp, EagerProps, HiddenProps, EntityName].forEach(
151
- sym => delete object[sym],
152
- );
153
- meta.props.filter(prop => object[prop.name] === undefined).forEach(prop => delete object[prop.name]);
154
- const ret = inspect(object, { depth });
155
- let name = this.constructor.name;
156
- const showEM = ['true', 't', '1'].includes(getEnv('MIKRO_ORM_LOG_EM_ID')?.toLowerCase() ?? '');
157
- if (showEM) {
158
- if (helper(this).__em) {
159
- name += ` [managed by ${helper(this).__em.id}]`;
160
- } else {
161
- name += ` [not managed]`;
197
+ else {
198
+ bidirectionalRelations = prop.targetMeta.bidirectionalRelations;
162
199
  }
163
- }
164
- // distinguish not initialized entities
165
- if (!helper(this).__initialized) {
166
- name = `(${name})`;
167
- }
168
- return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
169
- };
170
- }
171
- static defineReferenceProperty(meta, prop, ref, hydrator) {
172
- const wrapped = helper(ref);
173
- Object.defineProperty(ref, prop.name, {
174
- get() {
175
- return helper(ref).__data[prop.name];
176
- },
177
- set(val) {
178
- const entity = Reference.unwrapReference(val ?? wrapped.__data[prop.name]);
179
- const old = Reference.unwrapReference(wrapped.__data[prop.name]);
180
- // oxfmt-ignore
181
- if (old && old !== entity && prop.kind === ReferenceKind.MANY_TO_ONE && prop.inversedBy && old[prop.inversedBy]) {
182
- old[prop.inversedBy].removeWithoutPropagation(this);
200
+ for (const prop2 of bidirectionalRelations) {
201
+ if ((prop2.inversedBy || prop2.mappedBy) !== prop.name) {
202
+ continue;
203
+ }
204
+ // oxfmt-ignore
205
+ if (prop2.targetMeta.abstract ? prop2.targetMeta.root.class !== meta.root.class : prop2.targetMeta.class !== meta.class) {
206
+ continue;
207
+ }
208
+ const inverse = value?.[prop2.name];
209
+ if (prop.ref && owner[prop.name]) {
210
+ // eslint-disable-next-line dot-notation
211
+ owner[prop.name]['property'] = prop;
212
+ }
213
+ if (Utils.isCollection(inverse) && inverse.isPartial()) {
214
+ continue;
215
+ }
216
+ if (prop.kind === ReferenceKind.MANY_TO_ONE && Utils.isCollection(inverse) && inverse.isInitialized()) {
217
+ inverse.addWithoutPropagation(owner);
218
+ helper(owner).__em?.getUnitOfWork().cancelOrphanRemoval(owner);
219
+ }
220
+ if (prop.kind === ReferenceKind.ONE_TO_ONE) {
221
+ if ((value != null && Reference.unwrapReference(inverse) !== owner) ||
222
+ (value == null && entity?.[prop2.name] != null)) {
223
+ if (entity && (!prop.owner || helper(entity).__initialized)) {
224
+ EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old);
225
+ }
226
+ }
227
+ else if (old && old !== value) {
228
+ // Inverse already points to owner — propagation is not needed,
229
+ // but we still need to clean up old's inverse side.
230
+ helper(old).__pk ??= helper(old).getPrimaryKey();
231
+ // Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
232
+ if (old[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
233
+ delete helper(old).__data[prop2.name];
234
+ old[prop2.name] = null;
235
+ }
236
+ }
237
+ if (old && old !== value && prop.orphanRemoval) {
238
+ helper(old).__pk ??= helper(old).getPrimaryKey();
239
+ helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
183
240
  }
184
- wrapped.__data[prop.name] = Reference.wrapReference(val, prop);
185
- // when propagation from inside hydration, we set the FK to the entity data immediately
186
- if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
187
- wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(
188
- wrapped.__data[prop.name],
189
- prop.targetMeta,
190
- true,
191
- );
241
+ }
192
242
  }
193
- EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
194
- },
195
- enumerable: true,
196
- configurable: true,
197
- });
198
- }
199
- static propagate(meta, entity, owner, prop, value, old) {
200
- // For polymorphic relations, get bidirectional relations from the actual entity's metadata
201
- let bidirectionalRelations;
202
- if (prop.polymorphic && prop.polymorphTargets?.length) {
203
- // For polymorphic relations, we need to get the bidirectional relations from the actual value's metadata
204
- if (!value) {
205
- return; // No value means no propagation needed
206
- }
207
- bidirectionalRelations = helper(value).__meta.bidirectionalRelations;
208
- } else {
209
- bidirectionalRelations = prop.targetMeta.bidirectionalRelations;
210
243
  }
211
- for (const prop2 of bidirectionalRelations) {
212
- if ((prop2.inversedBy || prop2.mappedBy) !== prop.name) {
213
- continue;
214
- }
215
- // oxfmt-ignore
216
- if (prop2.targetMeta.abstract ? prop2.targetMeta.root.class !== meta.root.class : prop2.targetMeta.class !== meta.class) {
217
- continue;
244
+ static propagateOneToOne(entity, owner, prop, prop2, value, old) {
245
+ helper(entity).__pk = helper(entity).getPrimaryKey();
246
+ // the inverse side will be changed on the `value` too, so we need to clean-up and schedule orphan removal there too
247
+ if (!prop.primary &&
248
+ !prop2.mapToPk &&
249
+ value?.[prop2.name] != null &&
250
+ Reference.unwrapReference(value[prop2.name]) !== entity) {
251
+ const other = Reference.unwrapReference(value[prop2.name]);
252
+ delete helper(other).__data[prop.name];
253
+ if (prop2.orphanRemoval) {
254
+ helper(other).__em?.getUnitOfWork().scheduleOrphanRemoval(other);
218
255
  }
219
- const inverse = value?.[prop2.name];
220
- if (prop.ref && owner[prop.name]) {
221
- // eslint-disable-next-line dot-notation
222
- owner[prop.name]['property'] = prop;
223
- }
224
- if (Utils.isCollection(inverse) && inverse.isPartial()) {
225
- continue;
226
- }
227
- if (prop.kind === ReferenceKind.MANY_TO_ONE && Utils.isCollection(inverse) && inverse.isInitialized()) {
228
- inverse.addWithoutPropagation(owner);
229
- helper(owner).__em?.getUnitOfWork().cancelOrphanRemoval(owner);
230
- }
231
- if (prop.kind === ReferenceKind.ONE_TO_ONE) {
232
- if (
233
- (value != null && Reference.unwrapReference(inverse) !== owner) ||
234
- (value == null && entity?.[prop2.name] != null)
235
- ) {
236
- if (entity && (!prop.owner || helper(entity).__initialized)) {
237
- EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old);
238
- }
239
- } else if (old && old !== value) {
240
- // Inverse already points to owner — propagation is not needed,
241
- // but we still need to clean up old's inverse side.
242
- helper(old).__pk ??= helper(old).getPrimaryKey();
243
- // Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
244
- if (old[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
256
+ }
257
+ // Skip setting the inverse side to null if it's a primary key - the entity will be removed via orphan removal
258
+ // Setting a primary key to null would corrupt the entity and cause validation errors
259
+ if (value == null && prop.orphanRemoval && prop2.primary) {
260
+ return;
261
+ }
262
+ if (value == null) {
263
+ entity[prop2.name] = value;
264
+ }
265
+ else if (prop2.mapToPk) {
266
+ entity[prop2.name] = helper(owner).getPrimaryKey();
267
+ }
268
+ else {
269
+ entity[prop2.name] = Reference.wrapReference(owner, prop);
270
+ }
271
+ // Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
272
+ if (old?.[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
245
273
  delete helper(old).__data[prop2.name];
246
274
  old[prop2.name] = null;
247
- }
248
275
  }
249
- if (old && old !== value && prop.orphanRemoval) {
250
- helper(old).__pk ??= helper(old).getPrimaryKey();
251
- helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
252
- }
253
- }
254
- }
255
- }
256
- static propagateOneToOne(entity, owner, prop, prop2, value, old) {
257
- helper(entity).__pk = helper(entity).getPrimaryKey();
258
- // the inverse side will be changed on the `value` too, so we need to clean-up and schedule orphan removal there too
259
- if (
260
- !prop.primary &&
261
- !prop2.mapToPk &&
262
- value?.[prop2.name] != null &&
263
- Reference.unwrapReference(value[prop2.name]) !== entity
264
- ) {
265
- const other = Reference.unwrapReference(value[prop2.name]);
266
- delete helper(other).__data[prop.name];
267
- if (prop2.orphanRemoval) {
268
- helper(other).__em?.getUnitOfWork().scheduleOrphanRemoval(other);
269
- }
270
- }
271
- // Skip setting the inverse side to null if it's a primary key - the entity will be removed via orphan removal
272
- // Setting a primary key to null would corrupt the entity and cause validation errors
273
- if (value == null && prop.orphanRemoval && prop2.primary) {
274
- return;
275
- }
276
- if (value == null) {
277
- entity[prop2.name] = value;
278
- } else if (prop2.mapToPk) {
279
- entity[prop2.name] = helper(owner).getPrimaryKey();
280
- } else {
281
- entity[prop2.name] = Reference.wrapReference(owner, prop);
282
- }
283
- // Don't nullify the FK if it's part of the PK — the entity will be deleted via orphan removal
284
- if (old?.[prop2.name] != null && !(prop.orphanRemoval && prop2.primary)) {
285
- delete helper(old).__data[prop2.name];
286
- old[prop2.name] = null;
287
- }
288
- }
289
- static ensurePropagation(entity) {
290
- if (entity.__gettersDefined) {
291
- return;
292
- }
293
- const wrapped = helper(entity);
294
- const meta = wrapped.__meta;
295
- const platform = wrapped.__platform;
296
- const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
297
- const values = [];
298
- if (serializedPrimaryKey) {
299
- const pk = meta.getPrimaryProps()[0];
300
- const val = entity[serializedPrimaryKey.name];
301
- delete entity[serializedPrimaryKey.name];
302
- Object.defineProperty(entity, serializedPrimaryKey.name, {
303
- get() {
304
- return this[pk.name] ? platform.normalizePrimaryKey(this[pk.name]) : null;
305
- },
306
- set(id) {
307
- this[pk.name] = id ? platform.denormalizePrimaryKey(id) : null;
308
- },
309
- configurable: true,
310
- });
311
- if (entity[pk.name] == null && val != null) {
312
- values.push(serializedPrimaryKey.name, val);
313
- }
314
276
  }
315
- for (const prop of meta.trackingProps) {
316
- if (entity[prop.name] !== undefined) {
317
- values.push(prop.name, entity[prop.name]);
318
- }
319
- delete entity[prop.name];
320
- }
321
- Object.defineProperties(entity, meta.definedProperties);
322
- for (let i = 0; i < values.length; i += 2) {
323
- entity[values[i]] = values[i + 1];
277
+ static ensurePropagation(entity) {
278
+ if (entity.__gettersDefined) {
279
+ return;
280
+ }
281
+ const wrapped = helper(entity);
282
+ const meta = wrapped.__meta;
283
+ const platform = wrapped.__platform;
284
+ const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
285
+ const values = [];
286
+ if (serializedPrimaryKey) {
287
+ const pk = meta.getPrimaryProps()[0];
288
+ const val = entity[serializedPrimaryKey.name];
289
+ delete entity[serializedPrimaryKey.name];
290
+ Object.defineProperty(entity, serializedPrimaryKey.name, {
291
+ get() {
292
+ return this[pk.name] ? platform.normalizePrimaryKey(this[pk.name]) : null;
293
+ },
294
+ set(id) {
295
+ this[pk.name] = id ? platform.denormalizePrimaryKey(id) : null;
296
+ },
297
+ configurable: true,
298
+ });
299
+ if (entity[pk.name] == null && val != null) {
300
+ values.push(serializedPrimaryKey.name, val);
301
+ }
302
+ }
303
+ for (const prop of meta.trackingProps) {
304
+ if (entity[prop.name] !== undefined) {
305
+ values.push(prop.name, entity[prop.name]);
306
+ }
307
+ delete entity[prop.name];
308
+ }
309
+ Object.defineProperties(entity, meta.definedProperties);
310
+ for (let i = 0; i < values.length; i += 2) {
311
+ entity[values[i]] = values[i + 1];
312
+ }
324
313
  }
325
- }
326
314
  }
@@ -3,8 +3,8 @@ import type { IPrimaryKey } from '../typings.js';
3
3
  * @internal
4
4
  */
5
5
  export declare class EntityIdentifier {
6
- private value?;
7
- constructor(value?: IPrimaryKey | undefined);
8
- setValue(value: IPrimaryKey): void;
9
- getValue<T extends IPrimaryKey = IPrimaryKey>(): T;
6
+ private value?;
7
+ constructor(value?: IPrimaryKey | undefined);
8
+ setValue(value: IPrimaryKey): void;
9
+ getValue<T extends IPrimaryKey = IPrimaryKey>(): T;
10
10
  }
@@ -2,14 +2,14 @@
2
2
  * @internal
3
3
  */
4
4
  export class EntityIdentifier {
5
- value;
6
- constructor(value) {
7
- this.value = value;
8
- }
9
- setValue(value) {
10
- this.value = value;
11
- }
12
- getValue() {
13
- return this.value;
14
- }
5
+ value;
6
+ constructor(value) {
7
+ this.value = value;
8
+ }
9
+ setValue(value) {
10
+ this.value = value;
11
+ }
12
+ getValue() {
13
+ return this.value;
14
+ }
15
15
  }