@mikro-orm/mongodb 7.1.0-dev.3 → 7.1.0-dev.30

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.
@@ -68,6 +68,7 @@ export interface MongoFindOptions<T extends object> extends MongoQueryOptions {
68
68
  limit?: number;
69
69
  offset?: number;
70
70
  fields?: string[];
71
+ chunkSize?: number;
71
72
  ctx?: Transaction<ClientSession>;
72
73
  loggerContext?: LoggingOptions;
73
74
  }
@@ -143,6 +143,9 @@ export class MongoConnection extends Connection {
143
143
  if (opts.allowDiskUse != null) {
144
144
  options.allowDiskUse = opts.allowDiskUse;
145
145
  }
146
+ if (opts.chunkSize != null) {
147
+ options.batchSize = opts.chunkSize;
148
+ }
146
149
  const resultSet = this.getCollection(entityName).find(where, options);
147
150
  let query = `db.getCollection('${collection}').find(${this.logObject(where)}, ${this.logObject(options)})`;
148
151
  const orderBy = Utils.asArray(opts.orderBy);
package/MongoDriver.d.ts CHANGED
@@ -15,6 +15,7 @@ export declare class MongoDriver extends DatabaseDriver<MongoConnection> {
15
15
  rawResults?: boolean;
16
16
  }): AsyncIterableIterator<T>;
17
17
  find<T extends object, P extends string = never, F extends string = never, E extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: FindOptions<T, P, F, E>): Promise<EntityData<T>[]>;
18
+ private findWithPartitionLimit;
18
19
  findOne<T extends object, P extends string = never, F extends string = never, E extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: FindOneOptions<T, P, F, E>): Promise<EntityData<T> | null>;
19
20
  findVirtual<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
20
21
  streamVirtual<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): AsyncIterableIterator<EntityData<T>>;
@@ -31,7 +32,8 @@ export declare class MongoDriver extends DatabaseDriver<MongoConnection> {
31
32
  streamAggregate<T extends object>(entityName: EntityName<T>, pipeline: any[], ctx?: Transaction<ClientSession>): AsyncIterableIterator<T>;
32
33
  getPlatform(): MongoPlatform;
33
34
  private buildQueryOptions;
34
- private renameFields;
35
+ /** @internal */
36
+ renameFields<T extends object>(entityName: EntityName<T>, data: T, dotPaths?: boolean, object?: boolean, root?: boolean): T;
35
37
  private convertObjectIds;
36
38
  private buildFilterById;
37
39
  protected buildFields<T extends object, P extends string = never>(entityName: EntityName<T>, populate: PopulateOptions<T>[], fields?: readonly EntityField<T, P>[], exclude?: string[]): string[] | undefined;
package/MongoDriver.js CHANGED
@@ -30,6 +30,7 @@ export class MongoDriver extends DatabaseDriver {
30
30
  offset: options.offset,
31
31
  fields,
32
32
  ctx: options.ctx,
33
+ chunkSize: options.chunkSize ?? 100,
33
34
  ...this.buildQueryOptions(options),
34
35
  });
35
36
  for await (const item of res) {
@@ -78,6 +79,10 @@ export class MongoDriver extends DatabaseDriver {
78
79
  return res.map(r => this.mapResult(r, this.metadata.find(entityName)));
79
80
  }
80
81
  const orderBy = Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy, true));
82
+ const partitionLimit = options._partitionLimit;
83
+ if (partitionLimit) {
84
+ return this.findWithPartitionLimit(entityName, where, orderBy, fields ?? [], partitionLimit, options);
85
+ }
81
86
  const res = await this.rethrow(this.getConnection('read').find(entityName, where, {
82
87
  orderBy,
83
88
  limit: options.limit,
@@ -88,6 +93,48 @@ export class MongoDriver extends DatabaseDriver {
88
93
  }));
89
94
  return res.map(r => this.mapResult(r, this.metadata.find(entityName)));
90
95
  }
