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

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 +178 -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 +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 +213 -180
  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 +455 -383
  28. package/entity/EntityHelper.d.ts +35 -23
  29. package/entity/EntityHelper.js +289 -267
  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 +760 -723
  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 +1347 -637
  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 +1944 -1870
  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 +667 -644
  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 -737
  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 +63 -44
  166. package/unit-of-work/ChangeSetPersister.js +446 -421
  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 +1200 -1183
  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 +98 -81
  186. package/utils/EntityComparator.js +829 -735
  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
@@ -4,27 +4,39 @@ import { type EntityMetadata, type EntityProperty, type IHydrator } from '../typ
4
4
  * @internal
5
5
  */
6
6
  export declare class EntityHelper {
7
- static isEntity(data: any): boolean;
8
- static decorate<T extends object>(meta: EntityMetadata<T>, em: EntityManager): void;
9
- /**
10
- * As a performance optimization, we create entity state methods lazily. We first add
11
- * the `null` value to the prototype to reserve space in memory. Then we define a setter on the
12
- * prototype that will be executed exactly once per entity instance. There we redefine the given
13
- * property on the entity instance, so shadowing the prototype setter.
14
- */
15
- private static defineBaseProperties;
16
- /**
17
- * Defines getter and setter for every owning side of m:1 and 1:1 relation. This is then used for propagation of
18
- * changes to the inverse side of bi-directional relations. Rest of the properties are also defined this way to
19
- * achieve dirtiness, which is then used for fast checks whether we need to auto-flush because of managed entities.
20
- *
21
- * First defines a setter on the prototype, once called, actual get/set handlers are registered on the instance rather
22
- * than on its prototype. Thanks to this we still have those properties enumerable (e.g. part of `Object.keys(entity)`).
23
- */
24
- private static defineProperties;
25
- static defineCustomInspect<T extends object>(meta: EntityMetadata<T>): void;
26
- static defineReferenceProperty<T extends object>(meta: EntityMetadata<T>, prop: EntityProperty<T>, ref: T, hydrator: IHydrator): void;
27
- static propagate<T extends object>(meta: EntityMetadata<T>, entity: T, owner: T, prop: EntityProperty<T>, value?: T[keyof T & string], old?: T): void;
28
- private static propagateOneToOne;
29
- static ensurePropagation<T extends object>(entity: T): void;
7
+ static isEntity(data: any): boolean;
8
+ static decorate<T extends object>(meta: EntityMetadata<T>, em: EntityManager): void;
9
+ /**
10
+ * As a performance optimization, we create entity state methods lazily. We first add
11
+ * the `null` value to the prototype to reserve space in memory. Then we define a setter on the
12
+ * prototype that will be executed exactly once per entity instance. There we redefine the given
13
+ * property on the entity instance, so shadowing the prototype setter.
14
+ */
15
+ private static defineBaseProperties;
16
+ /**
17
+ * Defines getter and setter for every owning side of m:1 and 1:1 relation. This is then used for propagation of
18
+ * changes to the inverse side of bi-directional relations. Rest of the properties are also defined this way to
19
+ * achieve dirtiness, which is then used for fast checks whether we need to auto-flush because of managed entities.
20
+ *
21
+ * First defines a setter on the prototype, once called, actual get/set handlers are registered on the instance rather
22
+ * than on its prototype. Thanks to this we still have those properties enumerable (e.g. part of `Object.keys(entity)`).
23
+ */
24
+ private static defineProperties;
25
+ static defineCustomInspect<T extends object>(meta: EntityMetadata<T>): void;
26
+ static defineReferenceProperty<T extends object>(
27
+ meta: EntityMetadata<T>,
28
+ prop: EntityProperty<T>,
29
+ ref: T,
30
+ hydrator: IHydrator,
31
+ ): void;
32
+ static propagate<T extends object>(
33
+ meta: EntityMetadata<T>,
34
+ entity: T,
35
+ owner: T,
36
+ prop: EntityProperty<T>,
37
+ value?: T[keyof T & string],
38
+ old?: T,
39
+ ): void;
40
+ private static propagateOneToOne;
41
+ static ensurePropagation<T extends object>(entity: T): void;
30
42
  }
