diff --git a/README.md b/README.md index 988d309..f76427e 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,18 @@ migrator.config({ logIfLatest: true, // migrations collection name. Defaults to 'migrations' collectionName: 'migrations', - // mongdb url or mongo Db instance - db: "your connection string", + // mongdb connection properties object or mongo Db instance + db: { + // mongdb connection url + connectionUrl: "your connection string", + // optional database name, in case using it in connection string is not an option + name: "your database name", + // optional mongdb Client options + options: { + useNewUrlParser: true, + useUnifiedTopology: true, + }, + } }); // Returns a promise ``` @@ -59,8 +69,18 @@ var migrator = new Migration({ logIfLatest: true, // migrations collection name collectionName: 'migrations', - // mongdb url or mongo Db instance - db: "your connection string", + // mongdb connection properties object or mongo Db instance + db: { + // mongdb connection url + connectionUrl: "your connection string", + // optional database name, in case using it in connection string is not an option + name: "your database name", + // optional mongdb Client options + options: { + useNewUrlParser: true, + useUnifiedTopology: true, + }, + } }) await migrator.config(); // Returns a promise ``` @@ -193,8 +213,15 @@ migrator.config({ logIfLatest: true, // migrations collection name to use in the database collectionName: "migrations" - // mongdb url or mongo Db instance - db: "your connection string", + // mongdb connection properties object or mongo Db instance + db: { + // mongdb connection url + connectionUrl: "your connection string", + // optional database name, in case using it in connection string is not an option + name: null, + // optional mongdb Client options + options: null, + } }); ``` diff --git a/src/migration.ts b/src/migration.ts index 1058994..4b13607 100644 --- a/src/migration.ts +++ b/src/migration.ts @@ -24,18 +24,24 @@ */ import * as _ from 'lodash'; -import { Collection, Db, MongoClient } from 'mongodb'; +import { Collection, Db, MongoClient, MongoClientOptions } from 'mongodb'; import { typeCheck } from 'type-check'; const check = typeCheck; export type SyslogLevels = 'debug' | 'info' | 'notice' | 'warning' | 'error' | 'crit' | 'alert'; +export interface IDbProperties { + connectionUrl: string; + name?: string; + options?: MongoClientOptions; +} + export interface IMigrationOptions { log?: boolean; logger?: (level: SyslogLevels, ...args: any[]) => void; logIfLatest?: boolean; collectionName?: string; - db: string | Db; + db: IDbProperties | Db; } export interface IMigration { version: number; @@ -100,14 +106,19 @@ export class Migration { if (!(this.db instanceof Db) && !this.options.db) { throw new ReferenceError('Option.db canno\'t be null'); } - let db: string | Db; - if (typeof (this.options.db) === 'string') { - const client = await MongoClient.connect(this.options.db, { - useNewUrlParser: true, - }); - db = client.db(); - } else { + let db: IDbProperties | Db; + if (this.options.db instanceof Db) { db = this.options.db; + } else { + const options = { ...this.options.db.options }; + if (options.useNewUrlParser !== false) { + options.useNewUrlParser = true; + } + const client = await MongoClient.connect( + this.options.db.connectionUrl, + options, + ); + db = client.db(this.options.db.name || undefined); } this.collection = (db as Db).collection(this.options.collectionName); this.db = db as Db; @@ -385,13 +396,13 @@ export class Migration { const updateResult = await this.collection.updateOne({ _id: 'control', }, { - $set: { - version: control.version, - locked: control.locked, - }, - }, { - upsert: true, - }); + $set: { + version: control.version, + locked: control.locked, + }, + }, { + upsert: true, + }); if (updateResult && updateResult.result.ok) { return control; diff --git a/test/test.spec.ts b/test/test.spec.ts index b9e8146..066e49d 100644 --- a/test/test.spec.ts +++ b/test/test.spec.ts @@ -13,7 +13,7 @@ describe('Migration', () => { log: true, logIfLatest: true, collectionName: '_migration', - db: dbURL, + db: { connectionUrl: dbURL }, }); await migrator.config(); } catch (e) {