@mikro-orm/mongodb 7.3.0-dev.1 → 7.3.0-dev.3
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 +2 -0
- package/MongoDriver.js +41 -3
- package/package.json +2 -2
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, 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';
|
|
@@ -386,8 +386,10 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
386
386
|
}
|
|
387
387
|
return;
|
|
388
388
|
}
|
|
389
|
-
|
|
390
|
-
|
|
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,12 +424,31 @@ 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 {
|
|
428
446
|
const meta2 = this.metadata.find(prop.targetMeta.class);
|
|
429
447
|
const pk = meta2.properties[meta2.primaryKeys[0]];
|
|
430
448
|
isObjectId = pk.type === 'ObjectId';
|
|
449
|
+
if (dotPaths) {
|
|
450
|
+
copiedData[k] = this.inlineRelationPrimaryKey(meta, prop, meta2, copiedData[k]);
|
|
451
|
+
}
|
|
431
452
|
}
|
|
432
453
|
if (isObjectId) {
|
|
433
454
|
copiedData[k] = this.convertObjectIds(copiedData[k]);
|
|
@@ -442,6 +463,23 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
442
463
|
});
|
|
443
464
|
return copiedData;
|
|
444
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
|
+
}
|
|
445
483
|
convertObjectIds(data) {
|
|
446
484
|
if (data instanceof ObjectId) {
|
|
447
485
|
return data;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/mongodb",
|
|
3
|
-
"version": "7.3.0-dev.
|
|
3
|
+
"version": "7.3.0-dev.3",
|
|
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.
|
|
56
|
+
"@mikro-orm/core": "7.3.0-dev.3"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
59
59
|
"node": ">= 22.17.0"
|