Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(connections): Adds default database connection string support (#8354) #8355

Merged
merged 3 commits into from Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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