@mikro-orm/core 7.0.0-dev.9 → 7.0.0-dev.90

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 (191) hide show
  1. package/EntityManager.d.ts +77 -48
  2. package/EntityManager.js +288 -225
  3. package/MikroORM.d.ts +40 -31
  4. package/MikroORM.js +98 -137
  5. package/README.md +3 -2
  6. package/cache/FileCacheAdapter.d.ts +1 -1
  7. package/cache/FileCacheAdapter.js +6 -5
  8. package/cache/GeneratedCacheAdapter.d.ts +0 -1
  9. package/cache/GeneratedCacheAdapter.js +0 -2
  10. package/cache/index.d.ts +0 -1
  11. package/cache/index.js +0 -1
  12. package/connections/Connection.d.ts +11 -7
  13. package/connections/Connection.js +16 -14
  14. package/drivers/DatabaseDriver.d.ts +11 -5
  15. package/drivers/DatabaseDriver.js +23 -11
  16. package/drivers/IDatabaseDriver.d.ts +25 -4
  17. package/entity/BaseEntity.d.ts +0 -1
  18. package/entity/BaseEntity.js +0 -3
  19. package/entity/Collection.d.ts +95 -30
  20. package/entity/Collection.js +432 -93
  21. package/entity/EntityAssigner.d.ts +1 -1
  22. package/entity/EntityAssigner.js +17 -9
  23. package/entity/EntityFactory.d.ts +7 -0
  24. package/entity/EntityFactory.js +63 -41
  25. package/entity/EntityHelper.js +26 -12
  26. package/entity/EntityLoader.d.ts +5 -4
  27. package/entity/EntityLoader.js +63 -38
  28. package/entity/EntityRepository.d.ts +1 -1
  29. package/entity/Reference.d.ts +6 -5
  30. package/entity/Reference.js +34 -9
  31. package/entity/WrappedEntity.d.ts +2 -7
  32. package/entity/WrappedEntity.js +2 -7
  33. package/entity/defineEntity.d.ts +568 -0
  34. package/entity/defineEntity.js +529 -0
  35. package/entity/index.d.ts +3 -2
  36. package/entity/index.js +3 -2
  37. package/entity/utils.d.ts +7 -0
  38. package/entity/utils.js +16 -4
  39. package/entity/validators.d.ts +11 -0
  40. package/entity/validators.js +65 -0
  41. package/enums.d.ts +21 -6
  42. package/enums.js +14 -1
  43. package/errors.d.ts +10 -2
  44. package/errors.js +29 -10
  45. package/events/EventManager.d.ts +2 -1
  46. package/events/EventManager.js +19 -11
  47. package/events/EventSubscriber.d.ts +3 -1
  48. package/hydration/Hydrator.js +1 -2
  49. package/hydration/ObjectHydrator.d.ts +4 -4
  50. package/hydration/ObjectHydrator.js +35 -25
  51. package/index.d.ts +2 -2
  52. package/index.js +1 -2
  53. package/logging/DefaultLogger.d.ts +1 -1
  54. package/logging/DefaultLogger.js +1 -0
  55. package/logging/SimpleLogger.d.ts +1 -1
  56. package/logging/index.d.ts +1 -0
  57. package/logging/index.js +1 -0
  58. package/logging/inspect.d.ts +2 -0
  59. package/logging/inspect.js +16 -0
  60. package/metadata/EntitySchema.d.ts +9 -13
  61. package/metadata/EntitySchema.js +44 -26
  62. package/metadata/MetadataDiscovery.d.ts +6 -9
  63. package/metadata/MetadataDiscovery.js +165 -205
  64. package/metadata/MetadataProvider.d.ts +11 -2
  65. package/metadata/MetadataProvider.js +44 -2
  66. package/metadata/MetadataStorage.d.ts +1 -6
  67. package/metadata/MetadataStorage.js +6 -18
  68. package/metadata/MetadataValidator.d.ts +0 -7
  69. package/metadata/MetadataValidator.js +0 -10
  70. package/metadata/discover-entities.d.ts +5 -0
  71. package/metadata/discover-entities.js +40 -0
  72. package/metadata/index.d.ts +1 -1
  73. package/metadata/index.js +1 -1
  74. package/metadata/types.d.ts +480 -0
  75. package/metadata/types.js +1 -0
  76. package/naming-strategy/AbstractNamingStrategy.d.ts +5 -1
  77. package/naming-strategy/AbstractNamingStrategy.js +8 -2
  78. package/naming-strategy/NamingStrategy.d.ts +11 -1
  79. package/not-supported.d.ts +2 -0
  80. package/not-supported.js +4 -0
  81. package/package.json +18 -10
  82. package/platforms/ExceptionConverter.js +1 -1
  83. package/platforms/Platform.d.ts +6 -13
  84. package/platforms/Platform.js +15 -41
  85. package/serialization/EntitySerializer.d.ts +2 -0
  86. package/serialization/EntitySerializer.js +32 -14
  87. package/serialization/EntityTransformer.js +22 -12
  88. package/serialization/SerializationContext.js +16 -13
  89. package/types/ArrayType.d.ts +1 -1
  90. package/types/ArrayType.js +2 -3
  91. package/types/BigIntType.d.ts +8 -6
  92. package/types/BigIntType.js +1 -1
  93. package/types/BlobType.d.ts +0 -1
  94. package/types/BlobType.js +0 -3
  95. package/types/BooleanType.d.ts +2 -1
  96. package/types/BooleanType.js +3 -0
  97. package/types/DecimalType.d.ts +6 -4
  98. package/types/DecimalType.js +3 -3
  99. package/types/DoubleType.js +2 -2
  100. package/types/EnumArrayType.js +1 -2
  101. package/types/JsonType.d.ts +1 -1
  102. package/types/JsonType.js +7 -2
  103. package/types/TinyIntType.js +1 -1
  104. package/types/Type.d.ts +2 -4
  105. package/types/Type.js +3 -3
  106. package/types/Uint8ArrayType.d.ts +0 -1
  107. package/types/Uint8ArrayType.js +1 -4
  108. package/types/index.d.ts +1 -1
  109. package/typings.d.ts +109 -73
  110. package/typings.js +38 -35
  111. package/unit-of-work/ChangeSet.d.ts +0 -3
  112. package/unit-of-work/ChangeSet.js +2 -2
  113. package/unit-of-work/ChangeSetComputer.d.ts +1 -3
  114. package/unit-of-work/ChangeSetComputer.js +11 -9
  115. package/unit-of-work/ChangeSetPersister.d.ts +5 -4
  116. package/unit-of-work/ChangeSetPersister.js +51 -19
  117. package/unit-of-work/UnitOfWork.d.ts +8 -1
  118. package/unit-of-work/UnitOfWork.js +91 -49
  119. package/utils/AbstractSchemaGenerator.d.ts +5 -5
  120. package/utils/AbstractSchemaGenerator.js +11 -9
  121. package/utils/Configuration.d.ts +757 -206
  122. package/utils/Configuration.js +140 -188
  123. package/utils/ConfigurationLoader.d.ts +1 -54
  124. package/utils/ConfigurationLoader.js +1 -352
  125. package/utils/Cursor.d.ts +0 -3
  126. package/utils/Cursor.js +6 -3
  127. package/utils/DataloaderUtils.d.ts +15 -5
  128. package/utils/DataloaderUtils.js +54 -8
  129. package/utils/EntityComparator.d.ts +8 -4
  130. package/utils/EntityComparator.js +52 -17
  131. package/utils/QueryHelper.d.ts +9 -1
  132. package/utils/QueryHelper.js +70 -9
  133. package/utils/RawQueryFragment.d.ts +36 -13
  134. package/utils/RawQueryFragment.js +36 -16
  135. package/utils/TransactionManager.d.ts +65 -0
  136. package/utils/TransactionManager.js +223 -0
  137. package/utils/Utils.d.ts +8 -97
  138. package/utils/Utils.js +82 -302
  139. package/utils/clone.js +2 -3
  140. package/utils/env-vars.d.ts +3 -0
  141. package/utils/env-vars.js +87 -0
  142. package/utils/fs-utils.d.ts +12 -0
  143. package/utils/fs-utils.js +97 -0
  144. package/utils/index.d.ts +2 -1
  145. package/utils/index.js +2 -1
  146. package/utils/upsert-utils.d.ts +7 -2
  147. package/utils/upsert-utils.js +55 -4
  148. package/decorators/Check.d.ts +0 -3
  149. package/decorators/Check.js +0 -13
  150. package/decorators/CreateRequestContext.d.ts +0 -3
  151. package/decorators/CreateRequestContext.js +0 -32
  152. package/decorators/Embeddable.d.ts +0 -8
  153. package/decorators/Embeddable.js +0 -11
  154. package/decorators/Embedded.d.ts +0 -18
  155. package/decorators/Embedded.js +0 -18
  156. package/decorators/Entity.d.ts +0 -18
  157. package/decorators/Entity.js +0 -12
  158. package/decorators/Enum.d.ts +0 -9
  159. package/decorators/Enum.js +0 -16
  160. package/decorators/Filter.d.ts +0 -2
  161. package/decorators/Filter.js +0 -8
  162. package/decorators/Formula.d.ts +0 -4
  163. package/decorators/Formula.js +0 -15
  164. package/decorators/Indexed.d.ts +0 -19
  165. package/decorators/Indexed.js +0 -20
  166. package/decorators/ManyToMany.d.ts +0 -40
  167. package/decorators/ManyToMany.js +0 -14
  168. package/decorators/ManyToOne.d.ts +0 -30
  169. package/decorators/ManyToOne.js +0 -14
  170. package/decorators/OneToMany.d.ts +0 -28
  171. package/decorators/OneToMany.js +0 -17
  172. package/decorators/OneToOne.d.ts +0 -24
  173. package/decorators/OneToOne.js +0 -7
  174. package/decorators/PrimaryKey.d.ts +0 -8
  175. package/decorators/PrimaryKey.js +0 -20
  176. package/decorators/Property.d.ts +0 -250
  177. package/decorators/Property.js +0 -32
  178. package/decorators/Transactional.d.ts +0 -13
  179. package/decorators/Transactional.js +0 -28
  180. package/decorators/hooks.d.ts +0 -16
  181. package/decorators/hooks.js +0 -47
  182. package/decorators/index.d.ts +0 -17
  183. package/decorators/index.js +0 -17
  184. package/entity/ArrayCollection.d.ts +0 -116
  185. package/entity/ArrayCollection.js +0 -402
  186. package/entity/EntityValidator.d.ts +0 -19
  187. package/entity/EntityValidator.js +0 -150
  188. package/metadata/ReflectMetadataProvider.d.ts +0 -8
  189. package/metadata/ReflectMetadataProvider.js +0 -44
  190. package/utils/resolveContextProvider.d.ts +0 -10
  191. package/utils/resolveContextProvider.js +0 -28
