Skip to content

Commit 835b64b

Browse files
authored
Merge pull request #21 from BorntraegerMarc/master
feat(getDbInstance): Added getDbInstance method
2 parents 9b963d7 + e31465d commit 835b64b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,18 @@ var MongodbDriver = Base.extend({
213213
return this._run('insert', this.internals.seedTable, {name: name, run_on: new Date()})
214214
.nodeify(callback);
215215
},
216+
217+
/**
218+
* Returns the DB instance so custom updates can be made.
219+
* NOTE: This method exceptionally does not call close() on the database driver when the promise resolves. So the getDbInstance method caller
220+
* needs to call .close() on it's own after finish working with the database driver.
221+
*
222+
* @param callback with the database driver as 2nd callback argument
223+
*/
224+
getDbInstance: function (callback) {
225+
return this._run('getDbInstance', null, {run_on: new Date()})
226+
.nodeify(callback);
227+
},
216228

217229
/**
218230
* Runs a query
@@ -345,6 +357,9 @@ var MongodbDriver = Base.extend({
345357
case 'updateMany':
346358
db.collection(collection)[command](options.query, options.update, options.options, callbackFunction);
347359
break;
360+
case 'getDbInstance':
361+
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
362+
break;
348363
default:
349364
db[command](collection, callbackFunction);
350365
break;

test/mongodb_test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,18 @@ vows
468468
}
469469
}
470470
})
471+
.addBatch({
472+
getDbInstance: {
473+
topic: function() {
474+
db.getDbInstance(this.callback);
475+
},
476+
477+
teardown: function() {
478+
},
479+
480+
"has database reference": function(err, db) {
481+
assert.isNotNull(db);
482+
}
483+
}
484+
})
471485
.export(module);

0 commit comments

Comments
 (0)