@mikro-orm/core 7.0.2 → 7.0.3-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. package/EntityManager.d.ts +583 -883
  2. package/EntityManager.js +1869 -1897
  3. package/MikroORM.d.ts +74 -103
  4. package/MikroORM.js +179 -178
  5. package/cache/CacheAdapter.d.ts +36 -36
  6. package/cache/FileCacheAdapter.d.ts +24 -30
  7. package/cache/FileCacheAdapter.js +78 -80
  8. package/cache/GeneratedCacheAdapter.d.ts +20 -18
  9. package/cache/GeneratedCacheAdapter.js +30 -30
  10. package/cache/MemoryCacheAdapter.d.ts +20 -18
  11. package/cache/MemoryCacheAdapter.js +36 -35
  12. package/cache/NullCacheAdapter.d.ts +16 -16
  13. package/cache/NullCacheAdapter.js +24 -24
  14. package/connections/Connection.d.ts +84 -95
  15. package/connections/Connection.js +168 -165
  16. package/drivers/DatabaseDriver.d.ts +80 -186
  17. package/drivers/DatabaseDriver.js +443 -450
  18. package/drivers/IDatabaseDriver.d.ts +301 -440
  19. package/entity/BaseEntity.d.ts +83 -120
  20. package/entity/BaseEntity.js +43 -43
  21. package/entity/Collection.d.ts +179 -212
  22. package/entity/Collection.js +721 -727
  23. package/entity/EntityAssigner.d.ts +77 -88
  24. package/entity/EntityAssigner.js +230 -231
  25. package/entity/EntityFactory.d.ts +54 -66
  26. package/entity/EntityFactory.js +383 -425
  27. package/entity/EntityHelper.d.ts +22 -34
  28. package/entity/EntityHelper.js +267 -280
  29. package/entity/EntityIdentifier.d.ts +4 -4
  30. package/entity/EntityIdentifier.js +10 -10
  31. package/entity/EntityLoader.d.ts +73 -103
  32. package/entity/EntityLoader.js +723 -753
  33. package/entity/EntityRepository.d.ts +201 -316
  34. package/entity/EntityRepository.js +213 -213
  35. package/entity/PolymorphicRef.d.ts +5 -5
  36. package/entity/PolymorphicRef.js +10 -10
  37. package/entity/Reference.d.ts +82 -126
  38. package/entity/Reference.js +274 -278
  39. package/entity/WrappedEntity.d.ts +72 -115
  40. package/entity/WrappedEntity.js +166 -168
  41. package/entity/defineEntity.d.ts +614 -1280
  42. package/entity/defineEntity.js +511 -520
  43. package/entity/utils.d.ts +3 -13
  44. package/entity/utils.js +73 -71
  45. package/entity/validators.js +43 -43
  46. package/entity/wrap.js +8 -8
  47. package/enums.d.ts +253 -258
  48. package/enums.js +252 -251
  49. package/errors.d.ts +72 -114
  50. package/errors.js +253 -350
  51. package/events/EventManager.d.ts +14 -26
  52. package/events/EventManager.js +77 -79
  53. package/events/EventSubscriber.d.ts +29 -29
  54. package/events/TransactionEventBroadcaster.d.ts +8 -15
  55. package/events/TransactionEventBroadcaster.js +14 -14
  56. package/exceptions.d.ts +40 -23
  57. package/exceptions.js +52 -35
  58. package/hydration/Hydrator.d.ts +17 -42
  59. package/hydration/Hydrator.js +43 -43
  60. package/hydration/ObjectHydrator.d.ts +17 -50
  61. package/hydration/ObjectHydrator.js +416 -479
  62. package/index.d.ts +2 -116
  63. package/index.js +1 -10
  64. package/logging/DefaultLogger.d.ts +32 -34
  65. package/logging/DefaultLogger.js +86 -86
  66. package/logging/Logger.d.ts +41 -41
  67. package/logging/SimpleLogger.d.ts +11 -13
  68. package/logging/SimpleLogger.js +22 -22
  69. package/logging/colors.d.ts +6 -6
  70. package/logging/colors.js +10 -11
  71. package/logging/inspect.js +7 -7
  72. package/metadata/EntitySchema.d.ts +127 -211
  73. package/metadata/EntitySchema.js +398 -397
  74. package/metadata/MetadataDiscovery.d.ts +114 -114
  75. package/metadata/MetadataDiscovery.js +1863 -1947
  76. package/metadata/MetadataProvider.d.ts +21 -24
  77. package/metadata/MetadataProvider.js +84 -82
  78. package/metadata/MetadataStorage.d.ts +32 -38
  79. package/metadata/MetadataStorage.js +118 -118
  80. package/metadata/MetadataValidator.d.ts +39 -39
  81. package/metadata/MetadataValidator.js +338 -381
  82. package/metadata/discover-entities.d.ts +2 -5
  83. package/metadata/discover-entities.js +27 -27
  84. package/metadata/types.d.ts +531 -615
  85. package/naming-strategy/AbstractNamingStrategy.d.ts +39 -54
  86. package/naming-strategy/AbstractNamingStrategy.js +85 -90
  87. package/naming-strategy/EntityCaseNamingStrategy.d.ts +6 -6
  88. package/naming-strategy/EntityCaseNamingStrategy.js +22 -22
  89. package/naming-strategy/MongoNamingStrategy.d.ts +6 -6
  90. package/naming-strategy/MongoNamingStrategy.js +18 -18
  91. package/naming-strategy/NamingStrategy.d.ts +99 -109
  92. package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
  93. package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
  94. package/not-supported.js +4 -7
  95. package/package.json +1 -1
  96. package/platforms/ExceptionConverter.d.ts +1 -1
  97. package/platforms/ExceptionConverter.js +4 -4
  98. package/platforms/Platform.d.ts +299 -308
  99. package/platforms/Platform.js +636 -659
  100. package/serialization/EntitySerializer.d.ts +26 -48
  101. package/serialization/EntitySerializer.js +218 -224
  102. package/serialization/EntityTransformer.d.ts +6 -10
  103. package/serialization/EntityTransformer.js +217 -219
  104. package/serialization/SerializationContext.d.ts +23 -27
  105. package/serialization/SerializationContext.js +105 -105
  106. package/types/ArrayType.d.ts +8 -8
  107. package/types/ArrayType.js +33 -33
  108. package/types/BigIntType.d.ts +10 -17
  109. package/types/BigIntType.js +37 -37
  110. package/types/BlobType.d.ts +3 -3
  111. package/types/BlobType.js +13 -13
  112. package/types/BooleanType.d.ts +4 -4
  113. package/types/BooleanType.js +12 -12
  114. package/types/CharacterType.d.ts +2 -2
  115. package/types/CharacterType.js +6 -6
  116. package/types/DateTimeType.d.ts +5 -5
  117. package/types/DateTimeType.js +15 -15
  118. package/types/DateType.d.ts +5 -5
  119. package/types/DateType.js +15 -15
  120. package/types/DecimalType.d.ts +7 -7
  121. package/types/DecimalType.js +26 -26
  122. package/types/DoubleType.d.ts +3 -3
  123. package/types/DoubleType.js +12 -12
  124. package/types/EnumArrayType.d.ts +5 -5
  125. package/types/EnumArrayType.js +24 -24
  126. package/types/EnumType.d.ts +3 -3
  127. package/types/EnumType.js +11 -11
  128. package/types/FloatType.d.ts +3 -3
  129. package/types/FloatType.js +9 -9
  130. package/types/IntegerType.d.ts +3 -3
  131. package/types/IntegerType.js +9 -9
  132. package/types/IntervalType.d.ts +4 -4
  133. package/types/IntervalType.js +12 -12
  134. package/types/JsonType.d.ts +8 -8
  135. package/types/JsonType.js +32 -32
  136. package/types/MediumIntType.d.ts +1 -1
  137. package/types/MediumIntType.js +3 -3
  138. package/types/SmallIntType.d.ts +3 -3
  139. package/types/SmallIntType.js +9 -9
  140. package/types/StringType.d.ts +4 -4
  141. package/types/StringType.js +12 -12
  142. package/types/TextType.d.ts +3 -3
  143. package/types/TextType.js +9 -9
  144. package/types/TimeType.d.ts +5 -5
  145. package/types/TimeType.js +17 -17
  146. package/types/TinyIntType.d.ts +3 -3
  147. package/types/TinyIntType.js +10 -10
  148. package/types/Type.d.ts +79 -83
  149. package/types/Type.js +82 -82
  150. package/types/Uint8ArrayType.d.ts +4 -4
  151. package/types/Uint8ArrayType.js +21 -21
  152. package/types/UnknownType.d.ts +4 -4
  153. package/types/UnknownType.js +12 -12
  154. package/types/UuidType.d.ts +5 -5
  155. package/types/UuidType.js +19 -19
  156. package/types/index.d.ts +49 -75
  157. package/types/index.js +26 -52
  158. package/typings.d.ts +729 -1211
  159. package/typings.js +231 -244
  160. package/unit-of-work/ChangeSet.d.ts +26 -26
  161. package/unit-of-work/ChangeSet.js +56 -56
  162. package/unit-of-work/ChangeSetComputer.d.ts +12 -12
  163. package/unit-of-work/ChangeSetComputer.js +170 -178
  164. package/unit-of-work/ChangeSetPersister.d.ts +44 -63
  165. package/unit-of-work/ChangeSetPersister.js +421 -442
  166. package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
  167. package/unit-of-work/CommitOrderCalculator.js +88 -89
  168. package/unit-of-work/IdentityMap.d.ts +31 -31
  169. package/unit-of-work/IdentityMap.js +105 -105
  170. package/unit-of-work/UnitOfWork.d.ts +141 -181
  171. package/unit-of-work/UnitOfWork.js +1183 -1200
  172. package/utils/AbstractMigrator.d.ts +91 -111
  173. package/utils/AbstractMigrator.js +275 -275
  174. package/utils/AbstractSchemaGenerator.d.ts +34 -43
  175. package/utils/AbstractSchemaGenerator.js +122 -121
  176. package/utils/AsyncContext.d.ts +3 -3
  177. package/utils/AsyncContext.js +35 -34
  178. package/utils/Configuration.d.ts +808 -852
  179. package/utils/Configuration.js +344 -359
  180. package/utils/Cursor.d.ts +22 -40
  181. package/utils/Cursor.js +127 -135
  182. package/utils/DataloaderUtils.d.ts +43 -58
  183. package/utils/DataloaderUtils.js +198 -203
  184. package/utils/EntityComparator.d.ts +81 -98
  185. package/utils/EntityComparator.js +728 -824
  186. package/utils/NullHighlighter.d.ts +1 -1
  187. package/utils/NullHighlighter.js +3 -3
  188. package/utils/QueryHelper.d.ts +51 -79
  189. package/utils/QueryHelper.js +361 -372
  190. package/utils/RawQueryFragment.d.ts +34 -50
  191. package/utils/RawQueryFragment.js +105 -107
  192. package/utils/RequestContext.d.ts +32 -32
  193. package/utils/RequestContext.js +53 -52
  194. package/utils/TransactionContext.d.ts +16 -16
  195. package/utils/TransactionContext.js +27 -27
  196. package/utils/TransactionManager.d.ts +58 -58
  197. package/utils/TransactionManager.js +197 -199
  198. package/utils/Utils.d.ts +145 -204
  199. package/utils/Utils.js +813 -814
  200. package/utils/clone.js +113 -104
  201. package/utils/env-vars.js +88 -90
  202. package/utils/fs-utils.d.ts +15 -15
  203. package/utils/fs-utils.js +181 -180
  204. package/utils/upsert-utils.d.ts +5 -20
  205. package/utils/upsert-utils.js +116 -114
