@mikro-orm/core 7.0.0-dev.32 → 7.0.0-dev.321

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 (216) hide show
  1. package/EntityManager.d.ts +71 -63
  2. package/EntityManager.js +365 -283
  3. package/MikroORM.d.ts +44 -35
  4. package/MikroORM.js +109 -142
  5. package/README.md +7 -4
  6. package/cache/FileCacheAdapter.d.ts +1 -2
  7. package/cache/FileCacheAdapter.js +19 -14
  8. package/cache/GeneratedCacheAdapter.d.ts +0 -1
  9. package/cache/GeneratedCacheAdapter.js +0 -2
  10. package/cache/index.d.ts +1 -2
  11. package/cache/index.js +0 -2
  12. package/connections/Connection.d.ts +12 -5
  13. package/connections/Connection.js +37 -15
  14. package/drivers/DatabaseDriver.d.ts +25 -18
  15. package/drivers/DatabaseDriver.js +144 -45
  16. package/drivers/IDatabaseDriver.d.ts +118 -23
  17. package/entity/BaseEntity.d.ts +63 -4
  18. package/entity/BaseEntity.js +0 -3
  19. package/entity/Collection.d.ts +101 -29
  20. package/entity/Collection.js +473 -115
  21. package/entity/EntityAssigner.js +37 -25
  22. package/entity/EntityFactory.d.ts +7 -1
  23. package/entity/EntityFactory.js +116 -64
  24. package/entity/EntityHelper.d.ts +2 -2
  25. package/entity/EntityHelper.js +69 -27
  26. package/entity/EntityLoader.d.ts +11 -10
  27. package/entity/EntityLoader.js +264 -102
  28. package/entity/EntityRepository.d.ts +28 -8
  29. package/entity/EntityRepository.js +8 -2
  30. package/entity/PolymorphicRef.d.ts +12 -0
  31. package/entity/PolymorphicRef.js +18 -0
  32. package/entity/Reference.d.ts +2 -6
  33. package/entity/Reference.js +52 -19
  34. package/entity/WrappedEntity.d.ts +3 -8
  35. package/entity/WrappedEntity.js +6 -7
  36. package/entity/defineEntity.d.ts +525 -311
  37. package/entity/defineEntity.js +134 -290
  38. package/entity/index.d.ts +2 -2
  39. package/entity/index.js +2 -2
  40. package/entity/utils.d.ts +6 -1
  41. package/entity/utils.js +46 -11
  42. package/entity/validators.d.ts +11 -0
  43. package/entity/validators.js +66 -0
  44. package/enums.d.ts +8 -6
  45. package/enums.js +13 -17
  46. package/errors.d.ts +20 -10
  47. package/errors.js +63 -31
  48. package/events/EventManager.d.ts +2 -1
  49. package/events/EventManager.js +24 -13
  50. package/events/index.d.ts +1 -1
  51. package/events/index.js +0 -1
  52. package/exceptions.js +9 -2
  53. package/hydration/Hydrator.js +1 -2
  54. package/hydration/ObjectHydrator.d.ts +4 -4
  55. package/hydration/ObjectHydrator.js +105 -46
  56. package/index.d.ts +2 -2
  57. package/index.js +1 -2
  58. package/logging/DefaultLogger.d.ts +1 -1
  59. package/logging/DefaultLogger.js +3 -4
  60. package/logging/SimpleLogger.d.ts +1 -1
  61. package/logging/colors.d.ts +1 -1
  62. package/logging/colors.js +5 -7
  63. package/logging/index.d.ts +2 -1
  64. package/logging/index.js +1 -1
  65. package/logging/inspect.d.ts +2 -0
  66. package/logging/inspect.js +11 -0
  67. package/metadata/EntitySchema.d.ts +47 -23
  68. package/metadata/EntitySchema.js +103 -34
  69. package/metadata/MetadataDiscovery.d.ts +64 -9
  70. package/metadata/MetadataDiscovery.js +867 -354
  71. package/metadata/MetadataProvider.d.ts +11 -2
  72. package/metadata/MetadataProvider.js +71 -2
  73. package/metadata/MetadataStorage.d.ts +13 -11
  74. package/metadata/MetadataStorage.js +72 -41
  75. package/metadata/MetadataValidator.d.ts +32 -9
  76. package/metadata/MetadataValidator.js +214 -44
  77. package/metadata/discover-entities.d.ts +5 -0
  78. package/metadata/discover-entities.js +40 -0
  79. package/metadata/index.d.ts +1 -1
  80. package/metadata/index.js +0 -1
  81. package/metadata/types.d.ts +577 -0
  82. package/metadata/types.js +1 -0
  83. package/naming-strategy/AbstractNamingStrategy.d.ts +16 -4
  84. package/naming-strategy/AbstractNamingStrategy.js +26 -5
  85. package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
  86. package/naming-strategy/EntityCaseNamingStrategy.js +6 -5
  87. package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
  88. package/naming-strategy/MongoNamingStrategy.js +6 -6
  89. package/naming-strategy/NamingStrategy.d.ts +28 -4
  90. package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
  91. package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
  92. package/naming-strategy/index.d.ts +1 -1
  93. package/naming-strategy/index.js +0 -1
  94. package/not-supported.d.ts +2 -0
  95. package/not-supported.js +8 -0
  96. package/package.json +47 -36
  97. package/platforms/ExceptionConverter.js +1 -1
  98. package/platforms/Platform.d.ts +33 -15
  99. package/platforms/Platform.js +125 -69
  100. package/serialization/EntitySerializer.d.ts +6 -3
  101. package/serialization/EntitySerializer.js +53 -29
  102. package/serialization/EntityTransformer.js +33 -21
  103. package/serialization/SerializationContext.d.ts +6 -6
  104. package/serialization/SerializationContext.js +4 -4
  105. package/types/ArrayType.d.ts +1 -1
  106. package/types/ArrayType.js +2 -3
  107. package/types/BigIntType.js +1 -1
  108. package/types/BlobType.d.ts +0 -1
  109. package/types/BlobType.js +0 -3
  110. package/types/BooleanType.d.ts +1 -0
  111. package/types/BooleanType.js +3 -0
  112. package/types/DecimalType.js +2 -2
  113. package/types/DoubleType.js +1 -1
  114. package/types/EnumArrayType.js +1 -2
  115. package/types/JsonType.d.ts +1 -1
  116. package/types/JsonType.js +7 -2
  117. package/types/TinyIntType.js +1 -1
  118. package/types/Type.d.ts +2 -4
  119. package/types/Type.js +3 -3
  120. package/types/Uint8ArrayType.d.ts +0 -1
  121. package/types/Uint8ArrayType.js +1 -4
  122. package/types/UuidType.d.ts +2 -0
  123. package/types/UuidType.js +14 -2
  124. package/types/index.d.ts +3 -2
  125. package/typings.d.ts +427 -170
  126. package/typings.js +100 -45
  127. package/unit-of-work/ChangeSet.d.ts +4 -6
  128. package/unit-of-work/ChangeSet.js +8 -9
  129. package/unit-of-work/ChangeSetComputer.d.ts +3 -8
  130. package/unit-of-work/ChangeSetComputer.js +49 -26
  131. package/unit-of-work/ChangeSetPersister.d.ts +13 -12
  132. package/unit-of-work/ChangeSetPersister.js +107 -44
  133. package/unit-of-work/CommitOrderCalculator.d.ts +12 -10
  134. package/unit-of-work/CommitOrderCalculator.js +17 -15
  135. package/unit-of-work/IdentityMap.d.ts +12 -0
  136. package/unit-of-work/IdentityMap.js +39 -1
  137. package/unit-of-work/UnitOfWork.d.ts +34 -4
  138. package/unit-of-work/UnitOfWork.js +294 -107
  139. package/utils/AbstractMigrator.d.ts +101 -0
  140. package/utils/AbstractMigrator.js +303 -0
  141. package/utils/AbstractSchemaGenerator.d.ts +5 -5
  142. package/utils/AbstractSchemaGenerator.js +30 -18
  143. package/utils/AsyncContext.d.ts +6 -0
  144. package/utils/AsyncContext.js +42 -0
  145. package/utils/Configuration.d.ts +796 -211
  146. package/utils/Configuration.js +160 -197
  147. package/utils/ConfigurationLoader.d.ts +1 -52
  148. package/utils/ConfigurationLoader.js +1 -330
  149. package/utils/Cursor.d.ts +0 -3
  150. package/utils/Cursor.js +29 -14
  151. package/utils/DataloaderUtils.d.ts +10 -5
  152. package/utils/DataloaderUtils.js +42 -22
  153. package/utils/EntityComparator.d.ts +16 -9
  154. package/utils/EntityComparator.js +202 -96
  155. package/utils/QueryHelper.d.ts +34 -7
  156. package/utils/QueryHelper.js +183 -72
  157. package/utils/RawQueryFragment.d.ts +28 -34
  158. package/utils/RawQueryFragment.js +37 -72
  159. package/utils/RequestContext.js +2 -2
  160. package/utils/TransactionContext.js +2 -2
  161. package/utils/TransactionManager.js +11 -7
  162. package/utils/Utils.d.ts +16 -127
  163. package/utils/Utils.js +106 -401
  164. package/utils/clone.js +13 -23
  165. package/utils/env-vars.d.ts +7 -0
  166. package/utils/env-vars.js +98 -0
  167. package/utils/fs-utils.d.ts +34 -0
  168. package/utils/fs-utils.js +193 -0
  169. package/utils/index.d.ts +1 -3
  170. package/utils/index.js +1 -3
  171. package/utils/upsert-utils.d.ts +9 -4
  172. package/utils/upsert-utils.js +51 -5
  173. package/decorators/Check.d.ts +0 -3
  174. package/decorators/Check.js +0 -13
  175. package/decorators/CreateRequestContext.d.ts +0 -3
  176. package/decorators/CreateRequestContext.js +0 -32
  177. package/decorators/Embeddable.d.ts +0 -8
  178. package/decorators/Embeddable.js +0 -11
  179. package/decorators/Embedded.d.ts +0 -12
  180. package/decorators/Embedded.js +0 -18
  181. package/decorators/Entity.d.ts +0 -33
  182. package/decorators/Entity.js +0 -12
  183. package/decorators/Enum.d.ts +0 -9
  184. package/decorators/Enum.js +0 -16
  185. package/decorators/Filter.d.ts +0 -2
  186. package/decorators/Filter.js +0 -8
  187. package/decorators/Formula.d.ts +0 -4
  188. package/decorators/Formula.js +0 -15
  189. package/decorators/Indexed.d.ts +0 -19
  190. package/decorators/Indexed.js +0 -20
  191. package/decorators/ManyToMany.d.ts +0 -42
  192. package/decorators/ManyToMany.js +0 -14
  193. package/decorators/ManyToOne.d.ts +0 -34
  194. package/decorators/ManyToOne.js +0 -14
  195. package/decorators/OneToMany.d.ts +0 -28
  196. package/decorators/OneToMany.js +0 -17
  197. package/decorators/OneToOne.d.ts +0 -28
  198. package/decorators/OneToOne.js +0 -7
  199. package/decorators/PrimaryKey.d.ts +0 -8
  200. package/decorators/PrimaryKey.js +0 -20
  201. package/decorators/Property.d.ts +0 -250
  202. package/decorators/Property.js +0 -32
  203. package/decorators/Transactional.d.ts +0 -14
  204. package/decorators/Transactional.js +0 -28
  205. package/decorators/hooks.d.ts +0 -16
  206. package/decorators/hooks.js +0 -47
  207. package/decorators/index.d.ts +0 -17
  208. package/decorators/index.js +0 -17
  209. package/entity/ArrayCollection.d.ts +0 -118
  210. package/entity/ArrayCollection.js +0 -407
  211. package/entity/EntityValidator.d.ts +0 -19
  212. package/entity/EntityValidator.js +0 -150
  213. package/metadata/ReflectMetadataProvider.d.ts +0 -8
  214. package/metadata/ReflectMetadataProvider.js +0 -44
  215. package/utils/resolveContextProvider.d.ts +0 -10
  216. package/utils/resolveContextProvider.js +0 -28
