Skip to content

Commit

Permalink
refactor(connection): move openSet args logic into separate function
Browse files Browse the repository at this point in the history
Re: #5229
  • Loading branch information
vkarpov15 committed May 12, 2017
1 parent 0eda07a commit be8a6e4
Showing 1 changed file with 61 additions and 51 deletions.
112 changes: 61 additions & 51 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,57 +401,15 @@ Connection.prototype.dropDatabase = function(callback) {
return promise;
};

/**
* Opens the connection to a replica set.
*
* ####Example:
*
* var db = mongoose.createConnection();
* db.openSet("mongodb://user:pwd@localhost:27020,localhost:27021,localhost:27012/mydb");
*
* The database name and/or auth need only be included in one URI.
* The `options` is a hash which is passed to the internal driver connection object.
*
* Valid `options`
*
* db - passed to the connection db instance
* server - passed to the connection server instance(s)
* replset - passed to the connection ReplSetServer instance
* user - username for authentication
* pass - password for authentication
* auth - options for authentication (see http://mongodb.github.com/node-mongodb-native/api-generated/db.html#authenticate)
* mongos - Boolean - if true, enables High Availability support for mongos
*
* _Options passed take precedence over options included in connection strings._
*
* ####Notes:
*
* _If connecting to multiple mongos servers, set the `mongos` option to true._
*
* conn.open('mongodb://mongosA:27501,mongosB:27501', { mongos: true }, cb);
*
* Mongoose forces the db option `forceServerObjectId` false and cannot be overridden.
* Mongoose defaults the server `auto_reconnect` options to true which can be overridden.
* See the node-mongodb-native driver instance for options that it understands.
*
* _Options passed take precedence over options included in connection strings._
*
* @param {String} uris MongoDB connection string
* @param {String} [database] database name if not included in `uris`
* @param {Object} [options] passed to the internal driver
* @param {Function} [callback]
* @see node-mongodb-native https://github.com/mongodb/node-mongodb-native
* @see http://mongodb.github.com/node-mongodb-native/api-generated/db.html#authenticate
* @api public
/*!
* ignore
*/

Connection.prototype.openSet = function(uris, database, options, callback) {
Connection.prototype._handleOpenSetArgs = function(uris, database, options, callback) {
if (!rgxProtocol.test(uris)) {
uris = 'mongodb://' + uris;
}

var Promise = PromiseProvider.get();

switch (arguments.length) {
case 3:
switch (typeof database) {
Expand Down Expand Up @@ -495,9 +453,7 @@ Connection.prototype.openSet = function(uris, database, options, callback) {
parsed = muri(uris);
} catch (err) {
this.error(err, callback);
return new Promise.ES6(function(resolve, reject) {
reject(err);
});
throw err;
}

if (!this.name) {
Expand All @@ -511,9 +467,7 @@ Connection.prototype.openSet = function(uris, database, options, callback) {
if (!this.name) {
var err = new Error('No database name provided for replica set');
this.error(err, callback);
return new Promise.ES6(function(resolve, reject) {
reject(err);
});
throw err;
}

// authentication
Expand All @@ -531,6 +485,62 @@ Connection.prototype.openSet = function(uris, database, options, callback) {
if (options && options.config) {
this.config.autoIndex = options.config.autoIndex !== false;
}
};

/**
* Opens the connection to a replica set.
*
* ####Example:
*
* var db = mongoose.createConnection();
* db.openSet("mongodb://user:pwd@localhost:27020,localhost:27021,localhost:27012/mydb");
*
* The database name and/or auth need only be included in one URI.
* The `options` is a hash which is passed to the internal driver connection object.
*
* Valid `options`
*
* db - passed to the connection db instance
* server - passed to the connection server instance(s)
* replset - passed to the connection ReplSetServer instance
* user - username for authentication
* pass - password for authentication
* auth - options for authentication (see http://mongodb.github.com/node-mongodb-native/api-generated/db.html#authenticate)
* mongos - Boolean - if true, enables High Availability support for mongos
*
* _Options passed take precedence over options included in connection strings._
*
* ####Notes:
*
* _If connecting to multiple mongos servers, set the `mongos` option to true._
*
* conn.open('mongodb://mongosA:27501,mongosB:27501', { mongos: true }, cb);
*
* Mongoose forces the db option `forceServerObjectId` false and cannot be overridden.
* Mongoose defaults the server `auto_reconnect` options to true which can be overridden.
* See the node-mongodb-native driver instance for options that it understands.
*
* _Options passed take precedence over options included in connection strings._
*
* @param {String} uris MongoDB connection string
* @param {String} [database] database name if not included in `uris`
* @param {Object} [options] passed to the internal driver
* @param {Function} [callback]
* @see node-mongodb-native https://github.com/mongodb/node-mongodb-native
* @see http://mongodb.github.com/node-mongodb-native/api-generated/db.html#authenticate
* @api public
*/

Connection.prototype.openSet = function(uris, database, options, callback) {
var Promise = PromiseProvider.get();

try {
var callback = this._handleOpenSetArgs.apply(this, arguments);
} catch (err) {
return new Promise.ES6(function(resolve, reject) {
reject(err);
});
}

var _this = this;
var emitted = false;
Expand Down

0 comments on commit be8a6e4

Please sign in to comment.