@mikro-orm/core 7.0.2-dev.8 → 7.0.2

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 (207) hide show
  1. package/EntityManager.d.ts +883 -579
  2. package/EntityManager.js +1897 -1865
  3. package/MikroORM.d.ts +103 -72
  4. package/MikroORM.js +178 -177
  5. package/README.md +128 -294
  6. package/cache/CacheAdapter.d.ts +38 -36
  7. package/cache/FileCacheAdapter.d.ts +30 -24
  8. package/cache/FileCacheAdapter.js +80 -78
  9. package/cache/GeneratedCacheAdapter.d.ts +19 -20
  10. package/cache/GeneratedCacheAdapter.js +31 -30
  11. package/cache/MemoryCacheAdapter.d.ts +19 -20
  12. package/cache/MemoryCacheAdapter.js +36 -36
  13. package/cache/NullCacheAdapter.d.ts +17 -16
  14. package/cache/NullCacheAdapter.js +25 -24
  15. package/connections/Connection.d.ts +99 -75
  16. package/connections/Connection.js +166 -160
  17. package/drivers/DatabaseDriver.d.ts +187 -69
  18. package/drivers/DatabaseDriver.js +451 -432
  19. package/drivers/IDatabaseDriver.d.ts +464 -281
  20. package/drivers/IDatabaseDriver.js +1 -0
  21. package/entity/BaseEntity.d.ts +121 -73
  22. package/entity/BaseEntity.js +44 -33
  23. package/entity/Collection.d.ts +216 -157
  24. package/entity/Collection.js +728 -707
  25. package/entity/EntityAssigner.d.ts +90 -76
  26. package/entity/EntityAssigner.js +232 -229
  27. package/entity/EntityFactory.d.ts +68 -40
  28. package/entity/EntityFactory.js +427 -366
  29. package/entity/EntityHelper.d.ts +34 -22
  30. package/entity/EntityHelper.js +280 -267
  31. package/entity/EntityIdentifier.d.ts +4 -4
  32. package/entity/EntityIdentifier.js +10 -10
  33. package/entity/EntityLoader.d.ts +105 -56
  34. package/entity/EntityLoader.js +754 -722
  35. package/entity/EntityRepository.d.ts +317 -200
  36. package/entity/EntityRepository.js +214 -212
  37. package/entity/PolymorphicRef.d.ts +5 -5
  38. package/entity/PolymorphicRef.js +10 -10
  39. package/entity/Reference.d.ts +130 -66
  40. package/entity/Reference.js +280 -260
  41. package/entity/WrappedEntity.d.ts +116 -53
  42. package/entity/WrappedEntity.js +169 -147
  43. package/entity/defineEntity.d.ts +1290 -614
  44. package/entity/defineEntity.js +521 -511
  45. package/entity/utils.d.ts +13 -3
  46. package/entity/utils.js +71 -73
  47. package/entity/validators.js +43 -43
  48. package/entity/wrap.js +8 -8
  49. package/enums.d.ts +275 -138
  50. package/enums.js +268 -137
  51. package/errors.d.ts +120 -72
  52. package/errors.js +356 -253
  53. package/events/EventManager.d.ts +27 -10
  54. package/events/EventManager.js +80 -73
  55. package/events/EventSubscriber.d.ts +33 -29
  56. package/events/TransactionEventBroadcaster.d.ts +16 -7
  57. package/events/TransactionEventBroadcaster.js +15 -13
  58. package/exceptions.d.ts +23 -40
  59. package/exceptions.js +35 -52
  60. package/hydration/Hydrator.d.ts +43 -16
  61. package/hydration/Hydrator.js +44 -42
  62. package/hydration/ObjectHydrator.d.ts +51 -17
  63. package/hydration/ObjectHydrator.js +480 -416
  64. package/index.d.ts +116 -2
  65. package/index.js +10 -1
  66. package/logging/DefaultLogger.d.ts +35 -30
  67. package/logging/DefaultLogger.js +87 -84
  68. package/logging/Logger.d.ts +45 -40
  69. package/logging/SimpleLogger.d.ts +13 -11
  70. package/logging/SimpleLogger.js +22 -22
  71. package/logging/colors.d.ts +6 -6
  72. package/logging/colors.js +11 -10
  73. package/logging/inspect.js +7 -7
  74. package/metadata/EntitySchema.d.ts +214 -108
  75. package/metadata/EntitySchema.js +398 -379
  76. package/metadata/MetadataDiscovery.d.ts +115 -111
  77. package/metadata/MetadataDiscovery.js +1948 -1857
  78. package/metadata/MetadataProvider.d.ts +25 -14
  79. package/metadata/MetadataProvider.js +83 -77
  80. package/metadata/MetadataStorage.d.ts +39 -19
  81. package/metadata/MetadataStorage.js +119 -106
  82. package/metadata/MetadataValidator.d.ts +39 -39
  83. package/metadata/MetadataValidator.js +381 -338
  84. package/metadata/discover-entities.d.ts +5 -2
  85. package/metadata/discover-entities.js +27 -27
  86. package/metadata/types.d.ts +615 -531
  87. package/naming-strategy/AbstractNamingStrategy.d.ts +55 -39
  88. package/naming-strategy/AbstractNamingStrategy.js +91 -85
  89. package/naming-strategy/EntityCaseNamingStrategy.d.ts +6 -6
  90. package/naming-strategy/EntityCaseNamingStrategy.js +22 -22
  91. package/naming-strategy/MongoNamingStrategy.d.ts +7 -6
  92. package/naming-strategy/MongoNamingStrategy.js +19 -18
  93. package/naming-strategy/NamingStrategy.d.ts +109 -99
  94. package/naming-strategy/UnderscoreNamingStrategy.d.ts +8 -7
  95. package/naming-strategy/UnderscoreNamingStrategy.js +22 -21
  96. package/not-supported.js +7 -4
  97. package/package.json +1 -1
  98. package/platforms/ExceptionConverter.d.ts +2 -1
  99. package/platforms/ExceptionConverter.js +5 -4
  100. package/platforms/Platform.d.ts +310 -236
  101. package/platforms/Platform.js +661 -573
  102. package/serialization/EntitySerializer.d.ts +49 -25
  103. package/serialization/EntitySerializer.js +224 -216
  104. package/serialization/EntityTransformer.d.ts +11 -5
  105. package/serialization/EntityTransformer.js +220 -216
  106. package/serialization/SerializationContext.d.ts +27 -18
  107. package/serialization/SerializationContext.js +105 -100
  108. package/types/ArrayType.d.ts +9 -8
  109. package/types/ArrayType.js +34 -33
  110. package/types/BigIntType.d.ts +17 -10
  111. package/types/BigIntType.js +37 -37
  112. package/types/BlobType.d.ts +4 -3
  113. package/types/BlobType.js +14 -13
  114. package/types/BooleanType.d.ts +5 -4
  115. package/types/BooleanType.js +13 -12
  116. package/types/CharacterType.d.ts +3 -2
  117. package/types/CharacterType.js +7 -6
  118. package/types/DateTimeType.d.ts +6 -5
  119. package/types/DateTimeType.js +16 -15
  120. package/types/DateType.d.ts +6 -5
  121. package/types/DateType.js +16 -15
  122. package/types/DecimalType.d.ts +7 -7
  123. package/types/DecimalType.js +26 -26
  124. package/types/DoubleType.d.ts +3 -3
  125. package/types/DoubleType.js +12 -12
  126. package/types/EnumArrayType.d.ts +6 -5
  127. package/types/EnumArrayType.js +25 -24
  128. package/types/EnumType.d.ts +4 -3
  129. package/types/EnumType.js +12 -11
  130. package/types/FloatType.d.ts +4 -3
  131. package/types/FloatType.js +10 -9
  132. package/types/IntegerType.d.ts +4 -3
  133. package/types/IntegerType.js +10 -9
  134. package/types/IntervalType.d.ts +5 -4
  135. package/types/IntervalType.js +13 -12
  136. package/types/JsonType.d.ts +9 -8
  137. package/types/JsonType.js +33 -32
  138. package/types/MediumIntType.d.ts +2 -1
  139. package/types/MediumIntType.js +4 -3
  140. package/types/SmallIntType.d.ts +4 -3
  141. package/types/SmallIntType.js +10 -9
  142. package/types/StringType.d.ts +5 -4
  143. package/types/StringType.js +13 -12
  144. package/types/TextType.d.ts +4 -3
  145. package/types/TextType.js +10 -9
  146. package/types/TimeType.d.ts +6 -5
  147. package/types/TimeType.js +18 -17
  148. package/types/TinyIntType.d.ts +4 -3
  149. package/types/TinyIntType.js +11 -10
  150. package/types/Type.d.ts +88 -73
  151. package/types/Type.js +85 -74
  152. package/types/Uint8ArrayType.d.ts +5 -4
  153. package/types/Uint8ArrayType.js +22 -21
  154. package/types/UnknownType.d.ts +5 -4
  155. package/types/UnknownType.js +13 -12
  156. package/types/UuidType.d.ts +6 -5
  157. package/types/UuidType.js +20 -19
  158. package/types/index.d.ts +77 -49
  159. package/types/index.js +64 -26
  160. package/typings.d.ts +1388 -729
  161. package/typings.js +255 -231
  162. package/unit-of-work/ChangeSet.d.ts +28 -24
  163. package/unit-of-work/ChangeSet.js +58 -54
  164. package/unit-of-work/ChangeSetComputer.d.ts +13 -11
  165. package/unit-of-work/ChangeSetComputer.js +180 -159
  166. package/unit-of-work/ChangeSetPersister.d.ts +64 -41
  167. package/unit-of-work/ChangeSetPersister.js +443 -418
  168. package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
  169. package/unit-of-work/CommitOrderCalculator.js +89 -88
  170. package/unit-of-work/IdentityMap.d.ts +32 -25
  171. package/unit-of-work/IdentityMap.js +106 -99
  172. package/unit-of-work/UnitOfWork.d.ts +182 -127
  173. package/unit-of-work/UnitOfWork.js +1201 -1169
  174. package/utils/AbstractMigrator.d.ts +111 -91
  175. package/utils/AbstractMigrator.js +275 -275
  176. package/utils/AbstractSchemaGenerator.d.ts +43 -34
  177. package/utils/AbstractSchemaGenerator.js +121 -122
  178. package/utils/AsyncContext.d.ts +3 -3
  179. package/utils/AsyncContext.js +34 -35
  180. package/utils/Configuration.d.ts +853 -801
  181. package/utils/Configuration.js +360 -337
  182. package/utils/Cursor.d.ts +40 -22
  183. package/utils/Cursor.js +135 -127
  184. package/utils/DataloaderUtils.d.ts +58 -43
  185. package/utils/DataloaderUtils.js +203 -198
  186. package/utils/EntityComparator.d.ts +99 -80
  187. package/utils/EntityComparator.js +825 -727
  188. package/utils/NullHighlighter.d.ts +2 -1
  189. package/utils/NullHighlighter.js +4 -3
  190. package/utils/QueryHelper.d.ts +79 -51
  191. package/utils/QueryHelper.js +372 -361
  192. package/utils/RawQueryFragment.d.ts +54 -28
  193. package/utils/RawQueryFragment.js +110 -99
  194. package/utils/RequestContext.d.ts +33 -32
  195. package/utils/RequestContext.js +52 -53
  196. package/utils/TransactionContext.d.ts +17 -16
  197. package/utils/TransactionContext.js +28 -27
  198. package/utils/TransactionManager.d.ts +58 -58
  199. package/utils/TransactionManager.js +199 -197
  200. package/utils/Utils.d.ts +210 -145
  201. package/utils/Utils.js +820 -813
  202. package/utils/clone.js +104 -113
  203. package/utils/env-vars.js +90 -88
  204. package/utils/fs-utils.d.ts +15 -15
  205. package/utils/fs-utils.js +180 -181
  206. package/utils/upsert-utils.d.ts +20 -5
  207. package/utils/upsert-utils.js +114 -116
