Files

239 lines
6.0 KiB
JavaScript
Raw Permalink Normal View History

2014-09-14 07:04:16 -04:00
/*!
* Module dependencies.
*/
2015-11-24 22:08:58 -08:00
var MongooseCollection = require('../../collection'),
Collection = require('mongodb').Collection,
utils = require('../../utils');
2014-09-14 07:04:16 -04:00
/**
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation.
*
* All methods methods from the [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) driver are copied and wrapped in queue management.
*
* @inherits Collection
* @api private
*/
2015-11-24 22:08:58 -08:00
function NativeCollection() {
2014-09-14 07:04:16 -04:00
this.collection = null;
MongooseCollection.apply(this, arguments);
}
/*!
* Inherit from abstract Collection.
*/
NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
/**
* Called when the connection opens.
*
* @api private
*/
2015-11-24 22:08:58 -08:00
NativeCollection.prototype.onOpen = function() {
2014-09-14 07:04:16 -04:00
var self = 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) {
// non-capped
return self.conn.db.collection(self.name, callback);
}
// capped
2015-11-24 22:08:58 -08:00
return self.conn.db.collection(self.name, function(err, c) {
2014-09-14 07:04:16 -04:00
if (err) return callback(err);
// discover if this collection exists and if it is capped
2015-11-24 22:08:58 -08:00
self.conn.db.listCollections({ name: self.name }).toArray(function(err, docs) {
if (err) {
return callback(err);
}
var doc = docs[0];
var exists = !!doc;
2014-09-14 07:04:16 -04:00
if (exists) {
2015-11-24 22:08:58 -08:00
if (doc.options && doc.options.capped) {
2014-09-14 07:04:16 -04:00
callback(null, c);
} else {
2015-11-24 22:08:58 -08:00
var msg = 'A non-capped collection exists with the name: ' + self.name + '\n\n'
2014-09-14 07:04:16 -04:00
+ ' To use this collection as a capped collection, please '
+ 'first convert it.\n'
2015-11-24 22:08:58 -08:00
+ ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped';
2014-09-14 07:04:16 -04:00
err = new Error(msg);
callback(err);
}
} else {
// create
var opts = utils.clone(self.opts.capped);
opts.capped = true;
self.conn.db.createCollection(self.name, opts, callback);
}
});
});
2015-11-24 22:08:58 -08:00
function callback(err, collection) {
2014-09-14 07:04:16 -04:00
if (err) {
// likely a strict mode error
self.conn.emit('error', err);
} else {
self.collection = collection;
MongooseCollection.prototype.onOpen.call(self);
}
2015-11-24 22:08:58 -08:00
}
2014-09-14 07:04:16 -04:00
};
/**
* Called when the connection closes
*
* @api private
*/
2015-11-24 22:08:58 -08:00
NativeCollection.prototype.onClose = function() {
2014-09-14 07:04:16 -04:00
MongooseCollection.prototype.onClose.call(this);
};
/*!
* Copy the collection methods and make them subject to queues
*/
for (var i in Collection.prototype) {
2015-11-24 22:08:58 -08:00
// 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() {
2014-09-14 07:04:16 -04:00
if (this.buffer) {
this.addQueue(i, arguments);
return;
}
2015-11-24 22:08:58 -08:00
var collection = this.collection,
args = arguments,
self = this,
debug = self.conn.base.options.debug;
2014-09-14 07:04:16 -04:00
if (debug) {
if ('function' === typeof debug) {
debug.apply(debug
2015-11-24 22:08:58 -08:00
, [self.name, i].concat(utils.args(args, 0, args.length - 1)));
2014-09-14 07:04:16 -04:00
} else {
2015-11-24 22:08:58 -08:00
this.$print(self.name, i, args);
2014-09-14 07:04:16 -04:00
}
}
2015-11-24 22:08:58 -08:00
return collection[i].apply(collection, args);
2014-09-14 07:04:16 -04:00
};
})(i);
}
2015-11-24 22:08:58 -08:00
/**
2014-09-14 07:04:16 -04:00
* Debug print helper
2015-11-24 22:08:58 -08:00
*
* @api public
2014-09-14 07:04:16 -04:00
*/
2015-11-24 22:08:58 -08:00
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) {
2014-09-14 07:04:16 -04:00
var type = typeof arg;
if ('function' === type || 'undefined' === type) return '';
return format(arg);
2015-11-24 22:08:58 -08:00
};
2014-09-14 07:04:16 -04:00
/*!
* Debug print helper
*/
2015-11-24 22:08:58 -08:00
function format(obj, sub) {
var x = utils.clone(obj, { retainKeyOrder: 1 });
var representation;
2014-09-14 07:04:16 -04:00
if (x) {
if ('Binary' === x.constructor.name) {
x = '[object Buffer]';
} else if ('ObjectID' === x.constructor.name) {
2015-11-24 22:08:58 -08:00
representation = 'ObjectId("' + x.toHexString() + '")';
2014-09-14 07:04:16 -04:00
x = { inspect: function() { return representation; } };
} else if ('Date' === x.constructor.name) {
2015-11-24 22:08:58 -08:00
representation = 'new Date("' + x.toUTCString() + '")';
2014-09-14 07:04:16 -04:00
x = { inspect: function() { return representation; } };
} else if ('Object' === x.constructor.name) {
2015-11-24 22:08:58 -08:00
var keys = Object.keys(x);
var numKeys = keys.length;
var key;
for (var i = 0; i < numKeys; ++i) {
2014-09-14 07:04:16 -04:00
key = keys[i];
if (x[key]) {
if ('Binary' === x[key].constructor.name) {
x[key] = '[object Buffer]';
} else if ('Object' === x[key].constructor.name) {
x[key] = format(x[key], true);
} else if ('ObjectID' === x[key].constructor.name) {
2015-11-24 22:08:58 -08:00
(function(x) {
2014-09-14 07:04:16 -04:00
var representation = 'ObjectId("' + x[key].toHexString() + '")';
x[key] = { inspect: function() { return representation; } };
2015-11-24 22:08:58 -08:00
})(x);
2014-09-14 07:04:16 -04:00
} else if ('Date' === x[key].constructor.name) {
2015-11-24 22:08:58 -08:00
(function(x) {
2014-09-14 07:04:16 -04:00
var representation = 'new Date("' + x[key].toUTCString() + '")';
x[key] = { inspect: function() { return representation; } };
2015-11-24 22:08:58 -08:00
})(x);
2014-09-14 07:04:16 -04:00
} else if (Array.isArray(x[key])) {
2015-11-24 22:08:58 -08:00
x[key] = x[key].map(function(o) {
return format(o, true);
2014-09-14 07:04:16 -04:00
});
}
}
}
}
if (sub) return x;
}
return require('util')
.inspect(x, false, 10, true)
.replace(/\n/g, '')
2015-11-24 22:08:58 -08:00
.replace(/\s{2,}/g, ' ');
2014-09-14 07:04:16 -04:00
}
/**
* Retreives information about this collections indexes.
*
* @param {Function} callback
* @method getIndexes
* @api public
*/
NativeCollection.prototype.getIndexes = NativeCollection.prototype.indexInformation;
/*!
* Module exports.
*/
module.exports = NativeCollection;