Skip to content

Commit

Permalink
Backport useMongoClient option
Browse files Browse the repository at this point in the history
  • Loading branch information
MadLittleMods committed Dec 31, 2022
1 parent 3c3dced commit 54c78dd
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
47 changes: 47 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var STATES = require('./connectionstate');
var MongooseError = require('./error');
var muri = require('muri');
var PromiseProvider = require('./promise_provider');
var mongodb = require('mongodb');

/*!
* Protocol prefix regexp.
Expand Down Expand Up @@ -578,6 +579,52 @@ Connection.prototype.onOpen = function(callback) {
}
};

/**
* Opens the connection with a URI using `MongoClient.connect()`.
*
* @param {String} uri The URI to connect with.
* @param {Object} [options] Passed on to http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect
* @param {Function} [callback]
* @returns {Connection} this
* @api private
*/

Connection.prototype.openUri = function(uri, options, callback) {
this.readyState = STATES.connecting;
this._closeCalled = false;

if (typeof options === 'function') {
callback = options;
options = null;
}

var Promise = PromiseProvider.get();
var _this = this;

if (options && options.useMongoClient) {
options = utils.clone(options, { retainKeyOrder: true });
delete options.useMongoClient;
}

return new Promise.ES6(function(resolve, reject) {
mongodb.MongoClient.connect(uri, options, function(error, db) {
if (error) {
_this.readyState = STATES.disconnected;
if (_this.listeners('error').length) {
_this.emit('error', error);
}
return reject(error);
}
_this.db = db;
_this.readyState = STATES.connected;

callback && callback();
resolve(_this);
_this.emit('open');
});
});
};

/**
* Closes the connection
*
Expand Down
15 changes: 14 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ var checkReplicaSetInUri = function(uri) {
* @param {Boolean} [options.config.autoIndex] set to false to disable automatic index creation for all models associated with this connection.
* @see Connection#open #connection_Connection-open
* @see Connection#openSet #connection_Connection-openSet
* @return {Connection} the created Connection object
* @return {Connection|Promise} the created Connection object, or promise that resolves to the connection if `useMongoClient` option specified.
* @api public
*/

Expand All @@ -179,6 +179,11 @@ Mongoose.prototype.createConnection = function(uri, options) {
this.connections.push(conn);

var rsOption = options && (options.replset || options.replSet);

if (options && options.useMongoClient) {
return conn.openUri(uri, options);
}

if (arguments.length) {
if (rgxReplSet.test(arguments[0]) || checkReplicaSetInUri(arguments[0])) {
conn.openSet.apply(conn, arguments);
Expand Down Expand Up @@ -227,6 +232,7 @@ Mongoose.prototype.createConnection.$hasSideEffects = true;
*
* @param {String} uri(s)
* @param {Object} [options]
* @param {Boolean} [options.useMongoClient] false by default, set to true to use new mongoose connection logic
* @param {Function} [callback]
* @see Mongoose#createConnection #index_Mongoose-createConnection
* @api public
Expand All @@ -235,6 +241,13 @@ Mongoose.prototype.createConnection.$hasSideEffects = true;

Mongoose.prototype.connect = function() {
var conn = this.connection;

if ((arguments.length === 2 || arguments.length === 3) &&
typeof arguments[0] === 'string' &&
typeof arguments[1] === 'object' &&
arguments[1].useMongoClient === true) {
return conn.openUri(arguments[0], arguments[1], arguments[2]);
}
if (rgxReplSet.test(arguments[0]) || checkReplicaSetInUri(arguments[0])) {
return new MongooseThenable(this, conn.openSet.apply(conn, arguments));
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"install-browser": "npm install `node format_deps.js`",
"test": "mocha test/*.test.js test/**/*.test.js",
"posttest": "eslint . --quiet",
"test-cov": "istanbul cover --report text --report html _mocha test/*.test.js"
"test-cov": "istanbul cover --report text --report html _mocha test/*.test.js",
"mocha": "mocha"
},
"main": "./index.js",
"engines": {
Expand Down
24 changes: 24 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ var muri = require('muri');
*/

describe('connections:', function() {
describe('useMongoClient/openUri (gh-5304)', function() {
it('with mongoose.connect()', function(done) {
var promise = mongoose.connect('mongodb://localhost:27017/mongoosetest', { useMongoClient: true });
assert.equal(promise.constructor.name, 'Promise');

promise.then(function(conn) {
assert.equal(conn.constructor.name, 'NativeConnection');

return mongoose.disconnect().then(() => done());
}).catch(done);
});

it('with mongoose.createConnection()', function(done) {
var promise = mongoose.createConnection('mongodb://localhost:27017/mongoosetest', { useMongoClient: true });
assert.equal(promise.constructor.name, 'Promise');

promise.then(function(conn) {
assert.equal(conn.constructor.name, 'NativeConnection');

return mongoose.disconnect().then(() => done());
}).catch(done);
});
});

it('should allow closing a closed connection', function(done) {
var db = mongoose.createConnection();

Expand Down

0 comments on commit 54c78dd

Please sign in to comment.