@@ -1,31 +1,44 @@
1
- import type { ArrayElement, AutoPath, CleanTypeConfig, SerializeDTO, FromEntityType, TypeConfig, UnboxArray } from '../typings.js';
1
+ import type {
2
+ ArrayElement,
3
+ AutoPath,
4
+ CleanTypeConfig,
5
+ SerializeDTO,
6
+ FromEntityType,
7
+ TypeConfig,
8
+ UnboxArray,
9
+ } from '../typings.js';
2
10
  import { type PopulatePath } from '../enums.js';
11
+ /** Converts entity instances to plain DTOs via `serialize()`, with fine-grained control over populate, exclude, and serialization groups. */
3
12
  export declare class EntitySerializer {
4
- static serialize<T extends object, P extends string = never, E extends string = never>(entity: T, options?: SerializeOptions<T, P, E>): SerializeDTO<T, P, E>;
5
- private static propertyName;
6
- private static processProperty;
7
- private static processCustomType;
8
- private static extractChildOptions;
9
- private static processEntity;
10
- private static processCollection;
13
+ /** Serializes an entity to a plain DTO, with fine-grained control over population, exclusion, groups, and custom types. */
14
+ static serialize<T extends object, P extends string = never, E extends string = never>(
15
+ entity: T,
16
+ options?: SerializeOptions<T, P, E>,
17
+ ): SerializeDTO<T, P, E>;
18
+ private static propertyName;
19
+ private static processProperty;
20
+ private static processCustomType;
21
+ private static extractChildOptions;
22
+ private static processEntity;
23
+ private static processCollection;
11
24
  }