@@ -1,25 +1,6 @@
1
1
  import type { PopulatePath } from '../enums.js';
2
2
  import type { EntityManager } from '../EntityManager.js';
3
- import type {
4
- Dictionary,
5
- EntityData,
6
- EntityDictionary,
7
- EntityMetadata,
8
- IHydrator,
9
- EntityKey,
10
- PopulateOptions,
11
- Primary,
12
- AutoPath,
13
- Ref,
14
- AddEager,
15
- LoadedReference,
16
- EntityDTO,
17
- Loaded,
18
- SerializeDTO,
19
- FromEntityType,
20
- IsSubset,
21
- MergeSelected,
22
- } from '../typings.js';
3
+ import type { Dictionary, EntityData, EntityDictionary, EntityMetadata, IHydrator, EntityKey, PopulateOptions, Primary, AutoPath, Ref, AddEager, LoadedReference, EntityDTO, Loaded, SerializeDTO, FromEntityType, IsSubset, MergeSelected } from '../typings.js';
23
4
  import { Reference } from './Reference.js';
24
5
  import { type AssignOptions } from './EntityAssigner.js';
25
6
  import type { EntityLoaderOptions } from './EntityLoader.js';
@@ -31,99 +12,75 @@ import type { Platform } from '../platforms/Platform.js';
31
12
  import type { Configuration } from '../utils/Configuration.js';
