Updated mongoose

This commit is contained in:
Dobie Wollert
2015-11-24 22:08:58 -08:00
parent 71a05fb732
commit 8b5827c970
618 changed files with 122299 additions and 37754 deletions

View File

@ -3,10 +3,9 @@
* 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 +16,7 @@ var MongooseCollection = require('../../collection')
* @api private
*/
function NativeCollection () {
function NativeCollection() {
this.collection = null;
MongooseCollection.apply(this, arguments);
}
@ -34,7 +33,7 @@ NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
* @api private
*/
NativeCollection.prototype.onOpen = function () {
NativeCollection.prototype.onOpen = function() {
var self = this;
// always get a new collection in case the user changed host:port
@ -46,21 +45,25 @@ NativeCollection.prototype.onOpen = function () {
}
// capped
return self.conn.db.collection(self.name, function (err, c) {
return self.conn.db.collection(self.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);
self.conn.db.listCollections({ name: self.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'
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'
+ ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped';
err = new Error(msg);
callback(err);
}
@ -73,7 +76,7 @@ NativeCollection.prototype.onOpen = function () {
});
});
function callback (err, collection) {
function callback(err, collection) {
if (err) {
// likely a strict mode error
self.conn.emit('error', err);
@ -81,7 +84,7 @@ NativeCollection.prototype.onOpen = function () {
self.collection = collection;
MongooseCollection.prototype.onOpen.call(self);
}
};
}
};
/**
@ -90,7 +93,7 @@ NativeCollection.prototype.onOpen = function () {
* @api private
*/
NativeCollection.prototype.onClose = function () {
NativeCollection.prototype.onClose = function() {
MongooseCollection.prototype.onClose.call(this);
};
@ -99,68 +102,92 @@ NativeCollection.prototype.onClose = function () {
*/
for (var i in Collection.prototype) {
(function(i){
NativeCollection.prototype[i] = function () {
// 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;
}
(function(i) {
NativeCollection.prototype[i] = function() {
if (this.buffer) {
this.addQueue(i, arguments);
return;
}
var collection = this.collection
, args = arguments
, self = this
, debug = self.conn.base.options.debug;
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)));
, [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]))
this.$print(self.name, i, args);
}
}
collection[i].apply(collection, args);
return collection[i].apply(collection, args);
};
})(i);
}
/*!
/**
* Debug print helper
*
* @api public
*/
function print (arg) {
NativeCollection.prototype.$print = function(name, i, args) {
console.error(
'\x1B[0;36mMongoose:\x1B[0m %s.%s(%s) %s %s %s',
name,
i,
this.$format(args[0]),
this.$format(args[1]),
this.$format(args[2]),
this.$format(args[3]));
};
/**
* Formatter for debug print args
*
* @api public
*/
NativeCollection.prototype.$format = function(arg) {
var type = typeof arg;
if ('function' === type || 'undefined' === type) return '';
return format(arg);
}
};
/*!
* Debug print helper
*/
function format (obj, sub) {
var x = utils.clone(obj);
function format(obj, sub) {
var x = utils.clone(obj, { retainKeyOrder: 1 });
var representation;
if (x) {
if ('Binary' === x.constructor.name) {
x = '[object Buffer]';
} else if ('ObjectID' === x.constructor.name) {
var representation = 'ObjectId("' + x.toHexString() + '")';
representation = 'ObjectId("' + x.toHexString() + '")';
x = { inspect: function() { return representation; } };
} else if ('Date' === x.constructor.name) {
var representation = 'new Date("' + x.toUTCString() + '")';
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--) {
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) {
@ -168,18 +195,18 @@ function format (obj, sub) {
} else if ('Object' === x[key].constructor.name) {
x[key] = format(x[key], true);
} else if ('ObjectID' === x[key].constructor.name) {
;(function(x){
(function(x) {
var representation = 'ObjectId("' + x[key].toHexString() + '")';
x[key] = { inspect: function() { return representation; } };
})(x)
})(x);
} else if ('Date' === x[key].constructor.name) {
;(function(x){
(function(x) {
var representation = 'new Date("' + x[key].toUTCString() + '")';
x[key] = { inspect: function() { return representation; } };
})(x)
})(x);
} else if (Array.isArray(x[key])) {
x[key] = x[key].map(function (o) {
return format(o, true)
x[key] = x[key].map(function(o) {
return format(o, true);
});
}
}
@ -191,7 +218,7 @@ function format (obj, sub) {
return require('util')
.inspect(x, false, 10, true)
.replace(/\n/g, '')
.replace(/\s{2,}/g, ' ')
.replace(/\s{2,}/g, ' ');
}
/**