@mikro-orm/mongodb 7.0.0-dev.1 → 7.0.0-dev.100

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.js CHANGED
@@ -1,22 +1,39 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoDriver = void 0;
4
- const bson_1 = require("bson");
5
- const core_1 = require("@mikro-orm/core");
6
- const MongoConnection_1 = require("./MongoConnection");
7
- const MongoPlatform_1 = require("./MongoPlatform");
8
- const MongoEntityManager_1 = require("./MongoEntityManager");
9
- class MongoDriver extends core_1.DatabaseDriver {
10
- [core_1.EntityManagerType];
11
- connection = new MongoConnection_1.MongoConnection(this.config);
12
- platform = new MongoPlatform_1.MongoPlatform();
1
+ import { ObjectId } from 'mongodb';
2
+ import { DatabaseDriver, EntityManagerType, GroupOperator, ReferenceKind, Utils, } from '@mikro-orm/core';
3
+ import { MongoConnection } from './MongoConnection.js';
4
+ import { MongoPlatform } from './MongoPlatform.js';
5
+ import { MongoEntityManager } from './MongoEntityManager.js';
6
+ import { MongoMikroORM } from './MongoMikroORM.js';
7
+ export class MongoDriver extends DatabaseDriver {
8
+ [EntityManagerType];
9
+ connection = new MongoConnection(this.config);
10
+ platform = new MongoPlatform();
13
11
  constructor(config) {
14
12
  super(config, ['mongodb']);
15
13
  }
16
14
  createEntityManager(useContext) {
17
- const EntityManagerClass = this.config.get('entityManager', MongoEntityManager_1.MongoEntityManager);
15
+ const EntityManagerClass = this.config.get('entityManager', MongoEntityManager);
18
16
  return new EntityManagerClass(this.config, this, this.metadata, useContext);
19
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
+ entityName = Utils.className(entityName);
24
+ const fields = this.buildFields(entityName, options.populate || [], options.fields, options.exclude);
25
+ where = this.renameFields(entityName, where, true);
26
+ const orderBy = Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy, true));
27
+ const res = this.getConnection('read').stream(entityName, where, orderBy, options.limit, options.offset, fields, options.ctx);
28
+ for await (const item of res) {
29
+ if (options.rawResults) {
30
+ yield item;
31
+ }
32
+ else {
33
+ yield this.mapResult(item, this.metadata.find(entityName));
34
+ }
35
+ }
36
+ }
20
37
  async find(entityName, where, options = {}) {
21
38
  if (this.metadata.find(entityName)?.virtual) {
22
39
  return this.findVirtual(entityName, where, options);
@@ -27,10 +44,10 @@ class MongoDriver extends core_1.DatabaseDriver {
27
44
  const isCursorPagination = [first, last, before, after].some(v => v != null);
28
45
  if (isCursorPagination) {
29
46
  const andWhere = (cond1, cond2) => {
30
- if (core_1.Utils.isEmpty(cond1)) {
47
+ if (Utils.isEmpty(cond1)) {
31
48
  return cond2;
32
49
  }
33
- if (core_1.Utils.isEmpty(cond2)) {
50
+ if (Utils.isEmpty(cond2)) {
34
51
  return cond1;
35
52
  }
36
53
  return { $and: [cond1, cond2] };
@@ -38,29 +55,29 @@ class MongoDriver extends core_1.DatabaseDriver {
38
55
  const meta = this.metadata.find(entityName);
39
56
  const { orderBy: newOrderBy, where: newWhere } = this.processCursorOptions(meta, options, options.orderBy);
40
57
  const newWhereConverted = this.renameFields(entityName, newWhere, true);
41
- const orderBy = core_1.Utils.asArray(newOrderBy).map(order => this.renameFields(entityName, order, true));
58
+ const orderBy = Utils.asArray(newOrderBy).map(order => this.renameFields(entityName, order, true));
42
59
  const res = await this.rethrow(this.getConnection('read').find(entityName, andWhere(where, newWhereConverted), orderBy, options.limit, options.offset, fields, options.ctx, options.logging));
43
60
  if (isCursorPagination && !first && !!last) {
44
61
  res.reverse();
45
62
  }
46
63
  return res.map(r => this.mapResult(r, this.metadata.find(entityName)));
47
64
  }
48
- const orderBy = core_1.Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy, true));
65
+ const orderBy = Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy, true));
49
66
  const res = await this.rethrow(this.getConnection('read').find(entityName, where, orderBy, options.limit, options.offset, fields, options.ctx));
50
67
  return res.map(r => this.mapResult(r, this.metadata.find(entityName)));
51
68
  }
52
69
  async findOne(entityName, where, options = { populate: [], orderBy: {} }) {
53
70
  if (this.metadata.find(entityName)?.virtual) {
54
71
  const [item] = await this.findVirtual(entityName, where, options);
55
- /* istanbul ignore next */
72
+ /* v8 ignore next */
56
73
  return item ?? null;
57
74
  }
58
- if (core_1.Utils.isPrimaryKey(where)) {
75
+ if (Utils.isPrimaryKey(where)) {
59
76
  where = this.buildFilterById(entityName, where);
60
77
  }
61
78
  const fields = this.buildFields(entityName, options.populate || [], options.fields, options.exclude);
62
79
  where = this.renameFields(entityName, where, true);
63
- const orderBy = core_1.Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy, true));
80
+ const orderBy = Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy, true));
64
81
  const res = await this.rethrow(this.getConnection('read').find(entityName, where, orderBy, 1, undefined, fields, options.ctx, options.logging));
