Skip to content

Commit

Permalink
fix: Modify condition to call mongo url processing
Browse files Browse the repository at this point in the history
Signed-off-by: jaishirole <[email protected]>
  • Loading branch information
jaishirole committed Apr 28, 2022
1 parent f67b07a commit 8f71067
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
75 changes: 75 additions & 0 deletions test/mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 8f71067

Please sign in to comment.