12
25
  export interface SerializeOptions<T, P extends string = never, E extends string = never> {
13
- /** Specify which relation should be serialized as populated and which as a FK. */
14
- populate?: readonly AutoPath<T, P, `${PopulatePath.ALL}`>[];
15
- /** Specify which properties should be omitted. */
16
- exclude?: readonly AutoPath<T, E>[];
17
- /** Enforce unpopulated references to be returned as objects, e.g. `{ author: { id: 1 } }` instead of `{ author: 1 }`. */
18
- forceObject?: boolean;
19
- /** Ignore custom property serializers. */
20
- ignoreSerializers?: boolean;
21
- /** Include properties marked as `hidden`. */
22
- includeHidden?: boolean;
23
- /** Skip properties with `null` value. */
24
- skipNull?: boolean;
25
- /** Only include properties for a specific group. If a property does not specify any group, it will be included, otherwise only properties with a matching group are included. */
26
- groups?: string[];
27
- /** Convert custom types to their database representation. By default, the `Type.toJSON` method is invoked instead. */
28
- convertCustomTypes?: boolean;
26
+ /** Specify which relation should be serialized as populated and which as a FK. */
27
+ populate?: readonly AutoPath<T, P, `${PopulatePath.ALL}`>[];
28
+ /** Specify which properties should be omitted. */
29
+ exclude?: readonly AutoPath<T, E>[];
30
+ /** Enforce unpopulated references to be returned as objects, e.g. `{ author: { id: 1 } }` instead of `{ author: 1 }`. */
31
+ forceObject?: boolean;
32
+ /** Ignore custom property serializers. */
33
+ ignoreSerializers?: boolean;
34
+ /** Include properties marked as `hidden`. */
35
+ includeHidden?: boolean;
36
+ /** Skip properties with `null` value. */
37
+ skipNull?: boolean;
38
+ /** Only include properties for a specific group. If a property does not specify any group, it will be included, otherwise only properties with a matching group are included. */
39
+ groups?: string[];
40
+ /** Convert custom types to their database representation. By default, the `Type.toJSON` method is invoked instead. */
41
+ convertCustomTypes?: boolean;
29
42
  }