@@ -1,4 +1,4 @@
1
- import type { ConnectionType, EntityData, EntityMetadata, EntityProperty, FilterQuery, Primary, Dictionary, QBFilterQuery, IPrimaryKey, PopulateOptions, EntityDictionary, AutoPath, ObjectQuery, FilterObject, Populate } from '../typings.js';
1
+ import type { ConnectionType, Constructor, EntityData, EntityMetadata, EntityProperty, FilterQuery, Primary, Dictionary, IPrimaryKey, PopulateOptions, EntityDictionary, AutoPath, ObjectQuery, FilterObject, Populate, EntityName, PopulateHintOptions, Prefixes } from '../typings.js';
2
2
  import type { Connection, QueryResult, Transaction } from '../connections/Connection.js';
3
3
  import type { FlushMode, LockMode, QueryOrderMap, QueryFlag, LoadStrategy, PopulateHint, PopulatePath } from '../enums.js';
4
4
  import type { Platform } from '../platforms/Platform.js';
@@ -7,34 +7,40 @@ import type { Collection } from '../entity/Collection.js';
7
7
  import type { EntityManager } from '../EntityManager.js';
8
8
  import type { DriverException } from '../exceptions.js';
9
9
  import type { Configuration } from '../utils/Configuration.js';
10
+ import type { MikroORM } from '../MikroORM.js';
10
11
  import type { LoggingOptions, LogContext } from '../logging/Logger.js';
