5 var MongooseCollection = require('../../collection'),
6 Collection = require('mongodb').Collection,
7 utils = require('../../utils');
10 * A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation.
12 * All methods methods from the [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) driver are copied and wrapped in queue management.
14 * @inherits Collection
18 function NativeCollection() {
19 this.collection = null;
20 MongooseCollection.apply(this, arguments);
24 * Inherit from abstract Collection.
27 NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
30 * Called when the connection opens.
35 NativeCollection.prototype.onOpen = function() {
38 // always get a new collection in case the user changed host:port
39 // of parent db instance when re-opening the connection.
41 if (!_this.opts.capped.size) {
43 callback(null, _this.conn.db.collection(_this.name));
44 return _this.collection;
48 return _this.conn.db.collection(_this.name, function(err, c) {
49 if (err) return callback(err);
51 // discover if this collection exists and if it is capped
52 _this.conn.db.listCollections({name: _this.name}).toArray(function(err, docs) {
60 if (doc.options && doc.options.capped) {
63 var msg = 'A non-capped collection exists with the name: ' + _this.name + '\n\n'
64 + ' To use this collection as a capped collection, please '
65 + 'first convert it.\n'
66 + ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped';
72 var opts = utils.clone(_this.opts.capped);
74 _this.conn.db.createCollection(_this.name, opts, callback);
79 function callback(err, collection) {
81 // likely a strict mode error
82 _this.conn.emit('error', err);
84 _this.collection = collection;
85 MongooseCollection.prototype.onOpen.call(_this);
91 * Called when the connection closes
96 NativeCollection.prototype.onClose = function() {
97 MongooseCollection.prototype.onClose.call(this);
101 * Copy the collection methods and make them subject to queues
105 NativeCollection.prototype[i] = function() {
107 this.addQueue(i, arguments);
111 var collection = this.collection,
114 debug = _this.conn.base.options.debug;
117 if (typeof debug === 'function') {
119 [_this.name, i].concat(utils.args(args, 0, args.length - 1)));
121 this.$print(_this.name, i, args);
126 return collection[i].apply(collection, args);
128 // Collection operation may throw because of max bson size, catch it here
130 if (args.length > 0 &&
131 typeof args[args.length - 1] === 'function') {
132 args[args.length - 1](error);
140 for (var i in Collection.prototype) {
141 // Janky hack to work around gh-3005 until we can get rid of the mongoose
142 // collection abstraction
144 if (typeof Collection.prototype[i] !== 'function') {
161 NativeCollection.prototype.$print = function(name, i, args) {
162 var moduleName = '\x1B[0;36mMongoose:\x1B[0m ';
163 var functionCall = [name, i].join('.');
165 for (var j = args.length - 1; j >= 0; --j) {
166 if (this.$format(args[j]) || _args.length) {
167 _args.unshift(this.$format(args[j]));
170 var params = '(' + _args.join(', ') + ')';
172 console.error(moduleName + functionCall + params);
176 * Formatter for debug print args
182 NativeCollection.prototype.$format = function(arg) {
183 var type = typeof arg;
184 if (type === 'function' || type === 'undefined') return '';
193 return format(o, true);
195 function formatObjectId(x, key) {
196 var representation = 'ObjectId("' + x[key].toHexString() + '")';
197 x[key] = {inspect: function() { return representation; }};
199 function formatDate(x, key) {
200 var representation = 'new Date("' + x[key].toUTCString() + '")';
201 x[key] = {inspect: function() { return representation; }};
203 function format(obj, sub) {
204 if (obj && typeof obj.toBSON === 'function') {
207 var x = utils.clone(obj, {retainKeyOrder: 1, transform: false});
211 if (x.constructor.name === 'Binary') {
212 x = 'BinData(' + x.sub_type + ', "' + x.toString('base64') + '")';
213 } else if (x.constructor.name === 'ObjectID') {
214 representation = 'ObjectId("' + x.toHexString() + '")';
215 x = {inspect: function() { return representation; }};
216 } else if (x.constructor.name === 'Date') {
217 representation = 'new Date("' + x.toUTCString() + '")';
218 x = {inspect: function() { return representation; }};
219 } else if (x.constructor.name === 'Object') {
220 var keys = Object.keys(x);
221 var numKeys = keys.length;
223 for (var i = 0; i < numKeys; ++i) {
226 if (typeof x[key].toBSON === 'function') {
227 x[key] = x[key].toBSON();
229 if (x[key].constructor.name === 'Binary') {
230 x[key] = 'BinData(' + x[key].sub_type + ', "' +
231 x[key].buffer.toString('base64') + '")';
232 } else if (x[key].constructor.name === 'Object') {
233 x[key] = format(x[key], true);
234 } else if (x[key].constructor.name === 'ObjectID') {
235 formatObjectId(x, key);
236 } else if (x[key].constructor.name === 'Date') {
238 } else if (Array.isArray(x[key])) {
239 x[key] = x[key].map(map);
247 return require('util')
248 .inspect(x, false, 10, true)
250 .replace(/\s{2,}/g, ' ');
254 * Retreives information about this collections indexes.
256 * @param {Function} callback
261 NativeCollection.prototype.getIndexes = NativeCollection.prototype.indexInformation;
267 module.exports = NativeCollection;