Skip to content

Commit

Permalink
Merge pull request #100 from PizzaPartyInc/next
Browse files Browse the repository at this point in the history
 #74: optional options to customize mongodb connection added
  • Loading branch information
emmanuelbuah authored Jan 28, 2020
2 parents b452bdc + b53e992 commit 9f24122
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
39 changes: 33 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand All @@ -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
```
Expand Down Expand Up @@ -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,
}
});
```

Expand Down
43 changes: 27 additions & 16 deletions src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Migration', () => {
log: true,
logIfLatest: true,
collectionName: '_migration',
db: dbURL,
db: { connectionUrl: dbURL },
});
await migrator.config();
} catch (e) {
Expand Down

0 comments on commit 9f24122

Please sign in to comment.