@mikro-orm/mongodb 7.3.0-dev.2 → 7.3.0-dev.21
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 +4 -0
- package/MongoDriver.js +83 -6
- package/package.json +4 -4
package/MongoDriver.d.ts
CHANGED
|
@@ -35,6 +35,10 @@ 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;
|
|
40
|
+
private convertRegExpOperator;
|
|
41
|
+
private convertRegExpOperators;
|
|
38
42
|
private convertObjectIds;
|
|
39
43
|
private buildFilterById;
|
|
40
44
|
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';
|
|
@@ -95,13 +95,19 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
95
95
|
}
|
|
96
96
|
async findWithPartitionLimit(entityName, where, orderBy, fields, partitionLimit, options) {
|
|
97
97
|
const meta = this.metadata.find(entityName);
|
|
98
|
-
const { limit, offset = 0 } = partitionLimit;
|
|
98
|
+
const { limit, offset = 0, owners } = partitionLimit;
|
|
99
99
|
// Resolve the partition property to its DB field name; callers always pass
|
|
100
100
|
// a declared property on the target meta (FK of the owning side).
|
|
101
101
|
const prop = meta.properties[partitionLimit.partitionBy];
|
|
102
102
|
const partitionField = prop.fieldNames[0];
|
|
103
103
|
const pipeline = [];
|
|
104
104
|
pipeline.push({ $match: where });
|
|
105
|
+
// an M:N owner array holds several partitions, so rank each row once per requested owner, like a pivot row in SQL
|
|
106
|
+
if (owners) {
|
|
107
|
+
pipeline.push({ $addFields: { __partition: `$${partitionField}` } });
|
|
108
|
+
pipeline.push({ $unwind: '$__partition' });
|
|
109
|
+
pipeline.push({ $match: { __partition: { $in: owners } } });
|
|
110
|
+
}
|
|
105
111
|
if (orderBy.length > 0) {
|
|
106
112
|
const sortSpec = {};
|
|
107
113
|
for (const order of orderBy) {
|
|
@@ -115,7 +121,7 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
115
121
|
// $sort before $group ensures $push collects in correct order
|
|
116
122
|
pipeline.push({
|
|
117
123
|
$group: {
|
|
118
|
-
_id: `$${partitionField}`,
|
|
124
|
+
_id: owners ? '$__partition' : `$${partitionField}`,
|
|
119
125
|
__docs: { $push: '$$ROOT' },
|
|
120
126
|
},
|
|
121
127
|
});
|
|
@@ -131,9 +137,19 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
131
137
|
for (const field of fields) {
|
|
132
138
|
projection[field] = 1;
|
|
133
139
|
}
|
|
140
|
+
if (owners) {
|
|
141
|
+
projection.__partition = 1;
|
|
142
|
+
}
|
|
134
143
|
pipeline.push({ $project: projection });
|
|
135
144
|
}
|
|
136
145
|
const res = await this.rethrow(this.getConnection('read').aggregate(entityName, pipeline, options.ctx, options.logging, options.signal));
|
|
146
|
+
if (owners) {
|
|
147
|
+
partitionLimit.partitions = res.map((row) => {
|
|
148
|
+
const owner = row.__partition;
|
|
149
|
+
delete row.__partition;
|
|
150
|
+
return [owner, row._id];
|
|
151
|
+
});
|
|
152
|
+
}
|
|
137
153
|
return res.map(r => this.mapResult(r, meta));
|
|
138
154
|
}
|
|
139
155
|
async findOne(entityName, where, options = { populate: [], orderBy: {} }) {
|
|
@@ -200,7 +216,7 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
200
216
|
const meta = this.metadata.find(entityName);
|
|
201
217
|
const pk = meta.getPrimaryProps()[0].fieldNames[0] ?? '_id';
|
|
202
218
|
const normalizedWhere = Utils.isPrimaryKey(where) ? { [pk]: where } : where;
|
|
203
|
-
const renameWhere = this.renameFields(entityName, normalizedWhere);
|
|
219
|
+
const renameWhere = this.renameFields(entityName, normalizedWhere, true);
|
|
204
220
|
const source = await this.rethrow(this.getConnection('read').find(entityName, renameWhere, { ctx: options.ctx, limit: 1 }));
|
|
205
221
|
if (!source.length) {
|
|
206
222
|
throw new Error('Cannot clone: no entity found matching the given condition');
|
|
@@ -233,6 +249,14 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
233
249
|
if (Utils.isPrimaryKey(where)) {
|
|
234
250
|
where = this.buildFilterById(entityName, where);
|
|
235
251
|
}
|
|
252
|
+
// an empty filter would match (and overwrite) every document, so insert instead
|
|
253
|
+
if (options.upsert && Utils.isEmpty(where)) {
|
|
254
|
+
const res = await this.nativeInsert(entityName, data, options);
|
|
255
|
+
res.row = { _id: res.insertId };
|
|
256
|
+
return res;
|
|
257
|
+
}
|
|
258
|
+
// work on a copy, `em.upsert()` builds the entity from the caller's data and must not see the `$inc`
|
|
259
|
+
data = { ...data };
|
|
236
260
|
this.handleVersionProperty(entityName, data, true);
|
|
237
261
|
data = this.renameFields(entityName, data);
|
|
238
262
|
where = this.renameFields(entityName, where, true);
|
|
@@ -259,6 +283,7 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
259
283
|
return row;
|
|
260
284
|
});
|
|
261
285
|
data = data.map(row => {
|
|
286
|
+
row = { ...row };
|
|
262
287
|
this.handleVersionProperty(entityName, row, true);
|
|
263
288
|
return this.renameFields(entityName, row);
|
|
264
289
|
});
|
|
@@ -386,6 +411,7 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
386
411
|
}
|
|
387
412
|
return;
|
|
388
413
|
}
|
|
414
|
+
let key = k;
|
|
389
415
|
// per-target polymorphic keys like `likeable[Comment]` resolve to the relation, whose conditions need joins
|
|
390
416
|
const [propName, targetName] = QueryHelper.splitPolymorphicKey(k);
|
|
391
417
|
const prop = meta?.properties[propName];
|
|
@@ -446,20 +472,65 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
446
472
|
const meta2 = this.metadata.find(prop.targetMeta.class);
|
|
447
473
|
const pk = meta2.properties[meta2.primaryKeys[0]];
|
|
448
474
|
isObjectId = pk.type === 'ObjectId';
|
|
475
|
+
if (dotPaths) {
|
|
476
|
+
copiedData[k] = this.inlineRelationPrimaryKey(meta, prop, meta2, copiedData[k]);
|
|
477
|
+
}
|
|
449
478
|
}
|
|
450
479
|
if (isObjectId) {
|
|
451
480
|
copiedData[k] = this.convertObjectIds(copiedData[k]);
|
|
452
481
|
}
|
|
453
482
|
if (prop.fieldNames) {
|
|
454
483
|
Utils.renameKey(copiedData, k, prop.fieldNames[0]);
|
|
484
|
+
key = prop.fieldNames[0];
|
|
455
485
|
}
|
|
456
486
|
}
|
|
457
|
-
|
|
458
|
-
|
|
487
|
+
// `dotPaths` marks a query, entity data (e.g. JSON values) must stay as-is
|
|
488
|
+
if (dotPaths) {
|
|
489
|
+
copiedData[key] = this.convertRegExpOperator(copiedData[key]);
|
|
459
490
|
}
|
|
460
491
|
});
|
|
461
492
|
return copiedData;
|
|
462
493
|
}
|
|
494
|
+
/** Relations are stored as references, so conditions can only compare them by PK, e.g. `{ author: { id } }` becomes `{ author: _id }`. */
|
|
495
|
+
inlineRelationPrimaryKey(meta, prop, meta2, value) {
|
|
496
|
+
if (prop.kind === ReferenceKind.ONE_TO_MANY || (prop.kind === ReferenceKind.MANY_TO_MANY && !prop.owner)) {
|
|
497
|
+
throw new Error(`Unsupported condition on inverse side ${meta.className}.${prop.name}, query the owning side ${meta2.className}.${prop.mappedBy} instead.`);
|
|
498
|
+
}
|
|
499
|
+
if (!Utils.isPlainObject(value)) {
|
|
500
|
+
return value;
|
|
501
|
+
}
|
|
502
|
+
const keys = Object.keys(value);
|
|
503
|
+
if (keys.length === 1 && [meta2.primaryKeys[0], meta2.serializedPrimaryKey].includes(keys[0])) {
|
|
504
|
+
return value[keys[0]];
|
|
505
|
+
}
|
|
506
|
+
if (keys.every(k => Utils.isOperator(k) && !(k in GroupOperator) && k !== '$not')) {
|
|
507
|
+
return value;
|
|
508
|
+
}
|
|
509
|
+
throw new Error(`Unsupported condition on relation ${meta.className}.${prop.name}, MongoDB can query relations only by primary key or entity reference.`);
|
|
510
|
+
}
|
|
511
|
+
convertRegExpOperator(value) {
|
|
512
|
+
if (!Utils.isPlainObject(value)) {
|
|
513
|
+
return value;
|
|
514
|
+
}
|
|
515
|
+
let cond = '$not' in value ? { ...value, $not: this.convertRegExpOperator(value.$not) } : value;
|
|
516
|
+
if (Utils.isPlainObject(cond.$elemMatch)) {
|
|
517
|
+
cond = { ...cond, $elemMatch: this.convertRegExpOperators(cond.$elemMatch) };
|
|
518
|
+
}
|
|
519
|
+
if (!('$re' in cond)) {
|
|
520
|
+
return cond;
|
|
521
|
+
}
|
|
522
|
+
const { $re, ...rest } = cond;
|
|
523
|
+
// mongo has no `$re`: a RegExp when it stands alone, `$regex` next to other operators
|
|
524
|
+
return Utils.hasObjectKeys(rest) ? { ...rest, $regex: $re } : new RegExp($re);
|
|
525
|
+
}
|
|
526
|
+
convertRegExpOperators(where) {
|
|
527
|
+
return Object.fromEntries(Object.entries(where).map(([k, v]) => [
|
|
528
|
+
k,
|
|
529
|
+
k in GroupOperator && Array.isArray(v)
|
|
530
|
+
? v.map(c => this.convertRegExpOperators(c))
|
|
531
|
+
: this.convertRegExpOperator(v),
|
|
532
|
+
]));
|
|
533
|
+
}
|
|
463
534
|
convertObjectIds(data) {
|
|
464
535
|
if (data instanceof ObjectId) {
|
|
465
536
|
return data;
|
|
@@ -515,6 +586,12 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
515
586
|
}
|
|
516
587
|
}
|
|
517
588
|
ret.unshift(...meta.primaryKeys.filter(pk => !fields.includes(pk)));
|
|
589
|
+
// `targetKey` values identify the entity like its PK, references resolve it by them
|
|
590
|
+
for (const key of meta.root.targetKeys ?? []) {
|
|
591
|
+
if (meta.properties[key] && !fields.includes(key)) {
|
|
592
|
+
ret.push(meta.properties[key].fieldNames[0]);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
518
595
|
}
|
|
519
596
|
else if (!Utils.isEmpty(exclude) || lazyProps.some(p => !p.formula)) {
|
|
520
597
|
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.
|
|
3
|
+
"version": "7.3.0-dev.21",
|
|
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.
|
|
50
|
+
"mongodb": "7.8.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@mikro-orm/core": "^7.2.
|
|
53
|
+
"@mikro-orm/core": "^7.2.8"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
|
-
"@mikro-orm/core": "7.3.0-dev.
|
|
56
|
+
"@mikro-orm/core": "7.3.0-dev.21"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
59
59
|
"node": ">= 22.17.0"
|