@mikro-orm/mongodb 7.0.0-dev.97 → 7.0.0-dev.99

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.
@@ -1,12 +1,12 @@
1
1
  import { type ClientSession, type Collection, type Db, MongoClient, type MongoClientOptions, type TransactionOptions } from 'mongodb';
2
2
  import { type AnyEntity, type Configuration, Connection, type ConnectionOptions, type ConnectionType, type EntityData, type EntityName, type FilterQuery, type IsolationLevel, type LoggingOptions, type QueryOrderMap, type QueryResult, type Transaction, type TransactionEventBroadcaster, type UpsertManyOptions, type UpsertOptions } from '@mikro-orm/core';
3
3
  export declare class MongoConnection extends Connection {
4
- protected client: MongoClient;
5
- protected db: Db;
4
+ #private;
6
5
  constructor(config: Configuration, options?: ConnectionOptions, type?: ConnectionType);
7
6
  connect(options?: {
8
7
  skipOnConnect?: boolean;
9
8
  }): Promise<void>;
9
+ createClient(): void;
10
10
  close(force?: boolean): Promise<void>;
11
11
  isConnected(): Promise<boolean>;
12
12
  checkConnection(): Promise<{
@@ -1,8 +1,8 @@
1
1
  import { MongoClient, ObjectId, } from 'mongodb';
2
2
  import { Connection, EventType, QueryOrder, Utils, ValidationError, inspect, } from '@mikro-orm/core';
3
3
  export class MongoConnection extends Connection {
4
- client;
5
- db;
4
+ #client;
5
+ #db;
6
6
  constructor(config, options, type = 'write') {
7
7
  super(config, options, type);
8
8
  // @ts-ignore
@@ -15,37 +15,40 @@ export class MongoConnection extends Connection {
15
15
  };
16
16
  }
17
17
  async connect(options) {
18
+ this.getClient();
19
+ this.connected = true;
20
+ if (options?.skipOnConnect !== true) {
21
+ await this.onConnect();
22
+ }
23
+ }
24
+ createClient() {
18
25
  let driverOptions = this.options.driverOptions ?? this.config.get('driverOptions');
19
26
  if (typeof driverOptions === 'function') {
20
- driverOptions = await driverOptions();
27
+ driverOptions = driverOptions();
21
28
  }
22
29
  if (driverOptions instanceof MongoClient) {
23
30
  this.logger.log('info', 'Reusing MongoClient provided via `driverOptions`');
24
- this.client = driverOptions;
31
+ this.#client = driverOptions;
25
32
  }
26
33
  else {
27
- this.client = new MongoClient(this.config.get('clientUrl'), this.mapOptions(driverOptions));
28
- await this.client.connect();
34
+ this.#client = new MongoClient(this.config.get('clientUrl'), this.mapOptions(driverOptions));
29
35
  const onCreateConnection = this.options.onCreateConnection ?? this.config.get('onCreateConnection');
30
36
  /* v8 ignore next */
31
- this.client.on('connectionCreated', () => {
32
- void onCreateConnection?.(this.client);
37
+ this.#client.on('connectionCreated', () => {
38
+ void onCreateConnection?.(this.#client);
33
39
  });
34
40
  }
35
- this.db = this.client.db(this.config.get('dbName'));
36
- this.connected = true;
37
- if (options?.skipOnConnect !== true) {
38
- await this.onConnect();
39
- }
41
+ this.#db = this.#client.db(this.config.get('dbName'));
40
42
  }
41
43
  async close(force) {
42
- await this.client?.close(force);
44
+ await this.#client?.close(force);
43
45
  this.connected = false;
46
+ this.#client = undefined;
44
47
  }
45
48
  async isConnected() {
46
49
  try {
47
- const res = await this.db?.command({ ping: 1 });
48
- return this.connected = !!res.ok;
50
+ const res = await this.#db?.command({ ping: 1 });
51
+ return this.connected = !!res?.ok;
49
52
  }
50
53
  catch (error) {
51
54
  return this.connected = false;
@@ -53,8 +56,8 @@ export class MongoConnection extends Connection {
53
56
  }
54
57
  async checkConnection() {
55
58
  try {
56
- const res = await this.db?.command({ ping: 1 });
57
- return res.ok
59
+ const res = await this.#db?.command({ ping: 1 });
60
+ return res?.ok
58
61
  ? { ok: true }
59
62
  : { ok: false, reason: 'Ping reply does not feature "ok" property, or it evaluates to "false"' };
60
63
  }
@@ -63,20 +66,23 @@ export class MongoConnection extends Connection {
63
66
  }
64
67
  }
65
68
  getClient() {
66
- return this.client;
69
+ if (!this.#client) {
70
+ this.createClient();
71
+ }
72
+ return this.#client;
67
73
  }
68
74
  getCollection(name) {
69
- return this.db.collection(this.getCollectionName(name));
75
+ return this.getDb().collection(this.getCollectionName(name));
70
76
  }
71
77
  async createCollection(name) {
72
- return this.db.createCollection(this.getCollectionName(name));
78
+ return this.getDb().createCollection(this.getCollectionName(name));
73
79
  }
74
80
  async listCollections() {
75
- const collections = await this.db.listCollections({}, { nameOnly: true }).toArray();
81
+ const collections = await this.getDb().listCollections({}, { nameOnly: true }).toArray();
76
82
  return collections.map(c => c.name);
77
83
  }
78
84
  async dropCollection(name) {
79
- return this.db.dropCollection(this.getCollectionName(name));
85
+ return this.getDb().dropCollection(this.getCollectionName(name));
80
86
  }
81
87
  mapOptions(overrides) {
82
88
  const ret = {};
@@ -99,7 +105,8 @@ export class MongoConnection extends Connection {
99
105
  return Utils.mergeConfig(ret, overrides);
100
106
  }
101
107
  getDb() {
102
- return this.db;
108
+ this.#db ??= this.getClient().db(this.config.get('dbName'));
109
+ return this.#db;
103
110
  }
104
111
  async execute(query) {
105
112
  throw new Error(`${this.constructor.name} does not support generic execute method`);
@@ -210,7 +217,7 @@ export class MongoConnection extends Connection {
210
217
  if (!ctx) {
211
218
  await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart);
212
219
  }
213
- const session = ctx || this.client.startSession();
220
+ const session = ctx || this.getClient().startSession();
214
221
  session.startTransaction(txOptions);
215
222
  this.logQuery('db.begin();');
216
223
  await eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, session);
package/MongoDriver.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { type ClientSession } from 'mongodb';
2
- import { type Configuration, type CountOptions, DatabaseDriver, type EntityData, type EntityDictionary, type EntityField, EntityManagerType, type EntityName, type FilterQuery, type FindOneOptions, type FindOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type PopulateOptions, type PopulatePath, type QueryResult, type StreamOptions, type Transaction, type UpsertManyOptions, type UpsertOptions } from '@mikro-orm/core';
2
+ import { type Configuration, type Constructor, type CountOptions, DatabaseDriver, type EntityData, type EntityDictionary, type EntityField, EntityManagerType, type EntityName, type FilterQuery, type FindOneOptions, type FindOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type PopulateOptions, type PopulatePath, type QueryResult, type StreamOptions, type Transaction, type UpsertManyOptions, type UpsertOptions } 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 declare class MongoDriver extends DatabaseDriver<MongoConnection> {
7
8
  [EntityManagerType]: MongoEntityManager<this>;
8
9
  protected readonly connection: MongoConnection;
@@ -32,4 +33,6 @@ export declare class MongoDriver extends DatabaseDriver<MongoConnection> {
32
33
  private buildFilterById;
33
34
  protected buildFields<T extends object, P extends string = never>(entityName: string, populate: PopulateOptions<T>[], fields?: readonly EntityField<T, P>[], exclude?: string[]): string[] | undefined;
34
35
  private handleVersionProperty;
36
+ /** @inheritDoc */
37
+ getORMClass(): Constructor<MongoMikroORM>;
35
38
  }
package/MongoDriver.js CHANGED
@@ -3,6 +3,7 @@ import { DatabaseDriver, EntityManagerType, GroupOperator, ReferenceKind, Utils,
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);
@@ -360,4 +361,8 @@ export class MongoDriver extends DatabaseDriver {
360
361
  data[versionProperty.name] ??= update ? { $inc: 1 } : 1;
361
362
  }
362
363
  }
364
+ /** @inheritDoc */
365
+ getORMClass() {
366
+ return MongoMikroORM;
367
+ }
363
368
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/mongodb",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.97",
4
+ "version": "7.0.0-dev.99",
5
5
  "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.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -56,6 +56,6 @@
56
56
  "@mikro-orm/core": "^6.6.2"
57
57
  },
58
58
  "peerDependencies": {
59
- "@mikro-orm/core": "7.0.0-dev.97"
59
+ "@mikro-orm/core": "7.0.0-dev.99"
60
60
  }
61
61
  }