@mikro-orm/mongodb 7.3.0-dev.2 → 7.3.0-dev.4

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
+ /** Relations are stored as references, so conditions can only compare them by PK, e.g. `{ author: { id } }` becomes `{ author: _id }`. */
39
+ private inlineRelationPrimaryKey;
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, QueryHelper, ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ import { DatabaseDriver, EntityManagerType, GroupOperator, PolymorphicRef, QueryHelper, 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';
@@ -446,6 +446,9 @@ export class MongoDriver extends DatabaseDriver {
446
446
  const meta2 = this.metadata.find(prop.targetMeta.class);
447
447
  const pk = meta2.properties[meta2.primaryKeys[0]];
448
448
  isObjectId = pk.type === 'ObjectId';
449
+ if (dotPaths) {
450
+ copiedData[k] = this.inlineRelationPrimaryKey(meta, prop, meta2, copiedData[k]);
451
+ }
449
452
  }
450
453
  if (isObjectId) {
451
454
  copiedData[k] = this.convertObjectIds(copiedData[k]);
@@ -460,6 +463,23 @@ export class MongoDriver extends DatabaseDriver {
460
463
  });
461
464
  return copiedData;
462
465
  }
466
+ /** Relations are stored as references, so conditions can only compare them by PK, e.g. `{ author: { id } }` becomes `{ author: _id }`. */
467
+ inlineRelationPrimaryKey(meta, prop, meta2, value) {
468
+ if (prop.kind === ReferenceKind.ONE_TO_MANY || (prop.kind === ReferenceKind.MANY_TO_MANY && !prop.owner)) {
469
+ throw new Error(`Unsupported condition on inverse side ${meta.className}.${prop.name}, query the owning side ${meta2.className}.${prop.mappedBy} instead.`);
470
+ }
471
+ if (!Utils.isPlainObject(value)) {
472
+ return value;
473
+ }
474
+ const keys = Object.keys(value);
475
+ if (keys.length === 1 && [meta2.primaryKeys[0], meta2.serializedPrimaryKey].includes(keys[0])) {
476
+ return value[keys[0]];
477
+ }
478
+ if (keys.every(k => Utils.isOperator(k) && !(k in GroupOperator) && k !== '$not')) {
479
+ return value;
480
+ }
481
+ throw new Error(`Unsupported condition on relation ${meta.className}.${prop.name}, MongoDB can query relations only by primary key or entity reference.`);
482
+ }
463
483
  convertObjectIds(data) {
464
484
  if (data instanceof ObjectId) {
465
485
  return data;
@@ -515,6 +535,12 @@ export class MongoDriver extends DatabaseDriver {
515
535
  }
516
536
  }
517
537
  ret.unshift(...meta.primaryKeys.filter(pk => !fields.includes(pk)));
538
+ // `targetKey` values identify the entity like its PK, references resolve it by them
539
+ for (const key of meta.root.targetKeys ?? []) {
540
+ if (meta.properties[key] && !fields.includes(key)) {
541
+ ret.push(meta.properties[key].fieldNames[0]);
542
+ }
543
+ }
518
544
  }
519
545
  else if (!Utils.isEmpty(exclude) || lazyProps.some(p => !p.formula)) {
520
546
  const props = meta.props.filter(prop => this.platform.shouldHaveColumn(prop, populate, exclude));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
- "version": "7.3.0-dev.2",
3
+ "version": "7.3.0-dev.4",
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.1"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.3.0-dev.2"
56
+ "@mikro-orm/core": "7.3.0-dev.4"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"