32
13
  /** @internal Wrapper attached to every managed entity, holding ORM state such as initialization flags, identity map references, and change tracking snapshots. */
33
14
  export declare class WrappedEntity<Entity extends object> {
34
- __initialized: boolean;
35
- __populated?: boolean;
36
- __managed?: boolean;
37
- __onLoadFired?: boolean;
38
- __schema?: string;
39
- __em?: EntityManager;
40
- __loadedProperties: Set<string>;
41
- __data: Dictionary;
42
- __processing: boolean;
43
- __serializationContext: {
44
- root?: SerializationContext<Entity>;
45
- populate?: PopulateOptions<Entity>[];
46
- fields?: Set<string>;
47
- exclude?: readonly string[];
48
- };
49
- /** stores last known primary key, as its current state might be broken due to propagation/orphan removal, but we need to know the PK to be able t remove the entity */
50
- __pk?: Primary<Entity>;
51
- /** holds the reference wrapper instance (if created), so we can maintain the identity on reference wrappers too */
52
- __reference?: Reference<Entity>;
53
- /** holds last entity data snapshot, so we can compute changes when persisting managed entities */
54
- __originalEntityData?: EntityData<Entity>;
55
- /** holds wrapped primary key, so we can compute change set without eager commit */
56
- __identifier?: EntityIdentifier;
57
- private readonly entity;
58
- private readonly hydrator;
59
- private readonly pkGetter?;
60
- private readonly pkSerializer?;
61
- private readonly pkGetterConverted?;
62
- constructor(
63
- entity: Entity,
64
- hydrator: IHydrator,
65
- pkGetter?: (e: Entity) => Primary<Entity>,
66
- pkSerializer?: (e: Entity) => string,
67
- pkGetterConverted?: (e: Entity) => Primary<Entity>,
68
- );
69
- /** Returns whether the entity has been fully loaded from the database. */
70
- isInitialized(): boolean;
71
- /** Returns whether the entity is managed by an EntityManager (tracked in the identity map). */
72
- isManaged(): boolean;
73
- /** Marks the entity as populated or not for serialization purposes. */
74
- populated(populated?: boolean | undefined): void;
75
- /** Sets the serialization context with populate hints, field selections, and exclusions. */
76
- setSerializationContext<Hint extends string = never, Fields extends string = '*', Exclude extends string = never>(
77
- options: LoadHint<Entity, Hint, Fields, Exclude>,
78
- ): void;
79
- /** Returns a Reference wrapper for this entity, creating one if it does not already exist. */
80
- toReference(): Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>>;
81
- /** Converts the entity to a plain object representation, optionally excluding specified fields. */
82
- toObject<Ignored extends EntityKey<Entity> = never>(ignoreFields?: Ignored[]): Omit<EntityDTO<Entity>, Ignored>;
83
- /** Serializes the entity with control over which relations and fields to include or exclude. */
84
- serialize<Hint extends string = never, Exclude extends string = never>(
85
- options?: SerializeOptions<Entity, Hint, Exclude>,
86
- ): SerializeDTO<Entity, Hint, Exclude>;
87
- /** Converts the entity to a plain object, including all properties regardless of serialization rules. */
88
- toPOJO(): EntityDTO<Entity>;
89
- /** Serializes the entity using its `toJSON` method (supports `JSON.stringify`). */
90
- toJSON(...args: any[]): EntityDictionary<Entity>;
91
- /** Assigns the given data to this entity, updating its properties and relations. */
92
- assign<
93
- Naked extends FromEntityType<Entity> = FromEntityType<Entity>,
94
- Convert extends boolean = false,
95
- Data extends EntityData<Naked, Convert> | Partial<EntityDTO<Naked>> =
96
- | EntityData<Naked, Convert>
97
- | Partial<EntityDTO<Naked>>,
98
- >(
99
- data: Data & IsSubset<EntityData<Naked>, Data>,
100
- options?: AssignOptions<Convert>,
101
- ): MergeSelected<Entity, Naked, keyof Data & string>;
102
- /** Initializes (refreshes) the entity by reloading it from the database. Returns null if not found. */
103
- init<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(
104
- options?: FindOneOptions<Entity, Hint, Fields, Excludes>,
105
- ): Promise<Loaded<Entity, Hint, Fields, Excludes> | null>;
106
- /** Loads the specified relations on this entity. */
107
- populate<Hint extends string = never, Fields extends string = never>(
108
- populate: AutoPath<Entity, Hint, PopulatePath.ALL>[] | false,
109
- options?: EntityLoaderOptions<Entity, Fields>,
110
- ): Promise<Loaded<Entity, Hint>>;
111
- /** Returns whether this entity has a primary key value set. */
112
- hasPrimaryKey(): boolean;
113
- /** Returns the primary key value, optionally converting custom types to their database representation. */
114
- getPrimaryKey(convertCustomTypes?: boolean): Primary<Entity> | null;
115
- /** Returns all primary key values as an array. Used internally for composite key handling. */
116
- getPrimaryKeys(convertCustomTypes?: boolean): Primary<Entity>[] | null;
117
- /** Returns the database schema this entity belongs to. */
118
- getSchema(): string | undefined;
119
- /** Sets the database schema for this entity. */
120
- setSchema(schema?: string): void;
121
- /** Sets the primary key value on the entity. */
122
- setPrimaryKey(id: Primary<Entity> | null): void;
123
- /** Returns the primary key serialized as a string suitable for identity map lookups. */
124
- getSerializedPrimaryKey(): string;
125
- get __meta(): EntityMetadata<Entity>;
126
- get __platform(): Platform;
127
- get __config(): Configuration;
128
- get __primaryKeys(): Primary<Entity>[];
15
+ __initialized: boolean;
16
+ __populated?: boolean;
17
+ __managed?: boolean;
18
+ __onLoadFired?: boolean;
19
+ __schema?: string;
20
+ __em?: EntityManager;
21
+ __loadedProperties: Set<string>;
22
+ __data: Dictionary;
23
+ __processing: boolean;
24
+ __serializationContext: {
25
+ root?: SerializationContext<Entity>;
26
+ populate?: PopulateOptions<Entity>[];
27
+ fields?: Set<string>;
28
+ exclude?: readonly string[];
29
+ };
30
+ /** stores last known primary key, as its current state might be broken due to propagation/orphan removal, but we need to know the PK to be able t remove the entity */
31
+ __pk?: Primary<Entity>;
32
+ /** holds the reference wrapper instance (if created), so we can maintain the identity on reference wrappers too */
33
+ __reference?: Reference<Entity>;
34
+ /** holds last entity data snapshot, so we can compute changes when persisting managed entities */
35
+ __originalEntityData?: EntityData<Entity>;
36
+ /** holds wrapped primary key, so we can compute change set without eager commit */
37
+ __identifier?: EntityIdentifier;
38
+ private readonly entity;
39
+ private readonly hydrator;
40
+ private readonly pkGetter?;
41
+ private readonly pkSerializer?;
42
+ private readonly pkGetterConverted?;
43
+ constructor(entity: Entity, hydrator: IHydrator, pkGetter?: (e: Entity) => Primary<Entity>, pkSerializer?: (e: Entity) => string, pkGetterConverted?: (e: Entity) => Primary<Entity>);
44
+ /** Returns whether the entity has been fully loaded from the database. */
45
+ isInitialized(): boolean;
46
+ /** Returns whether the entity is managed by an EntityManager (tracked in the identity map). */
47
+ isManaged(): boolean;
48
+ /** Marks the entity as populated or not for serialization purposes. */
49
+ populated(populated?: boolean | undefined): void;
50
+ /** Sets the serialization context with populate hints, field selections, and exclusions. */
51
+ setSerializationContext<Hint extends string = never, Fields extends string = '*', Exclude extends string = never>(options: LoadHint<Entity, Hint, Fields, Exclude>): void;
52
+ /** Returns a Reference wrapper for this entity, creating one if it does not already exist. */
53
+ toReference(): Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>>;
54
+ /** Converts the entity to a plain object representation, optionally excluding specified fields. */
55
+ toObject<Ignored extends EntityKey<Entity> = never>(ignoreFields?: Ignored[]): Omit<EntityDTO<Entity>, Ignored>;
56
+ /** Serializes the entity with control over which relations and fields to include or exclude. */
57
+ serialize<Hint extends string = never, Exclude extends string = never>(options?: SerializeOptions<Entity, Hint, Exclude>): SerializeDTO<Entity, Hint, Exclude>;
58
+ /** Converts the entity to a plain object, including all properties regardless of serialization rules. */
59
+ toPOJO(): EntityDTO<Entity>;
60
+ /** Serializes the entity using its `toJSON` method (supports `JSON.stringify`). */
61
+ toJSON(...args: any[]): EntityDictionary<Entity>;
62
+ /** Assigns the given data to this entity, updating its properties and relations. */
63
+ assign<Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Convert extends boolean = false, Data extends EntityData<Naked, Convert> | Partial<EntityDTO<Naked>> = EntityData<Naked, Convert> | Partial<EntityDTO<Naked>>>(data: Data & IsSubset<EntityData<Naked>, Data>, options?: AssignOptions<Convert>): MergeSelected<Entity, Naked, keyof Data & string>;
64
+ /** Initializes (refreshes) the entity by reloading it from the database. Returns null if not found. */
65
+ init<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(options?: FindOneOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes> | null>;
66
+ /** Loads the specified relations on this entity. */
67
+ populate<Hint extends string = never, Fields extends string = never>(populate: AutoPath<Entity, Hint, PopulatePath.ALL>[] | false, options?: EntityLoaderOptions<Entity, Fields>): Promise<Loaded<Entity, Hint>>;
68
+ /** Returns whether this entity has a primary key value set. */
69
+ hasPrimaryKey(): boolean;
70
+ /** Returns the primary key value, optionally converting custom types to their database representation. */
71
+ getPrimaryKey(convertCustomTypes?: boolean): Primary<Entity> | null;
72
+ /** Returns all primary key values as an array. Used internally for composite key handling. */
73
+ getPrimaryKeys(convertCustomTypes?: boolean): Primary<Entity>[] | null;
74
+ /** Returns the database schema this entity belongs to. */
75
+ getSchema(): string | undefined;
76
+ /** Sets the database schema for this entity. */
77
+ setSchema(schema?: string): void;
78
+ /** Sets the primary key value on the entity. */
79
+ setPrimaryKey(id: Primary<Entity> | null): void;
80
+ /** Returns the primary key serialized as a string suitable for identity map lookups. */
81
+ getSerializedPrimaryKey(): string;
82
+ get __meta(): EntityMetadata<Entity>;
83
+ get __platform(): Platform;
84
+ get __config(): Configuration;
85
+ get __primaryKeys(): Primary<Entity>[];
129
86
  }
@@ -8,173 +8,171 @@ import { EntitySerializer } from '../serialization/EntitySerializer.js';
8
8
  import { expandDotPaths } from './utils.js';
9
9
  /** @internal Wrapper attached to every managed entity, holding ORM state such as initialization flags, identity map references, and change tracking snapshots. */
10
10
  export class WrappedEntity {
11
- constructor(entity, hydrator, pkGetter, pkSerializer, pkGetterConverted) {
12
- this.entity = entity;
13
- this.hydrator = hydrator;
14
- this.pkGetter = pkGetter;
15
- this.pkSerializer = pkSerializer;
16
- this.pkGetterConverted = pkGetterConverted;
17
- this.__initialized = true;
18
- this.__serializationContext = {};
19
- this.__loadedProperties = new Set();
20
- this.__data = {};
21
- this.__processing = false;
22
- }
23
- /** Returns whether the entity has been fully loaded from the database. */
24
- isInitialized() {
25
- return this.__initialized;
26
- }
27
- /** Returns whether the entity is managed by an EntityManager (tracked in the identity map). */
28
- isManaged() {
29
- return !!this.__managed;
30
- }
31
- /** Marks the entity as populated or not for serialization purposes. */
32
- populated(populated = true) {
33
- this.__populated = populated;
34
- }
35
- /** Sets the serialization context with populate hints, field selections, and exclusions. */
36
- setSerializationContext(options) {
37
- const exclude = options.exclude ?? [];
38
- const context = this.__serializationContext;
39
- const populate = expandDotPaths(this.__meta, options.populate);
40
- context.populate = context.populate ? context.populate.concat(populate) : populate;
41
- context.exclude = context.exclude ? context.exclude.concat(exclude) : exclude;
42
- if (context.fields && options.fields) {
43
- options.fields.forEach(f => context.fields.add(f));
44
- } else if (options.fields) {
45
- context.fields = new Set(options.fields);
46
- } else {
47
- context.fields = new Set(['*']);
48
- }
49
- }
50
- /** Returns a Reference wrapper for this entity, creating one if it does not already exist. */
51
- toReference() {
52
- this.__reference ??= new Reference(this.entity);
53
- return this.__reference;
54
- }
55
- /** Converts the entity to a plain object representation, optionally excluding specified fields. */
56
- toObject(ignoreFields) {
57
- return EntityTransformer.toObject(this.entity, ignoreFields);
58
- }
59
- /** Serializes the entity with control over which relations and fields to include or exclude. */
60
- serialize(options) {
61
- return EntitySerializer.serialize(this.entity, options);
62
- }
63
- /** Converts the entity to a plain object, including all properties regardless of serialization rules. */
64
- toPOJO() {
65
- return EntityTransformer.toObject(this.entity, [], true);
66
- }
67
- /** Serializes the entity using its `toJSON` method (supports `JSON.stringify`). */
68
- toJSON(...args) {
69
- // toJSON methods is added to the prototype during discovery to support automatic serialization via JSON.stringify()
70
- return this.entity.toJSON(...args);
71
- }
72
- /** Assigns the given data to this entity, updating its properties and relations. */
73
- assign(data, options) {
74
- if ('assign' in this.entity) {
75
- return this.entity.assign(data, options);
76
- }
77
- return EntityAssigner.assign(this.entity, data, options);
78
- }
79
- /** Initializes (refreshes) the entity by reloading it from the database. Returns null if not found. */
80
- async init(options) {
81
- if (!this.__em) {
82
- throw ValidationError.entityNotManaged(this.entity);
83
- }
84
- return this.__em.findOne(this.entity.constructor, this.entity, {
85
- ...options,
86
- refresh: true,
87
- schema: this.__schema,
88
- });
89
- }
90
- /** Loads the specified relations on this entity. */
91
- async populate(populate, options = {}) {
92
- if (!this.__em) {
93
- throw ValidationError.entityNotManaged(this.entity);
94
- }
95
- // @ts-ignore hard to type
96
- await this.__em.populate(this.entity, populate, options);
97
- return this.entity;
98
- }
99
- /** Returns whether this entity has a primary key value set. */
100
- hasPrimaryKey() {
101
- const pk = this.getPrimaryKey();
102
- return pk != null;
103
- }
104
- /** Returns the primary key value, optionally converting custom types to their database representation. */
105
- getPrimaryKey(convertCustomTypes = false) {
106
- const prop = this.__meta.getPrimaryProps()[0];
107
- if (!prop) {
108
- return null;
109
- }
110
- if (this.__pk != null && this.__meta.compositePK) {
111
- return Utils.getCompositeKeyValue(
112
- this.__pk,
113
- this.__meta,
114
- convertCustomTypes ? 'convertToDatabaseValue' : false,
115
- this.__platform,
116
- );
117
- }
118
- if (convertCustomTypes && this.__pk != null && prop.customType) {
119
- return prop.customType.convertToDatabaseValue(this.__pk, this.__platform);
120
- }
121
- if (convertCustomTypes) {
122
- return this.__pk ?? this.pkGetterConverted(this.entity);
123
- }
124
- return this.__pk ?? this.pkGetter(this.entity);
125
- }
126
- /** Returns all primary key values as an array. Used internally for composite key handling. */
127
- // TODO: currently used only in `Driver.syncCollection` — candidate for removal
128
- getPrimaryKeys(convertCustomTypes = false) {
129
- const pk = this.getPrimaryKey(convertCustomTypes);
130
- if (pk == null) {
131
- return null;
132
- }
133
- if (this.__meta.compositePK) {
134
- return this.__meta.primaryKeys.reduce((ret, pk) => {
135
- const child = this.entity[pk];
136
- if (Utils.isEntity(child, true)) {
137
- const childPk = helper(child).getPrimaryKeys(convertCustomTypes);
138
- ret.push(...childPk);
139
- } else {
140
- ret.push(child);
11
+ constructor(entity, hydrator, pkGetter, pkSerializer, pkGetterConverted) {
12
+ this.entity = entity;
13
+ this.hydrator = hydrator;
14
+ this.pkGetter = pkGetter;
15
+ this.pkSerializer = pkSerializer;
16
+ this.pkGetterConverted = pkGetterConverted;
17
+ this.__initialized = true;
18
+ this.__serializationContext = {};
19
+ this.__loadedProperties = new Set();
20
+ this.__data = {};
21
+ this.__processing = false;
22
+ }
23
+ /** Returns whether the entity has been fully loaded from the database. */
24
+ isInitialized() {
25
+ return this.__initialized;
26
+ }
27
+ /** Returns whether the entity is managed by an EntityManager (tracked in the identity map). */
28
+ isManaged() {
29
+ return !!this.__managed;
30
+ }
31
+ /** Marks the entity as populated or not for serialization purposes. */
32
+ populated(populated = true) {
33
+ this.__populated = populated;
34
+ }
35
+ /** Sets the serialization context with populate hints, field selections, and exclusions. */
36
+ setSerializationContext(options) {
37
+ const exclude = options.exclude ?? [];
38
+ const context = this.__serializationContext;
39
+ const populate = expandDotPaths(this.__meta, options.populate);
40
+ context.populate = context.populate ? context.populate.concat(populate) : populate;
41
+ context.exclude = context.exclude ? context.exclude.concat(exclude) : exclude;
42
+ if (context.fields && options.fields) {
43
+ options.fields.forEach(f => context.fields.add(f));
44
+ }
45
+ else if (options.fields) {
46
+ context.fields = new Set(options.fields);
47
+ }
48
+ else {
49
+ context.fields = new Set(['*']);
50
+ }
51
+ }
52
+ /** Returns a Reference wrapper for this entity, creating one if it does not already exist. */
53
+ toReference() {
54
+ this.__reference ??= new Reference(this.entity);
55
+ return this.__reference;
56
+ }
57
+ /** Converts the entity to a plain object representation, optionally excluding specified fields. */
58
+ toObject(ignoreFields) {
59
+ return EntityTransformer.toObject(this.entity, ignoreFields);
60
+ }
61
+ /** Serializes the entity with control over which relations and fields to include or exclude. */
62
+ serialize(options) {
63
+ return EntitySerializer.serialize(this.entity, options);
64
+ }
65
+ /** Converts the entity to a plain object, including all properties regardless of serialization rules. */
66
+ toPOJO() {
67
+ return EntityTransformer.toObject(this.entity, [], true);
68
+ }
69
+ /** Serializes the entity using its `toJSON` method (supports `JSON.stringify`). */
70
+ toJSON(...args) {
71
+ // toJSON methods is added to the prototype during discovery to support automatic serialization via JSON.stringify()
72
+ return this.entity.toJSON(...args);
73
+ }
74
+ /** Assigns the given data to this entity, updating its properties and relations. */
75
+ assign(data, options) {
76
+ if ('assign' in this.entity) {
77
+ return this.entity.assign(data, options);
78
+ }
79
+ return EntityAssigner.assign(this.entity, data, options);
80
+ }
81
+ /** Initializes (refreshes) the entity by reloading it from the database. Returns null if not found. */
82
+ async init(options) {
83
+ if (!this.__em) {
84
+ throw ValidationError.entityNotManaged(this.entity);
85
+ }
86
+ return this.__em.findOne(this.entity.constructor, this.entity, {
87
+ ...options,
88
+ refresh: true,
89
+ schema: this.__schema,
90
+ });
91
+ }
92
+ /** Loads the specified relations on this entity. */
93
+ async populate(populate, options = {}) {
94
+ if (!this.__em) {
95
+ throw ValidationError.entityNotManaged(this.entity);
96
+ }
97
+ // @ts-ignore hard to type
98
+ await this.__em.populate(this.entity, populate, options);
99
+ return this.entity;
100
+ }
101
+ /** Returns whether this entity has a primary key value set. */
102
+ hasPrimaryKey() {
103
+ const pk = this.getPrimaryKey();
104
+ return pk != null;
105
+ }
106
+ /** Returns the primary key value, optionally converting custom types to their database representation. */
107
+ getPrimaryKey(convertCustomTypes = false) {
108
+ const prop = this.__meta.getPrimaryProps()[0];
109
+ if (!prop) {
110
+ return null;
111
+ }
112
+ if (this.__pk != null && this.__meta.compositePK) {
113
+ return Utils.getCompositeKeyValue(this.__pk, this.__meta, convertCustomTypes ? 'convertToDatabaseValue' : false, this.__platform);
114
+ }
115
+ if (convertCustomTypes && this.__pk != null && prop.customType) {
116
+ return prop.customType.convertToDatabaseValue(this.__pk, this.__platform);
117
+ }
118
+ if (convertCustomTypes) {
119
+ return this.__pk ?? this.pkGetterConverted(this.entity);
141
120
  }
142
- return ret;
143
- }, []);
144
- }
145
- return [pk];
146
- }
147
- /** Returns the database schema this entity belongs to. */
148
- getSchema() {
149
- return this.__schema;
150
- }
151
- /** Sets the database schema for this entity. */
152
- setSchema(schema) {
153
- this.__schema = schema;
154
- }
155
- /** Sets the primary key value on the entity. */
156
- setPrimaryKey(id) {
157
- this.entity[this.__meta.primaryKeys[0]] = id;
158
- this.__pk = id;
159
- }
160
- /** Returns the primary key serialized as a string suitable for identity map lookups. */
161
- getSerializedPrimaryKey() {
162
- return this.pkSerializer(this.entity);
163
- }
164
- get __meta() {
165
- return this.entity.__meta;
166
- }
167
- get __platform() {
168
- return this.entity.__platform;
169
- }
170
- get __config() {
171
- return this.__em?.config ?? this.entity.__config;
172
- }
173
- get __primaryKeys() {
174
- return Utils.getPrimaryKeyValues(this.entity, this.__meta);
175
- }
176
- /** @ignore */
177
- [Symbol.for('nodejs.util.inspect.custom')]() {
178
- return `[WrappedEntity<${this.__meta.className}>]`;
179
- }
121
+ return this.__pk ?? this.pkGetter(this.entity);
122
+ }
123
+ /** Returns all primary key values as an array. Used internally for composite key handling. */
124
+ // TODO: currently used only in `Driver.syncCollection` — candidate for removal
125
+ getPrimaryKeys(convertCustomTypes = false) {
126
+ const pk = this.getPrimaryKey(convertCustomTypes);
127
+ if (pk == null) {
128
+ return null;
129
+ }
130
+ if (this.__meta.compositePK) {
131
+ return this.__meta.primaryKeys.reduce((ret, pk) => {
132
+ const child = this.entity[pk];
133
+ if (Utils.isEntity(child, true)) {
134
+ const childPk = helper(child).getPrimaryKeys(convertCustomTypes);
135
+ ret.push(...childPk);
136
+ }
137
+ else {
138
+ ret.push(child);
139
+ }
140
+ return ret;
141
+ }, []);
142
+ }
143
+ return [pk];
144
+ }
145
+ /** Returns the database schema this entity belongs to. */
146
+ getSchema() {
147
+ return this.__schema;
148
+ }
149
+ /** Sets the database schema for this entity. */
150
+ setSchema(schema) {
151
+ this.__schema = schema;
152
+ }
153
+ /** Sets the primary key value on the entity. */
154
+ setPrimaryKey(id) {
155
+ this.entity[this.__meta.primaryKeys[0]] = id;
156
+ this.__pk = id;
157
+ }
158
+ /** Returns the primary key serialized as a string suitable for identity map lookups. */
159
+ getSerializedPrimaryKey() {
160
+ return this.pkSerializer(this.entity);
161
+ }
162
+ get __meta() {
163
+ return this.entity.__meta;
164
+ }
165
+ get __platform() {
166
+ return this.entity.__platform;
167
+ }
168
+ get __config() {
169
+ return this.__em?.config ?? this.entity.__config;
170
+ }
171
+ get __primaryKeys() {
172
+ return Utils.getPrimaryKeyValues(this.entity, this.__meta);
173
+ }
174
+ /** @ignore */
175
+ [Symbol.for('nodejs.util.inspect.custom')]() {
176
+ return `[WrappedEntity<${this.__meta.className}>]`;
177
+ }
180
178
  }