@mikro-orm/core 7.0.0-dev.33 → 7.0.0-dev.331

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 (218) hide show
  1. package/EntityManager.d.ts +70 -75
  2. package/EntityManager.js +487 -402
  3. package/MikroORM.d.ts +45 -38
  4. package/MikroORM.js +123 -156
  5. package/README.md +7 -4
  6. package/cache/FileCacheAdapter.d.ts +2 -7
  7. package/cache/FileCacheAdapter.js +35 -30
  8. package/cache/GeneratedCacheAdapter.d.ts +1 -2
  9. package/cache/GeneratedCacheAdapter.js +6 -8
  10. package/cache/MemoryCacheAdapter.d.ts +1 -2
  11. package/cache/MemoryCacheAdapter.js +8 -8
  12. package/cache/index.d.ts +1 -2
  13. package/cache/index.js +0 -2
  14. package/connections/Connection.d.ts +12 -5
  15. package/connections/Connection.js +37 -15
  16. package/drivers/DatabaseDriver.d.ts +25 -18
  17. package/drivers/DatabaseDriver.js +144 -45
  18. package/drivers/IDatabaseDriver.d.ts +118 -23
  19. package/entity/BaseEntity.d.ts +63 -4
  20. package/entity/BaseEntity.js +0 -3
  21. package/entity/Collection.d.ts +95 -31
  22. package/entity/Collection.js +487 -139
  23. package/entity/EntityAssigner.js +37 -25
  24. package/entity/EntityFactory.d.ts +8 -9
  25. package/entity/EntityFactory.js +152 -100
  26. package/entity/EntityHelper.d.ts +2 -2
  27. package/entity/EntityHelper.js +69 -27
  28. package/entity/EntityLoader.d.ts +12 -13
  29. package/entity/EntityLoader.js +286 -125
  30. package/entity/EntityRepository.d.ts +28 -8
  31. package/entity/EntityRepository.js +8 -2
  32. package/entity/PolymorphicRef.d.ts +12 -0
  33. package/entity/PolymorphicRef.js +18 -0
  34. package/entity/Reference.d.ts +3 -8
  35. package/entity/Reference.js +62 -29
  36. package/entity/WrappedEntity.d.ts +7 -10
  37. package/entity/WrappedEntity.js +6 -7
  38. package/entity/defineEntity.d.ts +472 -313
  39. package/entity/defineEntity.js +134 -290
  40. package/entity/index.d.ts +2 -2
  41. package/entity/index.js +2 -2
  42. package/entity/utils.d.ts +6 -1
  43. package/entity/utils.js +46 -11
  44. package/entity/validators.d.ts +11 -0
  45. package/entity/validators.js +66 -0
  46. package/enums.d.ts +8 -6
  47. package/enums.js +13 -17
  48. package/errors.d.ts +26 -16
  49. package/errors.js +63 -31
  50. package/events/EventManager.d.ts +3 -5
  51. package/events/EventManager.js +37 -26
  52. package/events/index.d.ts +1 -1
  53. package/events/index.js +0 -1
  54. package/exceptions.js +9 -2
  55. package/hydration/Hydrator.js +1 -2
  56. package/hydration/ObjectHydrator.d.ts +5 -6
  57. package/hydration/ObjectHydrator.js +109 -50
  58. package/index.d.ts +2 -2
  59. package/index.js +1 -2
  60. package/logging/DefaultLogger.d.ts +1 -1
  61. package/logging/DefaultLogger.js +3 -4
  62. package/logging/SimpleLogger.d.ts +1 -1
  63. package/logging/colors.d.ts +1 -1
  64. package/logging/colors.js +4 -6
  65. package/logging/index.d.ts +2 -1
  66. package/logging/index.js +1 -1
  67. package/logging/inspect.d.ts +2 -0
  68. package/logging/inspect.js +11 -0
  69. package/metadata/EntitySchema.d.ts +47 -23
  70. package/metadata/EntitySchema.js +103 -34
  71. package/metadata/MetadataDiscovery.d.ts +65 -18
  72. package/metadata/MetadataDiscovery.js +940 -424
  73. package/metadata/MetadataProvider.d.ts +11 -2
  74. package/metadata/MetadataProvider.js +71 -2
  75. package/metadata/MetadataStorage.d.ts +11 -13
  76. package/metadata/MetadataStorage.js +79 -48
  77. package/metadata/MetadataValidator.d.ts +32 -9
  78. package/metadata/MetadataValidator.js +214 -44
  79. package/metadata/discover-entities.d.ts +5 -0
  80. package/metadata/discover-entities.js +40 -0
  81. package/metadata/index.d.ts +1 -1
  82. package/metadata/index.js +0 -1
  83. package/metadata/types.d.ts +577 -0
  84. package/metadata/types.js +1 -0
  85. package/naming-strategy/AbstractNamingStrategy.d.ts +16 -4
  86. package/naming-strategy/AbstractNamingStrategy.js +26 -5
  87. package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
  88. package/naming-strategy/EntityCaseNamingStrategy.js +7 -6
  89. package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
  90. package/naming-strategy/MongoNamingStrategy.js +6 -6
  91. package/naming-strategy/NamingStrategy.d.ts +28 -4
  92. package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
  93. package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
  94. package/naming-strategy/index.d.ts +1 -1
  95. package/naming-strategy/index.js +0 -1
  96. package/not-supported.d.ts +2 -0
  97. package/not-supported.js +8 -0
  98. package/package.json +47 -36
  99. package/platforms/ExceptionConverter.js +1 -1
  100. package/platforms/Platform.d.ts +33 -15
  101. package/platforms/Platform.js +125 -69
  102. package/serialization/EntitySerializer.d.ts +6 -3
  103. package/serialization/EntitySerializer.js +54 -30
  104. package/serialization/EntityTransformer.js +37 -22
  105. package/serialization/SerializationContext.d.ts +10 -14
  106. package/serialization/SerializationContext.js +24 -19
  107. package/types/ArrayType.d.ts +1 -1
  108. package/types/ArrayType.js +2 -3
  109. package/types/BigIntType.js +1 -1
  110. package/types/BlobType.d.ts +0 -1
  111. package/types/BlobType.js +0 -3
  112. package/types/BooleanType.d.ts +1 -0
  113. package/types/BooleanType.js +3 -0
  114. package/types/DecimalType.js +2 -2
  115. package/types/DoubleType.js +1 -1
  116. package/types/EnumArrayType.js +1 -2
  117. package/types/JsonType.d.ts +1 -1
  118. package/types/JsonType.js +7 -2
  119. package/types/TinyIntType.js +1 -1
  120. package/types/Type.d.ts +2 -4
  121. package/types/Type.js +3 -3
  122. package/types/Uint8ArrayType.d.ts +0 -1
  123. package/types/Uint8ArrayType.js +1 -4
  124. package/types/UuidType.d.ts +2 -0
  125. package/types/UuidType.js +14 -2
  126. package/types/index.d.ts +3 -2
  127. package/typings.d.ts +427 -170
  128. package/typings.js +100 -45
  129. package/unit-of-work/ChangeSet.d.ts +4 -6
  130. package/unit-of-work/ChangeSet.js +8 -9
  131. package/unit-of-work/ChangeSetComputer.d.ts +2 -12
  132. package/unit-of-work/ChangeSetComputer.js +61 -38
  133. package/unit-of-work/ChangeSetPersister.d.ts +10 -17
  134. package/unit-of-work/ChangeSetPersister.js +136 -73
  135. package/unit-of-work/CommitOrderCalculator.d.ts +13 -14
  136. package/unit-of-work/CommitOrderCalculator.js +22 -20
  137. package/unit-of-work/IdentityMap.d.ts +12 -3
  138. package/unit-of-work/IdentityMap.js +51 -13
  139. package/unit-of-work/UnitOfWork.d.ts +39 -23
  140. package/unit-of-work/UnitOfWork.js +441 -246
  141. package/utils/AbstractMigrator.d.ts +101 -0
  142. package/utils/AbstractMigrator.js +303 -0
  143. package/utils/AbstractSchemaGenerator.d.ts +5 -5
  144. package/utils/AbstractSchemaGenerator.js +30 -18
  145. package/utils/AsyncContext.d.ts +6 -0
  146. package/utils/AsyncContext.js +42 -0
  147. package/utils/Configuration.d.ts +647 -185
  148. package/utils/Configuration.js +215 -252
  149. package/utils/ConfigurationLoader.d.ts +1 -52
  150. package/utils/ConfigurationLoader.js +1 -330
  151. package/utils/Cursor.d.ts +3 -6
  152. package/utils/Cursor.js +32 -17
  153. package/utils/DataloaderUtils.d.ts +10 -5
  154. package/utils/DataloaderUtils.js +42 -22
  155. package/utils/EntityComparator.d.ts +21 -21
  156. package/utils/EntityComparator.js +224 -118
  157. package/utils/QueryHelper.d.ts +34 -7
  158. package/utils/QueryHelper.js +183 -72
  159. package/utils/RawQueryFragment.d.ts +28 -34
  160. package/utils/RawQueryFragment.js +37 -72
  161. package/utils/RequestContext.js +2 -2
  162. package/utils/TransactionContext.js +2 -2
  163. package/utils/TransactionManager.js +11 -8
  164. package/utils/Utils.d.ts +16 -127
  165. package/utils/Utils.js +104 -402
  166. package/utils/clone.js +13 -23
  167. package/utils/env-vars.d.ts +7 -0
  168. package/utils/env-vars.js +98 -0
  169. package/utils/fs-utils.d.ts +20 -0
  170. package/utils/fs-utils.js +193 -0
  171. package/utils/index.d.ts +1 -3
  172. package/utils/index.js +1 -3
  173. package/utils/upsert-utils.d.ts +9 -4
  174. package/utils/upsert-utils.js +51 -5
  175. package/decorators/Check.d.ts +0 -3
  176. package/decorators/Check.js +0 -13
  177. package/decorators/CreateRequestContext.d.ts +0 -3
  178. package/decorators/CreateRequestContext.js +0 -32
  179. package/decorators/Embeddable.d.ts +0 -8
  180. package/decorators/Embeddable.js +0 -11
  181. package/decorators/Embedded.d.ts +0 -12
  182. package/decorators/Embedded.js +0 -18
  183. package/decorators/Entity.d.ts +0 -33
  184. package/decorators/Entity.js +0 -12
  185. package/decorators/Enum.d.ts +0 -9
  186. package/decorators/Enum.js +0 -16
  187. package/decorators/Filter.d.ts +0 -2
  188. package/decorators/Filter.js +0 -8
  189. package/decorators/Formula.d.ts +0 -4
  190. package/decorators/Formula.js +0 -15
  191. package/decorators/Indexed.d.ts +0 -19
  192. package/decorators/Indexed.js +0 -20
  193. package/decorators/ManyToMany.d.ts +0 -42
  194. package/decorators/ManyToMany.js +0 -14
  195. package/decorators/ManyToOne.d.ts +0 -34
  196. package/decorators/ManyToOne.js +0 -14
  197. package/decorators/OneToMany.d.ts +0 -28
  198. package/decorators/OneToMany.js +0 -17
  199. package/decorators/OneToOne.d.ts +0 -28
  200. package/decorators/OneToOne.js +0 -7
  201. package/decorators/PrimaryKey.d.ts +0 -8
  202. package/decorators/PrimaryKey.js +0 -20
  203. package/decorators/Property.d.ts +0 -250
  204. package/decorators/Property.js +0 -32
  205. package/decorators/Transactional.d.ts +0 -14
  206. package/decorators/Transactional.js +0 -28
  207. package/decorators/hooks.d.ts +0 -16
  208. package/decorators/hooks.js +0 -47
  209. package/decorators/index.d.ts +0 -17
  210. package/decorators/index.js +0 -17
  211. package/entity/ArrayCollection.d.ts +0 -118
  212. package/entity/ArrayCollection.js +0 -407
  213. package/entity/EntityValidator.d.ts +0 -19
  214. package/entity/EntityValidator.js +0 -150
  215. package/metadata/ReflectMetadataProvider.d.ts +0 -8
  216. package/metadata/ReflectMetadataProvider.js +0 -44
  217. package/utils/resolveContextProvider.d.ts +0 -10
  218. package/utils/resolveContextProvider.js +0 -28