11
- import type { RawQueryFragment } from '../utils/RawQueryFragment.js';
12
+ import type { Raw } from '../utils/RawQueryFragment.js';
12
13
  export declare const EntityManagerType: unique symbol;
13
14
  export interface IDatabaseDriver<C extends Connection = Connection> {
14
15
  [EntityManagerType]: EntityManager<this>;
15
16
  readonly config: Configuration;
16
17
  createEntityManager(useContext?: boolean): this[typeof EntityManagerType];
17
- connect(): Promise<C>;
18
+ connect(options?: {
19
+ skipOnConnect?: boolean;
20
+ }): Promise<C>;
18
21
  close(force?: boolean): Promise<void>;
19
- reconnect(): Promise<C>;
22
+ reconnect(options?: {
23
+ skipOnConnect?: boolean;
24
+ }): Promise<C>;
20
25
  getConnection(type?: ConnectionType): C;
21
26
  /**
22
27
  * Finds selection of entities
23
28
  */
24
- find<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName: string, where: FilterQuery<T>, options?: FindOptions<T, P, F, E>): Promise<EntityData<T>[]>;
29
+ find<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: FindOptions<T, P, F, E>): Promise<EntityData<T>[]>;
25
30
  /**
26
31
  * Finds single entity (table row, document)
27
32
  */
28
- findOne<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName: string, where: FilterQuery<T>, options?: FindOneOptions<T, P, F, E>): Promise<EntityData<T> | null>;
29
- findVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
30
- nativeInsert<T extends object>(entityName: string, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
31
- nativeInsertMany<T extends object>(entityName: string, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>, transform?: (sql: string) => string): Promise<QueryResult<T>>;
32
- nativeUpdate<T extends object>(entityName: string, where: FilterQuery<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
33
- nativeUpdateMany<T extends object>(entityName: string, where: FilterQuery<T>[], data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
34
- nativeDelete<T extends object>(entityName: string, where: FilterQuery<T>, options?: NativeDeleteOptions<T>): Promise<QueryResult<T>>;
33
+ findOne<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: FindOneOptions<T, P, F, E>): Promise<EntityData<T> | null>;
34
+ findVirtual<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
35
+ stream<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: StreamOptions<T>): AsyncIterableIterator<T>;
36
+ nativeInsert<T extends object>(entityName: EntityName<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
37
+ nativeInsertMany<T extends object>(entityName: EntityName<T>, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>, transform?: (sql: string) => string): Promise<QueryResult<T>>;
38
+ nativeUpdate<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
39
+ nativeUpdateMany<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>[], data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
40
+ nativeDelete<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options?: NativeDeleteOptions<T>): Promise<QueryResult<T>>;
35
41
  syncCollections<T extends object, O extends object>(collections: Iterable<Collection<T, O>>, options?: DriverMethodOptions): Promise<void>;
36
- count<T extends object, P extends string = never>(entityName: string, where: FilterQuery<T>, options?: CountOptions<T, P>): Promise<number>;
37
- aggregate(entityName: string, pipeline: any[]): Promise<any[]>;
42
+ count<T extends object, P extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: CountOptions<T, P>): Promise<number>;
43
+ aggregate(entityName: EntityName, pipeline: any[]): Promise<any[]>;
38
44
  mapResult<T extends object>(result: EntityDictionary<T>, meta: EntityMetadata<T>, populate?: PopulateOptions<T>[]): EntityData<T> | null;
