@mikro-orm/mongodb 7.0.0-dev.32 → 7.0.0-dev.321
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/MongoConnection.d.ts +37 -15
- package/MongoConnection.js +154 -87
- package/MongoDriver.d.ts +21 -12
- package/MongoDriver.js +140 -28
- package/MongoEntityManager.d.ts +11 -3
- package/MongoEntityManager.js +19 -5
- package/MongoExceptionConverter.d.ts +1 -1
- package/MongoExceptionConverter.js +3 -4
- package/MongoMikroORM.d.ts +10 -7
- package/MongoMikroORM.js +14 -8
- package/MongoPlatform.d.ts +2 -4
- package/MongoPlatform.js +7 -10
- package/MongoSchemaGenerator.d.ts +8 -5
- package/MongoSchemaGenerator.js +81 -35
- package/README.md +7 -4
- package/index.d.ts +1 -1
- package/package.json +32 -32
- package/tsconfig.build.tsbuildinfo +1 -0
package/MongoDriver.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { ObjectId } from 'mongodb';
|
|
2
|
-
import { DatabaseDriver, EntityManagerType, ReferenceKind, Utils, } from '@mikro-orm/core';
|
|
2
|
+
import { DatabaseDriver, EntityManagerType, PolymorphicRef, 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';
|
|
6
|
+
import { MongoMikroORM } from './MongoMikroORM.js';
|
|
6
7
|
export class MongoDriver extends DatabaseDriver {
|
|
7
8
|
[EntityManagerType];
|
|
8
9
|
connection = new MongoConnection(this.config);
|
|
@@ -14,6 +15,31 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
14
15
|
const EntityManagerClass = this.config.get('entityManager', MongoEntityManager);
|
|
15
16
|
return new EntityManagerClass(this.config, this, this.metadata, useContext);
|
|
16
17
|
}
|
|
18
|
+
async *stream(entityName, where, options) {
|
|
19
|
+
if (this.metadata.find(entityName)?.virtual) {
|
|
20
|
+
yield* this.streamVirtual(entityName, where, options);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const fields = this.buildFields(entityName, options.populate || [], options.fields, options.exclude);
|
|
24
|
+
where = this.renameFields(entityName, where, true);
|
|
25
|
+
const orderBy = Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy, true));
|
|
26
|
+
const res = this.getConnection('read').stream(entityName, where, {
|
|
27
|
+
orderBy,
|
|
28
|
+
limit: options.limit,
|
|
29
|
+
offset: options.offset,
|
|
30
|
+
fields,
|
|
31
|
+
ctx: options.ctx,
|
|
32
|
+
...this.buildQueryOptions(options),
|
|
33
|
+
});
|
|
34
|
+
for await (const item of res) {
|
|
35
|
+
if (options.rawResults) {
|
|
36
|
+
yield item;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
yield this.mapResult(item, this.metadata.find(entityName));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
17
43
|
async find(entityName, where, options = {}) {
|
|
18
44
|
if (this.metadata.find(entityName)?.virtual) {
|
|
19
45
|
return this.findVirtual(entityName, where, options);
|
|
@@ -36,14 +62,29 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
36
62
|
const { orderBy: newOrderBy, where: newWhere } = this.processCursorOptions(meta, options, options.orderBy);
|
|
37
63
|
const newWhereConverted = this.renameFields(entityName, newWhere, true);
|
|
38
64
|
const orderBy = Utils.asArray(newOrderBy).map(order => this.renameFields(entityName, order, true));
|
|
39
|
-
const res = await this.rethrow(this.getConnection('read').find(entityName, andWhere(where, newWhereConverted),
|
|
65
|
+
const res = await this.rethrow(this.getConnection('read').find(entityName, andWhere(where, newWhereConverted), {
|
|
66
|
+
orderBy,
|
|
67
|
+
limit: options.limit,
|
|
68
|
+
offset: options.offset,
|
|
69
|
+
fields,
|
|
70
|
+
ctx: options.ctx,
|
|
71
|
+
loggerContext: options.logging,
|
|
72
|
+
...this.buildQueryOptions(options),
|
|
73
|
+
}));
|
|
40
74
|
if (isCursorPagination && !first && !!last) {
|
|
41
75
|
res.reverse();
|
|
42
76
|
}
|
|
43
77
|
return res.map(r => this.mapResult(r, this.metadata.find(entityName)));
|
|
44
78
|
}
|
|
45
79
|
const orderBy = Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy, true));
|
|
46
|
-
const res = await this.rethrow(this.getConnection('read').find(entityName, where,
|
|
80
|
+
const res = await this.rethrow(this.getConnection('read').find(entityName, where, {
|
|
81
|
+
orderBy,
|
|
82
|
+
limit: options.limit,
|
|
83
|
+
offset: options.offset,
|
|
84
|
+
fields,
|
|
85
|
+
ctx: options.ctx,
|
|
86
|
+
...this.buildQueryOptions(options),
|
|
87
|
+
}));
|
|
47
88
|
return res.map(r => this.mapResult(r, this.metadata.find(entityName)));
|
|
48
89
|
}
|
|
49
90
|
async findOne(entityName, where, options = { populate: [], orderBy: {} }) {
|
|
@@ -58,7 +99,14 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
58
99
|
const fields = this.buildFields(entityName, options.populate || [], options.fields, options.exclude);
|
|
59
100
|
where = this.renameFields(entityName, where, true);
|
|
60
101
|
const orderBy = Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy, true));
|
|
61
|
-
const res = await this.rethrow(this.getConnection('read').find(entityName, where,
|
|
102
|
+
const res = await this.rethrow(this.getConnection('read').find(entityName, where, {
|
|
103
|
+
orderBy,
|
|
104
|
+
limit: 1,
|
|
105
|
+
fields,
|
|
106
|
+
ctx: options.ctx,
|
|
107
|
+
loggerContext: options.logging,
|
|
108
|
+
...this.buildQueryOptions(options),
|
|
109
|
+
}));
|
|
62
110
|
return this.mapResult(res[0], this.metadata.find(entityName));
|
|
63
111
|
}
|
|
64
112
|
async findVirtual(entityName, where, options) {
|
|
@@ -70,13 +118,29 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
70
118
|
/* v8 ignore next */
|
|
71
119
|
return super.findVirtual(entityName, where, options);
|
|
72
120
|
}
|
|
73
|
-
async
|
|
74
|
-
|
|
121
|
+
async *streamVirtual(entityName, where, options) {
|
|
122
|
+
const meta = this.metadata.find(entityName);
|
|
123
|
+
if (meta.expression instanceof Function) {
|
|
124
|
+
const em = this.createEntityManager();
|
|
125
|
+
const stream = await meta.expression(em, where, options, true);
|
|
126
|
+
yield* stream;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
/* v8 ignore next */
|
|
130
|
+
return super.findVirtual(entityName, where, options);
|
|
131
|
+
}
|
|
132
|
+
async count(entityName, where, options = {}) {
|
|
133
|
+
/* v8 ignore next */
|
|
75
134
|
if (this.metadata.find(entityName)?.virtual) {
|
|
76
135
|
return this.countVirtual(entityName, where, options);
|
|
77
136
|
}
|
|
78
137
|
where = this.renameFields(entityName, where, true);
|
|
79
|
-
|
|
138
|
+
const queryOpts = this.buildQueryOptions(options);
|
|
139
|
+
return this.rethrow(this.getConnection('read').countDocuments(entityName, where, {
|
|
140
|
+
ctx: options.ctx,
|
|
141
|
+
loggerContext: options.logging,
|
|
142
|
+
...queryOpts,
|
|
143
|
+
}));
|
|
80
144
|
}
|
|
81
145
|
async nativeInsert(entityName, data, options = {}) {
|
|
82
146
|
this.handleVersionProperty(entityName, data);
|
|
@@ -165,10 +229,38 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
165
229
|
async aggregate(entityName, pipeline, ctx) {
|
|
166
230
|
return this.rethrow(this.getConnection('read').aggregate(entityName, pipeline, ctx));
|
|
167
231
|
}
|
|
232
|
+
async *streamAggregate(entityName, pipeline, ctx) {
|
|
233
|
+
yield* this.getConnection('read').streamAggregate(entityName, pipeline, ctx);
|
|
234
|
+
}
|
|
168
235
|
getPlatform() {
|
|
169
236
|
return this.platform;
|
|
170
237
|
}
|
|
171
|
-
|
|
238
|
+
buildQueryOptions(options) {
|
|
239
|
+
if (options.collation != null && typeof options.collation === 'string') {
|
|
240
|
+
throw new Error("Collation option for MongoDB must be a CollationOptions object (e.g. { locale: 'en' }). Use a string only with SQL drivers.");
|
|
241
|
+
}
|
|
242
|
+
const ret = {};
|
|
243
|
+
if (options.collation) {
|
|
244
|
+
ret.collation = options.collation;
|
|
245
|
+
}
|
|
246
|
+
if (options.indexHint != null) {
|
|
247
|
+
ret.indexHint = options.indexHint;
|
|
248
|
+
}
|
|
249
|
+
if (options.maxTimeMS != null) {
|
|
250
|
+
ret.maxTimeMS = options.maxTimeMS;
|
|
251
|
+
}
|
|
252
|
+
if (options.allowDiskUse != null) {
|
|
253
|
+
ret.allowDiskUse = options.allowDiskUse;
|
|
254
|
+
}
|
|
255
|
+
return ret;
|
|
256
|
+
}
|
|
257
|
+
renameFields(entityName, data, dotPaths = false, object, root = true) {
|
|
258
|
+
if (data == null && root) {
|
|
259
|
+
return {};
|
|
260
|
+
}
|
|
261
|
+
if (typeof data !== 'object' || data === null) {
|
|
262
|
+
return data;
|
|
263
|
+
}
|
|
172
264
|
// copy to new variable to prevent changing the T type or doing as unknown casts
|
|
173
265
|
const copiedData = Object.assign({}, data); // copy first
|
|
174
266
|
const meta = this.metadata.find(entityName);
|
|
@@ -184,7 +276,7 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
184
276
|
for (let i = 0; i < copiedData.$and.length; i++) {
|
|
185
277
|
const and = copiedData.$and[i];
|
|
186
278
|
if ('$fulltext' in and) {
|
|
187
|
-
/* v8 ignore next
|
|
279
|
+
/* v8 ignore next */
|
|
188
280
|
if ('$fulltext' in copiedData) {
|
|
189
281
|
throw new Error('Cannot merge multiple $fulltext conditions to top level of the query object.');
|
|
190
282
|
}
|
|
@@ -204,13 +296,12 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
204
296
|
throw new Error('Full text search is only supported on the top level of the query object.');
|
|
205
297
|
}
|
|
206
298
|
Utils.keys(copiedData).forEach(k => {
|
|
207
|
-
if (Utils.
|
|
208
|
-
/* v8 ignore next 5 */
|
|
299
|
+
if (Utils.isOperator(k)) {
|
|
209
300
|
if (Array.isArray(copiedData[k])) {
|
|
210
|
-
copiedData[k] = copiedData[k].map(v => this.renameFields(entityName, v));
|
|
301
|
+
copiedData[k] = copiedData[k].map(v => this.renameFields(entityName, v, dotPaths, object, false));
|
|
211
302
|
}
|
|
212
303
|
else {
|
|
213
|
-
copiedData[k] = this.renameFields(entityName, copiedData[k]);
|
|
304
|
+
copiedData[k] = this.renameFields(entityName, copiedData[k], dotPaths, object, false);
|
|
214
305
|
}
|
|
215
306
|
return;
|
|
216
307
|
}
|
|
@@ -218,23 +309,44 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
218
309
|
const prop = meta.properties[k];
|
|
219
310
|
let isObjectId = false;
|
|
220
311
|
if (prop.kind === ReferenceKind.SCALAR) {
|
|
221
|
-
isObjectId = prop.type
|
|
312
|
+
isObjectId = prop.type === 'ObjectId';
|
|
222
313
|
}
|
|
223
314
|
else if (prop.kind === ReferenceKind.EMBEDDED) {
|
|
224
315
|
if (copiedData[prop.name] == null) {
|
|
225
316
|
return;
|
|
226
317
|
}
|
|
227
318
|
if (prop.array && Array.isArray(copiedData[prop.name])) {
|
|
228
|
-
copiedData[prop.name] = copiedData[prop.name].map((item) => this.renameFields(prop.
|
|
319
|
+
copiedData[prop.name] = copiedData[prop.name].map((item) => this.renameFields(prop.targetMeta.class, item, dotPaths, true, false));
|
|
229
320
|
}
|
|
230
321
|
else {
|
|
231
|
-
copiedData[prop.name] = this.renameFields(prop.
|
|
322
|
+
copiedData[prop.name] = this.renameFields(prop.targetMeta.class, copiedData[prop.name], dotPaths, prop.object || object, false);
|
|
232
323
|
}
|
|
233
324
|
}
|
|
325
|
+
else if (prop.polymorphic && prop.fieldNames?.length >= 2) {
|
|
326
|
+
// Polymorphic M:1: split into discriminator + FK fields
|
|
327
|
+
const value = copiedData[k];
|
|
328
|
+
delete copiedData[k];
|
|
329
|
+
if (value instanceof PolymorphicRef) {
|
|
330
|
+
copiedData[prop.fieldNames[0]] = value.discriminator;
|
|
331
|
+
const idField = prop.fieldNames[1];
|
|
332
|
+
const targetMeta = this.metadata.find(prop.discriminatorMap[value.discriminator]);
|
|
333
|
+
const hasObjectId = targetMeta && targetMeta.properties[targetMeta.primaryKeys[0]]?.type === 'ObjectId';
|
|
334
|
+
copiedData[idField] = hasObjectId ? this.convertObjectIds(value.id) : value.id;
|
|
335
|
+
}
|
|
336
|
+
else if (Array.isArray(value)) {
|
|
337
|
+
// Tuple format: [discriminator, id]
|
|
338
|
+
copiedData[prop.fieldNames[0]] = value[0];
|
|
339
|
+
copiedData[prop.fieldNames[1]] = value[1] != null ? this.convertObjectIds(value[1]) : value[1];
|
|
340
|
+
}
|
|
341
|
+
else if (value == null) {
|
|
342
|
+
prop.fieldNames.forEach(f => (copiedData[f] = null));
|
|
343
|
+
}
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
234
346
|
else {
|
|
235
|
-
const meta2 = this.metadata.find(prop.
|
|
347
|
+
const meta2 = this.metadata.find(prop.targetMeta.class);
|
|
236
348
|
const pk = meta2.properties[meta2.primaryKeys[0]];
|
|
237
|
-
isObjectId = pk.type
|
|
349
|
+
isObjectId = pk.type === 'ObjectId';
|
|
238
350
|
}
|
|
239
351
|
if (isObjectId) {
|
|
240
352
|
copiedData[k] = this.convertObjectIds(copiedData[k]);
|
|
@@ -253,7 +365,7 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
253
365
|
if (data instanceof ObjectId) {
|
|
254
366
|
return data;
|
|
255
367
|
}
|
|
256
|
-
if (
|
|
368
|
+
if (typeof data === 'string' && /^[0-9a-f]{24}$/i.exec(data)) {
|
|
257
369
|
return new ObjectId(data);
|
|
258
370
|
}
|
|
259
371
|
if (Array.isArray(data)) {
|
|
@@ -268,21 +380,18 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
268
380
|
}
|
|
269
381
|
buildFilterById(entityName, id) {
|
|
270
382
|
const meta = this.metadata.find(entityName);
|
|
271
|
-
if (meta.properties[meta.primaryKeys[0]].type
|
|
383
|
+
if (meta.properties[meta.primaryKeys[0]].type === 'ObjectId') {
|
|
272
384
|
return { _id: new ObjectId(id) };
|
|
273
385
|
}
|
|
274
386
|
return { _id: id };
|
|
275
387
|
}
|
|
276
388
|
buildFields(entityName, populate, fields, exclude) {
|
|
277
|
-
const meta = this.metadata.
|
|
278
|
-
|
|
279
|
-
return fields;
|
|
280
|
-
}
|
|
281
|
-
const lazyProps = meta.props.filter(prop => prop.lazy && !populate.some(p => p.field === prop.name || p.all));
|
|
389
|
+
const meta = this.metadata.get(entityName);
|
|
390
|
+
const lazyProps = meta.props.filter(prop => prop.lazy && !populate.some(p => this.isPopulated(meta, prop, p)));
|
|
282
391
|
const ret = [];
|
|
283
392
|
if (fields) {
|
|
284
393
|
for (let field of fields) {
|
|
285
|
-
/* v8 ignore next
|
|
394
|
+
/* v8 ignore next */
|
|
286
395
|
if (Utils.isPlainObject(field)) {
|
|
287
396
|
continue;
|
|
288
397
|
}
|
|
@@ -290,7 +399,7 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
290
399
|
field = field.toString().substring(0, field.toString().indexOf('.'));
|
|
291
400
|
}
|
|
292
401
|
let prop = meta.properties[field];
|
|
293
|
-
/* v8 ignore
|
|
402
|
+
/* v8 ignore next */
|
|
294
403
|
if (prop) {
|
|
295
404
|
if (!prop.fieldNames) {
|
|
296
405
|
continue;
|
|
@@ -305,7 +414,6 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
305
414
|
else {
|
|
306
415
|
ret.push(field);
|
|
307
416
|
}
|
|
308
|
-
/* v8 ignore stop */
|
|
309
417
|
}
|
|
310
418
|
ret.unshift(...meta.primaryKeys.filter(pk => !fields.includes(pk)));
|
|
311
419
|
}
|
|
@@ -328,4 +436,8 @@ export class MongoDriver extends DatabaseDriver {
|
|
|
328
436
|
data[versionProperty.name] ??= update ? { $inc: 1 } : 1;
|
|
329
437
|
}
|
|
330
438
|
}
|
|
439
|
+
/** @inheritDoc */
|
|
440
|
+
getORMClass() {
|
|
441
|
+
return MongoMikroORM;
|
|
442
|
+
}
|
|
331
443
|
}
|
package/MongoEntityManager.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EntityManager, type EntityName, type EntityRepository, type GetRepository, type TransactionOptions } from '@mikro-orm/core';
|
|
1
|
+
import { EntityManager, type EntityName, type EntityRepository, type GetRepository, type TransactionOptions, type StreamOptions, type Loaded } from '@mikro-orm/core';
|
|
2
2
|
import type { Collection, Document, TransactionOptions as MongoTransactionOptions } from 'mongodb';
|
|
3
3
|
import type { MongoDriver } from './MongoDriver.js';
|
|
4
4
|
import type { MongoEntityRepository } from './MongoEntityRepository.js';
|
|
@@ -9,8 +9,16 @@ export declare class MongoEntityManager<Driver extends MongoDriver = MongoDriver
|
|
|
9
9
|
/**
|
|
10
10
|
* Shortcut to driver's aggregate method. Available in MongoDriver only.
|
|
11
11
|
*/
|
|
12
|
-
aggregate(entityName: EntityName
|
|
13
|
-
|
|
12
|
+
aggregate(entityName: EntityName, pipeline: any[]): Promise<any[]>;
|
|
13
|
+
/**
|
|
14
|
+
* Shortcut to driver's aggregate method. Returns a stream. Available in MongoDriver only.
|
|
15
|
+
*/
|
|
16
|
+
streamAggregate<T extends object>(entityName: EntityName, pipeline: any[]): AsyncIterableIterator<T>;
|
|
17
|
+
/**
|
|
18
|
+
* @inheritDoc
|
|
19
|
+
*/
|
|
20
|
+
stream<Entity extends object, Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(entityName: EntityName<Entity>, options?: StreamOptions<NoInfer<Entity>, Hint, Fields, Excludes>): AsyncIterableIterator<Loaded<Entity, Hint, Fields, Excludes>>;
|
|
21
|
+
getCollection<T extends Document>(entityOrCollectionName: EntityName<T> | string): Collection<T>;
|
|
14
22
|
/**
|
|
15
23
|
* @inheritDoc
|
|
16
24
|
*/
|
package/MongoEntityManager.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EntityManager, Utils } from '@mikro-orm/core';
|
|
1
|
+
import { EntityManager, Utils, } from '@mikro-orm/core';
|
|
2
2
|
/**
|
|
3
3
|
* @inheritDoc
|
|
4
4
|
*/
|
|
@@ -7,11 +7,25 @@ export class MongoEntityManager extends EntityManager {
|
|
|
7
7
|
* Shortcut to driver's aggregate method. Available in MongoDriver only.
|
|
8
8
|
*/
|
|
9
9
|
async aggregate(entityName, pipeline) {
|
|
10
|
-
entityName
|
|
11
|
-
return this.getDriver().aggregate(entityName, pipeline);
|
|
10
|
+
return this.getDriver().aggregate(entityName, pipeline, this.getTransactionContext());
|
|
12
11
|
}
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Shortcut to driver's aggregate method. Returns a stream. Available in MongoDriver only.
|
|
14
|
+
*/
|
|
15
|
+
async *streamAggregate(entityName, pipeline) {
|
|
16
|
+
yield* this.getDriver().streamAggregate(entityName, pipeline, this.getTransactionContext());
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* @inheritDoc
|
|
20
|
+
*/
|
|
21
|
+
async *stream(entityName, options = {}) {
|
|
22
|
+
if (!Utils.isEmpty(options.populate)) {
|
|
23
|
+
throw new Error('Populate option is not supported when streaming results in MongoDB');
|
|
24
|
+
}
|
|
25
|
+
yield* super.stream(entityName, options);
|
|
26
|
+
}
|
|
27
|
+
getCollection(entityOrCollectionName) {
|
|
28
|
+
return this.getConnection().getCollection(entityOrCollectionName);
|
|
15
29
|
}
|
|
16
30
|
/**
|
|
17
31
|
* @inheritDoc
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ExceptionConverter, type Dictionary, type DriverException } from '@mikro-orm/core';
|
|
2
2
|
export declare class MongoExceptionConverter extends ExceptionConverter {
|
|
3
3
|
/**
|
|
4
|
-
* @
|
|
4
|
+
* @see https://gist.github.com/rluvaton/a97a8da46ab6541a3e5702e83b9d357b
|
|
5
5
|
*/
|
|
6
6
|
convertException(exception: Error & Dictionary): DriverException;
|
|
7
7
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { UniqueConstraintViolationException, ExceptionConverter, TableExistsException } from '@mikro-orm/core';
|
|
2
|
-
/* v8 ignore start */
|
|
1
|
+
import { UniqueConstraintViolationException, ExceptionConverter, TableExistsException, } from '@mikro-orm/core';
|
|
3
2
|
export class MongoExceptionConverter extends ExceptionConverter {
|
|
4
3
|
/**
|
|
5
|
-
* @
|
|
4
|
+
* @see https://gist.github.com/rluvaton/a97a8da46ab6541a3e5702e83b9d357b
|
|
6
5
|
*/
|
|
6
|
+
/* v8 ignore next */
|
|
7
7
|
convertException(exception) {
|
|
8
8
|
switch (exception.code) {
|
|
9
9
|
case 48:
|
|
@@ -14,4 +14,3 @@ export class MongoExceptionConverter extends ExceptionConverter {
|
|
|
14
14
|
return super.convertException(exception);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
/* v8 ignore stop */
|
package/MongoMikroORM.d.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import { MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType } from '@mikro-orm/core';
|
|
1
|
+
import { type AnyEntity, type EntityClass, type EntitySchema, MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType, type IMigrator } from '@mikro-orm/core';
|
|
2
2
|
import { MongoDriver } from './MongoDriver.js';
|
|
3
3
|
import type { MongoEntityManager } from './MongoEntityManager.js';
|
|
4
|
+
export type MongoOptions<EM extends MongoEntityManager = MongoEntityManager, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> = Options<MongoDriver, EM, Entities>;
|
|
5
|
+
export declare function defineMongoConfig<EM extends MongoEntityManager = MongoEntityManager, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(options: MongoOptions<EM, Entities>): Options<MongoDriver, EM, Entities>;
|
|
4
6
|
/**
|
|
5
7
|
* @inheritDoc
|
|
6
8
|
*/
|
|
7
|
-
export declare class MongoMikroORM<EM extends
|
|
8
|
-
private static DRIVER;
|
|
9
|
+
export declare class MongoMikroORM<EM extends MongoEntityManager = MongoEntityManager, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> extends MikroORM<MongoDriver, EM, Entities> {
|
|
9
10
|
/**
|
|
10
11
|
* @inheritDoc
|
|
11
12
|
*/
|
|
12
|
-
static init<D extends IDatabaseDriver = MongoDriver, EM extends EntityManager = D[typeof EntityManagerType] & EntityManager>(options
|
|
13
|
+
static init<D extends IDatabaseDriver = MongoDriver, EM extends EntityManager<D> = D[typeof EntityManagerType] & EntityManager<D>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(options: Options<D, EM, Entities>): Promise<MikroORM<D, EM, Entities>>;
|
|
13
14
|
/**
|
|
14
15
|
* @inheritDoc
|
|
15
16
|
*/
|
|
16
|
-
|
|
17
|
+
constructor(options: Options<MongoDriver, EM, Entities>);
|
|
18
|
+
/**
|
|
19
|
+
* Gets the Migrator.
|
|
20
|
+
*/
|
|
21
|
+
get migrator(): IMigrator;
|
|
17
22
|
}
|
|
18
|
-
export type MongoOptions = Options<MongoDriver>;
|
|
19
|
-
export declare function defineMongoConfig(options: MongoOptions): Options<MongoDriver, MongoEntityManager<MongoDriver> & EntityManager<IDatabaseDriver<import("@mikro-orm/core").Connection>>>;
|
package/MongoMikroORM.js
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
import { defineConfig, MikroORM, } from '@mikro-orm/core';
|
|
2
2
|
import { MongoDriver } from './MongoDriver.js';
|
|
3
|
+
export function defineMongoConfig(options) {
|
|
4
|
+
return defineConfig({ driver: MongoDriver, ...options });
|
|
5
|
+
}
|
|
3
6
|
/**
|
|
4
7
|
* @inheritDoc
|
|
5
8
|
*/
|
|
6
9
|
export class MongoMikroORM extends MikroORM {
|
|
7
|
-
static DRIVER = MongoDriver;
|
|
8
10
|
/**
|
|
9
11
|
* @inheritDoc
|
|
10
12
|
*/
|
|
11
13
|
static async init(options) {
|
|
12
|
-
return super.init(options);
|
|
14
|
+
return super.init(defineMongoConfig(options));
|
|
13
15
|
}
|
|
14
16
|
/**
|
|
15
17
|
* @inheritDoc
|
|
16
18
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
constructor(options) {
|
|
20
|
+
super(defineMongoConfig(options));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Gets the Migrator.
|
|
24
|
+
*/
|
|
25
|
+
get migrator() {
|
|
26
|
+
return this.driver
|
|
27
|
+
.getPlatform()
|
|
28
|
+
.getExtension('Migrator', '@mikro-orm/migrator', '@mikro-orm/migrations-mongodb', this.em);
|
|
19
29
|
}
|
|
20
|
-
}
|
|
21
|
-
/* v8 ignore next 3 */
|
|
22
|
-
export function defineMongoConfig(options) {
|
|
23
|
-
return defineConfig({ driver: MongoDriver, ...options });
|
|
24
30
|
}
|
package/MongoPlatform.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ObjectId } from 'mongodb';
|
|
2
|
-
import { Platform, type IPrimaryKey, type Primary, type NamingStrategy, type Constructor, type EntityRepository, type EntityProperty, type PopulateOptions, type EntityMetadata, type IDatabaseDriver, type EntityManager, type Configuration, type MikroORM } from '@mikro-orm/core';
|
|
2
|
+
import { Platform, type IPrimaryKey, type Primary, type NamingStrategy, type Constructor, type EntityRepository, type EntityProperty, type PopulateOptions, type EntityMetadata, type IDatabaseDriver, type EntityManager, type Configuration, type MikroORM, type TransformContext } from '@mikro-orm/core';
|
|
3
3
|
import { MongoExceptionConverter } from './MongoExceptionConverter.js';
|
|
4
4
|
import { MongoSchemaGenerator } from './MongoSchemaGenerator.js';
|
|
5
5
|
export declare class MongoPlatform extends Platform {
|
|
@@ -16,12 +16,10 @@ export declare class MongoPlatform extends Platform {
|
|
|
16
16
|
getSchemaGenerator(driver: IDatabaseDriver, em?: EntityManager): MongoSchemaGenerator;
|
|
17
17
|
normalizePrimaryKey<T extends number | string = number | string>(data: Primary<T> | IPrimaryKey | ObjectId): T;
|
|
18
18
|
denormalizePrimaryKey(data: number | string): IPrimaryKey;
|
|
19
|
-
getSerializedPrimaryKeyField(field: string): string;
|
|
20
|
-
usesDifferentSerializedPrimaryKey(): boolean;
|
|
21
19
|
usesImplicitTransactions(): boolean;
|
|
22
20
|
convertsJsonAutomatically(): boolean;
|
|
23
21
|
convertJsonToDatabaseValue(value: unknown): unknown;
|
|
24
|
-
convertJsonToJSValue(value: unknown,
|
|
22
|
+
convertJsonToJSValue(value: unknown, context?: TransformContext): unknown;
|
|
25
23
|
marshallArray(values: string[]): string;
|
|
26
24
|
cloneEmbeddable<T>(data: T): T;
|
|
27
25
|
shouldHaveColumn<T>(prop: EntityProperty<T>, populate: PopulateOptions<T>[], exclude?: string[]): boolean;
|
package/MongoPlatform.js
CHANGED
|
@@ -32,24 +32,18 @@ export class MongoPlatform extends Platform {
|
|
|
32
32
|
/* v8 ignore next */
|
|
33
33
|
return super.getExtension(extensionName, extensionKey, moduleName, em);
|
|
34
34
|
}
|
|
35
|
-
/* v8 ignore next
|
|
35
|
+
/* v8 ignore next: kept for type inference only */
|
|
36
36
|
getSchemaGenerator(driver, em) {
|
|
37
37
|
return new MongoSchemaGenerator(em ?? driver);
|
|
38
38
|
}
|
|
39
39
|
normalizePrimaryKey(data) {
|
|
40
|
-
if (Utils.
|
|
40
|
+
if (Utils.isObject(data) && data.constructor?.name === 'ObjectId') {
|
|
41
41
|
return data.toHexString();
|
|
42
42
|
}
|
|
43
43
|
return data;
|
|
44
44
|
}
|
|
45
45
|
denormalizePrimaryKey(data) {
|
|
46
|
-
return new ObjectId(data);
|
|
47
|
-
}
|
|
48
|
-
getSerializedPrimaryKeyField(field) {
|
|
49
|
-
return 'id';
|
|
50
|
-
}
|
|
51
|
-
usesDifferentSerializedPrimaryKey() {
|
|
52
|
-
return true;
|
|
46
|
+
return new ObjectId('' + data);
|
|
53
47
|
}
|
|
54
48
|
usesImplicitTransactions() {
|
|
55
49
|
return false;
|
|
@@ -60,7 +54,7 @@ export class MongoPlatform extends Platform {
|
|
|
60
54
|
convertJsonToDatabaseValue(value) {
|
|
61
55
|
return Utils.copy(value);
|
|
62
56
|
}
|
|
63
|
-
convertJsonToJSValue(value,
|
|
57
|
+
convertJsonToJSValue(value, context) {
|
|
64
58
|
return value;
|
|
65
59
|
}
|
|
66
60
|
marshallArray(values) {
|
|
@@ -78,6 +72,9 @@ export class MongoPlatform extends Platform {
|
|
|
78
72
|
return prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner;
|
|
79
73
|
}
|
|
80
74
|
validateMetadata(meta) {
|
|
75
|
+
if (meta.inheritanceType === 'tpt') {
|
|
76
|
+
throw MetadataError.tptNotSupportedByDriver(meta);
|
|
77
|
+
}
|
|
81
78
|
const pk = meta.getPrimaryProps()[0];
|
|
82
79
|
if (pk && pk.fieldNames?.[0] !== '_id') {
|
|
83
80
|
throw MetadataError.invalidPrimaryKey(meta, pk, '_id');
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type CreateSchemaOptions, type MikroORM } from '@mikro-orm/core';
|
|
2
|
+
import { AbstractSchemaGenerator } from '@mikro-orm/core/schema';
|
|
2
3
|
import type { MongoDriver } from './MongoDriver.js';
|
|
3
4
|
export declare class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoDriver> {
|
|
4
5
|
static register(orm: MikroORM): void;
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
create(options?: MongoCreateSchemaOptions): Promise<void>;
|
|
7
|
+
drop(options?: {
|
|
7
8
|
dropMigrationsTable?: boolean;
|
|
8
9
|
}): Promise<void>;
|
|
9
|
-
|
|
10
|
+
update(options?: MongoCreateSchemaOptions): Promise<void>;
|
|
10
11
|
ensureDatabase(): Promise<boolean>;
|
|
11
|
-
|
|
12
|
+
refresh(options?: MongoCreateSchemaOptions): Promise<void>;
|
|
12
13
|
dropIndexes(options?: {
|
|
13
14
|
skipIndexes?: {
|
|
14
15
|
collection: string;
|
|
@@ -17,7 +18,9 @@ export declare class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoD
|
|
|
17
18
|
collectionsWithFailedIndexes?: string[];
|
|
18
19
|
}): Promise<void>;
|
|
19
20
|
ensureIndexes(options?: EnsureIndexesOptions): Promise<void>;
|
|
21
|
+
private mapIndexProperties;
|
|
20
22
|
private createIndexes;
|
|
23
|
+
private executeQuery;
|
|
21
24
|
private createUniqueIndexes;
|
|
22
25
|
private createPropertyIndexes;
|
|
23
26
|
}
|