@@ -2,7 +2,7 @@ import type { PopulatePath } from '../enums.js';
2
2
  import type { CreateOptions, EntityManager, MergeOptions } from '../EntityManager.js';
3
3
  import type { AssignOptions } from './EntityAssigner.js';
4
4
  import type { EntityData, EntityName, Primary, Loaded, FilterQuery, EntityDictionary, AutoPath, RequiredEntityData, Ref, EntityType, EntityDTO, MergeSelected, FromEntityType, IsSubset, MergeLoaded, ArrayElement } from '../typings.js';
5
- import type { CountOptions, DeleteOptions, FindAllOptions, FindByCursorOptions, FindOneOptions, FindOneOrFailOptions, FindOptions, GetReferenceOptions, NativeInsertUpdateOptions, UpdateOptions, UpsertManyOptions, UpsertOptions } from '../drivers/IDatabaseDriver.js';
5
+ import type { CountOptions, DeleteOptions, FindAllOptions, FindByCursorOptions, FindOneOptions, FindOneOrFailOptions, FindOptions, GetReferenceOptions, NativeInsertUpdateOptions, StreamOptions, UpdateOptions, UpsertManyOptions, UpsertOptions } from '../drivers/IDatabaseDriver.js';
6
6
  import type { EntityLoaderOptions } from './EntityLoader.js';
