From cfe208c5805ee244560b9343490b9e5e31eca323 Mon Sep 17 00:00:00 2001 From: BorntraegerMarc Date: Thu, 4 Jan 2018 13:14:41 +0100 Subject: [PATCH 1/4] feat(getDbInstance): Added getDbInstance method Signed-off-by: BorntraegerMarc --- index.js | 13 +++++++++++++ test/mongodb_test.js | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/index.js b/index.js index 4dab058..27ef10a 100755 --- a/index.js +++ b/index.js @@ -213,6 +213,16 @@ 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 + * + * @param callback + */ + getDbInstance: function (callback) { + return this._run('getDbInstance', null, {run_on: new Date()}) + .nodeify(callback); + }, /** * Runs a query @@ -345,6 +355,9 @@ var MongodbDriver = Base.extend({ case 'updateMany': db.collection(collection)[command](options.query, options.update, options.options, callbackFunction); break; + case 'getDbInstance': + callbackFunction(null, db); + break; default: db[command](collection, callbackFunction); break; diff --git a/test/mongodb_test.js b/test/mongodb_test.js index 94577a7..de24789 100644 --- a/test/mongodb_test.js +++ b/test/mongodb_test.js @@ -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); From 87c81700eacc1ba6d2da91dcc50af8accce609af Mon Sep 17 00:00:00 2001 From: BorntraegerMarc Date: Sun, 7 Jan 2018 16:09:35 +0100 Subject: [PATCH 2/4] feat(getDbInstance): Added check before driver is closed when getDbInstance method is called Signed-off-by: BorntraegerMarc --- index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 27ef10a..ad8842c 100755 --- a/index.js +++ b/index.js @@ -215,9 +215,11 @@ var MongodbDriver = Base.extend({ }, /** - * Returns the DB instance so custom updates can be made + * 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 + * @param callback with the database driver as 2nd callback argument */ getDbInstance: function (callback) { return this._run('getDbInstance', null, {run_on: new Date()}) @@ -303,7 +305,9 @@ var MongodbDriver = Base.extend({ } prCB(null, data); - db.close(); + if (command !== 'getDbInstance') { + db.close(); + } }; // Depending on the command, we need to use different mongo methods From 0a869319a1a353b28cf1ae492be35c93a2c5acc1 Mon Sep 17 00:00:00 2001 From: BorntraegerMarc Date: Mon, 8 Jan 2018 07:57:01 +0100 Subject: [PATCH 3/4] feat(getDbInstance): moved from string based check to promise call Signed-off-by: BorntraegerMarc --- index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ad8842c..55defef 100755 --- a/index.js +++ b/index.js @@ -305,9 +305,7 @@ var MongodbDriver = Base.extend({ } prCB(null, data); - if (command !== 'getDbInstance') { - db.close(); - } + db.close(); }; // Depending on the command, we need to use different mongo methods @@ -360,7 +358,7 @@ var MongodbDriver = Base.extend({ db.collection(collection)[command](options.query, options.update, options.options, callbackFunction); break; case 'getDbInstance': - callbackFunction(null, db); + prCB(null, db); // When the user wants to get the DB instance we need to retrun the promise callback, so the DB connection is not automatically closed break; default: db[command](collection, callbackFunction); From e31465d6920449d61325fe3f086d185c72cacc3b Mon Sep 17 00:00:00 2001 From: BorntraegerMarc Date: Mon, 8 Jan 2018 08:43:03 +0100 Subject: [PATCH 4/4] feat(getDbInstance): Fixed typo Signed-off-by: BorntraegerMarc --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 55defef..d70abe2 100755 --- a/index.js +++ b/index.js @@ -358,7 +358,7 @@ var MongodbDriver = Base.extend({ 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 retrun the promise callback, so the DB connection is not automatically closed + 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);