Skip to content

Commit

Permalink
Merge pull request #8355 from BuildingConnected/feat/use-default-db-i…
Browse files Browse the repository at this point in the history
…n-connection-string

feat(connections): Adds default database connection string support (#8354)
  • Loading branch information
vkarpov15 committed Nov 18, 2019
2 parents 3e9faef + 8155c49 commit e366bca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/connections.pug
Expand Up @@ -134,7 +134,7 @@ block content
* `bufferCommands` - This is a mongoose-specific option (not passed to the MongoDB driver) that disables [mongoose's buffering mechanism](http://mongoosejs.com/docs/faq.html#callback_never_executes)
* `user`/`pass` - The username and password for authentication. These options are mongoose-specific, they are equivalent to the MongoDB driver's `auth.user` and `auth.password` options.
* `autoIndex` - By default, mongoose will automatically build indexes defined in your schema when it connects. This is great for development, but not ideal for large production deployments, because index builds can cause performance degradation. If you set `autoIndex` to false, mongoose will not automatically build indexes for **any** model associated with this connection.
* `dbName` - Specifies which database to connect to and overrides any database specified in the connection string. If you're using the `mongodb+srv` syntax to connect to [MongoDB Atlas](https://www.mongodb.com/cloud/atlas), you [should use `dbName` to specify the database](https://stackoverflow.com/questions/48917591/fail-to-connect-mongoose-to-atlas/48917626#48917626) because you currently cannot in the connection string.
* `dbName` - Specifies which database to connect to and overrides any database specified in the connection string. This is useful if you are unable to specify a default database in the connection string like with [some `mongodb+srv` syntax connections](https://stackoverflow.com/questions/48917591/fail-to-connect-mongoose-to-atlas/48917626#48917626).

Below are some of the options that are important for tuning mongoose.

Expand Down
8 changes: 7 additions & 1 deletion lib/connection.js
Expand Up @@ -605,7 +605,13 @@ Connection.prototype.openUri = function(uri, options, callback) {
if (err) {
return reject(err);
}
this.name = dbName != null ? dbName : get(parsed, 'auth.db', null);
if (dbName) {
this.name = dbName;
} else if (parsed.defaultDatabase) {
this.name = parsed.defaultDatabase;
} else {
this.name = get(parsed, 'auth.db', null);
}
this.host = get(parsed, 'hosts.0.host', 'localhost');
this.port = get(parsed, 'hosts.0.port', 27017);
this.user = this.user || get(parsed, 'auth.username');
Expand Down
7 changes: 7 additions & 0 deletions test/connection.test.js
Expand Up @@ -744,6 +744,13 @@ describe('connections:', function() {
});
});

it('uses default database in uri if options.dbName is not provided', function() {
return mongoose.createConnection('mongodb://localhost:27017/default-db-name').then(db => {
assert.equal(db.name, 'default-db-name');
db.close();
});
});

it('startSession() (gh-6653)', function() {
const conn = mongoose.createConnection('mongodb://localhost:27017/test');

Expand Down

0 comments on commit e366bca

Please sign in to comment.