7
7
  import type { Cursor } from '../utils/Cursor.js';
8
8
  export declare class EntityRepository<Entity extends object> {
@@ -80,11 +80,15 @@ export declare class EntityRepository<Entity extends object> {
80
80
  /**
81
81
  * @inheritDoc EntityManager.findByCursor
82
82
  */
83
- findByCursor<Hint extends string = never, Fields extends string = '*', Excludes extends string = never, IncludeCount extends boolean = true>(where: FilterQuery<Entity>, options: FindByCursorOptions<Entity, Hint, Fields, Excludes, IncludeCount>): Promise<Cursor<Entity, Hint, Fields, Excludes, IncludeCount>>;
83
+ findByCursor<Hint extends string = never, Fields extends string = '*', Excludes extends string = never, IncludeCount extends boolean = true>(options: FindByCursorOptions<Entity, Hint, Fields, Excludes, IncludeCount>): Promise<Cursor<Entity, Hint, Fields, Excludes, IncludeCount>>;
84
84
  /**
85
85
  * Finds all entities of given type. You can pass additional options via the `options` parameter.
86
86
  */
87
87
  findAll<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(options?: FindAllOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes>[]>;
88
+ /**
89
+ * @inheritDoc EntityManager.stream
90
+ */
91
+ stream<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(options?: StreamOptions<Entity, Hint, Fields, Excludes>): AsyncIterableIterator<Loaded<Entity, Hint, Fields, Excludes>>;
88
92
  /**
89
93
  * @inheritDoc EntityManager.insert
90
94
  */
@@ -107,10 +111,26 @@ export declare class EntityRepository<Entity extends object> {
107
111
  map(result: EntityDictionary<Entity>, options?: {
108
112
  schema?: string;
109
113
  }): Entity;
114
+ /**
115
+ * Gets a reference to the entity identified by the given type and alternate key property without actually loading it.
116
+ * The key option specifies which property to use for identity map lookup instead of the primary key.
117
+ */
118
+ getReference<K extends string & keyof Entity>(id: Entity[K], options: Omit<GetReferenceOptions, 'key' | 'wrapped'> & {
119
+ key: K;
120
+ wrapped: true;
121
+ }): Ref<Entity>;
122
+ /**
123
+ * Gets a reference to the entity identified by the given type and alternate key property without actually loading it.
124
+ * The key option specifies which property to use for identity map lookup instead of the primary key.
125
+ */
126
+ getReference<K extends string & keyof Entity>(id: Entity[K], options: Omit<GetReferenceOptions, 'key'> & {
127
+ key: K;
128
+ wrapped?: false;
129
+ }): Entity;
110
130
  /**
111
131
  * Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded
112
132
  */
113
- getReference(id: Primary<Entity>, options: Omit<GetReferenceOptions, 'wrapped'> & {
133
+ getReference(id: Primary<Entity>, options: Omit<GetReferenceOptions, 'wrapped' | 'key'> & {
114
134
  wrapped: true;
115
135
  }): Ref<Entity>;
116
136
  /**
@@ -120,7 +140,7 @@ export declare class EntityRepository<Entity extends object> {
120
140
  /**
121
141
  * Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded
122
142
  */
123
- getReference(id: Primary<Entity>, options: Omit<GetReferenceOptions, 'wrapped'> & {
143
+ getReference(id: Primary<Entity>, options: Omit<GetReferenceOptions, 'wrapped' | 'key'> & {
124
144
  wrapped: false;
125
145
  }): Entity;
126
146
  /**
@@ -130,7 +150,7 @@ export declare class EntityRepository<Entity extends object> {
130
150
  /**
131
151
  * Loads specified relations in batch. This will execute one query for each relation, that will populate it on all the specified entities.
132
152
  */
133
- populate<Ent extends Entity | Entity[], Hint extends string = never, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Fields extends string = '*', Excludes extends string = never>(entities: Ent, populate: AutoPath<Naked, Hint, PopulatePath.ALL>[] | false, options?: EntityLoaderOptions<Naked, Fields, Excludes>): Promise<Ent extends object[] ? MergeLoaded<ArrayElement<Ent>, Naked, Hint, Fields, Excludes>[] : MergeLoaded<Ent, Naked, Hint, Fields, Excludes>>;
153
+ populate<Ent extends Entity | Entity[], Hint extends string = never, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Fields extends string = never, Excludes extends string = never>(entities: Ent, populate: AutoPath<Naked, Hint, PopulatePath.ALL>[] | false, options?: EntityLoaderOptions<Naked, Fields, Excludes>): Promise<Ent extends object[] ? MergeLoaded<ArrayElement<Ent>, Naked, Hint, Fields, Excludes>[] : MergeLoaded<Ent, Naked, Hint, Fields, Excludes>>;
134
154
  /**
135
155
  * Creates new instance of given entity and populates it with given data.
136
156
  * The entity constructor will be used unless you provide `{ managed: true }` in the `options` parameter.
@@ -146,13 +166,13 @@ export declare class EntityRepository<Entity extends object> {
146
166
  * The newly created entity will be automatically marked for persistence via `em.persist` unless you disable this
147
167
  * behavior, either locally via `persist: false` option, or globally via `persistOnCreate` ORM config option.
148
168
  */
149
- create<Convert extends boolean = false>(data: RequiredEntityData<Entity, never, Convert>, options?: CreateOptions<Convert>): Entity;
169
+ create<Convert extends boolean = false, Data extends RequiredEntityData<Entity, never, Convert> = RequiredEntityData<Entity, never, Convert>>(data: Data & IsSubset<RequiredEntityData<Entity, never, Convert>, Data>, options?: CreateOptions<Convert>): Entity;
150
170
  /**
151
171
  * Creates new instance of given entity and populates it with given data.
152
172
  * The entity constructor will be used unless you provide `{ managed: true }` in the `options` parameter.
153
173
  * The constructor will be given parameters based on the defined constructor of the entity. If the constructor
154
174
  * parameter matches a property name, its value will be extracted from `data`. If no matching property exists,
155
- * the whole `data` parameter will be passed. This means we can also define `constructor(data: Partial<T>)` and
175
+ * the whole `data` parameter will be pass. This means we can also define `constructor(data: Partial<T>)` and
156
176
  * `em.create()` will pass the data into it (unless we have a property named `data` too).
157
177
  *
158
178
  * The parameters are strictly checked, you need to provide all required properties. You can use `OptionalProps`
@@ -162,7 +182,7 @@ export declare class EntityRepository<Entity extends object> {
162
182
  * The newly created entity will be automatically marked for persistence via `em.persist` unless you disable this
163
183
  * behavior, either locally via `persist: false` option, or globally via `persistOnCreate` ORM config option.
164
184
  */
165
- create<Convert extends boolean = false>(data: EntityData<Entity, Convert>, options: CreateOptions<Convert> & {
185
+ create<Convert extends boolean = false, Data extends EntityData<Entity, Convert> = EntityData<Entity, Convert>>(data: Data & IsSubset<EntityData<Entity, Convert>, Data>, options: CreateOptions<Convert> & {
166
186
  partial: true;
167
187
  }): Entity;
168
188
  /**
@@ -90,8 +90,8 @@ export class EntityRepository {
90
90
  /**
91
91
  * @inheritDoc EntityManager.findByCursor
92
92
  */
93
- async findByCursor(where, options) {
94
- return this.getEntityManager().findByCursor(this.entityName, where, options);
93
+ async findByCursor(options) {
94
+ return this.getEntityManager().findByCursor(this.entityName, options);
95
95
  }
96
96
  /**
97
97
  * Finds all entities of given type. You can pass additional options via the `options` parameter.
@@ -99,6 +99,12 @@ export class EntityRepository {
99
99
  async findAll(options) {
100
100
  return this.getEntityManager().findAll(this.entityName, options);
101
101
  }
102
+ /**
103
+ * @inheritDoc EntityManager.stream
104
+ */
105
+ async *stream(options) {
106
+ yield* this.getEntityManager().stream(this.entityName, options);
107
+ }
102
108
  /**
103
109
  * @inheritDoc EntityManager.insert
104
110
  */
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Wrapper class for polymorphic relation reference data.
3
+ * Holds the discriminator value (type identifier) and the primary key value(s).
4
+ * Used internally to track polymorphic FK values before hydration.
5
+ */
6
+ export declare class PolymorphicRef {
7
+ readonly discriminator: string;
8
+ id: unknown;
9
+ constructor(discriminator: string, id: unknown);
10
+ /** Returns `[discriminator, ...idValues]` tuple suitable for column-level expansion. */
11
+ toTuple(): unknown[];
12
+ }
@@ -0,0 +1,18 @@
1
+ import { Utils } from '../utils/Utils.js';
2
+ /**
3
+ * Wrapper class for polymorphic relation reference data.
4
+ * Holds the discriminator value (type identifier) and the primary key value(s).
5
+ * Used internally to track polymorphic FK values before hydration.
6
+ */
7
+ export class PolymorphicRef {
8
+ discriminator;
9
+ id;
10
+ constructor(discriminator, id) {
11
+ this.discriminator = discriminator;
12
+ this.id = id;
13
+ }
14
+ /** Returns `[discriminator, ...idValues]` tuple suitable for column-level expansion. */
15
+ toTuple() {
16
+ return [this.discriminator, ...Utils.asArray(this.id)];
17
+ }
18
+ }
@@ -1,8 +1,8 @@
1
- import { inspect } from 'node:util';
2
1
  import type { AddEager, AddOptional, Dictionary, EntityClass, EntityKey, EntityProperty, Loaded, LoadedReference, Primary, Ref } from '../typings.js';
3
2
  import type { FindOneOptions, FindOneOrFailOptions } from '../drivers/IDatabaseDriver.js';
4
3
  export declare class Reference<T extends object> {
5
4
  private entity;
5
+ private property?;
6
6
  constructor(entity: T);
7
7
  static create<T extends object>(entity: T | Ref<T>): Ref<T>;
8
8
  static createFromPK<T extends object>(entityType: EntityClass<T>, pk: Primary<T>, options?: {
@@ -42,14 +42,11 @@ export declare class Reference<T extends object> {
42
42
  isInitialized(): boolean;
43
43
  populated(populated?: boolean): void;
44
44
  toJSON(...args: any[]): Dictionary;
45
- /** @ignore */
46
- [inspect.custom](depth?: number): string;
47
45
  }
48
46
  export declare class ScalarReference<Value> {
47
+ #private;
49
48
  private value?;
50
- private initialized;
51
49
  private entity?;
52
- private property?;
53
50
  constructor(value?: Value | undefined, initialized?: boolean);
54
51
  /**
55
52
  * Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
@@ -65,8 +62,6 @@ export declare class ScalarReference<Value> {
65
62
  bind<Entity extends object>(entity: Entity, property: EntityKey<Entity>): void;
66
63
  unwrap(): Value | undefined;
67
64
  isInitialized(): boolean;
68
- /** @ignore */
69
- [inspect.custom](): string;
70
65
  }
71
66
  export interface LoadReferenceOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never> extends FindOneOptions<T, P, F, E> {
72
67
  dataloader?: boolean;
@@ -77,7 +72,7 @@ export interface LoadReferenceOrFailOptions<T extends object, P extends string =
77
72
  /**
78
73
  * shortcut for `wrap(entity).toReference()`
79
74
  */
80
- export declare function ref<I extends unknown | Ref<unknown> | undefined | null, T extends I & {}>(entity: I): Ref<T> & LoadedReference<Loaded<T, AddEager<T>>> | AddOptional<typeof entity>;
75
+ export declare function ref<I extends unknown | Ref<unknown> | undefined | null, T extends I & {}>(entity: I): (Ref<T> & LoadedReference<Loaded<T, AddEager<T>>>) | AddOptional<typeof entity>;
81
76
  /**
82
77
  * shortcut for `Reference.createFromPK(entityType, pk)`
83
78
  */
@@ -1,10 +1,12 @@
1
- import { inspect } from 'node:util';
2
1
  import { DataloaderType } from '../enums.js';
3
2
  import { helper, wrap } from './wrap.js';
4
3
  import { Utils } from '../utils/Utils.js';
4
+ import { QueryHelper } from '../utils/QueryHelper.js';
5
5
  import { NotFoundError } from '../errors.js';
6
+ import { inspect } from '../logging/inspect.js';
6
7
  export class Reference {
7
8
  entity;
9
+ property;
8
10
  constructor(entity) {
9
11
  this.entity = entity;
10
12
  this.set(entity);
@@ -64,7 +66,9 @@ export class Reference {
64
66
  */
65
67
  static wrapReference(entity, prop) {
66
68
  if (entity && prop.ref && !Reference.isReference(entity)) {
67
- return Reference.create(entity);
69
+ const ref = Reference.create(entity);
70
+ ref.property = prop;
71
+ return ref;
68
72
  }
69
73
  return entity;
70
74
  }
@@ -84,13 +88,15 @@ export class Reference {
84
88
  if (!wrapped.__em) {
85
89
  return this.entity;
86
90
  }
91
+ options = { ...options, filters: QueryHelper.mergePropertyFilters(this.property?.filters, options.filters) };
87
92
  if (this.isInitialized() && !options.refresh && options.populate) {
88
93
  await wrapped.__em.populate(this.entity, options.populate, options);
89
94
  }
90
95
  if (!this.isInitialized() || options.refresh) {
91
- if (options.dataloader ?? [DataloaderType.ALL, DataloaderType.REFERENCE].includes(wrapped.__em.config.getDataloaderType())) {
92
- // eslint-disable-next-line dot-notation
93
- return wrapped.__em['refLoader'].load([this, options]);
96
+ if (options.dataloader ??
97
+ [DataloaderType.ALL, DataloaderType.REFERENCE].includes(wrapped.__em.config.getDataloaderType())) {
98
+ const dataLoader = await wrapped.__em.getDataLoader('ref');
99
+ return dataLoader.load([this, options]);
94
100
  }
95
101
  return wrapped.init(options);
96
102
  }
@@ -141,9 +147,9 @@ export class Reference {
141
147
  return wrap(this.entity).toJSON(...args);
142
148
  }
143
149
  /** @ignore */
144
- [inspect.custom](depth = 2) {
150
+ [Symbol.for('nodejs.util.inspect.custom')](depth = 2) {
145
151
  const object = { ...this };
146
- const hidden = ['meta'];
152
+ const hidden = ['meta', 'property'];
147
153
  hidden.forEach(k => delete object[k]);
148
154
  const ret = inspect(object, { depth });
149
155
  const wrapped = helper(this.entity);
@@ -156,12 +162,12 @@ export class Reference {
156
162
  }
157
163
  export class ScalarReference {
158
164
  value;
159
- initialized;
160
165
  entity;
161
- property;
166
+ #property;
167
+ #initialized;
162
168
  constructor(value, initialized = value != null) {
163
169
  this.value = value;
164
- this.initialized = initialized;
170
+ this.#initialized = initialized;
165
171
  }
166
172
  /**
167
173
  * Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
@@ -169,11 +175,11 @@ export class ScalarReference {
169
175
  */
170
176
  async load(options) {
171
177
  const opts = typeof options === 'object' ? options : { prop: options };
172
- if (!this.initialized || opts.refresh) {
173
- if (this.entity == null || this.property == null) {
178
+ if (!this.#initialized || opts.refresh) {
179
+ if (this.entity == null || this.#property == null) {
174
180
  throw new Error('Cannot load scalar reference that is not bound to an entity property.');
175
181
  }
176
- await helper(this.entity).populate([this.property], opts);
182
+ await helper(this.entity).populate([this.#property], opts);
177
183
  }
178
184
  return this.value;
179
185
  }
@@ -183,48 +189,75 @@ export class ScalarReference {
183
189
  */
184
190
  async loadOrFail(options = {}) {
185
191
  const ret = await this.load(options);
186
- if (!ret) {
192
+ if (ret == null) {
187
193
  const wrapped = helper(this.entity);
188
194
  options.failHandler ??= wrapped.__em.config.get('findOneOrFailHandler');
189
195
  const entityName = this.entity.constructor.name;
190
- const where = wrapped.getPrimaryKey();
191
- throw new NotFoundError(`${entityName} (${where}) failed to load property '${this.property}'`);
196
+ throw NotFoundError.failedToLoadProperty(entityName, this.#property, wrapped.getPrimaryKey());
192
197
  }
193
198
  return ret;
194
199
  }
195
200
  set(value) {
196
201
  this.value = value;
197
- this.initialized = true;
202
+ this.#initialized = true;
198
203
  }
199
204
  bind(entity, property) {
200
205
  this.entity = entity;
201
- this.property = property;
206
+ this.#property = property;
202
207
  Object.defineProperty(this, 'entity', { enumerable: false, value: entity });
203
208
  }
204
209
  unwrap() {
205
210
  return this.value;
206
211
  }
207
212
  isInitialized() {
208
- return this.initialized;
213
+ return this.#initialized;
209
214
  }
210
- /* v8 ignore next 4 */
211
215
  /** @ignore */
212
- [inspect.custom]() {
213
- return this.initialized ? `Ref<${inspect(this.value)}>` : `Ref<?>`;
216
+ /* v8 ignore next */
217
+ [Symbol.for('nodejs.util.inspect.custom')]() {
218
+ return this.#initialized ? `Ref<${inspect(this.value)}>` : `Ref<?>`;
214
219
  }
215
220
  }
216
221
  Object.defineProperties(Reference.prototype, {
217
222
  __reference: { value: true, enumerable: false },
218
- __meta: { get() { return this.entity.__meta; } },
219
- __platform: { get() { return this.entity.__platform; } },
220
- __helper: { get() { return this.entity.__helper; } },
221
- $: { get() { return this.entity; } },
222
- get: { get() { return () => this.entity; } },
223
+ __meta: {
224
+ get() {
225
+ return this.entity.__meta;
226
+ },
227
+ },
228
+ __platform: {
229
+ get() {
230
+ return this.entity.__platform;
231
+ },
232
+ },
233
+ __helper: {
234
+ get() {
235
+ return this.entity.__helper;
236
+ },
237
+ },
238
+ $: {
239
+ get() {
240
+ return this.entity;
241
+ },
242
+ },
243
+ get: {
244
+ get() {
245
+ return () => this.entity;
246
+ },
247
+ },
223
248
  });
224
249
  Object.defineProperties(ScalarReference.prototype, {
225
250
  __scalarReference: { value: true, enumerable: false },
226
- $: { get() { return this.value; } },
227
- get: { get() { return () => this.value; } },
251
+ $: {
252
+ get() {
253
+ return this.value;
254
+ },
255
+ },
256
+ get: {
257
+ get() {
258
+ return () => this.value;
259
+ },
260
+ },
228
261
  });
229
262
  /**
230
263
  * shortcut for `wrap(entity).toReference()`
@@ -1,7 +1,6 @@
1
1
  import type { PopulatePath } from '../enums.js';
2
- import { inspect } from 'node:util';
3
2
  import type { EntityManager } from '../EntityManager.js';
4
- import type { Dictionary, EntityData, EntityDictionary, EntityMetadata, IHydrator, EntityKey, PopulateOptions, Primary, AutoPath, Ref, AddEager, LoadedReference, EntityDTO, Loaded, FromEntityType, IsSubset, MergeSelected } 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';
5
4
  import { Reference } from './Reference.js';
6
5
  import { type AssignOptions } from './EntityAssigner.js';
7
6
  import type { EntityLoaderOptions } from './EntityLoader.js';
@@ -9,9 +8,10 @@ import type { EntityIdentifier } from './EntityIdentifier.js';
9
8
  import type { SerializationContext } from '../serialization/SerializationContext.js';
10
9
  import { type SerializeOptions } from '../serialization/EntitySerializer.js';
11
10
  import type { FindOneOptions, LoadHint } from '../drivers/IDatabaseDriver.js';
11
+ import type { Platform } from '../platforms/Platform.js';
12
+ import type { Configuration } from '../utils/Configuration.js';
12
13
  export declare class WrappedEntity<Entity extends object> {
13
14
  __initialized: boolean;
14
- __touched: boolean;
15
15
  __populated?: boolean;
16
16
  __managed?: boolean;
17
17
  __onLoadFired?: boolean;
@@ -41,18 +41,17 @@ export declare class WrappedEntity<Entity extends object> {
41
41
  private readonly pkGetterConverted?;
42
42
  constructor(entity: Entity, hydrator: IHydrator, pkGetter?: (e: Entity) => Primary<Entity>, pkSerializer?: (e: Entity) => string, pkGetterConverted?: (e: Entity) => Primary<Entity>);
43
43
  isInitialized(): boolean;
44
- isTouched(): boolean;
45
44
  isManaged(): boolean;
46
45
  populated(populated?: boolean | undefined): void;
47
46
  setSerializationContext<Hint extends string = never, Fields extends string = '*', Exclude extends string = never>(options: LoadHint<Entity, Hint, Fields, Exclude>): void;
48
47
  toReference(): Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>>;
49
48
  toObject<Ignored extends EntityKey<Entity> = never>(ignoreFields?: Ignored[]): Omit<EntityDTO<Entity>, Ignored>;
50
- serialize<Hint extends string = never, Exclude extends string = never>(options?: SerializeOptions<Entity, Hint, Exclude>): EntityDTO<Loaded<Entity, Hint>>;
49
+ serialize<Hint extends string = never, Exclude extends string = never>(options?: SerializeOptions<Entity, Hint, Exclude>): SerializeDTO<Entity, Hint, Exclude>;
51
50
  toPOJO(): EntityDTO<Entity>;
52
51
  toJSON(...args: any[]): EntityDictionary<Entity>;
53
52
  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>;
54
53
  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>;
55
- populate<Hint extends string = never>(populate: AutoPath<Entity, Hint, PopulatePath.ALL>[] | false, options?: EntityLoaderOptions<Entity>): Promise<Loaded<Entity, Hint>>;
54
+ populate<Hint extends string = never, Fields extends string = never>(populate: AutoPath<Entity, Hint, PopulatePath.ALL>[] | false, options?: EntityLoaderOptions<Entity, Fields>): Promise<Loaded<Entity, Hint>>;
56
55
  hasPrimaryKey(): boolean;
57
56
  getPrimaryKey(convertCustomTypes?: boolean): Primary<Entity> | null;
58
57
  getPrimaryKeys(convertCustomTypes?: boolean): Primary<Entity>[] | null;
@@ -61,9 +60,7 @@ export declare class WrappedEntity<Entity extends object> {
61
60
  setPrimaryKey(id: Primary<Entity> | null): void;
62
61
  getSerializedPrimaryKey(): string;
63
62
  get __meta(): EntityMetadata<Entity>;
64
- get __platform(): import("../index.js").Platform;
65
- get __config(): import("../index.js").Configuration<import("../drivers/IDatabaseDriver.js").IDatabaseDriver<import("../index.js").Connection>, EntityManager<import("../drivers/IDatabaseDriver.js").IDatabaseDriver<import("../index.js").Connection>>>;
63
+ get __platform(): Platform;
64
+ get __config(): Configuration;
66
65
  get __primaryKeys(): Primary<Entity>[];
67
- /** @ignore */
68
- [inspect.custom](): string;
69
66
  }
@@ -1,4 +1,3 @@
1
- import { inspect } from 'node:util';
2
1
  import { Reference } from './Reference.js';
3
2
  import { EntityTransformer } from '../serialization/EntityTransformer.js';
4
3
  import { EntityAssigner } from './EntityAssigner.js';
@@ -15,7 +14,6 @@ export class WrappedEntity {
15
14
  this.pkSerializer = pkSerializer;
16
15
  this.pkGetterConverted = pkGetterConverted;
17
16
  this.__initialized = true;
18
- this.__touched = false;
19
17
  this.__serializationContext = {};
20
18
  this.__loadedProperties = new Set();
21
19
  this.__data = {};
@@ -24,9 +22,6 @@ export class WrappedEntity {
24
22
  isInitialized() {
25
23
  return this.__initialized;
26
24
  }
27
- isTouched() {
28
- return this.__touched;
29
- }
30
25
  isManaged() {
31
26
  return !!this.__managed;
32
27
  }
@@ -76,7 +71,11 @@ export class WrappedEntity {
76
71
  if (!this.__em) {
77
72
  throw ValidationError.entityNotManaged(this.entity);
78
73
  }
79
- return this.__em.findOne(this.entity.constructor.name, this.entity, { ...options, refresh: true, schema: this.__schema });
74
+ return this.__em.findOne(this.entity.constructor, this.entity, {
75
+ ...options,
76
+ refresh: true,
77
+ schema: this.__schema,
78
+ });
80
79
  }
81
80
  async populate(populate, options = {}) {
82
81
  if (!this.__em) {
@@ -153,7 +152,7 @@ export class WrappedEntity {
153
152
  return Utils.getPrimaryKeyValues(this.entity, this.__meta);
154
153
  }
155
154
  /** @ignore */
156
- [inspect.custom]() {
155
+ [Symbol.for('nodejs.util.inspect.custom')]() {
157
156
  return `[WrappedEntity<${this.__meta.className}>]`;
158
157
  }
159
158
  }