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

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.
Files changed (2) hide show
  1. package/MongoDriver.js +21 -3
  2. package/package.json +2 -2
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, 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';
@@ -386,8 +386,10 @@ export class MongoDriver extends DatabaseDriver {
386
386
  }
387
387
  return;
388
388
  }
389
- if (meta?.properties[k]) {
390
- const prop = meta.properties[k];
389
+ // per-target polymorphic keys like `likeable[Comment]` resolve to the relation, whose conditions need joins
390
+ const [propName, targetName] = QueryHelper.splitPolymorphicKey(k);
391
+ const prop = meta?.properties[propName];
392
+ if (prop && (!targetName || prop.polymorphic)) {
391
393
  let isObjectId = false;
392
394
  if (prop.kind === ReferenceKind.SCALAR) {
393
395
  isObjectId = prop.type === 'ObjectId';
@@ -422,6 +424,22 @@ export class MongoDriver extends DatabaseDriver {
422
424
  else if (value == null) {
423
425
  prop.fieldNames.forEach(f => (copiedData[f] = null));
424
426
  }
427
+ else if (k === prop.fieldNames[1] &&
428
+ Utils.isPlainObject(value) &&
429
+ Object.keys(value).every(o => Utils.isOperator(o))) {
430
+ // the FK field is named after the property, e.g. `{ likeable: { $in: ids } }` when loading the inverse side
431
+ copiedData[k] = this.convertObjectIds(value);
432
+ }
433
+ else if (!targetName &&
434
+ Utils.isPlainObject(value) &&
435
+ Utils.getObjectKeysSize(value) === 1 &&
436
+ prop.targetMeta.primaryKeys[0] in value) {
437
+ // PK-only condition, e.g. `{ likeable: { _id: { $in: ids } } }`, compares the FK field
438
+ copiedData[prop.fieldNames[1]] = this.convertObjectIds(value[prop.targetMeta.primaryKeys[0]]);
439
+ }
440
+ else {
441
+ throw new Error(`Unsupported condition on polymorphic relation ${meta.className}.${prop.name}, use an entity reference or \`null\` instead.`);
442
+ }
425
443
  return;
426
444
  }
427
445
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
- "version": "7.3.0-dev.0",
3
+ "version": "7.3.0-dev.2",
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.0"
56
+ "@mikro-orm/core": "7.3.0-dev.2"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">= 22.17.0"