@@ -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';
@@ -12,291 +19,306 @@ const entitySymbol = Symbol('Entity');
12
19
  * @internal
13
20
  */
14
21
  export class EntityHelper {
15
- static isEntity(data) {
16
- return data != null && typeof data === 'object' && !!data[entitySymbol];
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
+ });
17
38
  }
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
- }
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);
54
43
  }
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
- });
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
+ });
82
60
  }
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
- });
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,
125
110
  });
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, {
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, {
166
119
  get() {
167
- return helper(ref).__data[prop.name];
120
+ return this.__helper?.__data[prop.name];
168
121
  },
169
122
  set(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);
123
+ this.__helper.__data[prop.name] = val;
182
124
  },
183
125
  enumerable: true,
184
126
  configurable: true,
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;
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];
196
142
  }
197
- else {
198
- bidirectionalRelations = prop.targetMeta.bidirectionalRelations;
143
+ }
144
+ for (const key of keys) {
145
+ if (!meta.properties[key]) {
146
+ object[key] = this[key];
199
147
  }
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
- if (old && prop.orphanRemoval) {
227
- helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
228
- }
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]`;
162
+ }
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);
229
183
  }
230
- }
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
+ );
231
192
  }
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;
232
210
  }
233
- static propagateOneToOne(entity, owner, prop, prop2, value, old) {
234
- helper(entity).__pk = helper(entity).getPrimaryKey();
235
- // the inverse side will be changed on the `value` too, so we need to clean-up and schedule orphan removal there too
236
- if (!prop.primary &&
237
- !prop2.mapToPk &&
238
- value?.[prop2.name] != null &&
239
- Reference.unwrapReference(value[prop2.name]) !== entity) {
240
- const other = Reference.unwrapReference(value[prop2.name]);
241
- delete helper(other).__data[prop.name];
242
- if (prop2.orphanRemoval) {
243
- helper(other).__em?.getUnitOfWork().scheduleOrphanRemoval(other);
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
218
  }
245
- }
246
- // Skip setting the inverse side to null if it's a primary key - the entity will be removed via orphan removal
247
- // Setting a primary key to null would corrupt the entity and cause validation errors
248
- if (value == null && prop.orphanRemoval && prop2.primary) {
249
- return;
250
- }
251
- if (value == null) {
252
- entity[prop2.name] = value;
253
- }
254
- else if (prop2.mapToPk) {
255
- entity[prop2.name] = helper(owner).getPrimaryKey();
256
- }
257
- else {
258
- entity[prop2.name] = Reference.wrapReference(owner, prop);
259
- }
260
- if (old?.[prop2.name] != null) {
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
+ if (old[prop2.name] != null) {
261
244
  delete helper(old).__data[prop2.name];
262
245
  old[prop2.name] = null;
246
+ }
263
247
  }
264
- }
265
- static ensurePropagation(entity) {
266
- if (entity.__gettersDefined) {
267
- return;
268
- }
269
- const wrapped = helper(entity);
270
- const meta = wrapped.__meta;
271
- const platform = wrapped.__platform;
272
- const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
273
- const values = [];
274
- if (serializedPrimaryKey) {
275
- const pk = meta.getPrimaryProps()[0];
276
- const val = entity[serializedPrimaryKey.name];
277
- delete entity[serializedPrimaryKey.name];
278
- Object.defineProperty(entity, serializedPrimaryKey.name, {
279
- get() {
280
- return this[pk.name] ? platform.normalizePrimaryKey(this[pk.name]) : null;
281
- },
282
- set(id) {
283
- this[pk.name] = id ? platform.denormalizePrimaryKey(id) : null;
284
- },
285
- configurable: true,
286
- });
287
- if (entity[pk.name] == null && val != null) {
288
- values.push(serializedPrimaryKey.name, val);
289
- }
290
- }
291
- for (const prop of meta.trackingProps) {
292
- if (entity[prop.name] !== undefined) {
293
- values.push(prop.name, entity[prop.name]);
294
- }
295
- delete entity[prop.name];
296
- }
297
- Object.defineProperties(entity, meta.definedProperties);
298
- for (let i = 0; i < values.length; i += 2) {
299
- entity[values[i]] = values[i + 1];
248
+ if (old && old !== value && prop.orphanRemoval) {
249
+ helper(old).__pk ??= helper(old).getPrimaryKey();
250
+ helper(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
300
251
  }
252
+ }
253
+ }
254
+ }
255
+ static propagateOneToOne(entity, owner, prop, prop2, value, old) {
256
+ helper(entity).__pk = helper(entity).getPrimaryKey();
257
+ // the inverse side will be changed on the `value` too, so we need to clean-up and schedule orphan removal there too
258
+ if (
259
+ !prop.primary &&
260
+ !prop2.mapToPk &&
261
+ value?.[prop2.name] != null &&
262
+ Reference.unwrapReference(value[prop2.name]) !== entity
263
+ ) {
264
+ const other = Reference.unwrapReference(value[prop2.name]);
265
+ delete helper(other).__data[prop.name];
266
+ if (prop2.orphanRemoval) {
267
+ helper(other).__em?.getUnitOfWork().scheduleOrphanRemoval(other);
268
+ }
269
+ }
270
+ // Skip setting the inverse side to null if it's a primary key - the entity will be removed via orphan removal
271
+ // Setting a primary key to null would corrupt the entity and cause validation errors
272
+ if (value == null && prop.orphanRemoval && prop2.primary) {
273
+ return;
274
+ }
275
+ if (value == null) {
276
+ entity[prop2.name] = value;
277
+ } else if (prop2.mapToPk) {
278
+ entity[prop2.name] = helper(owner).getPrimaryKey();
279
+ } else {
280
+ entity[prop2.name] = Reference.wrapReference(owner, prop);
281
+ }
282
+ if (old?.[prop2.name] != null) {
283
+ delete helper(old).__data[prop2.name];
284
+ old[prop2.name] = null;
285
+ }
286
+ }
287
+ static ensurePropagation(entity) {
288
+ if (entity.__gettersDefined) {
289
+ return;
290
+ }
291
+ const wrapped = helper(entity);
292
+ const meta = wrapped.__meta;
293
+ const platform = wrapped.__platform;
294
+ const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
295
+ const values = [];
296
+ if (serializedPrimaryKey) {
297
+ const pk = meta.getPrimaryProps()[0];
298
+ const val = entity[serializedPrimaryKey.name];
299
+ delete entity[serializedPrimaryKey.name];
300
+ Object.defineProperty(entity, serializedPrimaryKey.name, {
301
+ get() {
302
+ return this[pk.name] ? platform.normalizePrimaryKey(this[pk.name]) : null;
303
+ },
304
+ set(id) {
305
+ this[pk.name] = id ? platform.denormalizePrimaryKey(id) : null;
306
+ },
307
+ configurable: true,
308
+ });
309
+ if (entity[pk.name] == null && val != null) {
310
+ values.push(serializedPrimaryKey.name, val);
311
+ }
312
+ }
313
+ for (const prop of meta.trackingProps) {
314
+ if (entity[prop.name] !== undefined) {
315
+ values.push(prop.name, entity[prop.name]);
316
+ }
317
+ delete entity[prop.name];
318
+ }
319
+ Object.defineProperties(entity, meta.definedProperties);
320
+ for (let i = 0; i < values.length; i += 2) {
321
+ entity[values[i]] = values[i + 1];
301
322
  }
323
+ }
302
324
  }