65
82
  return this.mapResult(res[0], this.metadata.find(entityName));
66
83
  }
@@ -70,11 +87,22 @@ class MongoDriver extends core_1.DatabaseDriver {
70
87
  const em = this.createEntityManager();
71
88
  return meta.expression(em, where, options);
72
89
  }
73
- /* istanbul ignore next */
90
+ /* v8 ignore next */
91
+ return super.findVirtual(entityName, where, options);
92
+ }
93
+ async *streamVirtual(entityName, where, options) {
94
+ const meta = this.metadata.find(entityName);
95
+ if (meta.expression instanceof Function) {
96
+ const em = this.createEntityManager();
97
+ const stream = await meta.expression(em, where, options, true);
98
+ yield* stream;
99
+ return;
100
+ }
101
+ /* v8 ignore next */
74
102
  return super.findVirtual(entityName, where, options);
75
103
  }
76
104
  async count(entityName, where, options = {}, ctx) {
77
- /* istanbul ignore next */
105
+ /* v8 ignore next */
78
106
  if (this.metadata.find(entityName)?.virtual) {
79
107
  return this.countVirtual(entityName, where, options);
80
108
  }
@@ -82,27 +110,32 @@ class MongoDriver extends core_1.DatabaseDriver {
82
110
  return this.rethrow(this.getConnection('read').countDocuments(entityName, where, ctx));
83
111
  }
84
112
  async nativeInsert(entityName, data, options = {}) {
113
+ this.handleVersionProperty(entityName, data);
85
114
  data = this.renameFields(entityName, data);
86
115
  return this.rethrow(this.getConnection('write').insertOne(entityName, data, options.ctx));
87
116
  }
88
117
  async nativeInsertMany(entityName, data, options = {}) {
89
- data = data.map(d => this.renameFields(entityName, d));
118
+ data = data.map(item => {
119
+ this.handleVersionProperty(entityName, item);
120
+ return this.renameFields(entityName, item);
121
+ });
90
122
  const meta = this.metadata.find(entityName);
91
- /* istanbul ignore next */
123
+ /* v8 ignore next */
92
124
  const pk = meta?.getPrimaryProps()[0].fieldNames[0] ?? '_id';
93
125
  const res = await this.rethrow(this.getConnection('write').insertMany(entityName, data, options.ctx));
94
126
  res.rows = res.insertedIds.map(id => ({ [pk]: id }));
95
127
  return res;
96
128
  }
97
129
  async nativeUpdate(entityName, where, data, options = {}) {
98
- if (core_1.Utils.isPrimaryKey(where)) {
130
+ if (Utils.isPrimaryKey(where)) {
99
131
  where = this.buildFilterById(entityName, where);
100
132
  }
101
- where = this.renameFields(entityName, where, true);
133
+ this.handleVersionProperty(entityName, data, true);
102
134
  data = this.renameFields(entityName, data);
135
+ where = this.renameFields(entityName, where, true);
103
136
  options = { ...options };
104
137
  const meta = this.metadata.find(entityName);
105
- /* istanbul ignore next */
138
+ /* v8 ignore next */
106
139
  const rename = (field) => meta ? (meta.properties[field]?.fieldNames[0] ?? field) : field;
107
140
  if (options.onConflictFields && Array.isArray(options.onConflictFields)) {
108
141
  options.onConflictFields = options.onConflictFields.map(rename);
@@ -117,15 +150,18 @@ class MongoDriver extends core_1.DatabaseDriver {
117
150
  }
118
151
  async nativeUpdateMany(entityName, where, data, options = {}) {
119
152
  where = where.map(row => {
120
- if (core_1.Utils.isPlainObject(row)) {
153
+ if (Utils.isPlainObject(row)) {
121
154
  return this.renameFields(entityName, row, true);
122
155
  }
123
156
  return row;
124
157
  });
125
- data = data.map(row => this.renameFields(entityName, row));
158
+ data = data.map(row => {
159
+ this.handleVersionProperty(entityName, row, true);
160
+ return this.renameFields(entityName, row);
161
+ });
126
162
  options = { ...options };
127
163
  const meta = this.metadata.find(entityName);
128
- /* istanbul ignore next */
164
+ /* v8 ignore next */
129
165
  const rename = (field) => meta ? (meta.properties[field]?.fieldNames[0] ?? field) : field;
130
166
  if (options.onConflictFields && Array.isArray(options.onConflictFields)) {
131
167
  options.onConflictFields = options.onConflictFields.map(rename);
@@ -136,13 +172,13 @@ class MongoDriver extends core_1.DatabaseDriver {
136
172
  if (options.onConflictExcludeFields) {
137
173
  options.onConflictExcludeFields = options.onConflictExcludeFields.map(rename);
138
174
  }
139
- /* istanbul ignore next */
175
+ /* v8 ignore next */
140
176
  const pk = meta?.getPrimaryProps()[0].fieldNames[0] ?? '_id';
141
177
  const res = await this.rethrow(this.getConnection('write').bulkUpdateMany(entityName, where, data, options.ctx, options.upsert, options));
142
178
  if (res.insertedIds) {
143
179
  let i = 0;
144
180
  res.rows = where.map(cond => {
145
- if (core_1.Utils.isEmpty(cond)) {
181
+ if (Utils.isEmpty(cond)) {
146
182
  return { [pk]: res.insertedIds[i++] };
147
183
  }
148
184
  return { [pk]: cond[pk] };
@@ -151,7 +187,7 @@ class MongoDriver extends core_1.DatabaseDriver {
151
187
  return res;
152
188
  }
153
189
  async nativeDelete(entityName, where, options = {}) {
154
- if (core_1.Utils.isPrimaryKey(where)) {
190
+ if (Utils.isPrimaryKey(where)) {
155
191
  where = this.buildFilterById(entityName, where);
156
192
  }
157
193
  where = this.renameFields(entityName, where, true);
@@ -160,6 +196,9 @@ class MongoDriver extends core_1.DatabaseDriver {
160
196
  async aggregate(entityName, pipeline, ctx) {
161
197
  return this.rethrow(this.getConnection('read').aggregate(entityName, pipeline, ctx));
162
198
  }
199
+ async *streamAggregate(entityName, pipeline, ctx) {
200
+ yield* this.getConnection('read').streamAggregate(entityName, pipeline, ctx);
201
+ }
163
202
  getPlatform() {
164
203
  return this.platform;
165
204
  }
@@ -168,7 +207,7 @@ class MongoDriver extends core_1.DatabaseDriver {
168
207
  const copiedData = Object.assign({}, data); // copy first
169
208
  const meta = this.metadata.find(entityName);
170
209
  if (meta?.serializedPrimaryKey && !meta.embeddable && meta.serializedPrimaryKey !== meta.primaryKeys[0]) {
171
- core_1.Utils.renameKey(copiedData, meta.serializedPrimaryKey, meta.primaryKeys[0]);
210
+ Utils.renameKey(copiedData, meta.serializedPrimaryKey, meta.primaryKeys[0]);
172
211
  }
173
212
  if (meta && !meta.embeddable) {
174
213
  this.inlineEmbeddables(meta, copiedData, dotPaths);
@@ -179,7 +218,7 @@ class MongoDriver extends core_1.DatabaseDriver {
179
218
  for (let i = 0; i < copiedData.$and.length; i++) {
180
219
  const and = copiedData.$and[i];
181
220
  if ('$fulltext' in and) {
182
- /* istanbul ignore next */
221
+ /* v8 ignore next */
183
222
  if ('$fulltext' in copiedData) {
184
223
  throw new Error('Cannot merge multiple $fulltext conditions to top level of the query object.');
185
224
  }
@@ -195,12 +234,12 @@ class MongoDriver extends core_1.DatabaseDriver {
195
234
  }
196
235
  // mongo only allows the $text operator in the root of the object and will
197
236
  // search all documents where the field has a text index.
198
- if (core_1.Utils.hasNestedKey(copiedData, '$fulltext')) {
237
+ if (Utils.hasNestedKey(copiedData, '$fulltext')) {
199
238
  throw new Error('Full text search is only supported on the top level of the query object.');
200
239
  }
201
- core_1.Utils.keys(copiedData).forEach(k => {
202
- if (core_1.Utils.isGroupOperator(k)) {
203
- /* istanbul ignore else */
240
+ Utils.keys(copiedData).forEach(k => {
241
+ if (k in GroupOperator) {
242
+ /* v8 ignore next */
204
243
  if (Array.isArray(copiedData[k])) {
205
244
  copiedData[k] = copiedData[k].map(v => this.renameFields(entityName, v));
206
245
  }
@@ -212,10 +251,10 @@ class MongoDriver extends core_1.DatabaseDriver {
212
251
  if (meta?.properties[k]) {
213
252
  const prop = meta.properties[k];
214
253
  let isObjectId = false;
215
- if (prop.kind === core_1.ReferenceKind.SCALAR) {
216
- isObjectId = prop.type.toLowerCase() === 'objectid';
254
+ if (prop.kind === ReferenceKind.SCALAR) {
255
+ isObjectId = prop.type === 'ObjectId';
217
256
  }
218
- else if (prop.kind === core_1.ReferenceKind.EMBEDDED) {
257
+ else if (prop.kind === ReferenceKind.EMBEDDED) {
219
258
  if (copiedData[prop.name] == null) {
220
259
  return;
221
260
  }
@@ -229,32 +268,32 @@ class MongoDriver extends core_1.DatabaseDriver {
229
268
  else {
230
269
  const meta2 = this.metadata.find(prop.type);
231
270
  const pk = meta2.properties[meta2.primaryKeys[0]];
232
- isObjectId = pk.type.toLowerCase() === 'objectid';
271
+ isObjectId = pk.type === 'ObjectId';
233
272
  }
234
273
  if (isObjectId) {
235
274
  copiedData[k] = this.convertObjectIds(copiedData[k]);
236
275
  }
237
276
  if (prop.fieldNames) {
238
- core_1.Utils.renameKey(copiedData, k, prop.fieldNames[0]);
277
+ Utils.renameKey(copiedData, k, prop.fieldNames[0]);
239
278
  }
240
279
  }
241
- if (core_1.Utils.isPlainObject(copiedData[k]) && '$re' in copiedData[k]) {
280
+ if (Utils.isPlainObject(copiedData[k]) && '$re' in copiedData[k]) {
242
281
  copiedData[k] = new RegExp(copiedData[k].$re);
243
282
  }
244
283
  });
245
284
  return copiedData;
246
285
  }
247
286
  convertObjectIds(data) {
248
- if (data instanceof bson_1.ObjectId) {
287
+ if (data instanceof ObjectId) {
249
288
  return data;
250
289
  }
251
- if (core_1.Utils.isString(data) && data.match(/^[0-9a-f]{24}$/i)) {
252
- return new bson_1.ObjectId(data);
290
+ if (typeof data === 'string' && data.match(/^[0-9a-f]{24}$/i)) {
291
+ return new ObjectId(data);
253
292
  }
254
293
  if (Array.isArray(data)) {
255
294
  return data.map((item) => this.convertObjectIds(item));
256
295
  }
257
- if (core_1.Utils.isObject(data)) {
296
+ if (Utils.isObject(data)) {
258
297
  Object.keys(data).forEach(k => {
259
298
  data[k] = this.convertObjectIds(data[k]);
260
299
  });
@@ -263,8 +302,8 @@ class MongoDriver extends core_1.DatabaseDriver {
263
302
  }
264
303
  buildFilterById(entityName, id) {
265
304
  const meta = this.metadata.find(entityName);
266
- if (meta.properties[meta.primaryKeys[0]].type.toLowerCase() === 'objectid') {
267
- return { _id: new bson_1.ObjectId(id) };
305
+ if (meta.properties[meta.primaryKeys[0]].type === 'ObjectId') {
306
+ return { _id: new ObjectId(id) };
268
307
  }
269
308
  return { _id: id };
270
309
  }
@@ -273,19 +312,19 @@ class MongoDriver extends core_1.DatabaseDriver {
273
312
  if (!meta) {
274
313
  return fields;
275
314
  }
276
- const lazyProps = meta.props.filter(prop => prop.lazy && !populate.some(p => p.field === prop.name || p.all));
315
+ const lazyProps = meta.props.filter(prop => prop.lazy && !populate.some(p => this.isPopulated(meta, prop, p)));
277
316
  const ret = [];
278
317
  if (fields) {
279
318
  for (let field of fields) {
280
- /* istanbul ignore next */
281
- if (core_1.Utils.isPlainObject(field)) {
319
+ /* v8 ignore next */
320
+ if (Utils.isPlainObject(field)) {
282
321
  continue;
283
322
  }
284
323
  if (field.toString().includes('.')) {
285
324
  field = field.toString().substring(0, field.toString().indexOf('.'));
286
325
  }
287
326
  let prop = meta.properties[field];
288
- /* istanbul ignore else */
327
+ /* v8 ignore next */
289
328
  if (prop) {
290
329
  if (!prop.fieldNames) {
291
330
  continue;
@@ -295,7 +334,7 @@ class MongoDriver extends core_1.DatabaseDriver {
295
334
  }
296
335
  else if (field === '*') {
297
336
  const props = meta.props.filter(prop => this.platform.shouldHaveColumn(prop, populate));
298
- ret.push(...core_1.Utils.flatten(props.filter(p => !lazyProps.includes(p)).map(p => p.fieldNames)));
337
+ ret.push(...Utils.flatten(props.filter(p => !lazyProps.includes(p)).map(p => p.fieldNames)));
299
338
  }
300
339
  else {
301
340
  ret.push(field);
@@ -303,11 +342,27 @@ class MongoDriver extends core_1.DatabaseDriver {
303
342
  }
304
343
  ret.unshift(...meta.primaryKeys.filter(pk => !fields.includes(pk)));
305
344
  }
306
- else if (!core_1.Utils.isEmpty(exclude) || lazyProps.some(p => !p.formula)) {
345
+ else if (!Utils.isEmpty(exclude) || lazyProps.some(p => !p.formula)) {
307
346
  const props = meta.props.filter(prop => this.platform.shouldHaveColumn(prop, populate, exclude));
308
- ret.push(...core_1.Utils.flatten(props.filter(p => !lazyProps.includes(p)).map(p => p.fieldNames)));
347
+ ret.push(...Utils.flatten(props.filter(p => !lazyProps.includes(p)).map(p => p.fieldNames)));
309
348
  }
310
349
  return ret.length > 0 ? ret : undefined;
311
350
  }
351
+ handleVersionProperty(entityName, data, update = false) {
352
+ const meta = this.metadata.find(entityName);
353
+ if (!meta?.versionProperty) {
354
+ return;
355
+ }
356
+ const versionProperty = meta.properties[meta.versionProperty];
357
+ if (versionProperty.runtimeType === 'Date') {
358
+ data[versionProperty.name] ??= new Date();
359
+ }
360
+ else {
361
+ data[versionProperty.name] ??= update ? { $inc: 1 } : 1;
362
+ }
363
+ }
364
+ /** @inheritDoc */
365
+ getORMClass() {
366
+ return MongoMikroORM;
367
+ }
312
368
  }
313
- exports.MongoDriver = MongoDriver;
@@ -1,7 +1,7 @@
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 NoInfer, type Loaded } from '@mikro-orm/core';
2
2
  import type { Collection, Document, TransactionOptions as MongoTransactionOptions } from 'mongodb';
3
- import type { MongoDriver } from './MongoDriver';
4
- import type { MongoEntityRepository } from './MongoEntityRepository';
3
+ import type { MongoDriver } from './MongoDriver.js';
4
+ import type { MongoEntityRepository } from './MongoEntityRepository.js';
5
5
  /**
6
6
  * @inheritDoc
7
7
  */
@@ -10,6 +10,14 @@ export declare class MongoEntityManager<Driver extends MongoDriver = MongoDriver
10
10
  * Shortcut to driver's aggregate method. Available in MongoDriver only.
11
11
  */
12
12
  aggregate(entityName: EntityName<any>, 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<any>, 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>>;
13
21
  getCollection<T extends Document>(entityName: EntityName<T>): Collection<T>;
14
22
  /**
15
23
  * @inheritDoc
@@ -1,17 +1,30 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoEntityManager = void 0;
4
- const core_1 = require("@mikro-orm/core");
1
+ import { EntityManager, Utils, } from '@mikro-orm/core';
5
2
  /**
6
3
  * @inheritDoc
7
4
  */
8
- class MongoEntityManager extends core_1.EntityManager {
5
+ export class MongoEntityManager extends EntityManager {
9
6
  /**
10
7
  * Shortcut to driver's aggregate method. Available in MongoDriver only.
11
8
  */
12
9
  async aggregate(entityName, pipeline) {
13
- entityName = core_1.Utils.className(entityName);
14
- return this.getDriver().aggregate(entityName, pipeline);
10
+ entityName = Utils.className(entityName);
11
+ return this.getDriver().aggregate(entityName, pipeline, this.getTransactionContext());
12
+ }
13
+ /**
14
+ * Shortcut to driver's aggregate method. Returns a stream. Available in MongoDriver only.
15
+ */
16
+ async *streamAggregate(entityName, pipeline) {
17
+ entityName = Utils.className(entityName);
18
+ yield* this.getDriver().streamAggregate(entityName, pipeline, this.getTransactionContext());
19
+ }
20
+ /**
21
+ * @inheritDoc
22
+ */
23
+ async *stream(entityName, options = {}) {
24
+ if (!Utils.isEmpty(options.populate)) {
25
+ throw new Error('Populate option is not supported when streaming results in MongoDB');
26
+ }
27
+ yield* super.stream(entityName, options);
15
28
  }
16
29
  getCollection(entityName) {
17
30
  return this.getConnection().getCollection(entityName);
@@ -35,4 +48,3 @@ class MongoEntityManager extends core_1.EntityManager {
35
48
  return super.transactional(cb, options);
36
49
  }
37
50
  }
38
- exports.MongoEntityManager = MongoEntityManager;
@@ -1,6 +1,6 @@
1
1
  import { EntityRepository, type EntityName } from '@mikro-orm/core';
2
2
  import type { Collection } from 'mongodb';
3
- import type { MongoEntityManager } from './MongoEntityManager';
3
+ import type { MongoEntityManager } from './MongoEntityManager.js';
4
4
  export declare class MongoEntityRepository<T extends object> extends EntityRepository<T> {
5
5
  protected readonly em: MongoEntityManager;
6
6
  constructor(em: MongoEntityManager, entityName: EntityName<T>);
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoEntityRepository = void 0;
4
- const core_1 = require("@mikro-orm/core");
5
- class MongoEntityRepository extends core_1.EntityRepository {
1
+ import { EntityRepository } from '@mikro-orm/core';
2
+ export class MongoEntityRepository extends EntityRepository {
6
3
  em;
7
4
  constructor(em, entityName) {
8
5
  super(em, entityName);
@@ -24,4 +21,3 @@ class MongoEntityRepository extends core_1.EntityRepository {
24
21
  return this.em;
25
22
  }
26
23
  }
27
- exports.MongoEntityRepository = MongoEntityRepository;
@@ -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
- * @link https://gist.github.com/rluvaton/a97a8da46ab6541a3e5702e83b9d357b
4
+ * @see https://gist.github.com/rluvaton/a97a8da46ab6541a3e5702e83b9d357b
5
5
  */
6
6
  convertException(exception: Error & Dictionary): DriverException;
7
7
  }
@@ -1,20 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoExceptionConverter = void 0;
4
- const core_1 = require("@mikro-orm/core");
5
- class MongoExceptionConverter extends core_1.ExceptionConverter {
6
- /* istanbul ignore next */
1
+ import { UniqueConstraintViolationException, ExceptionConverter, TableExistsException } from '@mikro-orm/core';
2
+ export class MongoExceptionConverter extends ExceptionConverter {
7
3
  /**
8
- * @link https://gist.github.com/rluvaton/a97a8da46ab6541a3e5702e83b9d357b
4
+ * @see https://gist.github.com/rluvaton/a97a8da46ab6541a3e5702e83b9d357b
9
5
  */
6
+ /* v8 ignore next */
10
7
  convertException(exception) {
11
8
  switch (exception.code) {
12
9
  case 48:
13
- return new core_1.TableExistsException(exception);
10
+ return new TableExistsException(exception);
14
11
  case 11000:
15
- return new core_1.UniqueConstraintViolationException(exception);
12
+ return new UniqueConstraintViolationException(exception);
16
13
  }
17
14
  return super.convertException(exception);
18
15
  }
19
16
  }
20
- exports.MongoExceptionConverter = MongoExceptionConverter;
@@ -1,19 +1,22 @@
1
- import { MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType } from '@mikro-orm/core';
2
- import { MongoDriver } from './MongoDriver';
3
- import type { MongoEntityManager } from './MongoEntityManager';
1
+ import { type AnyEntity, type EntityClass, type EntitySchema, MikroORM, type Options, type IDatabaseDriver, type EntityManager, type EntityManagerType, type IMigrator } from '@mikro-orm/core';
2
+ import { MongoDriver } from './MongoDriver.js';
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 EntityManager = MongoEntityManager> extends MikroORM<MongoDriver, EM> {
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?: Options<D, EM>): Promise<MikroORM<D, EM>>;
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
- static initSync<D extends IDatabaseDriver = MongoDriver, EM extends EntityManager = D[typeof EntityManagerType] & EntityManager>(options: Options<D, EM>): MikroORM<D, EM>;
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,29 +1,28 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoMikroORM = void 0;
4
- exports.defineMongoConfig = defineMongoConfig;
5
- const core_1 = require("@mikro-orm/core");
6
- const MongoDriver_1 = require("./MongoDriver");
1
+ import { defineConfig, MikroORM, } from '@mikro-orm/core';
2
+ import { MongoDriver } from './MongoDriver.js';
3
+ export function defineMongoConfig(options) {
4
+ return defineConfig({ driver: MongoDriver, ...options });
5
+ }
7
6
  /**
8
7
  * @inheritDoc
9
8
  */
10
- class MongoMikroORM extends core_1.MikroORM {
11
- static DRIVER = MongoDriver_1.MongoDriver;
9
+ export class MongoMikroORM extends MikroORM {
12
10
  /**
13
11
  * @inheritDoc
14
12
  */
15
13
  static async init(options) {
16
- return super.init(options);
14
+ return super.init(defineMongoConfig(options));
17
15
  }
18
16
  /**
19
17
  * @inheritDoc
20
18
  */
21
- static initSync(options) {
22
- return super.initSync(options);
19
+ constructor(options) {
20
+ super(defineMongoConfig(options));
21
+ }
22
+ /**
23
+ * Gets the Migrator.
24
+ */
25
+ get migrator() {
26
+ return this.driver.getPlatform().getExtension('Migrator', '@mikro-orm/migrator', '@mikro-orm/migrations-mongodb', this.em);
23
27
  }
24
- }
25
- exports.MongoMikroORM = MongoMikroORM;
26
- /* istanbul ignore next */
27
- function defineMongoConfig(options) {
28
- return (0, core_1.defineConfig)({ driver: MongoDriver_1.MongoDriver, ...options });
29
28
  }
@@ -1,7 +1,7 @@
1
- import { ObjectId } from 'bson';
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';
3
- import { MongoExceptionConverter } from './MongoExceptionConverter';
4
- import { MongoSchemaGenerator } from './MongoSchemaGenerator';
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, type TransformContext } from '@mikro-orm/core';
3
+ import { MongoExceptionConverter } from './MongoExceptionConverter.js';
4
+ import { MongoSchemaGenerator } from './MongoSchemaGenerator.js';
5
5
  export declare class MongoPlatform extends Platform {
6
6
  protected readonly exceptionConverter: MongoExceptionConverter;
7
7
  setConfig(config: Configuration): void;
@@ -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, prop: EntityProperty): 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;