30
43
  /**
31
44
  * Converts entity instance to POJO, converting the `Collection`s to arrays and unwrapping the `Reference` wrapper, while respecting the serialization options.
@@ -39,4 +52,15 @@ export interface SerializeOptions<T, P extends string = never, E extends string
39
52
  * const dto2 = wrap(user).serialize({ exclude: ['id', 'email'], forceObject: true });
40
53
  * ```
41
54
  */
42
- export declare function serialize<Entity extends object, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Populate extends string = never, Exclude extends string = never, Config extends TypeConfig = never>(entity: Entity, options?: Config & SerializeOptions<UnboxArray<Entity>, Populate, Exclude>): Naked extends object[] ? SerializeDTO<ArrayElement<Naked>, Populate, Exclude, CleanTypeConfig<Config>>[] : SerializeDTO<Naked, Populate, Exclude, CleanTypeConfig<Config>>;
55
+ export declare function serialize<
56
+ Entity extends object,
57
+ Naked extends FromEntityType<Entity> = FromEntityType<Entity>,
58
+ Populate extends string = never,
59
+ Exclude extends string = never,
60
+ Config extends TypeConfig = never,
61
+ >(
62
+ entity: Entity,
63
+ options?: Config & SerializeOptions<UnboxArray<Entity>, Populate, Exclude>,
64
+ ): Naked extends object[]
65
+ ? SerializeDTO<ArrayElement<Naked>, Populate, Exclude, CleanTypeConfig<Config>>[]
66
+ : SerializeDTO<Naked, Populate, Exclude, CleanTypeConfig<Config>>;
@@ -5,227 +5,235 @@ import { Reference } from '../entity/Reference.js';
5
5
  import { SerializationContext } from './SerializationContext.js';
6
6
  import { isRaw } from '../utils/RawQueryFragment.js';