39
45
  /**
40
46
  * When driver uses pivot tables for M:N, this method will load identifiers for given collections from them
@@ -60,6 +66,10 @@ export interface IDatabaseDriver<C extends Connection = Connection> {
60
66
  schema?: string;
61
67
  parentSchema?: string;
62
68
  }): string | undefined;
69
+ /**
70
+ * @internal
71
+ */
72
+ getORMClass(): Constructor<MikroORM>;
63
73
  }
64
74
  export type EntityField<T, P extends string = PopulatePath.ALL> = keyof T | PopulatePath.ALL | AutoPath<T, P, `${PopulatePath.ALL}`>;
65
75
  export type OrderDefinition<T> = (QueryOrderMap<T> & {
@@ -68,6 +78,18 @@ export type OrderDefinition<T> = (QueryOrderMap<T> & {
68
78
  export interface FindAllOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends FindOptions<T, P, F, E> {
69
79
  where?: FilterQuery<T>;
70
80
  }
81
+ export interface StreamOptions<Entity, Populate extends string = never, Fields extends string = '*', Exclude extends string = never> extends Omit<FindAllOptions<Entity, Populate, Fields, Exclude>, 'cache' | 'before' | 'after' | 'first' | 'last' | 'overfetch' | 'strategy'> {
82
+ /**
83
+ * When populating to-many relations, the ORM streams fully merged entities instead of yielding every row.
84
+ * You can opt out of this behavior by specifying `mergeResults: false`. This will yield every row from
85
+ * the SQL result, but still mapped to entities, meaning that to-many collections will contain at most
86
+ * a single item, and you will get duplicate root entities when they have multiple items in the populated
87
+ * collection.
88
+ *
89
+ * @default true
90
+ */
91
+ mergeResults?: boolean;
92
+ }
71
93
  export type FilterOptions = Dictionary<boolean | Dictionary> | string[] | boolean;
72
94
  export interface LoadHint<Entity, Hint extends string = never, Fields extends string = PopulatePath.ALL, Excludes extends string = never> {
73
95
  populate?: Populate<Entity, Hint>;
@@ -88,8 +110,28 @@ export interface FindOptions<Entity, Hint extends string = never, Fields extends
88
110
  * when nesting the condition. This is used for implementation of joined filters.
89
111
  */
90
112
  populateFilter?: ObjectQuery<Entity>;
113
+ /**
114
+ * Index-friendly alternative to `$or` for conditions that span joined relations.
115
+ * Each array element becomes an independent branch combined via `UNION ALL` subquery:
116
+ * `WHERE pk IN (branch_1 UNION ALL branch_2 ... branch_N)`.
117
+ * The database plans each branch independently, enabling per-table index usage
118
+ * (e.g. GIN trigram indexes for fuzzy search across related entities).
119
+ * sql only
120
+ */
121
+ unionWhere?: ObjectQuery<Entity>[];
122
+ /**
123
+ * Strategy for combining `unionWhere` branches.
124
+ * - `'union-all'` (default) — skips deduplication, faster for most use cases.
125
+ * - `'union'` — deduplicates rows between branches; useful when branch overlap is very high.
126
+ * sql only
127
+ */
128
+ unionWhereStrategy?: 'union-all' | 'union';
91
129
  /** Used for ordering of the populate queries. If not specified, the value of `options.orderBy` is used. */
92
130
  populateOrderBy?: OrderDefinition<Entity>;
131
+ /** Per-relation overrides for populate loading behavior. Keys are populate paths (same as used in `populate`). */
132
+ populateHints?: [Hint] extends [never] ? never : {
133
+ [K in Prefixes<Hint>]?: PopulateHintOptions;
134
+ };
93
135
  /** Ordering of the results.Can be an object or array of objects, keys are property names, values are ordering (asc/desc) */
94
136
  orderBy?: OrderDefinition<Entity>;
95
137
  /** Control result caching for this query. Result cache is by default disabled, not to be confused with the identity map. */
@@ -127,7 +169,7 @@ export interface FindOptions<Entity, Hint extends string = never, Fields extends
127
169
  flags?: QueryFlag[];
128
170
  /** sql only */
129
171
  groupBy?: string | string[];
130
- having?: QBFilterQuery<Entity>;
172
+ having?: FilterQuery<Entity>;
131
173
  /** sql only */
132
174
  strategy?: LoadStrategy | `${LoadStrategy}`;
133
175
  flushMode?: FlushMode | `${FlushMode}`;
@@ -138,21 +180,27 @@ export interface FindOptions<Entity, Hint extends string = never, Fields extends
138
180
  lockTableAliases?: string[];
139
181
  ctx?: Transaction;
140
182
  connectionType?: ConnectionType;
141
- /** sql only */
142
- indexHint?: string;
183
+ /** SQL: appended to FROM clause (e.g. `'force index(my_index)'`); MongoDB: index name or spec passed as `hint`. */
184
+ indexHint?: string | Dictionary;
143
185
  /** sql only */
144
186
  comments?: string | string[];
145
187
  /** sql only */
146
188
  hintComments?: string | string[];
189
+ /** SQL: collation name string applied as COLLATE to ORDER BY; MongoDB: CollationOptions object. */
190
+ collation?: CollationOptions | string;
191
+ /** mongodb only */
192
+ maxTimeMS?: number;
193
+ /** mongodb only */
194
+ allowDiskUse?: boolean;
147
195
  loggerContext?: LogContext;
148
196
  logging?: LoggingOptions;
149
197
  /** @internal used to apply filters to the auto-joined relations */
150
198
  em?: EntityManager;
151
199
  }
152
- export interface FindByCursorOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never, I extends boolean = true> extends Omit<FindOptions<T, P, F, E>, 'limit' | 'offset'> {
200
+ export interface FindByCursorOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never, I extends boolean = true> extends Omit<FindAllOptions<T, P, F, E>, 'limit' | 'offset'> {
153
201
  includeCount?: I;
154
202
  }
155
- export interface FindOneOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never> extends Omit<FindOptions<T, P, F, E>, 'limit' | 'lockMode'> {
203
+ export interface FindOneOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends Omit<FindOptions<T, P, F, E>, 'limit' | 'lockMode'> {
156
204
  lockMode?: LockMode;
157
205
  lockVersion?: number | Date;
158
206
  }
@@ -167,15 +215,23 @@ export interface NativeInsertUpdateOptions<T> {
167
215
  /** `nativeUpdate()` only option */
168
216
  upsert?: boolean;
169
217
  loggerContext?: LogContext;
218
+ /** sql only */
219
+ unionWhere?: ObjectQuery<T>[];
220
+ /** sql only */
221
+ unionWhereStrategy?: 'union-all' | 'union';
222
+ filters?: FilterOptions;
223
+ /** @internal */
224
+ em?: EntityManager;
170
225
  }
171
226
  export interface NativeInsertUpdateManyOptions<T> extends NativeInsertUpdateOptions<T> {
172
227
  processCollections?: boolean;
173
228
  }
174
229
  export interface UpsertOptions<Entity, Fields extends string = never> extends Omit<NativeInsertUpdateOptions<Entity>, 'upsert'> {
175
- onConflictFields?: (keyof Entity)[] | RawQueryFragment;
230
+ onConflictFields?: (keyof Entity)[] | Raw;
176
231
  onConflictAction?: 'ignore' | 'merge';
177
232
  onConflictMergeFields?: AutoPath<Entity, Fields, `${PopulatePath.ALL}`>[];
178
233
  onConflictExcludeFields?: AutoPath<Entity, Fields, `${PopulatePath.ALL}`>[];
234
+ onConflictWhere?: FilterQuery<Entity>;
179
235
  disableIdentityMap?: boolean;
180
236
  }
181
237
  export interface UpsertManyOptions<Entity, Fields extends string = never> extends UpsertOptions<Entity, Fields> {
@@ -185,20 +241,28 @@ export interface CountOptions<T extends object, P extends string = never> {
185
241
  filters?: FilterOptions;
186
242
  schema?: string;
187
243
  groupBy?: string | readonly string[];
188
- having?: QBFilterQuery<T>;
244
+ having?: FilterQuery<T>;
189
245
  cache?: boolean | number | [string, number];
190
246
  populate?: Populate<T, P>;
191
247
  populateWhere?: ObjectQuery<T> | PopulateHint | `${PopulateHint}`;
192
248
  populateFilter?: ObjectQuery<T>;
249
+ /** @see FindOptions.unionWhere */
250
+ unionWhere?: ObjectQuery<T>[];
251
+ /** @see FindOptions.unionWhereStrategy */
252
+ unionWhereStrategy?: 'union-all' | 'union';
193
253
  ctx?: Transaction;
194
254
  connectionType?: ConnectionType;
195
255
  flushMode?: FlushMode | `${FlushMode}`;
196
- /** sql only */
197
- indexHint?: string;
256
+ /** SQL: appended to FROM clause (e.g. `'force index(my_index)'`); MongoDB: index name or spec passed as `hint`. */
257
+ indexHint?: string | Dictionary;
198
258
  /** sql only */
199
259
  comments?: string | string[];
200
260
  /** sql only */
201
261
  hintComments?: string | string[];
262
+ /** SQL: collation name string applied as COLLATE; MongoDB: CollationOptions object. */
263
+ collation?: CollationOptions | string;
264
+ /** mongodb only */
265
+ maxTimeMS?: number;
202
266
  loggerContext?: LogContext;
203
267
  logging?: LoggingOptions;
204
268
  /** @internal used to apply filters to the auto-joined relations */
@@ -208,12 +272,28 @@ export interface UpdateOptions<T> {
208
272
  filters?: FilterOptions;
209
273
  schema?: string;
210
274
  ctx?: Transaction;
275
+ /** sql only */
276
+ unionWhere?: ObjectQuery<T>[];
277
+ /** sql only */
278
+ unionWhereStrategy?: 'union-all' | 'union';
211
279
  }
212
280
  export interface DeleteOptions<T> extends DriverMethodOptions {
213
281
  filters?: FilterOptions;
282
+ /** sql only */
283
+ unionWhere?: ObjectQuery<T>[];
284
+ /** sql only */
285
+ unionWhereStrategy?: 'union-all' | 'union';
286
+ /** @internal */
287
+ em?: EntityManager;
214
288
  }
215
289
  export interface NativeDeleteOptions<T> extends DriverMethodOptions {
216
290
  filters?: FilterOptions;
291
+ /** sql only */
292
+ unionWhere?: ObjectQuery<T>[];
293
+ /** sql only */
294
+ unionWhereStrategy?: 'union-all' | 'union';
295
+ /** @internal */
296
+ em?: EntityManager;
217
297
  }
218
298
  export interface LockOptions extends DriverMethodOptions {
219
299
  lockMode?: LockMode;
@@ -226,8 +306,23 @@ export interface DriverMethodOptions {
226
306
  schema?: string;
227
307
  loggerContext?: LogContext;
228
308
  }
309
+ export interface CollationOptions {
310
+ locale: string;
311
+ caseLevel?: boolean;
312
+ caseFirst?: string;
313
+ strength?: number;
314
+ numericOrdering?: boolean;
315
+ alternate?: string;
316
+ maxVariable?: string;
317
+ backwards?: boolean;
318
+ }
229
319
  export interface GetReferenceOptions {
230
320
  wrapped?: boolean;
231
321
  convertCustomTypes?: boolean;
232
322
  schema?: string;
323
+ /**
324
+ * Property name to use for identity map lookup instead of the primary key.
325
+ * This is useful for creating references by unique non-PK properties.
326
+ */
327
+ key?: string;
233
328
  }
@@ -1,20 +1,79 @@
1
1
  import { type Ref } from './Reference.js';
2
- import type { AutoPath, EntityData, EntityDTO, Loaded, LoadedReference, AddEager, EntityKey, FromEntityType, IsSubset, MergeSelected } from '../typings.js';
2
+ import type { AutoPath, EntityData, EntityDTO, Loaded, LoadedReference, AddEager, EntityKey, FromEntityType, IsSubset, MergeSelected, SerializeDTO } from '../typings.js';
3
3
  import { type AssignOptions } from './EntityAssigner.js';
4
4
  import type { EntityLoaderOptions } from './EntityLoader.js';
5
5
  import { type SerializeOptions } from '../serialization/EntitySerializer.js';
6
6
  import type { FindOneOptions } from '../drivers/IDatabaseDriver.js';
7
+ import type { PopulatePath } from '../enums.js';
7
8
  export declare abstract class BaseEntity {
8
9
  isInitialized(): boolean;
9
- isTouched(): boolean;
10
10
  populated(populated?: boolean): void;
11
- populate<Entity extends this = this, Hint extends string = never>(populate: AutoPath<Entity, Hint>[] | false, options?: EntityLoaderOptions<Entity>): Promise<Loaded<Entity, Hint>>;
11
+ populate<Entity extends this = this, Hint extends string = never, Fields extends string = never>(populate: AutoPath<Entity, Hint, PopulatePath.ALL>[] | false, options?: EntityLoaderOptions<Entity, Fields>): Promise<Loaded<Entity, Hint>>;
12
12
  toReference<Entity extends this = this>(): Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>>;
13
+ /**
14
+ * Converts the entity to a plain object representation.
15
+ *
16
+ * **Note on typing with `Loaded` entities:** When called on a `Loaded<Entity, 'relation'>` type,
17
+ * the return type will be `EntityDTO<Entity>` (with relations as primary keys), not
18
+ * `EntityDTO<Loaded<Entity, 'relation'>>` (with loaded relations as nested objects).
19
+ * This is a TypeScript limitation - the `this` type resolves to the class, not the `Loaded` wrapper.
20
+ *
21
+ * For correct typing that reflects loaded relations, use `wrap()`:
22
+ * ```ts
23
+ * const result = await em.find(User, {}, { populate: ['profile'] });
24
+ * // Type: EntityDTO<User> (profile is number)
25
+ * const obj1 = result[0].toObject();
26
+ * // Type: EntityDTO<Loaded<User, 'profile'>> (profile is nested object)
27
+ * const obj2 = wrap(result[0]).toObject();
28
+ * ```
29
+ *
30
+ * Runtime values are correct in both cases - only the static types differ.
31
+ */
13
32
  toObject<Entity extends this = this>(): EntityDTO<Entity>;
33
+ /**
34
+ * Converts the entity to a plain object representation.
35
+ *
36
+ * **Note on typing with `Loaded` entities:** When called on a `Loaded<Entity, 'relation'>` type,
37
+ * the return type will be `EntityDTO<Entity>` (with relations as primary keys), not
38
+ * `EntityDTO<Loaded<Entity, 'relation'>>` (with loaded relations as nested objects).
39
+ * This is a TypeScript limitation - the `this` type resolves to the class, not the `Loaded` wrapper.
40
+ *
41
+ * For correct typing that reflects loaded relations, use `wrap()`:
42
+ * ```ts
43
+ * const result = await em.find(User, {}, { populate: ['profile'] });
44
+ * // Type: EntityDTO<User> (profile is number)
45
+ * const obj1 = result[0].toObject();
46
+ * // Type: EntityDTO<Loaded<User, 'profile'>> (profile is nested object)
47
+ * const obj2 = wrap(result[0]).toObject();
48
+ * ```
49
+ *
50
+ * Runtime values are correct in both cases - only the static types differ.
51
+ */
14
52
  toObject<Entity extends this = this>(ignoreFields: never[]): EntityDTO<Entity>;
53
+ /**
54
+ * Converts the entity to a plain object representation.
55
+ *
56
+ * **Note on typing with `Loaded` entities:** When called on a `Loaded<Entity, 'relation'>` type,
57
+ * the return type will be `EntityDTO<Entity>` (with relations as primary keys), not
58
+ * `EntityDTO<Loaded<Entity, 'relation'>>` (with loaded relations as nested objects).
59
+ * This is a TypeScript limitation - the `this` type resolves to the class, not the `Loaded` wrapper.
60
+ *
61
+ * For correct typing that reflects loaded relations, use `wrap()`:
62
+ * ```ts
63
+ * const result = await em.find(User, {}, { populate: ['profile'] });
64
+ * // Type: EntityDTO<User> (profile is number)
65
+ * const obj1 = result[0].toObject();
66
+ * // Type: EntityDTO<Loaded<User, 'profile'>> (profile is nested object)
67
+ * const obj2 = wrap(result[0]).toObject();
68
+ * ```
69
+ *
70
+ * Runtime values are correct in both cases - only the static types differ.
71
+ *
72
+ * @param ignoreFields - Array of field names to omit from the result.
73
+ */
15
74
  toObject<Entity extends this = this, Ignored extends EntityKey<Entity> = never>(ignoreFields: Ignored[]): Omit<EntityDTO<Entity>, Ignored>;
16
75
  toPOJO<Entity extends this = this>(): EntityDTO<Entity>;
17
- serialize<Entity extends this = this, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Hint extends string = never, Exclude extends string = never>(options?: SerializeOptions<Naked, Hint, Exclude>): EntityDTO<Loaded<Naked, Hint>>;
76
+ serialize<Entity extends this = this, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Hint extends string = never, Exclude extends string = never>(options?: SerializeOptions<Naked, Hint, Exclude>): SerializeDTO<Naked, Hint, Exclude>;
18
77
  assign<Entity extends this, 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>;
19
78
  init<Entity extends this = this, Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(options?: FindOneOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes> | null>;
20
79
  getSchema(): string | undefined;
@@ -6,9 +6,6 @@ export class BaseEntity {
6
6
  isInitialized() {
7
7
  return helper(this).__initialized;
8
8
  }
9
- isTouched() {
10
- return helper(this).__touched;
11
- }
12
9
  populated(populated = true) {
13
10
  helper(this).populated(populated);
14
11
  }
@@ -1,5 +1,4 @@
1
- import type { EntityDTO, EntityKey, FilterQuery, Loaded, LoadedCollection, Populate } from '../typings.js';
2
- import { ArrayCollection } from './ArrayCollection.js';
1
+ import type { EntityDTO, EntityKey, EntityProperty, FilterQuery, IPrimaryKey, Loaded, LoadedCollection, Populate, Primary } from '../typings.js';
3
2
  import { Reference } from './Reference.js';
4
3
  import type { Transaction } from '../connections/Connection.js';
5
4
  import type { CountOptions, FindOptions } from '../drivers/IDatabaseDriver.js';
@@ -9,10 +8,18 @@ export interface MatchingOptions<T extends object, P extends string = never> ext
9
8
  store?: boolean;
10
9
  ctx?: Transaction;
11
10
  }
12
- export declare class Collection<T extends object, O extends object = object> extends ArrayCollection<T, O> {
11
+ export declare class Collection<T extends object, O extends object = object> {
12
+ readonly owner: O;
13
+ [k: number]: T;
14
+ private readonly items;
15
+ private initialized;
16
+ private dirty;
17
+ private partial;
18
+ private snapshot;
13
19
  private readonly?;
20
+ private _count?;
21
+ private _property?;
14
22
  private _populated?;
15
- private _snapshot?;
16
23
  constructor(owner: O, items?: T[], initialized?: boolean);
17
24
  /**
18
25
  * Creates new Collection instance, assigns it to the owning entity and sets the items to it (propagating them to their inverse sides)
@@ -39,59 +46,124 @@ export declare class Collection<T extends object, O extends object = object> ext
39
46
  */
40
47
  getItems(check?: boolean): T[];
41
48
  toJSON<TT extends T>(): EntityDTO<TT>[];
42
- add<TT extends T>(entity: TT | Reference<TT> | Iterable<TT | Reference<TT>>, ...entities: (TT | Reference<TT>)[]): void;
49
+ add<TT extends T>(entity: TT | Reference<TT> | Iterable<TT | Reference<TT>>, ...entities: (TT | Reference<TT>)[]): number;
43
50
  /**
44
- * @inheritDoc
51
+ * Remove specified item(s) from the collection. Note that removing item from collection does not necessarily imply deleting the target entity,
52
+ * it means we are disconnecting the relation - removing items from collection, not removing entities from database - `Collection.remove()`
53
+ * is not the same as `em.remove()`. If we want to delete the entity by removing it from collection, we need to enable `orphanRemoval: true`,
54
+ * which tells the ORM we don't want orphaned entities to exist, so we know those should be removed.
45
55
  */
46
- remove<TT extends T>(entity: TT | Reference<TT> | Iterable<TT | Reference<TT>> | ((item: TT) => boolean), ...entities: (TT | Reference<TT>)[]): void;
56
+ remove<TT extends T>(entity: TT | Reference<TT> | Iterable<TT | Reference<TT>> | ((item: TT) => boolean), ...entities: (TT | Reference<TT>)[]): number;
47
57
  contains<TT extends T>(item: TT | Reference<TT>, check?: boolean): boolean;
48
58
  count(): number;
49
59
  isEmpty(): boolean;
60
+ shouldPopulate(populated?: boolean): boolean;
61
+ populated(populated?: boolean | undefined): void;
62
+ init<TT extends T, P extends string = never>(options?: InitCollectionOptions<TT, P>): Promise<LoadedCollection<Loaded<TT, P>>>;
63
+ private getEntityManager;
64
+ private createCondition;
65
+ private createManyToManyCondition;
66
+ private createLoadCountCondition;
67
+ private checkInitialized;
50
68
  /**
51
- * @inheritDoc
69
+ * re-orders items after searching with `$in` operator
70
+ */
71
+ private reorderItems;
72
+ private cancelOrphanRemoval;
73
+ private validateModification;
74
+ toArray<TT extends T>(): EntityDTO<TT>[];
75
+ getIdentifiers<U extends IPrimaryKey = Primary<T> & IPrimaryKey>(field?: string | string[]): U[];
76
+ /**
77
+ * @internal
78
+ */
79
+ addWithoutPropagation(entity: T): void;
80
+ set(items: Iterable<T | Reference<T>>): void;
81
+ private compare;
82
+ /**
83
+ * @internal
84
+ */
85
+ hydrate(items: T[], forcePropagate?: boolean, partial?: boolean): void;
86
+ /**
87
+ * Remove all items from the collection. Note that removing items from collection does not necessarily imply deleting the target entity,
88
+ * it means we are disconnecting the relation - removing items from collection, not removing entities from database - `Collection.remove()`
89
+ * is not the same as `em.remove()`. If we want to delete the entity by removing it from collection, we need to enable `orphanRemoval: true`,
90
+ * which tells the ORM we don't want orphaned entities to exist, so we know those should be removed.
91
+ */
92
+ removeAll(): void;
93
+ /**
94
+ * @internal
95
+ */
96
+ removeWithoutPropagation(entity: T): void;
97
+ /**
98
+ * Extracts a slice of the collection items starting at position start to end (exclusive) of the collection.
99
+ * If end is null it returns all elements from start to the end of the collection.
52
100
  */
53
101
  slice(start?: number, end?: number): T[];
54
102
  /**
55
- * @inheritDoc
103
+ * Tests for the existence of an element that satisfies the given predicate.
56
104
  */
57
105
  exists(cb: (item: T) => boolean): boolean;
58
106
  /**
59
- * @inheritDoc
107
+ * Returns the first element of this collection that satisfies the predicate.
108
+ */
109
+ find<S extends T>(cb: (item: T, index: number) => item is S): S | undefined;
110
+ /**
111
+ * Returns the first element of this collection that satisfies the predicate.
60
112
  */
61
113
  find(cb: (item: T, index: number) => boolean): T | undefined;
62
114
  /**
63
- * @inheritDoc
115
+ * Extracts a subset of the collection items.
116
+ */
117
+ filter<S extends T>(cb: (item: T, index: number) => item is S): S[];
118
+ /**
119
+ * Extracts a subset of the collection items.
64
120
  */
65
121
  filter(cb: (item: T, index: number) => boolean): T[];
66
122
  /**
67
- * @inheritDoc
123
+ * Maps the collection items based on your provided mapper function.
68
124
  */
69
125
  map<R>(mapper: (item: T, index: number) => R): R[];
70
126
  /**
71
- * @inheritDoc
127
+ * Maps the collection items based on your provided mapper function to a single object.
128
+ */
129
+ reduce<R>(cb: (obj: R, item: T, index: number) => R, initial?: R): R;
130
+ /**
131
+ * Maps the collection items to a dictionary, indexed by the key you specify.
132
+ * If there are more items with the same key, only the first one will be present.
72
133
  */
73
134
  indexBy<K1 extends keyof T, K2 extends keyof T = never>(key: K1): Record<T[K1] & PropertyKey, T>;
74
135
  /**
75
- * @inheritDoc
136
+ * Maps the collection items to a dictionary, indexed by the key you specify.
137
+ * If there are more items with the same key, only the first one will be present.
76
138
  */
77
139
  indexBy<K1 extends keyof T, K2 extends keyof T = never>(key: K1, valueKey: K2): Record<T[K1] & PropertyKey, T[K2]>;
78
- shouldPopulate(populated?: boolean): boolean;
79
- populated(populated?: boolean | undefined): void;
80
- init<TT extends T, P extends string = never>(options?: InitCollectionOptions<TT, P>): Promise<LoadedCollection<Loaded<TT, P>>>;
81
- private getEntityManager;
82
- private createCondition;
83
- private createOrderBy;
84
- private createManyToManyCondition;
85
- private createLoadCountCondition;
86
- private modify;
87
- private checkInitialized;
140
+ isInitialized(fully?: boolean): boolean;
141
+ isDirty(): boolean;
142
+ isPartial(): boolean;
143
+ setDirty(dirty?: boolean): void;
144
+ get length(): number;
145
+ [Symbol.iterator](): IterableIterator<T>;
88
146
  /**
89
- * re-orders items after searching with `$in` operator
147
+ * @internal
90
148
  */
91
- private reorderItems;
92
- private cancelOrphanRemoval;
93
- private validateItemType;
94
- private validateModification;
149
+ takeSnapshot(forcePropagate?: boolean): void;
150
+ /**
151
+ * @internal
152
+ */
153
+ getSnapshot(): T[] | undefined;
154
+ /**
155
+ * @internal
156
+ */
157
+ get property(): EntityProperty;
158
+ /**
159
+ * @internal
160
+ */
161
+ set property(prop: EntityProperty);
162
+ protected propagate(item: T, method: 'add' | 'remove' | 'takeSnapshot'): void;
163
+ protected propagateToInverseSide(item: T, method: 'add' | 'remove' | 'takeSnapshot'): void;
164
+ protected propagateToOwningSide(item: T, method: 'add' | 'remove' | 'takeSnapshot'): void;
165
+ protected shouldPropagateToCollection(collection: Collection<O, T>, method: 'add' | 'remove' | 'takeSnapshot'): boolean;
166
+ private incrementCount;
95
167
  }
96
168
  export interface InitCollectionOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends EntityLoaderOptions<T, F, E> {
97
169
  dataloader?: boolean;