mirror of
https://github.com/arnaucube/thoughts.git
synced 2026-02-07 11:46:52 +01:00
database externa
This commit is contained in:
5
node_modules/mongoose/lib/drivers/browser/ReadPreference.js
generated
vendored
Normal file
5
node_modules/mongoose/lib/drivers/browser/ReadPreference.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = function() {};
|
||||
12
node_modules/mongoose/lib/drivers/browser/binary.js
generated
vendored
Normal file
12
node_modules/mongoose/lib/drivers/browser/binary.js
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Binary = require('bson').Binary;
|
||||
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
module.exports = exports = Binary;
|
||||
7
node_modules/mongoose/lib/drivers/browser/index.js
generated
vendored
Normal file
7
node_modules/mongoose/lib/drivers/browser/index.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
exports.Binary = require('./binary');
|
||||
exports.ObjectId = require('./objectid');
|
||||
exports.ReadPreference = require('./ReadPreference');
|
||||
14
node_modules/mongoose/lib/drivers/browser/objectid.js
generated
vendored
Normal file
14
node_modules/mongoose/lib/drivers/browser/objectid.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
/*!
|
||||
* [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) ObjectId
|
||||
* @constructor NodeMongoDbObjectId
|
||||
* @see ObjectId
|
||||
*/
|
||||
|
||||
var ObjectId = require('bson').ObjectID;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = exports = ObjectId;
|
||||
17
node_modules/mongoose/lib/drivers/index.js
generated
vendored
Normal file
17
node_modules/mongoose/lib/drivers/index.js
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
var driver;
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
driver = require(global.MONGOOSE_DRIVER_PATH || './node-mongodb-native');
|
||||
} else {
|
||||
driver = require('./browser');
|
||||
}
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
module.exports = driver;
|
||||
45
node_modules/mongoose/lib/drivers/node-mongodb-native/ReadPreference.js
generated
vendored
Normal file
45
node_modules/mongoose/lib/drivers/node-mongodb-native/ReadPreference.js
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var mongodb = require('mongodb');
|
||||
var ReadPref = mongodb.ReadPreference;
|
||||
|
||||
/*!
|
||||
* Converts arguments to ReadPrefs the driver
|
||||
* can understand.
|
||||
*
|
||||
* @param {String|Array} pref
|
||||
* @param {Array} [tags]
|
||||
*/
|
||||
|
||||
module.exports = function readPref(pref, tags) {
|
||||
if (Array.isArray(pref)) {
|
||||
tags = pref[1];
|
||||
pref = pref[0];
|
||||
}
|
||||
|
||||
if (pref instanceof ReadPref) {
|
||||
return pref;
|
||||
}
|
||||
|
||||
switch (pref) {
|
||||
case 'p':
|
||||
pref = 'primary';
|
||||
break;
|
||||
case 'pp':
|
||||
pref = 'primaryPreferred';
|
||||
break;
|
||||
case 's':
|
||||
pref = 'secondary';
|
||||
break;
|
||||
case 'sp':
|
||||
pref = 'secondaryPreferred';
|
||||
break;
|
||||
case 'n':
|
||||
pref = 'nearest';
|
||||
break;
|
||||
}
|
||||
|
||||
return new ReadPref(pref, tags);
|
||||
};
|
||||
2
node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js
generated
vendored
2
node_modules/mongoose/lib/drivers/node-mongodb-native/binary.js
generated
vendored
@@ -3,6 +3,6 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Binary = require('mongodb').BSONPure.Binary;
|
||||
var Binary = require('mongodb').Binary;
|
||||
|
||||
module.exports = exports = Binary;
|
||||
|
||||
232
node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js
generated
vendored
232
node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js
generated
vendored
@@ -1,12 +1,10 @@
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseCollection = require('../../collection')
|
||||
, Collection = require('mongodb').Collection
|
||||
, STATES = require('../../connectionstate')
|
||||
, utils = require('../../utils')
|
||||
var MongooseCollection = require('../../collection'),
|
||||
Collection = require('mongodb').Collection,
|
||||
utils = require('../../utils');
|
||||
|
||||
/**
|
||||
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation.
|
||||
@@ -17,7 +15,7 @@ var MongooseCollection = require('../../collection')
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function NativeCollection () {
|
||||
function NativeCollection() {
|
||||
this.collection = null;
|
||||
MongooseCollection.apply(this, arguments);
|
||||
}
|
||||
@@ -34,54 +32,58 @@ NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.onOpen = function () {
|
||||
var self = this;
|
||||
NativeCollection.prototype.onOpen = function() {
|
||||
var _this = this;
|
||||
|
||||
// always get a new collection in case the user changed host:port
|
||||
// of parent db instance when re-opening the connection.
|
||||
|
||||
if (!self.opts.capped.size) {
|
||||
if (!_this.opts.capped.size) {
|
||||
// non-capped
|
||||
return self.conn.db.collection(self.name, callback);
|
||||
return _this.conn.db.collection(_this.name, callback);
|
||||
}
|
||||
|
||||
// capped
|
||||
return self.conn.db.collection(self.name, function (err, c) {
|
||||
return _this.conn.db.collection(_this.name, function(err, c) {
|
||||
if (err) return callback(err);
|
||||
|
||||
// discover if this collection exists and if it is capped
|
||||
c.options(function (err, exists) {
|
||||
if (err) return callback(err);
|
||||
_this.conn.db.listCollections({name: _this.name}).toArray(function(err, docs) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
var doc = docs[0];
|
||||
var exists = !!doc;
|
||||
|
||||
if (exists) {
|
||||
if (exists.capped) {
|
||||
if (doc.options && doc.options.capped) {
|
||||
callback(null, c);
|
||||
} else {
|
||||
var msg = 'A non-capped collection exists with the name: '+ self.name +'\n\n'
|
||||
+ ' To use this collection as a capped collection, please '
|
||||
+ 'first convert it.\n'
|
||||
+ ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped'
|
||||
var msg = 'A non-capped collection exists with the name: ' + _this.name + '\n\n'
|
||||
+ ' To use this collection as a capped collection, please '
|
||||
+ 'first convert it.\n'
|
||||
+ ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped';
|
||||
err = new Error(msg);
|
||||
callback(err);
|
||||
}
|
||||
} else {
|
||||
// create
|
||||
var opts = utils.clone(self.opts.capped);
|
||||
var opts = utils.clone(_this.opts.capped);
|
||||
opts.capped = true;
|
||||
self.conn.db.createCollection(self.name, opts, callback);
|
||||
_this.conn.db.createCollection(_this.name, opts, callback);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function callback (err, collection) {
|
||||
function callback(err, collection) {
|
||||
if (err) {
|
||||
// likely a strict mode error
|
||||
self.conn.emit('error', err);
|
||||
_this.conn.emit('error', err);
|
||||
} else {
|
||||
self.collection = collection;
|
||||
MongooseCollection.prototype.onOpen.call(self);
|
||||
_this.collection = collection;
|
||||
MongooseCollection.prototype.onOpen.call(_this);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -90,7 +92,7 @@ NativeCollection.prototype.onOpen = function () {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.onClose = function () {
|
||||
NativeCollection.prototype.onClose = function() {
|
||||
MongooseCollection.prototype.onClose.call(this);
|
||||
};
|
||||
|
||||
@@ -98,89 +100,135 @@ NativeCollection.prototype.onClose = function () {
|
||||
* Copy the collection methods and make them subject to queues
|
||||
*/
|
||||
|
||||
function iter(i) {
|
||||
NativeCollection.prototype[i] = function() {
|
||||
if (this.buffer) {
|
||||
this.addQueue(i, arguments);
|
||||
return;
|
||||
}
|
||||
|
||||
var collection = this.collection,
|
||||
args = arguments,
|
||||
_this = this,
|
||||
debug = _this.conn.base.options.debug;
|
||||
|
||||
if (debug) {
|
||||
if (typeof debug === 'function') {
|
||||
debug.apply(debug,
|
||||
[_this.name, i].concat(utils.args(args, 0, args.length - 1)));
|
||||
} else {
|
||||
this.$print(_this.name, i, args);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return collection[i].apply(collection, args);
|
||||
} catch (error) {
|
||||
// Collection operation may throw because of max bson size, catch it here
|
||||
// See gh-3906
|
||||
if (args.length > 0 &&
|
||||
typeof args[args.length - 1] === 'function') {
|
||||
args[args.length - 1](error);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
for (var i in Collection.prototype) {
|
||||
(function(i){
|
||||
NativeCollection.prototype[i] = function () {
|
||||
if (this.buffer) {
|
||||
this.addQueue(i, arguments);
|
||||
return;
|
||||
}
|
||||
// Janky hack to work around gh-3005 until we can get rid of the mongoose
|
||||
// collection abstraction
|
||||
try {
|
||||
if (typeof Collection.prototype[i] !== 'function') {
|
||||
continue;
|
||||
}
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var collection = this.collection
|
||||
, args = arguments
|
||||
, self = this
|
||||
, debug = self.conn.base.options.debug;
|
||||
|
||||
if (debug) {
|
||||
if ('function' === typeof debug) {
|
||||
debug.apply(debug
|
||||
, [self.name, i].concat(utils.args(args, 0, args.length-1)));
|
||||
} else {
|
||||
console.error('\x1B[0;36mMongoose:\x1B[0m %s.%s(%s) %s %s %s'
|
||||
, self.name
|
||||
, i
|
||||
, print(args[0])
|
||||
, print(args[1])
|
||||
, print(args[2])
|
||||
, print(args[3]))
|
||||
}
|
||||
}
|
||||
|
||||
collection[i].apply(collection, args);
|
||||
};
|
||||
})(i);
|
||||
iter(i);
|
||||
}
|
||||
|
||||
/*!
|
||||
/**
|
||||
* Debug print helper
|
||||
*
|
||||
* @api public
|
||||
* @method $print
|
||||
*/
|
||||
|
||||
function print (arg) {
|
||||
NativeCollection.prototype.$print = function(name, i, args) {
|
||||
var moduleName = '\x1B[0;36mMongoose:\x1B[0m ';
|
||||
var functionCall = [name, i].join('.');
|
||||
var _args = [];
|
||||
for (var j = args.length - 1; j >= 0; --j) {
|
||||
if (this.$format(args[j]) || _args.length) {
|
||||
_args.unshift(this.$format(args[j]));
|
||||
}
|
||||
}
|
||||
var params = '(' + _args.join(', ') + ')';
|
||||
|
||||
console.error(moduleName + functionCall + params);
|
||||
};
|
||||
|
||||
/**
|
||||
* Formatter for debug print args
|
||||
*
|
||||
* @api public
|
||||
* @method $format
|
||||
*/
|
||||
|
||||
NativeCollection.prototype.$format = function(arg) {
|
||||
var type = typeof arg;
|
||||
if ('function' === type || 'undefined' === type) return '';
|
||||
if (type === 'function' || type === 'undefined') return '';
|
||||
return format(arg);
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Debug print helper
|
||||
*/
|
||||
|
||||
function format (obj, sub) {
|
||||
var x = utils.clone(obj);
|
||||
if (x) {
|
||||
if ('Binary' === x.constructor.name) {
|
||||
function map(o) {
|
||||
return format(o, true);
|
||||
}
|
||||
function formatObjectId(x, key) {
|
||||
var representation = 'ObjectId("' + x[key].toHexString() + '")';
|
||||
x[key] = {inspect: function() { return representation; }};
|
||||
}
|
||||
function formatDate(x, key) {
|
||||
var representation = 'new Date("' + x[key].toUTCString() + '")';
|
||||
x[key] = {inspect: function() { return representation; }};
|
||||
}
|
||||
function format(obj, sub) {
|
||||
var x = utils.clone(obj, {retainKeyOrder: 1});
|
||||
var representation;
|
||||
|
||||
if (x != null) {
|
||||
if (x.constructor.name === 'Binary') {
|
||||
x = '[object Buffer]';
|
||||
} else if ('ObjectID' === x.constructor.name) {
|
||||
var representation = 'ObjectId("' + x.toHexString() + '")';
|
||||
x = { inspect: function() { return representation; } };
|
||||
} else if ('Date' === x.constructor.name) {
|
||||
var representation = 'new Date("' + x.toUTCString() + '")';
|
||||
x = { inspect: function() { return representation; } };
|
||||
} else if ('Object' === x.constructor.name) {
|
||||
var keys = Object.keys(x)
|
||||
, i = keys.length
|
||||
, key
|
||||
while (i--) {
|
||||
} else if (x.constructor.name === 'ObjectID') {
|
||||
representation = 'ObjectId("' + x.toHexString() + '")';
|
||||
x = {inspect: function() { return representation; }};
|
||||
} else if (x.constructor.name === 'Date') {
|
||||
representation = 'new Date("' + x.toUTCString() + '")';
|
||||
x = {inspect: function() { return representation; }};
|
||||
} else if (x.constructor.name === 'Object') {
|
||||
var keys = Object.keys(x);
|
||||
var numKeys = keys.length;
|
||||
var key;
|
||||
for (var i = 0; i < numKeys; ++i) {
|
||||
key = keys[i];
|
||||
if (x[key]) {
|
||||
if ('Binary' === x[key].constructor.name) {
|
||||
if (x[key].constructor.name === 'Binary') {
|
||||
x[key] = '[object Buffer]';
|
||||
} else if ('Object' === x[key].constructor.name) {
|
||||
} else if (x[key].constructor.name === 'Object') {
|
||||
x[key] = format(x[key], true);
|
||||
} else if ('ObjectID' === x[key].constructor.name) {
|
||||
;(function(x){
|
||||
var representation = 'ObjectId("' + x[key].toHexString() + '")';
|
||||
x[key] = { inspect: function() { return representation; } };
|
||||
})(x)
|
||||
} else if ('Date' === x[key].constructor.name) {
|
||||
;(function(x){
|
||||
var representation = 'new Date("' + x[key].toUTCString() + '")';
|
||||
x[key] = { inspect: function() { return representation; } };
|
||||
})(x)
|
||||
} else if (x[key].constructor.name === 'ObjectID') {
|
||||
formatObjectId(x, key);
|
||||
} else if (x[key].constructor.name === 'Date') {
|
||||
formatDate(x, key);
|
||||
} else if (Array.isArray(x[key])) {
|
||||
x[key] = x[key].map(function (o) {
|
||||
return format(o, true)
|
||||
});
|
||||
x[key] = x[key].map(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -189,9 +237,9 @@ function format (obj, sub) {
|
||||
}
|
||||
|
||||
return require('util')
|
||||
.inspect(x, false, 10, true)
|
||||
.replace(/\n/g, '')
|
||||
.replace(/\s{2,}/g, ' ')
|
||||
.inspect(x, false, 10, true)
|
||||
.replace(/\n/g, '')
|
||||
.replace(/\s{2,}/g, ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
248
node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js
generated
vendored
248
node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js
generated
vendored
@@ -2,13 +2,14 @@
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var MongooseConnection = require('../../connection')
|
||||
, mongo = require('mongodb')
|
||||
, Db = mongo.Db
|
||||
, Server = mongo.Server
|
||||
, Mongos = mongo.Mongos
|
||||
, STATES = require('../../connectionstate')
|
||||
, ReplSetServers = mongo.ReplSetServers;
|
||||
var MongooseConnection = require('../../connection');
|
||||
var mongo = require('mongodb');
|
||||
var Db = mongo.Db;
|
||||
var Server = mongo.Server;
|
||||
var Mongos = mongo.Mongos;
|
||||
var STATES = require('../../connectionstate');
|
||||
var ReplSetServers = mongo.ReplSet;
|
||||
var DisconnectedError = require('../../error/disconnected');
|
||||
|
||||
/**
|
||||
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) connection implementation.
|
||||
@@ -20,7 +21,7 @@ var MongooseConnection = require('../../connection')
|
||||
function NativeConnection() {
|
||||
MongooseConnection.apply(this, arguments);
|
||||
this._listening = false;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose the possible connection states.
|
||||
@@ -43,33 +44,107 @@ NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NativeConnection.prototype.doOpen = function (fn) {
|
||||
if (this.db) {
|
||||
mute(this);
|
||||
NativeConnection.prototype.doOpen = function(fn) {
|
||||
var _this = this;
|
||||
var server = new Server(this.host, this.port, this.options.server);
|
||||
|
||||
if (this.options && this.options.mongos) {
|
||||
var mongos = new Mongos([server], this.options.mongos);
|
||||
this.db = new Db(this.name, mongos, this.options.db);
|
||||
} else {
|
||||
this.db = new Db(this.name, server, this.options.db);
|
||||
}
|
||||
|
||||
var server = new Server(this.host, this.port, this.options.server);
|
||||
this.db = new Db(this.name, server, this.options.db);
|
||||
this.db.open(function(err) {
|
||||
listen(_this);
|
||||
|
||||
if (!mongos) {
|
||||
server.s.server.on('error', function(error) {
|
||||
if (/after \d+ retries/.test(error.message)) {
|
||||
_this.emit('error', new DisconnectedError(server.s.server.name));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var self = this;
|
||||
this.db.open(function (err) {
|
||||
if (err) return fn(err);
|
||||
listen(self);
|
||||
|
||||
fn();
|
||||
});
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Switches to a different database using the same connection pool.
|
||||
*
|
||||
* Returns a new connection object, with the new db.
|
||||
*
|
||||
* @param {String} name The database name
|
||||
* @return {Connection} New Connection Object
|
||||
* @api public
|
||||
*/
|
||||
|
||||
NativeConnection.prototype.useDb = function(name) {
|
||||
// we have to manually copy all of the attributes...
|
||||
var newConn = new this.constructor();
|
||||
newConn.name = name;
|
||||
newConn.base = this.base;
|
||||
newConn.collections = {};
|
||||
newConn.models = {};
|
||||
newConn.replica = this.replica;
|
||||
newConn.hosts = this.hosts;
|
||||
newConn.host = this.host;
|
||||
newConn.port = this.port;
|
||||
newConn.user = this.user;
|
||||
newConn.pass = this.pass;
|
||||
newConn.options = this.options;
|
||||
newConn._readyState = this._readyState;
|
||||
newConn._closeCalled = this._closeCalled;
|
||||
newConn._hasOpened = this._hasOpened;
|
||||
newConn._listening = false;
|
||||
|
||||
// First, when we create another db object, we are not guaranteed to have a
|
||||
// db object to work with. So, in the case where we have a db object and it
|
||||
// is connected, we can just proceed with setting everything up. However, if
|
||||
// we do not have a db or the state is not connected, then we need to wait on
|
||||
// the 'open' event of the connection before doing the rest of the setup
|
||||
// the 'connected' event is the first time we'll have access to the db object
|
||||
|
||||
var _this = this;
|
||||
|
||||
if (this.db && this._readyState === STATES.connected) {
|
||||
wireup();
|
||||
} else {
|
||||
this.once('connected', wireup);
|
||||
}
|
||||
|
||||
function wireup() {
|
||||
newConn.db = _this.db.db(name);
|
||||
newConn.onOpen();
|
||||
// setup the events appropriately
|
||||
listen(newConn);
|
||||
}
|
||||
|
||||
newConn.name = name;
|
||||
|
||||
// push onto the otherDbs stack, this is used when state changes
|
||||
this.otherDbs.push(newConn);
|
||||
newConn.otherDbs.push(this);
|
||||
|
||||
return newConn;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Register listeners for important events and bubble appropriately.
|
||||
*/
|
||||
|
||||
function listen (conn) {
|
||||
if (conn._listening) return;
|
||||
conn._listening = true;
|
||||
function listen(conn) {
|
||||
if (conn.db._listening) {
|
||||
return;
|
||||
}
|
||||
conn.db._listening = true;
|
||||
|
||||
conn.db.on('close', function(){
|
||||
conn.db.on('close', function() {
|
||||
if (conn._closeCalled) return;
|
||||
|
||||
// the driver never emits an `open` event. auto_reconnect still
|
||||
@@ -82,33 +157,26 @@ function listen (conn) {
|
||||
}
|
||||
conn.onClose();
|
||||
});
|
||||
conn.db.on('error', function(err){
|
||||
conn.db.on('error', function(err) {
|
||||
conn.emit('error', err);
|
||||
});
|
||||
conn.db.on('timeout', function(err){
|
||||
conn.db.on('reconnect', function() {
|
||||
conn.readyState = STATES.connected;
|
||||
conn.emit('reconnected');
|
||||
});
|
||||
conn.db.on('timeout', function(err) {
|
||||
var error = new Error(err && err.err || 'connection timeout');
|
||||
conn.emit('error', error);
|
||||
});
|
||||
conn.db.on('open', function (err, db) {
|
||||
conn.db.on('open', function(err, db) {
|
||||
if (STATES.disconnected === conn.readyState && db && db.databaseName) {
|
||||
conn.readyState = STATES.connected;
|
||||
conn.emit('reconnected')
|
||||
conn.emit('reconnected');
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/*!
|
||||
* Remove listeners registered in `listen`
|
||||
*/
|
||||
|
||||
function mute (conn) {
|
||||
if (!conn.db) throw new Error('missing db');
|
||||
conn.db.removeAllListeners("close");
|
||||
conn.db.removeAllListeners("error");
|
||||
conn.db.removeAllListeners("timeout");
|
||||
conn.db.removeAllListeners("open");
|
||||
conn.db.removeAllListeners("fullsetup");
|
||||
conn._listening = false;
|
||||
});
|
||||
conn.db.on('parseError', function(err) {
|
||||
conn.emit('parseError', err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,33 +189,33 @@ function mute (conn) {
|
||||
* @return {Connection} this
|
||||
*/
|
||||
|
||||
NativeConnection.prototype.doOpenSet = function (fn) {
|
||||
if (this.db) {
|
||||
mute(this);
|
||||
}
|
||||
NativeConnection.prototype.doOpenSet = function(fn) {
|
||||
var servers = [],
|
||||
_this = this;
|
||||
|
||||
var servers = []
|
||||
, self = this;
|
||||
|
||||
this.hosts.forEach(function (server) {
|
||||
this.hosts.forEach(function(server) {
|
||||
var host = server.host || server.ipc;
|
||||
var port = server.port || 27017;
|
||||
servers.push(new Server(host, port, self.options.server));
|
||||
})
|
||||
servers.push(new Server(host, port, _this.options.server));
|
||||
});
|
||||
|
||||
var server = this.options.mongos
|
||||
? new Mongos(servers, this.options.mongos)
|
||||
: new ReplSetServers(servers, this.options.replset);
|
||||
: new ReplSetServers(servers, this.options.replset || this.options.replSet);
|
||||
this.db = new Db(this.name, server, this.options.db);
|
||||
|
||||
this.db.on('fullsetup', function () {
|
||||
self.emit('fullsetup')
|
||||
this.db.on('fullsetup', function() {
|
||||
_this.emit('fullsetup');
|
||||
});
|
||||
|
||||
this.db.open(function (err) {
|
||||
this.db.on('all', function() {
|
||||
_this.emit('all');
|
||||
});
|
||||
|
||||
this.db.open(function(err) {
|
||||
if (err) return fn(err);
|
||||
fn();
|
||||
listen(self);
|
||||
listen(_this);
|
||||
});
|
||||
|
||||
return this;
|
||||
@@ -161,11 +229,10 @@ NativeConnection.prototype.doOpenSet = function (fn) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NativeConnection.prototype.doClose = function (fn) {
|
||||
this.db.close();
|
||||
if (fn) fn();
|
||||
NativeConnection.prototype.doClose = function(fn) {
|
||||
this.db.close(fn);
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepares default connection options for the node-mongodb-native driver.
|
||||
@@ -177,69 +244,75 @@ NativeConnection.prototype.doClose = function (fn) {
|
||||
* @api private
|
||||
*/
|
||||
|
||||
NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
|
||||
NativeConnection.prototype.parseOptions = function(passed, connStrOpts) {
|
||||
var o = passed || {};
|
||||
o.db || (o.db = {});
|
||||
o.auth || (o.auth = {});
|
||||
o.server || (o.server = {});
|
||||
o.replset || (o.replset = {});
|
||||
o.replset || (o.replset = o.replSet) || (o.replset = {});
|
||||
o.server.socketOptions || (o.server.socketOptions = {});
|
||||
o.replset.socketOptions || (o.replset.socketOptions = {});
|
||||
o.mongos || (o.mongos = (connStrOpts && connStrOpts.mongos));
|
||||
(o.mongos === true) && (o.mongos = {});
|
||||
|
||||
var opts = connStrOpts || {};
|
||||
Object.keys(opts).forEach(function (name) {
|
||||
Object.keys(opts).forEach(function(name) {
|
||||
switch (name) {
|
||||
case 'ssl':
|
||||
o.server.ssl = opts.ssl;
|
||||
o.replset.ssl = opts.ssl;
|
||||
o.mongos && (o.mongos.ssl = opts.ssl);
|
||||
break;
|
||||
case 'poolSize':
|
||||
if ('undefined' == typeof o.server.poolSize) {
|
||||
o.server.poolSize = o.replset.poolSize = opts[name];
|
||||
if (typeof o.server[name] === 'undefined') {
|
||||
o.server[name] = o.replset[name] = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'slaveOk':
|
||||
if ('undefined' == typeof o.server.slave_ok) {
|
||||
if (typeof o.server.slave_ok === 'undefined') {
|
||||
o.server.slave_ok = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'autoReconnect':
|
||||
if ('undefined' == typeof o.server.auto_reconnect) {
|
||||
if (typeof o.server.auto_reconnect === 'undefined') {
|
||||
o.server.auto_reconnect = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'ssl':
|
||||
case 'socketTimeoutMS':
|
||||
case 'connectTimeoutMS':
|
||||
if ('undefined' == typeof o.server.socketOptions[name]) {
|
||||
if (typeof o.server.socketOptions[name] === 'undefined') {
|
||||
o.server.socketOptions[name] = o.replset.socketOptions[name] = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'authdb':
|
||||
if ('undefined' == typeof o.auth.authdb) {
|
||||
if (typeof o.auth.authdb === 'undefined') {
|
||||
o.auth.authdb = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'authSource':
|
||||
if ('undefined' == typeof o.auth.authSource) {
|
||||
if (typeof o.auth.authSource === 'undefined') {
|
||||
o.auth.authSource = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'retries':
|
||||
case 'reconnectWait':
|
||||
case 'rs_name':
|
||||
if ('undefined' == typeof o.replset[name]) {
|
||||
if (typeof o.replset[name] === 'undefined') {
|
||||
o.replset[name] = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'replicaSet':
|
||||
if ('undefined' == typeof o.replset.rs_name) {
|
||||
if (typeof o.replset.rs_name === 'undefined') {
|
||||
o.replset.rs_name = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'readSecondary':
|
||||
if ('undefined' == typeof o.replset.read_secondary) {
|
||||
if (typeof o.replset.read_secondary === 'undefined') {
|
||||
o.replset.read_secondary = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'nativeParser':
|
||||
if ('undefined' == typeof o.db.native_parser) {
|
||||
if (typeof o.db.native_parser === 'undefined') {
|
||||
o.db.native_parser = opts[name];
|
||||
}
|
||||
break;
|
||||
@@ -248,32 +321,31 @@ NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
|
||||
case 'fsync':
|
||||
case 'journal':
|
||||
case 'wtimeoutMS':
|
||||
if ('undefined' == typeof o.db[name]) {
|
||||
if (typeof o.db[name] === 'undefined') {
|
||||
o.db[name] = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'readPreference':
|
||||
if ('undefined' == typeof o.db.read_preference) {
|
||||
o.db.read_preference = opts[name];
|
||||
if (typeof o.db.readPreference === 'undefined') {
|
||||
o.db.readPreference = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'readPreferenceTags':
|
||||
if ('undefined' == typeof o.db.read_preference_tags) {
|
||||
if (typeof o.db.read_preference_tags === 'undefined') {
|
||||
o.db.read_preference_tags = opts[name];
|
||||
}
|
||||
break;
|
||||
case 'sslValidate':
|
||||
o.server.sslValidate = opts.sslValidate;
|
||||
o.replset.sslValidate = opts.sslValidate;
|
||||
o.mongos && (o.mongos.sslValidate = opts.sslValidate);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
if (!('auto_reconnect' in o.server)) {
|
||||
o.server.auto_reconnect = true;
|
||||
}
|
||||
|
||||
if (!o.db.read_preference) {
|
||||
// read from primaries by default
|
||||
o.db.read_preference = 'primary';
|
||||
}
|
||||
|
||||
// mongoose creates its own ObjectIds
|
||||
o.db.forceServerObjectId = false;
|
||||
|
||||
@@ -283,9 +355,13 @@ NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
|
||||
o.db.w = 1;
|
||||
}
|
||||
|
||||
if (o.promiseLibrary) {
|
||||
o.db.promiseLibrary = o.promiseLibrary;
|
||||
}
|
||||
|
||||
validate(o);
|
||||
return o;
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Validates the driver db options.
|
||||
@@ -293,8 +369,8 @@ NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
|
||||
* @param {Object} o
|
||||
*/
|
||||
|
||||
function validate (o) {
|
||||
if (-1 === o.db.w || 0 === o.db.w) {
|
||||
function validate(o) {
|
||||
if (o.db.w === -1 || o.db.w === 0) {
|
||||
if (o.db.journal || o.db.fsync || o.db.safe) {
|
||||
throw new Error(
|
||||
'Invalid writeConcern: '
|
||||
|
||||
7
node_modules/mongoose/lib/drivers/node-mongodb-native/index.js
generated
vendored
Normal file
7
node_modules/mongoose/lib/drivers/node-mongodb-native/index.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
/*!
|
||||
* Module exports.
|
||||
*/
|
||||
|
||||
exports.Binary = require('./binary');
|
||||
exports.ObjectId = require('./objectid');
|
||||
exports.ReadPreference = require('./ReadPreference');
|
||||
17
node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js
generated
vendored
17
node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js
generated
vendored
@@ -5,25 +5,10 @@
|
||||
* @see ObjectId
|
||||
*/
|
||||
|
||||
var ObjectId = require('mongodb').BSONPure.ObjectID;
|
||||
var ObjectId = require('mongodb').ObjectId;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
var ObjectIdToString = ObjectId.toString.bind(ObjectId);
|
||||
module.exports = exports = ObjectId;
|
||||
|
||||
ObjectId.fromString = function(str){
|
||||
// patch native driver bug in V0.9.6.4
|
||||
if (!('string' === typeof str && 24 === str.length)) {
|
||||
throw new Error("Invalid ObjectId");
|
||||
}
|
||||
|
||||
return ObjectId.createFromHexString(str);
|
||||
};
|
||||
|
||||
ObjectId.toString = function(oid){
|
||||
if (!arguments.length) return ObjectIdToString();
|
||||
return oid.toHexString();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user