Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove instanceof comparison for MongoDB DB instance check #112

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,36 @@ export class Migration {
* @memberof Migration
*/
public async config(opts?: IMigrationOptions): Promise<void> {

this.options = Object.assign({}, this.options, opts);

if (!this.options.logger && this.options.log) {
// tslint:disable-next-line: no-console
this.options.logger = (level: string, ...args) => console.log(level, ...args);
}

if (this.options.log === false) {
// tslint:disable-next-line:no-empty
this.options.logger = (level: string, ...args) => { };
}
if (!(this.db instanceof Db) && !this.options.db) {
throw new ReferenceError('Option.db canno\'t be null');

let db: IDbProperties | Db = this.options.db || this.db;
if (!db) {
throw new ReferenceError('db option must be defined');
}
let db: IDbProperties | Db;
if (this.options.db instanceof Db) {
db = this.options.db;
} else {
const options = { ...this.options.db.options };

// Check if connectionUrl exists. If it does, assume its IDbProperties object
Copy link
Owner

@emmanuelbuah emmanuelbuah Feb 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per discussion with @stieg, one solution is to make MongoDB a devDependency making it so the logic uses the consumer's own MongoDB package.

if ((db as IDbProperties).connectionUrl) {
const dbProps = db as IDbProperties;
const options = { ...dbProps.options };
if (options.useNewUrlParser !== false) {
options.useNewUrlParser = true;
}
const client = await MongoClient.connect(
this.options.db.connectionUrl,
dbProps.connectionUrl,
options,
);
db = client.db(this.options.db.name || undefined);
// XXX: This never gets disconnected.
db = client.db(dbProps.name);
}
this.collection = (db as Db).collection(this.options.collectionName);
this.db = db as Db;
Expand Down