7
7
  function isVisible(meta, propName, options) {
8
- const prop = meta.properties[propName];
9
- if (options.groups && prop?.groups) {
10
- return prop.groups.some(g => options.groups.includes(g));
8
+ const prop = meta.properties[propName];
9
+ if (options.groups && prop?.groups) {
10
+ return prop.groups.some(g => options.groups.includes(g));
11
+ }
12
+ if (
13
+ Array.isArray(options.populate) &&
14
+ options.populate?.find(item => item === propName || item.startsWith(propName + '.') || item === '*')
15
+ ) {
16
+ return true;
17
+ }
18
+ if (options.exclude?.find(item => item === propName)) {
19
+ return false;
20
+ }
21
+ const visible = prop && !(prop.hidden && !options.includeHidden);
22
+ const prefixed = prop && !prop.primary && !prop.accessor && propName.startsWith('_'); // ignore prefixed properties, if it's not a PK
23
+ return visible && !prefixed;
24
+ }
25
+ function isPopulated(propName, options) {
26
+ if (
27
+ typeof options.populate !== 'boolean' &&
28
+ options.populate?.find(item => item === propName || item.startsWith(propName + '.') || item === '*')
29
+ ) {
30
+ return true;
31
+ }
32
+ if (typeof options.populate === 'boolean') {
33
+ return options.populate;
34
+ }
35
+ return false;
36
+ }
37
+ /** Converts entity instances to plain DTOs via `serialize()`, with fine-grained control over populate, exclude, and serialization groups. */
38
+ export class EntitySerializer {
39
+ /** Serializes an entity to a plain DTO, with fine-grained control over population, exclusion, groups, and custom types. */
40
+ static serialize(entity, options = {}) {
41
+ const wrapped = helper(entity);
42
+ const meta = wrapped.__meta;
43
+ let contextCreated = false;
44
+ if (!wrapped.__serializationContext.root) {
45
+ const root = new SerializationContext();
46
+ SerializationContext.propagate(
47
+ root,
48
+ entity,
49
+ (meta, prop) => meta.properties[prop]?.kind !== ReferenceKind.SCALAR,
50
+ );
51
+ options.populate = options.populate ? Utils.asArray(options.populate) : options.populate;
52
+ contextCreated = true;
11
53
  }
12
- if (Array.isArray(options.populate) &&
13
- options.populate?.find(item => item === propName || item.startsWith(propName + '.') || item === '*')) {
14
- return true;
54
+ const root = wrapped.__serializationContext.root;
55
+ const ret = {};
56
+ const props = new Set();
57
+ if (meta.serializedPrimaryKey && !meta.compositePK) {
58
+ props.add(meta.serializedPrimaryKey);
59
+ } else {
60
+ meta.primaryKeys.forEach(pk => props.add(pk));
15
61
  }
16
- if (options.exclude?.find(item => item === propName)) {
17
- return false;
62
+ if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
63
+ const entityKeys = new Set(Object.keys(entity));
64
+ for (const prop of meta.props) {
65
+ if (entityKeys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
66
+ props.add(prop.name);
67
+ }
68
+ }
69
+ for (const key of entityKeys) {
70
+ if (!meta.properties[key]) {
71
+ props.add(key);
72
+ }
73
+ }
18
74
  }
19
- const visible = prop && !(prop.hidden && !options.includeHidden);
20
- const prefixed = prop && !prop.primary && !prop.accessor && propName.startsWith('_'); // ignore prefixed properties, if it's not a PK
21
- return visible && !prefixed;
22
- }
23
- function isPopulated(propName, options) {
24
- if (typeof options.populate !== 'boolean' &&
25
- options.populate?.find(item => item === propName || item.startsWith(propName + '.') || item === '*')) {
26
- return true;
75
+ const visited = root.visited.has(entity);
76
+ if (!visited) {
77
+ root.visited.add(entity);
27
78
  }
28
- if (typeof options.populate === 'boolean') {
29
- return options.populate;
79
+ for (const prop of props) {
80
+ if (!isVisible(meta, prop, options)) {
81
+ continue;
82
+ }
83
+ const cycle = root.visit(meta.class, prop);
84
+ if (cycle && visited) {
85
+ continue;
86
+ }
87
+ const val = this.processProperty(prop, entity, options);
88
+ if (!cycle) {
89
+ root.leave(meta.class, prop);
90
+ }
91
+ if (options.skipNull && Utils.isPlainObject(val)) {
92
+ Utils.dropUndefinedProperties(val, null);
93
+ }
94
+ if (isRaw(val)) {
95
+ throw new Error(`Trying to serialize raw SQL fragment: '${val.sql}'`);
96
+ }
97
+ const visible = typeof val !== 'undefined' && !(val === null && options.skipNull);
98
+ if (visible) {
99
+ ret[this.propertyName(meta, prop)] = val;
100
+ }
30
101
  }
31
- return false;
32
- }
33
- export class EntitySerializer {
34
- static serialize(entity, options = {}) {
35
- const wrapped = helper(entity);
36
- const meta = wrapped.__meta;
37
- let contextCreated = false;
38
- if (!wrapped.__serializationContext.root) {
39
- const root = new SerializationContext();
40
- SerializationContext.propagate(root, entity, (meta, prop) => meta.properties[prop]?.kind !== ReferenceKind.SCALAR);
41
- options.populate = (options.populate ? Utils.asArray(options.populate) : options.populate);
42
- contextCreated = true;
43
- }
44
- const root = wrapped.__serializationContext.root;
45
- const ret = {};
46
- const props = new Set();
47
- if (meta.serializedPrimaryKey && !meta.compositePK) {
48
- props.add(meta.serializedPrimaryKey);
49
- }
50
- else {
51
- meta.primaryKeys.forEach(pk => props.add(pk));
52
- }
53
- if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
54
- const entityKeys = new Set(Object.keys(entity));
55
- for (const prop of meta.props) {
56
- if (entityKeys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
57
- props.add(prop.name);
58
- }
59
- }
60
- for (const key of entityKeys) {
61
- if (!meta.properties[key]) {
62
- props.add(key);
63
- }
64
- }
65
- }
66
- const visited = root.visited.has(entity);
67
- if (!visited) {
68
- root.visited.add(entity);
69
- }
70
- for (const prop of props) {
71
- if (!isVisible(meta, prop, options)) {
72
- continue;
73
- }
74
- const cycle = root.visit(meta.class, prop);
75
- if (cycle && visited) {
76
- continue;
77
- }
78
- const val = this.processProperty(prop, entity, options);
79
- if (!cycle) {
80
- root.leave(meta.class, prop);
81
- }
82
- if (options.skipNull && Utils.isPlainObject(val)) {
83
- Utils.dropUndefinedProperties(val, null);
84
- }
85
- if (isRaw(val)) {
86
- throw new Error(`Trying to serialize raw SQL fragment: '${val.sql}'`);
87
- }
88
- const visible = typeof val !== 'undefined' && !(val === null && options.skipNull);
89
- if (visible) {
90
- ret[this.propertyName(meta, prop)] = val;
91
- }
92
- }
93
- if (contextCreated) {
94
- root.close();
95
- }
96
- if (!wrapped.isInitialized()) {
97
- return ret;
98
- }
99
- for (const prop of meta.getterProps) {
100
- // decorated get methods
101
- if (prop.getterName != null) {
102
- const visible = entity[prop.getterName] instanceof Function && isVisible(meta, prop.name, options);
103
- if (visible) {
104
- ret[this.propertyName(meta, prop.name)] = this.processProperty(prop.getterName, entity, options);
105
- }
106
- }
107
- else {
108
- // decorated getters
109
- const visible = typeof entity[prop.name] !== 'undefined' && isVisible(meta, prop.name, options);
110
- if (visible) {
111
- ret[this.propertyName(meta, prop.name)] = this.processProperty(prop.name, entity, options);
112
- }
113
- }
114
- }
115
- return ret;
102
+ if (contextCreated) {
103
+ root.close();
116
104
  }
117
- static propertyName(meta, prop) {
118
- /* v8 ignore next */
119
- if (meta.properties[prop]?.serializedName) {
120
- return meta.properties[prop].serializedName;
121
- }
122
- if (meta.properties[prop]?.primary && meta.serializedPrimaryKey) {
123
- return meta.serializedPrimaryKey;
124
- }
125
- return prop;
126
- }
127
- static processProperty(prop, entity, options) {
128
- const parts = prop.split('.');
129
- prop = parts[0];
130
- const wrapped = helper(entity);
131
- const property = wrapped.__meta.properties[prop] ?? { name: prop };
132
- const serializer = property?.serializer;
133
- const value = entity[prop];
134
- // getter method
135
- if (entity[prop] instanceof Function) {
136
- const returnValue = entity[prop]();
137
- if (!options.ignoreSerializers && serializer) {
138
- return serializer(returnValue, this.extractChildOptions(options, prop));
139
- }
140
- return returnValue;
141
- }
142
- /* v8 ignore next */
143
- if (!options.ignoreSerializers && serializer) {
144
- return serializer(value);
145
- }
146
- if (Utils.isCollection(value)) {
147
- return this.processCollection(property, entity, options);
148
- }
149
- if (Utils.isEntity(value, true)) {
150
- return this.processEntity(property, entity, wrapped.__platform, options);
151
- }
152
- if (Utils.isScalarReference(value)) {
153
- return value.unwrap();
154
- }
155
- /* v8 ignore next */
156
- if (property?.kind === ReferenceKind.EMBEDDED) {
157
- if (Array.isArray(value)) {
158
- return value.map(item => helper(item).toJSON());
159
- }
160
- if (Utils.isObject(value)) {
161
- return helper(value).toJSON();
162
- }
163
- }
164
- if (property.customType) {
165
- return this.processCustomType(value, property, wrapped.__platform, options.convertCustomTypes);
166
- }
167
- return wrapped.__platform.normalizePrimaryKey(value);
105
+ if (!wrapped.isInitialized()) {
106
+ return ret;
168
107
  }
169
- static processCustomType(value, prop, platform, convertCustomTypes) {
170
- if (!prop.customType) {
171
- return value;
172
- }
173
- if (convertCustomTypes) {
174
- return prop.customType.convertToDatabaseValue(value, platform, { mode: 'serialization' });
175
- }
176
- return prop.customType.toJSON(value, platform);
177
- }
178
- static extractChildOptions(options, prop) {
179
- return {
180
- ...options,
181
- populate: Array.isArray(options.populate)
182
- ? Utils.extractChildElements(options.populate, prop, '*')
183
- : options.populate,
184
- exclude: Array.isArray(options.exclude) ? Utils.extractChildElements(options.exclude, prop) : options.exclude,
185
- };
186
- }
187
- static processEntity(prop, entity, platform, options) {
188
- const child = Reference.unwrapReference(entity[prop.name]);
189
- const wrapped = helper(child);
190
- const populated = isPopulated(prop.name, options) && wrapped.isInitialized();
191
- const expand = populated || !wrapped.__managed;
192
- const meta = wrapped.__meta;
193
- const childOptions = this.extractChildOptions(options, prop.name);
194
- const visible = meta.primaryKeys.filter(prop => isVisible(meta, prop, childOptions));
195
- if (expand) {
196
- return this.serialize(child, childOptions);
197
- }
198
- const pk = this.processCustomType(wrapped.getPrimaryKey(), prop, wrapped.__platform, options.convertCustomTypes);
199
- if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
200
- return Utils.primaryKeyToObject(meta, pk, visible);
201
- }
202
- if (Utils.isPlainObject(pk)) {
203
- const pruned = Utils.primaryKeyToObject(meta, pk, visible);
204
- if (visible.length === 1) {
205
- return platform.normalizePrimaryKey(pruned[visible[0]]);
206
- }
207
- return pruned;
208
- }
209
- return platform.normalizePrimaryKey(pk);
108
+ for (const prop of meta.getterProps) {
109
+ // decorated get methods
110
+ if (prop.getterName != null) {
111
+ const visible = entity[prop.getterName] instanceof Function && isVisible(meta, prop.name, options);
112
+ if (visible) {
113
+ ret[this.propertyName(meta, prop.name)] = this.processProperty(prop.getterName, entity, options);
114
+ }
115
+ } else {
116
+ // decorated getters
117
+ const visible = typeof entity[prop.name] !== 'undefined' && isVisible(meta, prop.name, options);
118
+ if (visible) {
119
+ ret[this.propertyName(meta, prop.name)] = this.processProperty(prop.name, entity, options);
120
+ }
121
+ }
210
122
  }
211
- static processCollection(prop, entity, options) {
212
- const col = entity[prop.name];
213
- if (!col.isInitialized()) {
214
- return undefined;
215
- }
216
- return col.getItems(false).map(item => {
217
- const populated = isPopulated(prop.name, options);
218
- const wrapped = helper(item);
219
- if (populated || !wrapped.__managed) {
220
- return this.serialize(item, this.extractChildOptions(options, prop.name));
221
- }
222
- const pk = this.processCustomType(wrapped.getPrimaryKey(), prop, wrapped.__platform, options.convertCustomTypes);
223
- if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
224
- return Utils.primaryKeyToObject(wrapped.__meta, pk);
225
- }
226
- return pk;
227
- });
123
+ return ret;
124
+ }
125
+ static propertyName(meta, prop) {
126
+ /* v8 ignore next */
127
+ if (meta.properties[prop]?.serializedName) {
128
+ return meta.properties[prop].serializedName;
129
+ }
130
+ if (meta.properties[prop]?.primary && meta.serializedPrimaryKey) {
131
+ return meta.serializedPrimaryKey;
132
+ }
133
+ return prop;
134
+ }
135
+ static processProperty(prop, entity, options) {
136
+ const parts = prop.split('.');
137
+ prop = parts[0];
138
+ const wrapped = helper(entity);
139
+ const property = wrapped.__meta.properties[prop] ?? { name: prop };
140
+ const serializer = property?.serializer;
141
+ const value = entity[prop];
142
+ // getter method
143
+ if (entity[prop] instanceof Function) {
144
+ const returnValue = entity[prop]();
145
+ if (!options.ignoreSerializers && serializer) {
146
+ return serializer(returnValue, this.extractChildOptions(options, prop));
147
+ }
148
+ return returnValue;
149
+ }
150
+ /* v8 ignore next */
151
+ if (!options.ignoreSerializers && serializer) {
152
+ return serializer(value);
228
153
  }
154
+ if (Utils.isCollection(value)) {
155
+ return this.processCollection(property, entity, options);
156
+ }
157
+ if (Utils.isEntity(value, true)) {
158
+ return this.processEntity(property, entity, wrapped.__platform, options);
159
+ }
160
+ if (Utils.isScalarReference(value)) {
161
+ return value.unwrap();
162
+ }
163
+ /* v8 ignore next */
164
+ if (property?.kind === ReferenceKind.EMBEDDED) {
165
+ if (Array.isArray(value)) {
166
+ return value.map(item => helper(item).toJSON());
167
+ }
168
+ if (Utils.isObject(value)) {
169
+ return helper(value).toJSON();
170
+ }
171
+ }
172
+ if (property.customType) {
173
+ return this.processCustomType(value, property, wrapped.__platform, options.convertCustomTypes);
174
+ }
175
+ return wrapped.__platform.normalizePrimaryKey(value);
176
+ }
177
+ static processCustomType(value, prop, platform, convertCustomTypes) {
178
+ if (!prop.customType) {
179
+ return value;
180
+ }
181
+ if (convertCustomTypes) {
182
+ return prop.customType.convertToDatabaseValue(value, platform, { mode: 'serialization' });
183
+ }
184
+ return prop.customType.toJSON(value, platform);
185
+ }
186
+ static extractChildOptions(options, prop) {
187
+ return {
188
+ ...options,
189
+ populate: Array.isArray(options.populate)
190
+ ? Utils.extractChildElements(options.populate, prop, '*')
191
+ : options.populate,
192
+ exclude: Array.isArray(options.exclude) ? Utils.extractChildElements(options.exclude, prop) : options.exclude,
193
+ };
194
+ }
195
+ static processEntity(prop, entity, platform, options) {
196
+ const child = Reference.unwrapReference(entity[prop.name]);
197
+ const wrapped = helper(child);
198
+ const populated = isPopulated(prop.name, options) && wrapped.isInitialized();
199
+ const expand = populated || !wrapped.__managed;
200
+ const meta = wrapped.__meta;
201
+ const childOptions = this.extractChildOptions(options, prop.name);
202
+ const visible = meta.primaryKeys.filter(prop => isVisible(meta, prop, childOptions));
203
+ if (expand) {
204
+ return this.serialize(child, childOptions);
205
+ }
206
+ const pk = this.processCustomType(wrapped.getPrimaryKey(), prop, wrapped.__platform, options.convertCustomTypes);
207
+ if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
208
+ return Utils.primaryKeyToObject(meta, pk, visible);
209
+ }
210
+ if (Utils.isPlainObject(pk)) {
211
+ const pruned = Utils.primaryKeyToObject(meta, pk, visible);
212
+ if (visible.length === 1) {
213
+ return platform.normalizePrimaryKey(pruned[visible[0]]);
214
+ }
215
+ return pruned;
216
+ }
217
+ return platform.normalizePrimaryKey(pk);
218
+ }
219
+ static processCollection(prop, entity, options) {
220
+ const col = entity[prop.name];
221
+ if (!col.isInitialized()) {
222
+ return undefined;
223
+ }
224
+ return col.getItems(false).map(item => {
225
+ const populated = isPopulated(prop.name, options);
226
+ const wrapped = helper(item);
227
+ if (populated || !wrapped.__managed) {
228
+ return this.serialize(item, this.extractChildOptions(options, prop.name));
229
+ }
230
+ const pk = this.processCustomType(wrapped.getPrimaryKey(), prop, wrapped.__platform, options.convertCustomTypes);
231
+ if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
232
+ return Utils.primaryKeyToObject(wrapped.__meta, pk);
233
+ }
234
+ return pk;
235
+ });
236
+ }
229
237
  }
230
238
  /**
231
239
  * Converts entity instance to POJO, converting the `Collection`s to arrays and unwrapping the `Reference` wrapper, while respecting the serialization options.
@@ -240,8 +248,8 @@ export class EntitySerializer {
240
248
  * ```
241
249
  */
242
250
  export function serialize(entities, options) {
243
- if (Array.isArray(entities)) {
244
- return entities.map(e => EntitySerializer.serialize(e, options));
245
- }
246
- return EntitySerializer.serialize(entities, options);
251
+ if (Array.isArray(entities)) {
252
+ return entities.map(e => EntitySerializer.serialize(e, options));
253
+ }
254
+ return EntitySerializer.serialize(entities, options);
247
255
  }
@@ -1,8 +1,14 @@
1
1
  import type { EntityDTO, EntityKey } from '../typings.js';
2
+ /** Converts entity instances to plain objects via `toObject()`, respecting populate hints, hidden fields, and serialization context. */
2
3
  export declare class EntityTransformer {
3
- static toObject<Entity extends object, Ignored extends EntityKey<Entity> = never>(entity: Entity, ignoreFields?: Ignored[], raw?: boolean): Omit<EntityDTO<Entity>, Ignored>;
4
- private static propertyName;
5
- private static processProperty;
6
- private static processEntity;
7
- private static processCollection;
4
+ /** Converts an entity to a plain object, respecting populate hints, hidden fields, and custom serializers. */
5
+ static toObject<Entity extends object, Ignored extends EntityKey<Entity> = never>(
6
+ entity: Entity,
7
+ ignoreFields?: Ignored[],
8
+ raw?: boolean,
9
+ ): Omit<EntityDTO<Entity>, Ignored>;
10
+ private static propertyName;
11
+ private static processProperty;
12
+ private static processEntity;
13
+ private static processCollection;
8
14
  }