Skip to content

feat(getDbInstance): Added getDbInstance method #21

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

Merged
merged 4 commits into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ var MongodbDriver = Base.extend({
return this._run('insert', this.internals.seedTable, {name: name, run_on: new Date()})
.nodeify(callback);
},

/**
* Returns the DB instance so custom updates can be made.
* NOTE: This method exceptionally does not call close() on the database driver when the promise resolves. So the getDbInstance method caller
* needs to call .close() on it's own after finish working with the database driver.
*
* @param callback with the database driver as 2nd callback argument
*/
getDbInstance: function (callback) {
return this._run('getDbInstance', null, {run_on: new Date()})
.nodeify(callback);
},

/**
* Runs a query
Expand Down Expand Up @@ -345,6 +357,9 @@ var MongodbDriver = Base.extend({
case 'updateMany':
db.collection(collection)[command](options.query, options.update, options.options, callbackFunction);
break;
case 'getDbInstance':
prCB(null, db); // When the user wants to get the DB instance we need to return the promise callback, so the DB connection is not automatically closed
break;
default:
db[command](collection, callbackFunction);
break;
Expand Down
14 changes: 14 additions & 0 deletions test/mongodb_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,18 @@ vows
}
}
})
.addBatch({
getDbInstance: {
topic: function() {
db.getDbInstance(this.callback);
},

teardown: function() {
},

"has database reference": function(err, db) {
assert.isNotNull(db);
}
}
})
.export(module);