Skip to content

Commit

Permalink
Backport useMongoClient option to 4.6.8 (#1)
Browse files Browse the repository at this point in the history
Backport `useMongoClient` option to `4.6.8`

Originally introduced in: Automattic#5355

Then updated with latest changes from `4.13.21`:

 - https://github.com/Automattic/mongoose/blame/4.13.21/lib/connection.js
 - https://github.com/Automattic/mongoose/blame/4.13.21/lib/index.js
 - https://github.com/Automattic/mongoose/blame/4.13.21/test/connection.test.js
 - https://github.com/Automattic/mongoose/blob/4.13.21/test/common.js


New tests passing:
```sh
$ npm run mocha -- test/connection.test.js
[...]
71 passing (15s)
```
  • Loading branch information
MadLittleMods committed Dec 31, 2022
1 parent 3c3dced commit fda11e7
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -18,6 +18,7 @@ test/triage/*.js
bin/mongoose.min.js
coverage
npm-debug.log
data/

# Visual Studio
# =========
Expand Down
126 changes: 126 additions & 0 deletions lib/connection.js
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 @@ -64,6 +65,7 @@ function Connection(base) {
this.name = null;
this.options = null;
this.otherDbs = [];
this.states = STATES;
this._readyState = STATES.disconnected;
this._closeCalled = false;
this._hasOpened = false;
Expand Down Expand Up @@ -578,6 +580,130 @@ 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;

try {
var parsed = muri(uri);
this.name = parsed.db;
this.host = parsed.hosts[0].host || parsed.hosts[0].ipc;
this.port = parsed.hosts[0].port || 27017;
if (parsed.auth) {
this.user = parsed.auth.user;
this.pass = parsed.auth.pass;
}
} catch (error) {
this.error(error, callback);
throw error;
}

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

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

if (options) {
options = utils.clone(options, { retainKeyOrder: true });
delete options.useMongoClient;
var autoIndex = options.config && options.config.autoIndex != null ?
options.config.autoIndex :
options.autoIndex;
if (autoIndex != null) {
this.config.autoIndex = autoIndex !== false;
delete options.config;
delete options.autoIndex;
}

// Backwards compat
if (options.user || options.pass) {
options.auth = options.auth || {};
options.auth.user = options.user;
options.auth.password = options.pass;
delete options.user;
delete options.pass;
this.user = options.auth.user;
this.pass = options.auth.password;
}

if (options.bufferCommands != null) {
throw new Error('options.bufferCommands not supported on this custom fork of mongoose for Gitter');
// options.bufferMaxEntries = 0;
// this.config.bufferCommands = options.bufferCommands;
// delete options.bufferCommands;
}
}

this._connectionOptions = options;

var promise = 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);
}
callback && callback(error);
return reject(error);
}
// Backwards compat for mongoose 4.x
db.on('reconnect', function() {
_this.readyState = STATES.connected;
_this.emit('reconnect');
_this.emit('reconnected');
});
db.s.topology.on('reconnectFailed', function() {
_this.emit('reconnectFailed');
});
db.s.topology.on('close', function() {
// Implicitly emits 'disconnected'
_this.readyState = STATES.disconnected;
});
db.on('timeout', function() {
_this.emit('timeout');
});

delete _this.then;
delete _this.catch;

_this.db = db;
_this.readyState = STATES.connected;

for (var i in _this.collections) {
if (utils.object.hasOwnProperty(_this.collections, i)) {
_this.collections[i].onOpen();
}
}

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

this.then = function(resolve, reject) {
return promise.then(resolve, reject);
};
this.catch = function(reject) {
return promise.catch(reject);
};

return this;
};

/**
* Closes the connection
*
Expand Down
15 changes: 14 additions & 1 deletion lib/index.js
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
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -46,6 +46,7 @@
"markdown": "0.3.1",
"marked": "0.3.6",
"mocha": "3.1.2",
"mongodb-topology-manager": "^1.0.11",
"mongoose-long": "0.1.1",
"node-static": "0.7.7",
"power-assert": "1.4.1",
Expand All @@ -72,7 +73,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
23 changes: 18 additions & 5 deletions test/common.js
Expand Up @@ -4,13 +4,16 @@

Error.stackTraceLimit = 10;

var Server = require('mongodb-topology-manager').Server;
var mongoose = require('../'),
Collection = mongoose.Collection,
assert = require('power-assert'),
queryCount = 0,
opened = 0,
closed = 0;

var server;

if (process.env.D === '1') {
mongoose.set('debug', true);
}
Expand Down Expand Up @@ -156,13 +159,23 @@ function dropDBs(done) {
});
}

before(function() {
return server.purge();
});

after(function() {
this.timeout(15000);

return server.stop();
});

before(function(done) {
this.timeout(10 * 1000);
dropDBs(done);
});
after(function(done) {
// DropDBs can be extraordinarily slow on 3.2
//this.timeout(120 * 1000);
//dropDBs(done);
done();

module.exports.server = server = new Server('mongod', {
port: 27000,
dbpath: './data/db/27000',
storageEngine: 'mmapv1'
});

0 comments on commit fda11e7

Please sign in to comment.