@mikro-orm/mongodb 7.2.8-dev.1 → 7.2.8-dev.10

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
@@ -35,6 +35,8 @@ export declare class MongoDriver extends DatabaseDriver<MongoConnection> {
35
35
  private buildQueryOptions;
36
36
  /** @internal */
37
37
  renameFields<T extends object>(entityName: EntityName<T>, data: T, dotPaths?: boolean, object?: boolean, root?: boolean): T;
38
+ private convertRegExpOperator;
39
+ private convertRegExpOperators;
38
40
  private convertObjectIds;
39
41
  private buildFilterById;
40
42
  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
@@ -1,5 +1,5 @@
1
1
  import { ObjectId } from 'mongodb';
2
- import { DatabaseDriver, EntityManagerType, PolymorphicRef, ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ import { DatabaseDriver, EntityManagerType, GroupOperator, PolymorphicRef, ReferenceKind, Utils, } from '@mikro-orm/core';
3
3
  import { MongoConnection } from './MongoConnection.js';
4
4
  import { MongoPlatform } from './MongoPlatform.js';
5
5
  import { MongoEntityManager } from './MongoEntityManager.js';
@@ -200,7 +200,7 @@ export class MongoDriver extends DatabaseDriver {
200
200
  const meta = this.metadata.find(entityName);
201
201
  const pk = meta.getPrimaryProps()[0].fieldNames[0] ?? '_id';
202
202
  const normalizedWhere = Utils.isPrimaryKey(where) ? { [pk]: where } : where;
203
- const renameWhere = this.renameFields(entityName, normalizedWhere);
203
+ const renameWhere = this.renameFields(entityName, normalizedWhere, true);
204
204
  const source = await this.rethrow(this.getConnection('read').find(entityName, renameWhere, { ctx: options.ctx, limit: 1 }));
205
205
  if (!source.length) {
206
206
  throw new Error('Cannot clone: no entity found matching the given condition');
@@ -392,6 +392,7 @@ export class MongoDriver extends DatabaseDriver {
392
392
  }
393
393
  return;
394
394
  }
395
+ let key = k;
395
396
  if (meta?.properties[k]) {
396
397
  const prop = meta.properties[k];
397
398
  let isObjectId = false;
@@ -455,14 +456,39 @@ export class MongoDriver extends DatabaseDriver {
455
456
  }
456
457
  if (prop.fieldNames) {
457
458
  Utils.renameKey(copiedData, k, prop.fieldNames[0]);
459
+ key = prop.fieldNames[0];
458
460
  }
459
461
  }
460
- if (Utils.isPlainObject(copiedData[k]) && '$re' in copiedData[k]) {
461
- copiedData[k] = new RegExp(copiedData[k].$re);
462
+ // `dotPaths` marks a query, entity data (e.g. JSON values) must stay as-is
463
+ if (dotPaths) {
464
+ copiedData[key] = this.convertRegExpOperator(copiedData[key]);
462
465
  }
463
466
  });
464
467
  return copiedData;
465
468
  }
469
+ convertRegExpOperator(value) {
470
+ if (!Utils.isPlainObject(value)) {
471
+ return value;
472
+ }
473
+ let cond = '$not' in value ? { ...value, $not: this.convertRegExpOperator(value.$not) } : value;
474
+ if (Utils.isPlainObject(cond.$elemMatch)) {
475
+ cond = { ...cond, $elemMatch: this.convertRegExpOperators(cond.$elemMatch) };
476
+ }
477
+ if (!('$re' in cond)) {
478
+ return cond;
479
+ }
480
+ const { $re, ...rest } = cond;
481
+ // mongo has no `$re`: a RegExp when it stands alone, `$regex` next to other operators
482
+ return Utils.hasObjectKeys(rest) ? { ...rest, $regex: $re } : new RegExp($re);
483
+ }
484
+ convertRegExpOperators(where) {
485
+ return Object.fromEntries(Object.entries(where).map(([k, v]) => [
486
+ k,
487
+ k in GroupOperator && Array.isArray(v)
488
+ ? v.map(c => this.convertRegExpOperators(c))
489
+ : this.convertRegExpOperator(v),
490
+ ]));
491
+ }
466
492
  convertObjectIds(data) {
467
493
  if (data instanceof ObjectId) {
468
494
  return data;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
- "version": "7.2.8-dev.1",
3
+ "version": "7.2.8-dev.10",
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.2.7"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.2.8-dev.1"
56
+ "@mikro-orm/core": "7.2.8-dev.10"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"