Skip to content

Commit 937b6a3

Browse files
committed
fix: Modify condition to call mongo url processing
1 parent f67b07a commit 937b6a3

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

lib/mongodb.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ MongoDB.prototype.connect = function(callback) {
333333
}
334334

335335
// This is special processing if database is not part of url, but is in settings
336-
if (self.settings.database && self.settings.url) {
337-
if ((self.settings.url.indexOf('/' + self.settings.database) === -1) && (self.settings.url.indexOf('authSource=' + self.settings.database) === -1)) {
336+
if (self.settings.url && self.settings.database) {
337+
if (self.settings.url.indexOf('/' + self.settings.database) === -1) {
338338
self.settings.url = processMongoDBURL(self.settings.database, self.settings.url);
339339
}
340340
}

test/mongodb.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,81 @@ describe('mongodb connector', function() {
449449
});
450450
});
451451
});
452+
453+
it("should honor the settings database if url doesn't have db", function(done) {
454+
const cfg = JSON.parse(JSON.stringify(global.config));
455+
const testDb = cfg.database;
456+
cfg.url = 'mongodb://' + cfg.host + ':' + cfg.port;
457+
const ds = global.getDataSource(cfg);
458+
ds.once('connected', function() {
459+
const db = ds.connector.db;
460+
let validationError = null;
461+
try {
462+
db.should.have.property('databaseName', testDb); // check the db name in the db instance
463+
} catch (err) {
464+
// async error
465+
validationError = err;
466+
}
467+
ds.ping(function(err) {
468+
if (err && !validationError) validationError = err;
469+
ds.disconnect(function(disconnectError) {
470+
if (disconnectError && !validationError)
471+
validationError = disconnectError;
472+
done(validationError);
473+
});
474+
});
475+
});
476+
});
477+
478+
it('should honor the url database if both replicaset url and settings has db', function(done) {
479+
const cfg = JSON.parse(JSON.stringify(global.config));
480+
const testDb = 'lb-ds-overriden-test-1';
481+
cfg.url = 'mongodb://' + cfg.host + ':' + cfg.port + ',' + cfg.host + ':' + cfg.port + '/' + testDb;
482+
const ds = global.getDataSource(cfg);
483+
ds.once('connected', function() {
484+
const db = ds.connector.db;
485+
let validationError = null;
486+
try {
487+
db.should.have.property('databaseName', testDb); // check the db name in the db instance
488+
} catch (err) {
489+
// async error
490+
validationError = err;
491+
}
492+
ds.ping(function(err) {
493+
if (err && !validationError) validationError = err;
494+
ds.disconnect(function(disconnectError) {
495+
if (disconnectError && !validationError)
496+
validationError = disconnectError;
497+
done(validationError);
498+
});
499+
});
500+
});
501+
});
502+
503+
it("should honor the settings database if replicaset url doesn't have db has slash", function(done) {
504+
const cfg = JSON.parse(JSON.stringify(global.config));
505+
const testDb = cfg.database;
506+
cfg.url = 'mongodb://' + cfg.host + ':' + cfg.port + ',' + cfg.host + ':' + cfg.port + '/';
507+
const ds = global.getDataSource(cfg);
508+
ds.once('connected', function() {
509+
const db = ds.connector.db;
510+
let validationError = null;
511+
try {
512+
db.should.have.property('databaseName', testDb); // check the db name in the db instance
513+
} catch (err) {
514+
// async error
515+
validationError = err;
516+
}
517+
ds.ping(function(err) {
518+
if (err && !validationError) validationError = err;
519+
ds.disconnect(function(disconnectError) {
520+
if (disconnectError && !validationError)
521+
validationError = disconnectError;
522+
done(validationError);
523+
});
524+
});
525+
});
526+
});
452527
});
453528

454529
describe('order filters', function() {

0 commit comments

Comments
 (0)