From 937b6a33635d55aae21d54c346d212b3913f4d67 Mon Sep 17 00:00:00 2001 From: jaishirole Date: Thu, 28 Apr 2022 23:53:23 +0530 Subject: [PATCH] fix: Modify condition to call mongo url processing --- lib/mongodb.js | 4 +-- test/mongodb.test.js | 75 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/lib/mongodb.js b/lib/mongodb.js index 7a19eb6c7..c0036cb77 100644 --- a/lib/mongodb.js +++ b/lib/mongodb.js @@ -333,8 +333,8 @@ MongoDB.prototype.connect = function(callback) { } // This is special processing if database is not part of url, but is in settings - if (self.settings.database && self.settings.url) { - if ((self.settings.url.indexOf('/' + self.settings.database) === -1) && (self.settings.url.indexOf('authSource=' + self.settings.database) === -1)) { + if (self.settings.url && self.settings.database) { + if (self.settings.url.indexOf('/' + self.settings.database) === -1) { self.settings.url = processMongoDBURL(self.settings.database, self.settings.url); } } diff --git a/test/mongodb.test.js b/test/mongodb.test.js index 1a5172627..2a89d1118 100644 --- a/test/mongodb.test.js +++ b/test/mongodb.test.js @@ -449,6 +449,81 @@ describe('mongodb connector', function() { }); }); }); + + it("should honor the settings database if url doesn't have db", function(done) { + const cfg = JSON.parse(JSON.stringify(global.config)); + const testDb = cfg.database; + cfg.url = 'mongodb://' + cfg.host + ':' + cfg.port; + const ds = global.getDataSource(cfg); + ds.once('connected', function() { + const db = ds.connector.db; + let validationError = null; + try { + db.should.have.property('databaseName', testDb); // check the db name in the db instance + } catch (err) { + // async error + validationError = err; + } + ds.ping(function(err) { + if (err && !validationError) validationError = err; + ds.disconnect(function(disconnectError) { + if (disconnectError && !validationError) + validationError = disconnectError; + done(validationError); + }); + }); + }); + }); + + it('should honor the url database if both replicaset url and settings has db', function(done) { + const cfg = JSON.parse(JSON.stringify(global.config)); + const testDb = 'lb-ds-overriden-test-1'; + cfg.url = 'mongodb://' + cfg.host + ':' + cfg.port + ',' + cfg.host + ':' + cfg.port + '/' + testDb; + const ds = global.getDataSource(cfg); + ds.once('connected', function() { + const db = ds.connector.db; + let validationError = null; + try { + db.should.have.property('databaseName', testDb); // check the db name in the db instance + } catch (err) { + // async error + validationError = err; + } + ds.ping(function(err) { + if (err && !validationError) validationError = err; + ds.disconnect(function(disconnectError) { + if (disconnectError && !validationError) + validationError = disconnectError; + done(validationError); + }); + }); + }); + }); + + it("should honor the settings database if replicaset url doesn't have db has slash", function(done) { + const cfg = JSON.parse(JSON.stringify(global.config)); + const testDb = cfg.database; + cfg.url = 'mongodb://' + cfg.host + ':' + cfg.port + ',' + cfg.host + ':' + cfg.port + '/'; + const ds = global.getDataSource(cfg); + ds.once('connected', function() { + const db = ds.connector.db; + let validationError = null; + try { + db.should.have.property('databaseName', testDb); // check the db name in the db instance + } catch (err) { + // async error + validationError = err; + } + ds.ping(function(err) { + if (err && !validationError) validationError = err; + ds.disconnect(function(disconnectError) { + if (disconnectError && !validationError) + validationError = disconnectError; + done(validationError); + }); + }); + }); + }); }); describe('order filters', function() {