@@ -1,4 +1,3 @@
1
- import { URL } from 'node:url';
2
1
  import { Utils } from '../utils/Utils.js';
3
2
  export class Connection {
4
3
  config;
@@ -33,23 +32,35 @@ export class Connection {
33
32
  .forEach(k => delete this.options[k]);
34
33
  }
35
34
  /**
36
- * Ensure the connection exists, this is used to support lazy connect when using `MikroORM.initSync()`
35
+ * Ensure the connection exists, this is used to support lazy connect when using `new MikroORM()` instead of the async `init` method.
37
36
  */
38
37
  async ensureConnection() {
39
38
  if (!this.connected) {
40
39
  await this.connect();
41
40
  }
42
41
  }
42
+ async onConnect() {
43
+ const schemaGenerator = this.config.getExtension('@mikro-orm/schema-generator');
44
+ if (this.type === 'write' && schemaGenerator) {
45
+ if (this.config.get('ensureDatabase')) {
46
+ const options = this.config.get('ensureDatabase');
47
+ await schemaGenerator.ensureDatabase(typeof options === 'boolean' ? {} : { ...options, forceCheck: true });
48
+ }
49
+ if (this.config.get('ensureIndexes')) {
50
+ await schemaGenerator.ensureIndexes();
51
+ }
52
+ }
53
+ }
43
54
  async transactional(cb, options) {
44
55
  throw new Error(`Transactions are not supported by current driver`);
45
56
  }
46
57
  async begin(options) {
47
58
  throw new Error(`Transactions are not supported by current driver`);
48
59
  }
49
- async commit(ctx, eventBroadcaster) {
60
+ async commit(ctx, eventBroadcaster, loggerContext) {
50
61
  throw new Error(`Transactions are not supported by current driver`);
51
62
  }
52
- async rollback(ctx, eventBroadcaster) {
63
+ async rollback(ctx, eventBroadcaster, loggerContext) {
53
64
  throw new Error(`Transactions are not supported by current driver`);
54
65
  }
55
66
  getConnectionOptions() {
@@ -67,7 +78,7 @@ export class Connection {
67
78
  }
68
79
  }
69
80
  else {
70
- const url = new URL(this.config.getClientUrl());
81
+ const url = new URL(this.config.get('clientUrl'));
71
82
  this.options.host = ret.host = this.options.host ?? this.config.get('host', decodeURIComponent(url.hostname));
72
83
  this.options.port = ret.port = this.options.port ?? this.config.get('port', +url.port);
73
84
  this.options.user = ret.user = this.options.user ?? this.config.get('user', decodeURIComponent(url.username));
@@ -76,15 +87,6 @@ export class Connection {
76
87
  }
77
88
  return ret;
78
89
  }
79
- getClientUrl() {
80
- const options = this.getConnectionOptions();
81
- const url = new URL(this.config.getClientUrl(true));
82
- const password = options.password ? ':*****' : '';
83
- const schema = options.schema && options.schema !== this.platform.getDefaultSchemaName()
84
- ? `?schema=${options.schema}`
85
- : '';
86
- return `${url.protocol}//${options.user}${password}@${options.host}:${options.port}${schema}`;
87
- }
88
90
  setMetadata(metadata) {
89
91
  this.metadata = metadata;
90
92
  }
@@ -1,5 +1,5 @@
1
- import { type CountOptions, type DeleteOptions, type DriverMethodOptions, EntityManagerType, type FindOneOptions, type FindOptions, type IDatabaseDriver, type LockOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type OrderDefinition } from './IDatabaseDriver.js';
2
- import type { ConnectionType, Dictionary, EntityData, EntityDictionary, EntityMetadata, EntityProperty, FilterQuery, PopulateOptions, Primary } from '../typings.js';
1
+ import { type CountOptions, type DeleteOptions, type DriverMethodOptions, EntityManagerType, type FindOneOptions, type FindOptions, type IDatabaseDriver, type LockOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type OrderDefinition, type StreamOptions } from './IDatabaseDriver.js';
2
+ import type { ConnectionType, Dictionary, EntityData, EntityDictionary, EntityMetadata, EntityName, EntityProperty, FilterQuery, PopulateOptions, Primary } from '../typings.js';
3
3
  import type { MetadataStorage } from '../metadata/MetadataStorage.js';
4
4
  import type { Connection, QueryResult, Transaction } from '../connections/Connection.js';
5
5
  import { type Configuration, type ConnectionOptions } from '../utils/Configuration.js';
@@ -30,20 +30,25 @@ export declare abstract class DatabaseDriver<C extends Connection> implements ID
30
30
  abstract nativeDelete<T extends object>(entityName: string, where: FilterQuery<T>, options?: DeleteOptions<T>): Promise<QueryResult<T>>;
31
31
  abstract count<T extends object, P extends string = never>(entityName: string, where: FilterQuery<T>, options?: CountOptions<T, P>): Promise<number>;
32
32
  createEntityManager(useContext?: boolean): this[typeof EntityManagerType];
33
- findVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
33
+ findVirtual<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
34
34
  countVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: CountOptions<T, any>): Promise<number>;
35
35
  aggregate(entityName: string, pipeline: any[]): Promise<any[]>;
36
36
  loadFromPivotTable<T extends object, O extends object>(prop: EntityProperty, owners: Primary<O>[][], where?: FilterQuery<any>, orderBy?: OrderDefinition<T>, ctx?: Transaction, options?: FindOptions<T, any, any, any>, pivotJoin?: boolean): Promise<Dictionary<T[]>>;
37
37
  syncCollections<T extends object, O extends object>(collections: Iterable<Collection<T, O>>, options?: DriverMethodOptions): Promise<void>;
38
38
  mapResult<T extends object>(result: EntityDictionary<T>, meta?: EntityMetadata<T>, populate?: PopulateOptions<T>[]): EntityData<T> | null;
39
- connect(): Promise<C>;
40
- reconnect(): Promise<C>;
39
+ connect(options?: {
40
+ skipOnConnect?: boolean;
41
+ }): Promise<C>;
42
+ reconnect(options?: {
43
+ skipOnConnect?: boolean;
44
+ }): Promise<C>;
41
45
  getConnection(type?: ConnectionType): C;
42
46
  close(force?: boolean): Promise<void>;
43
47
  getPlatform(): Platform;
44
48
  setMetadata(metadata: MetadataStorage): void;
45
49
  getMetadata(): MetadataStorage;
46
50
  getDependencies(): string[];
51
+ protected isPopulated<T extends object>(meta: EntityMetadata<T>, prop: EntityProperty<T>, hint: PopulateOptions<T>, name?: string): boolean;
47
52
  protected processCursorOptions<T extends object, P extends string>(meta: EntityMetadata<T>, options: FindOptions<T, P, any, any>, orderBy: OrderDefinition<T>): {
48
53
  orderBy: OrderDefinition<T>[];
49
54
  where: FilterQuery<T>;
@@ -55,6 +60,7 @@ export declare abstract class DatabaseDriver<C extends Connection> implements ID
55
60
  protected getPrimaryKeyFields(entityName: string): string[];
56
61
  protected createReplicas(cb: (c: ConnectionOptions) => C): C[];
57
62
  lockPessimistic<T extends object>(entity: T, options: LockOptions): Promise<void>;
63
+ abstract stream<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: StreamOptions<T>): AsyncIterableIterator<T>;
58
64
  /**
59
65
  * @inheritDoc
60
66
  */
@@ -31,11 +31,11 @@ export class DatabaseDriver {
31
31
  const EntityManagerClass = this.config.get('entityManager', EntityManager);
32
32
  return new EntityManagerClass(this.config, this, this.metadata, useContext);
33
33
  }
34
- /* v8 ignore next 3 */
34
+ /* v8 ignore next */
35
35
  async findVirtual(entityName, where, options) {
36
36
  throw new Error(`Virtual entities are not supported by ${this.constructor.name} driver.`);
37
37
  }
38
- /* v8 ignore next 3 */
38
+ /* v8 ignore next */
39
39
  async countVirtual(entityName, where, options) {
40
40
  throw new Error(`Counting virtual entities is not supported by ${this.constructor.name} driver.`);
41
41
  }
@@ -47,16 +47,19 @@ export class DatabaseDriver {
47
47
  }
48
48
  async syncCollections(collections, options) {
49
49
  for (const coll of collections) {
50
+ /* v8 ignore else */
50
51
  if (!coll.property.owner) {
51
52
  if (coll.getSnapshot() === undefined) {
52
53
  throw ValidationError.cannotModifyInverseCollection(coll.owner, coll.property);
53
54
  }
54
55
  continue;
55
56
  }
56
- /* v8 ignore next 3 */
57
- const pk = coll.property.targetMeta.primaryKeys[0];
58
- const data = { [coll.property.name]: coll.getIdentifiers(pk) };
59
- await this.nativeUpdate(coll.owner.constructor.name, helper(coll.owner).getPrimaryKey(), data, options);
57
+ /* v8 ignore next */
58
+ {
59
+ const pk = coll.property.targetMeta.primaryKeys[0];
60
+ const data = { [coll.property.name]: coll.getIdentifiers(pk) };
61
+ await this.nativeUpdate(coll.owner.constructor.name, helper(coll.owner).getPrimaryKey(), data, options);
62
+ }
60
63
  }
61
64
  }
62
65
  mapResult(result, meta, populate = []) {
@@ -65,14 +68,14 @@ export class DatabaseDriver {
65
68
  }
66
69
  return this.comparator.mapResult(meta.className, result);
67
70
  }
68
- async connect() {
69
- await this.connection.connect();
71
+ async connect(options) {
72
+ await this.connection.connect(options);
70
73
  await Promise.all(this.replicas.map(replica => replica.connect()));
71
74
  return this.connection;
72
75
  }
73
- async reconnect() {
76
+ async reconnect(options) {
74
77
  await this.close(true);
75
- await this.connect();
78
+ await this.connect(options);
76
79
  return this.connection;
77
80
  }
78
81
  getConnection(type = 'write') {
@@ -105,6 +108,15 @@ export class DatabaseDriver {
105
108
  getDependencies() {
106
109
  return this.dependencies;
107
110
  }
111
+ isPopulated(meta, prop, hint, name) {
112
+ if (hint.field === prop.name || hint.field === name || hint.all) {
113
+ return true;
114
+ }
115
+ if (prop.embedded && hint.children && meta.properties[prop.embedded[0]].name === hint.field) {
116
+ return hint.children.some(c => this.isPopulated(meta, prop, c, prop.embedded[1]));
117
+ }
118
+ return false;
119
+ }
108
120
  processCursorOptions(meta, options, orderBy) {
109
121
  const { first, last, before, after, overfetch } = options;
110
122
  const limit = first ?? last;
@@ -244,7 +256,7 @@ export class DatabaseDriver {
244
256
  return data;
245
257
  }
246
258
  inlineEmbeddables(meta, data, where) {
247
- /* v8 ignore next 3 */
259
+ /* v8 ignore next */
248
260
  if (data == null) {
249
261
  return;
250
262
  }
@@ -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, EntityData, EntityMetadata, EntityProperty, FilterQuery, Primary, Dictionary, QBFilterQuery, IPrimaryKey, PopulateOptions, EntityDictionary, AutoPath, ObjectQuery, FilterObject, Populate, EntityName } 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';
@@ -14,9 +14,13 @@ export interface IDatabaseDriver<C extends Connection = Connection> {
14
14
  [EntityManagerType]: EntityManager<this>;
15
15
  readonly config: Configuration;
16
16
  createEntityManager(useContext?: boolean): this[typeof EntityManagerType];
17
- connect(): Promise<C>;
17
+ connect(options?: {
18
+ skipOnConnect?: boolean;
19
+ }): Promise<C>;
18
20
  close(force?: boolean): Promise<void>;
19
- reconnect(): Promise<C>;
21
+ reconnect(options?: {
22
+ skipOnConnect?: boolean;
23
+ }): Promise<C>;
20
24
  getConnection(type?: ConnectionType): C;
21
25
  /**
22
26
  * Finds selection of entities
@@ -27,6 +31,7 @@ export interface IDatabaseDriver<C extends Connection = Connection> {
27
31
  */
28
32
  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
33
  findVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
34
+ stream<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: StreamOptions<T>): AsyncIterableIterator<T>;
30
35
  nativeInsert<T extends object>(entityName: string, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
31
36
  nativeInsertMany<T extends object>(entityName: string, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>, transform?: (sql: string) => string): Promise<QueryResult<T>>;
32
37
  nativeUpdate<T extends object>(entityName: string, where: FilterQuery<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
@@ -68,6 +73,18 @@ export type OrderDefinition<T> = (QueryOrderMap<T> & {
68
73
  export interface FindAllOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends FindOptions<T, P, F, E> {
69
74
  where?: FilterQuery<T>;
70
75
  }
76
+ 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'> {
77
+ /**
78
+ * When populating to-many relations, the ORM streams fully merged entities instead of yielding every row.
79
+ * You can opt out of this behavior by specifying `mergeResults: false`. This will yield every row from
80
+ * the SQL result, but still mapped to entities, meaning that to-many collections will contain at most
81
+ * a single item, and you will get duplicate root entities when they have multiple items in the populated
82
+ * collection.
83
+ *
84
+ * @default true
85
+ */
86
+ mergeResults?: boolean;
87
+ }
71
88
  export type FilterOptions = Dictionary<boolean | Dictionary> | string[] | boolean;
72
89
  export interface LoadHint<Entity, Hint extends string = never, Fields extends string = PopulatePath.ALL, Excludes extends string = never> {
73
90
  populate?: Populate<Entity, Hint>;
@@ -152,7 +169,7 @@ export interface FindOptions<Entity, Hint extends string = never, Fields extends
152
169
  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'> {
153
170
  includeCount?: I;
154
171
  }
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'> {
172
+ 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
173
  lockMode?: LockMode;
157
174
  lockVersion?: number | Date;
158
175
  }
@@ -166,6 +183,7 @@ export interface NativeInsertUpdateOptions<T> {
166
183
  schema?: string;
167
184
  /** `nativeUpdate()` only option */
168
185
  upsert?: boolean;
186
+ loggerContext?: LogContext;
169
187
  }
170
188
  export interface NativeInsertUpdateManyOptions<T> extends NativeInsertUpdateOptions<T> {
171
189
  processCollections?: boolean;
@@ -200,6 +218,8 @@ export interface CountOptions<T extends object, P extends string = never> {
200
218
  hintComments?: string | string[];
201
219
  loggerContext?: LogContext;
202
220
  logging?: LoggingOptions;
221
+ /** @internal used to apply filters to the auto-joined relations */
222
+ em?: EntityManager;
203
223
  }
204
224
  export interface UpdateOptions<T> {
205
225
  filters?: FilterOptions;
@@ -221,6 +241,7 @@ export interface LockOptions extends DriverMethodOptions {
221
241
  export interface DriverMethodOptions {
222
242
  ctx?: Transaction;
223
243
  schema?: string;
244
+ loggerContext?: LogContext;
224
245
  }
225
246
  export interface GetReferenceOptions {
226
247
  wrapped?: boolean;
@@ -6,7 +6,6 @@ import { type SerializeOptions } from '../serialization/EntitySerializer.js';
6
6
  import type { FindOneOptions } from '../drivers/IDatabaseDriver.js';
7
7
  export declare abstract class BaseEntity {
8
8
  isInitialized(): boolean;
9
- isTouched(): boolean;
10
9
  populated(populated?: boolean): void;
11
10
  populate<Entity extends this = this, Hint extends string = never>(populate: AutoPath<Entity, Hint>[] | false, options?: EntityLoaderOptions<Entity>): Promise<Loaded<Entity, Hint>>;
12
11
  toReference<Entity extends this = this>(): Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>>;
@@ -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,18 +1,25 @@
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
- import type { FindOptions, CountOptions } from '../drivers/IDatabaseDriver.js';
4
+ import type { CountOptions, FindOptions } from '../drivers/IDatabaseDriver.js';
6
5
  import type { EntityLoaderOptions } from './EntityLoader.js';
7
6
  export interface MatchingOptions<T extends object, P extends string = never> extends FindOptions<T, P> {
8
7
  where?: FilterQuery<T>;
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
+ protected readonly items: Set<T>;
15
+ protected initialized: boolean;
16
+ protected dirty: boolean;
17
+ protected partial: boolean;
18
+ protected snapshot: T[] | undefined;
13
19
  private readonly?;
20
+ protected _count?: number;
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,117 @@ 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 createOrderBy;
66
+ private createManyToManyCondition;
67
+ private createLoadCountCondition;
68
+ private checkInitialized;
69
+ /**
70
+ * re-orders items after searching with `$in` operator
71
+ */
72
+ private reorderItems;
73
+ private cancelOrphanRemoval;
74
+ private validateModification;
75
+ toArray<TT extends T>(): EntityDTO<TT>[];
76
+ getIdentifiers<U extends IPrimaryKey = Primary<T> & IPrimaryKey>(field?: string | string[]): U[];
77
+ /**
78
+ * @internal
79
+ */
80
+ addWithoutPropagation(entity: T): void;
81
+ set(items: Iterable<T | Reference<T>>): void;
82
+ private compare;
83
+ /**
84
+ * @internal
85
+ */
86
+ hydrate(items: T[], forcePropagate?: boolean, partial?: boolean): void;
87
+ /**
88
+ * Remove all items from the collection. Note that removing items from collection does not necessarily imply deleting the target entity,
89
+ * it means we are disconnecting the relation - removing items from collection, not removing entities from database - `Collection.remove()`
90
+ * 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`,
91
+ * which tells the ORM we don't want orphaned entities to exist, so we know those should be removed.
92
+ */
93
+ removeAll(): void;
94
+ /**
95
+ * @internal
96
+ */
97
+ removeWithoutPropagation(entity: T): void;
50
98
  /**
51
- * @inheritDoc
99
+ * Extracts a slice of the collection items starting at position start to end (exclusive) of the collection.
100
+ * If end is null it returns all elements from start to the end of the collection.
52
101
  */
53
102
  slice(start?: number, end?: number): T[];
54
103
  /**
55
- * @inheritDoc
104
+ * Tests for the existence of an element that satisfies the given predicate.
56
105
  */
57
106
  exists(cb: (item: T) => boolean): boolean;
58
107
  /**
59
- * @inheritDoc
108
+ * Returns the first element of this collection that satisfies the predicate.
60
109
  */
61
110
  find(cb: (item: T, index: number) => boolean): T | undefined;
62
111
  /**
63
- * @inheritDoc
112
+ * Extracts a subset of the collection items.
64
113
  */
65
114
  filter(cb: (item: T, index: number) => boolean): T[];
66
115
  /**
67
- * @inheritDoc
116
+ * Maps the collection items based on your provided mapper function.
68
117
  */
69
118
  map<R>(mapper: (item: T, index: number) => R): R[];
70
119
  /**
71
- * @inheritDoc
120
+ * Maps the collection items based on your provided mapper function to a single object.
121
+ */
122
+ reduce<R>(cb: (obj: R, item: T, index: number) => R, initial?: R): R;
123
+ /**
124
+ * Maps the collection items to a dictionary, indexed by the key you specify.
125
+ * If there are more items with the same key, only the first one will be present.
72
126
  */
73
127
  indexBy<K1 extends keyof T, K2 extends keyof T = never>(key: K1): Record<T[K1] & PropertyKey, T>;
74
128
  /**
75
- * @inheritDoc
129
+ * Maps the collection items to a dictionary, indexed by the key you specify.
130
+ * If there are more items with the same key, only the first one will be present.
76
131
  */
77
132
  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;
133
+ isInitialized(fully?: boolean): boolean;
134
+ isDirty(): boolean;
135
+ isPartial(): boolean;
136
+ setDirty(dirty?: boolean): void;
137
+ get length(): number;
138
+ [Symbol.iterator](): IterableIterator<T>;
88
139
  /**
89
- * re-orders items after searching with `$in` operator
140
+ * @internal
90
141
  */
91
- private reorderItems;
92
- private cancelOrphanRemoval;
93
- private validateItemType;
94
- private validateModification;
142
+ takeSnapshot(forcePropagate?: boolean): void;
143
+ /**
144
+ * @internal
145
+ */
146
+ getSnapshot(): T[] | undefined;
147
+ /**
148
+ * @internal
149
+ */
150
+ get property(): EntityProperty;
151
+ /**
152
+ * @internal
153
+ */
154
+ set property(prop: EntityProperty);
155
+ protected propagate(item: T, method: 'add' | 'remove' | 'takeSnapshot'): void;
156
+ protected propagateToInverseSide(item: T, method: 'add' | 'remove' | 'takeSnapshot'): void;
157
+ protected propagateToOwningSide(item: T, method: 'add' | 'remove' | 'takeSnapshot'): void;
158
+ protected shouldPropagateToCollection(collection: Collection<O, T>, method: 'add' | 'remove' | 'takeSnapshot'): boolean;
159
+ protected incrementCount(value: number): void;
95
160
  }
96
161
  export interface InitCollectionOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends EntityLoaderOptions<T, F, E> {
97
162
  dataloader?: boolean;