Skip to content

Commit

Permalink
Merge pull request #7905 from Fonger/feat/multiple-connect-error
Browse files Browse the repository at this point in the history
feat(mongoose): throw an error if calling `mongoose.connect()` multiple times while connected
  • Loading branch information
vkarpov15 committed Jun 20, 2019
2 parents 4322ed1 + 3536527 commit 2a2c71a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/connections.pug
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ block content

// Or using promises
mongoose.connect(uri, options).then(
() => { /** ready to use. The `mongoose.connect()` promise resolves to undefined. */ },
() => { /** ready to use. The `mongoose.connect()` promise resolves to mongoose instance. */ },
err => { /** handle initial connection error */ }
);
```
Expand Down
8 changes: 4 additions & 4 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,11 +726,11 @@ Connection.prototype._close = function(force, callback) {
this._closeCalled = true;

switch (this.readyState) {
case 0: // disconnected
case STATES.disconnected:
callback();
break;

case 1: // connected
case STATES.connected:
this.readyState = STATES.disconnecting;
this.doClose(force, function(err) {
if (err) {
Expand All @@ -741,13 +741,13 @@ Connection.prototype._close = function(force, callback) {
});

break;
case 2: // connecting
case STATES.connecting:
this.once('open', function() {
_this.close(callback);
});
break;

case 3: // disconnecting
case STATES.disconnecting:
this.once('close', function() {
callback();
});
Expand Down
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ Mongoose.prototype.createConnection = function(uri, options, callback) {
Mongoose.prototype.connect = function() {
const _mongoose = this instanceof Mongoose ? this : mongoose;
const conn = _mongoose.connection;
if (conn.readyState !== STATES.disconnected) {
throw new _mongoose.Error('You can not `mongoose.connect()` multiple times while connected.');
}
return conn.openUri(arguments[0], arguments[1], arguments[2]).then(() => _mongoose);
};

Expand Down
21 changes: 21 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,28 @@ describe('mongoose module:', function() {
const Model = mongoose.model('gh6813', new Schema({ name: String }));
assert.ok(Model);
});
it('throws an error if calling `mongoose.connect()` multiple times while connected (gh-7814)', function() {
const mong = new Mongoose();
return co(function*() {
const errorRegex = /multiple times while connected/;

// throws while connecting
const connectPromise = mong.connect(uri, options);
assert.throws(() => mong.connect(uri, options), errorRegex);

// throws after connected
yield connectPromise;
assert.throws(() => mong.connect(uri, options), errorRegex);

// throws while disconnecting
const disconnectPromise = mong.disconnect();
assert.throws(() => mong.connect(uri, options), errorRegex);

// does not throw after disconnected
yield disconnectPromise;
yield mong.connect(uri, options);
});
});
describe('connecting with a signature of uri, options, function', function() {
it('with single mongod', function(done) {
const mong = new Mongoose();
Expand Down

0 comments on commit 2a2c71a

Please sign in to comment.