@mikro-orm/mongodb 7.1.0-dev.7 → 7.1.0-dev.9

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.
package/MongoDriver.d.ts CHANGED
@@ -31,7 +31,8 @@ export declare class MongoDriver extends DatabaseDriver<MongoConnection> {
31
31
  streamAggregate<T extends object>(entityName: EntityName<T>, pipeline: any[], ctx?: Transaction<ClientSession>): AsyncIterableIterator<T>;
32
32
  getPlatform(): MongoPlatform;
33
33
  private buildQueryOptions;
34
- private renameFields;
34
+ /** @internal */
35
+ renameFields<T extends object>(entityName: EntityName<T>, data: T, dotPaths?: boolean, object?: boolean, root?: boolean): T;
35
36
  private convertObjectIds;
36
37
  private buildFilterById;
37
38
  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
@@ -283,6 +283,7 @@ export class MongoDriver extends DatabaseDriver {
283
283
  }
284
284
  return ret;
285
285
  }
286
+ /** @internal */
286
287
  renameFields(entityName, data, dotPaths = false, object, root = true) {
287
288
  if (data == null && root) {
288
289
  return {};
@@ -1,4 +1,4 @@
1
- import { EntityManager, type EntityName, type EntityRepository, type GetRepository, type TransactionOptions, type StreamOptions, type Loaded, type WithUsingOptions } 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';
@@ -19,6 +19,10 @@ export declare class MongoEntityManager<Driver extends MongoDriver = MongoDriver
19
19
  */
20
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
@@ -76,6 +76,9 @@ export class MongoPlatform extends Platform {
76
76
  if (meta.inheritanceType === 'tpt') {
77
77
  throw MetadataError.tptNotSupportedByDriver(meta);
78
78
  }
79
+ if (meta.triggers?.length > 0) {
80
+ throw MetadataError.triggersNotSupportedByDriver(meta);
81
+ }
79
82
  const pk = meta.getPrimaryProps()[0];
80
83
  if (pk && pk.fieldNames?.[0] !== '_id') {
81
84
  throw MetadataError.invalidPrimaryKey(meta, pk, '_id');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
- "version": "7.1.0-dev.7",
3
+ "version": "7.1.0-dev.9",
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",
@@ -53,7 +53,7 @@
53
53
  "@mikro-orm/core": "^7.0.11"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.1.0-dev.7"
56
+ "@mikro-orm/core": "7.1.0-dev.9"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"