96
+ async findWithPartitionLimit(entityName, where, orderBy, fields, partitionLimit, options) {
97
+ const meta = this.metadata.find(entityName);
98
+ const { limit, offset = 0 } = partitionLimit;
99
+ // Resolve the partition property to its DB field name; callers always pass
100
+ // a declared property on the target meta (FK of the owning side).
101
+ const prop = meta.properties[partitionLimit.partitionBy];
102
+ const partitionField = prop.fieldNames[0];
103
+ const pipeline = [];
104
+ pipeline.push({ $match: where });
105
+ if (orderBy.length > 0) {
106
+ const sortSpec = {};
107
+ for (const order of orderBy) {
108
+ for (const [key, dir] of Object.entries(order)) {
109
+ sortSpec[key] = dir === 'ASC' || dir === 'asc' || dir === 1 ? 1 : -1;
110
+ }
111
+ }
112
+ pipeline.push({ $sort: sortSpec });
113
+ }
114
+ // $sort before $group ensures $push collects in correct order
115
+ pipeline.push({
116
+ $group: {
117
+ _id: `$${partitionField}`,
118
+ __docs: { $push: '$$ROOT' },
119
+ },
120
+ });
121
+ pipeline.push({
122
+ $project: {
123
+ __docs: { $slice: ['$__docs', offset, limit] },
124
+ },
125
+ });
126
+ pipeline.push({ $unwind: '$__docs' });
127
+ pipeline.push({ $replaceRoot: { newRoot: '$__docs' } });
128
+ if (fields.length > 0) {
129
+ const projection = {};
130
+ for (const field of fields) {
131
+ projection[field] = 1;
132
+ }
133
+ pipeline.push({ $project: projection });
134
+ }
135
+ const res = await this.rethrow(this.getConnection('read').aggregate(entityName, pipeline, options.ctx, options.logging));
136
+ return res.map(r => this.mapResult(r, meta));
137
+ }
91
138
  async findOne(entityName, where, options = { populate: [], orderBy: {} }) {
92
139
  if (this.metadata.find(entityName)?.virtual) {
93
140
  const [item] = await this.findVirtual(entityName, where, options);
@@ -268,6 +315,13 @@ export class MongoDriver extends DatabaseDriver {
268
315
  if (options.indexHint != null) {
269
316
  ret.indexHint = options.indexHint;
270
317
  }
318
+ else if (options.using != null) {
319
+ const names = Utils.asArray(options.using);
320
+ if (names.length > 1) {
321
+ throw new Error('MongoDB only supports a single index hint per query. Provide one index name instead of an array.');
322
+ }
323
+ ret.indexHint = names[0];
324
+ }
271
325
  if (options.maxTimeMS != null) {
272
326
  ret.maxTimeMS = options.maxTimeMS;
273
327
  }
@@ -276,6 +330,7 @@ export class MongoDriver extends DatabaseDriver {
276
330
  }
277
331
  return ret;
278
332
  }
333
+ /** @internal */
279
334
  renameFields(entityName, data, dotPaths = false, object, root = true) {
280
335
  if (data == null && root) {
281
336
  return {};
@@ -1,4 +1,4 @@
1
- import { EntityManager, type EntityName, type EntityRepository, type GetRepository, type TransactionOptions, type StreamOptions, type Loaded } from '@mikro-orm/core';
1
+ import { EntityManager, type CountByOptions, type Dictionary, type EntityKey, type EntityName, type EntityRepository, type GetRepository, type Loaded, type StreamOptions, type TransactionOptions, type WithUsingOptions } from '@mikro-orm/core';
2
2
  import type { Collection, Document, TransactionOptions as MongoTransactionOptions } from 'mongodb';
3
3
  import type { MongoDriver } from './MongoDriver.js';
4
4
  import type { MongoEntityRepository } from './MongoEntityRepository.js';
@@ -17,8 +17,12 @@ export declare class MongoEntityManager<Driver extends MongoDriver = MongoDriver
17
17
  /**
18
18
  * @inheritDoc
19
19
  */
20
- stream<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never>(entityName: EntityName<Entity>, options?: StreamOptions<NoInfer<Entity>, Hint, Fields, Excludes>): AsyncIterableIterator<Loaded<Entity, Hint, Fields, Excludes>>;
20
+ stream<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, options?: WithUsingOptions<StreamOptions<NoInfer<Entity>, Hint, Fields, Excludes>, NoInfer<Entity>, Using>): AsyncIterableIterator<Loaded<Entity, Hint, Fields, Excludes>>;
21
21
  getCollection<T extends Document>(entityOrCollectionName: EntityName<T> | string): Collection<T>;
22
+ /**
23
+ * @inheritDoc
24
+ */
25
+ countBy<Entity extends object>(entityName: EntityName<Entity>, groupBy: EntityKey<Entity> | readonly EntityKey<Entity>[], options?: CountByOptions<Entity>): Promise<Dictionary<number>>;
22
26
  /**
23
27
  * @inheritDoc
24
28
  */
@@ -27,6 +27,37 @@ export class MongoEntityManager extends EntityManager {
27
27
  getCollection(entityOrCollectionName) {
28
28
  return this.getConnection().getCollection(entityOrCollectionName);
29
29
  }
30
+ /**
31
+ * @inheritDoc
32
+ */
33
+ async countBy(entityName, groupBy, options = {}) {
34
+ const em = this.getContext(false);
35
+ options = { ...options };
36
+ em.prepareOptions(options);
37
+ const meta = em.getMetadata().find(entityName);
38
+ const fields = Utils.asArray(groupBy);
39
+ if (options.having) {
40
+ throw new Error('The `having` option is not supported for MongoDB in `countBy`.');
41
+ }
42
+ await em.tryFlush(entityName, options);
43
+ const rawWhere = options.where;
44
+ const where = await em.processWhere(entityName, rawWhere ?? {}, options, 'read');
45
+ const renamedWhere = em.getDriver().renameFields(meta.class, where, true);
46
+ const fieldNames = fields.map(f => meta.properties[f]?.fieldNames?.[0] ?? f);
47
+ const groupId = fieldNames.length === 1 ? `$${fieldNames[0]}` : Object.fromEntries(fieldNames.map(f => [f, `$${f}`]));
48
+ const pipeline = [];
49
+ if (renamedWhere && Object.keys(renamedWhere).length > 0) {
50
+ pipeline.push({ $match: renamedWhere });
51
+ }
52
+ pipeline.push({ $group: { _id: groupId, count: { $sum: 1 } } });
53
+ const rows = await em.getDriver().aggregate(meta.class, pipeline, em.getTransactionContext());
54
+ const results = {};
55
+ for (const row of rows) {
56
+ const key = fieldNames.length === 1 ? String(row._id) : fieldNames.map(f => String(row._id[f])).join(Utils.PK_SEPARATOR);
57
+ results[key] = +row.count;
58
+ }
59
+ return results;
60
+ }
30
61
  /**
31
62
  * @inheritDoc
32
63
  */
package/MongoPlatform.js CHANGED
@@ -73,9 +73,13 @@ export class MongoPlatform extends Platform {
73
73
  return prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner;
74
74
  }
75
75
  validateMetadata(meta) {
76
+ super.validateMetadata(meta);
76
77
  if (meta.inheritanceType === 'tpt') {
77
78
  throw MetadataError.tptNotSupportedByDriver(meta);
78
79
  }
80
+ if (meta.triggers?.length > 0) {
81
+ throw MetadataError.triggersNotSupportedByDriver(meta);
82
+ }
79
83
  const pk = meta.getPrimaryProps()[0];
80
84
  if (pk && pk.fieldNames?.[0] !== '_id') {
81
85
  throw MetadataError.invalidPrimaryKey(meta, pk, '_id');
@@ -21,6 +21,11 @@ export declare class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoD
21
21
  ensureIndexes(options?: EnsureIndexesOptions): Promise<void>;
22
22
  private mapIndexProperties;
23
23
  private createIndexes;
24
+ /**
25
+ * An explicit `options.partialFilterExpression` wins over `where` — this preserves the
26
+ * long-standing `options: { partialFilterExpression }` escape hatch.
27
+ */
28
+ private applyPartialFilter;
24
29
  private executeQuery;
25
30
  private createUniqueIndexes;
26
31
  private createPropertyIndexes;
@@ -132,10 +132,21 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
132
132
  const properties = this.mapIndexProperties(index, meta);
133
133
  const collection = this.connection.getCollection(meta.class);
134
134
  if (Array.isArray(index.options) && index.options.length === 2 && properties.length === 0) {
135
- res.push([collection.collectionName, collection.createIndex(index.options[0], index.options[1])]);
135
+ // The array-form escape hatch passes raw [spec, options] to the driver; fold `where`
136
+ // into the options dict (on a clone, to avoid mutating the user's metadata).
137
+ const opts = { ...index.options[1] };
138
+ this.applyPartialFilter(opts, index.where, index.name, meta.className);
139
+ res.push([collection.collectionName, collection.createIndex(index.options[0], opts)]);
136
140
  return;
137
141
  }
138
142
  if (index.options && properties.length === 0) {
143
+ // The plain escape hatch forwards `index.options` as the sole argument; there is
144
+ // no options slot for `partialFilterExpression`, so warn instead of silently dropping.
145
+ if (index.where != null) {
146
+ this.config
147
+ .getLogger()
148
+ .warn('schema', `Index '${index.name ?? '(unnamed)'}' on entity '${meta.className}': \`where\` was ignored because \`options\` is used as the raw index spec and leaves no slot for \`partialFilterExpression\` — pass \`options: [spec, { partialFilterExpression }]\` (array form) to combine both.`);
149
+ }
139
150
  res.push([collection.collectionName, collection.createIndex(index.options)]);
140
151
  return;
141
152
  }
@@ -162,10 +173,30 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
162
173
  if (index.invisible) {
163
174
  indexOptions.hidden = true;
164
175
  }
176
+ this.applyPartialFilter(indexOptions, index.where, index.name, meta.className);
165
177
  res.push([collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, indexOptions)]);
166
178
  });
167
179
  return res;
168
180
  }
181
+ /**
182
+ * An explicit `options.partialFilterExpression` wins over `where` — this preserves the
183
+ * long-standing `options: { partialFilterExpression }` escape hatch.
184
+ */
185
+ applyPartialFilter(options, where, indexName, entityName) {
186
+ if (where == null) {
187
+ return;
188
+ }
189
+ if (options.partialFilterExpression != null) {
190
+ this.config
191
+ .getLogger()
192
+ .warn('schema', `Index '${indexName ?? '(unnamed)'}' on entity '${entityName}': both \`where\` and \`options.partialFilterExpression\` are set; \`options.partialFilterExpression\` wins.`);
193
+ return;
194
+ }
195
+ if (typeof where === 'string') {
196
+ throw new Error(`Index '${indexName ?? '(unnamed)'}' on entity '${entityName}': string \`where\` is not supported on MongoDB; pass an object/FilterQuery (it maps to MongoDB's \`partialFilterExpression\`).`);
197
+ }
198
+ options.partialFilterExpression = where;
199
+ }
169
200
  async executeQuery(collection, method, ...args) {
170
201
  const now = Date.now();
171
202
  return collection[method](...args).then((res) => {
@@ -188,14 +219,13 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator {
188
219
  return o;
189
220
  }, {});
190
221
  const collection = this.connection.getCollection(meta.class);
191
- res.push([
192
- collection.collectionName,
193
- this.executeQuery(collection, 'createIndex', fieldOrSpec, {
194
- name: index.name,
195
- unique: true,
196
- ...index.options,
197
- }),
198
- ]);
222
+ const indexOptions = {
223
+ name: index.name,
224
+ unique: true,
225
+ ...index.options,
226
+ };
227
+ this.applyPartialFilter(indexOptions, index.where, index.name, meta.className);
228
+ res.push([collection.collectionName, this.executeQuery(collection, 'createIndex', fieldOrSpec, indexOptions)]);
199
229
  });
200
230
  return res;
201
231
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
- "version": "7.1.0-dev.3",
3
+ "version": "7.1.0-dev.30",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -47,13 +47,13 @@
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
49
49
  "dependencies": {
50
- "mongodb": "7.1.1"
50
+ "mongodb": "7.2.0"
51
51
  },
52
52
  "devDependencies": {
53
- "@mikro-orm/core": "^7.0.11"
53
+ "@mikro-orm/core": "^7.0.15"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.1.0-dev.3"
56
+ "@mikro-orm/core": "7.1.0-dev.30"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"