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

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