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

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';
@@ -233,6 +233,12 @@ export class MongoDriver extends DatabaseDriver {
233
233
  if (Utils.isPrimaryKey(where)) {
234
234
  where = this.buildFilterById(entityName, where);
235
235
  }
236
+ // an empty filter would match (and overwrite) every document, so insert instead
237
+ if (options.upsert && Utils.isEmpty(where)) {
238
+ const res = await this.nativeInsert(entityName, data, options);
239
+ res.row = { _id: res.insertId };
240
+ return res;
241
+ }
236
242
  this.handleVersionProperty(entityName, data, true);
237
243
  data = this.renameFields(entityName, data);
238
244
  where = this.renameFields(entityName, where, true);
@@ -446,6 +452,9 @@ export class MongoDriver extends DatabaseDriver {
446
452
  const meta2 = this.metadata.find(prop.targetMeta.class);
447
453
  const pk = meta2.properties[meta2.primaryKeys[0]];
448
454
  isObjectId = pk.type === 'ObjectId';
455
+ if (dotPaths) {
456
+ copiedData[k] = this.inlineRelationPrimaryKey(meta, prop, meta2, copiedData[k]);
457
+ }
449
458
  }
450
459
  if (isObjectId) {
451
460
  copiedData[k] = this.convertObjectIds(copiedData[k]);
@@ -460,6 +469,23 @@ export class MongoDriver extends DatabaseDriver {
460
469
  });
461
470
  return copiedData;
462
471
  }
472
+ /** Relations are stored as references, so conditions can only compare them by PK, e.g. `{ author: { id } }` becomes `{ author: _id }`. */
473
+ inlineRelationPrimaryKey(meta, prop, meta2, value) {
474
+ if (prop.kind === ReferenceKind.ONE_TO_MANY || (prop.kind === ReferenceKind.MANY_TO_MANY && !prop.owner)) {
475
+ throw new Error(`Unsupported condition on inverse side ${meta.className}.${prop.name}, query the owning side ${meta2.className}.${prop.mappedBy} instead.`);
476
+ }
477
+ if (!Utils.isPlainObject(value)) {
478
+ return value;
479
+ }
480
+ const keys = Object.keys(value);
481
+ if (keys.length === 1 && [meta2.primaryKeys[0], meta2.serializedPrimaryKey].includes(keys[0])) {
482
+ return value[keys[0]];
483
+ }
484
+ if (keys.every(k => Utils.isOperator(k) && !(k in GroupOperator) && k !== '$not')) {
485
+ return value;
486
+ }
487
+ throw new Error(`Unsupported condition on relation ${meta.className}.${prop.name}, MongoDB can query relations only by primary key or entity reference.`);
488
+ }
463
489
  convertObjectIds(data) {
464
490
  if (data instanceof ObjectId) {
465
491
  return data;
@@ -515,6 +541,12 @@ export class MongoDriver extends DatabaseDriver {
515
541
  }
516
542
  }
517
543
  ret.unshift(...meta.primaryKeys.filter(pk => !fields.includes(pk)));
544
+ // `targetKey` values identify the entity like its PK, references resolve it by them
545
+ for (const key of meta.root.targetKeys ?? []) {
546
+ if (meta.properties[key] && !fields.includes(key)) {
547
+ ret.push(meta.properties[key].fieldNames[0]);
548
+ }
549
+ }
518
550
  }
519
551
  else if (!Utils.isEmpty(exclude) || lazyProps.some(p => !p.formula)) {
520
552
  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.20",
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.6.0"
50
+ "mongodb": "7.8.0"
51
51
  },
52
52
  "devDependencies": {
53
- "@mikro-orm/core": "^7.2.1"
53
+ "@mikro-orm/core": "^7.2.7"
54
54
  },
55
55
  "peerDependencies": {
56
- "@mikro-orm/core": "7.3.0-dev.2"
56
+ "@mikro-orm/core": "7.3.0-dev.20"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"