summaryrefslogtreecommitdiff
path: root/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js
blob: 06176da3c64558ac2ef28153af895f4a9b37de12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 * Module dependencies.
 */

var Collection = require('../../collection')
  , NativeCollection = require('mongodb').Collection
  , utils = require('../../utils')

/**
 * Native collection
 *
 * @api private
 */

function MongooseCollection () {
  Collection.apply(this, arguments);
};

/**
 * Inherit from abstract Collection.
 */

MongooseCollection.prototype.__proto__ = Collection.prototype;

/**
 * Called when the connection opens
 *
 * @api private
 */

MongooseCollection.prototype.onOpen = function () {
  var self = this;
  this.conn.db.collection(this.name, function (err, collection) {
    if (err) {
      // likely a strict mode error
      self.conn.emit('error', err);
    } else {
      self.collection = collection;
      Collection.prototype.onOpen.call(self);
    }
  });
};

/**
 * Called when the connection closes
 *
 * @api private
 */

MongooseCollection.prototype.onClose = function () {
  Collection.prototype.onClose.call(this);
};

/**
 * Copy the collection methods and make them subject to queues
 */

for (var i in NativeCollection.prototype)
  (function(i){
    MongooseCollection.prototype[i] = function () {
      // BENCHMARKME: is it worth caching the prototype methods? probably
      if (!this.buffer) {
        var collection = this.collection
          , args = arguments
          , self = this;

        process.nextTick(function(){
          var 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);
        });
      } else {
        this.addQueue(i, arguments);
      }
    };
  })(i);

/**
 * Debug print helper
 * @private
 */

function print (arg) {
  var type = typeof arg;
  if ('function' === type || 'undefined' === type) return '';
  return format(arg);
}

/**
 * Debug print helper
 * @private
 */

function format (obj, sub) {
  var x = utils.clone(obj);
  if (x) {
    if ('Binary' === x.constructor.name) {
      x = '[object Buffer]';
    } else if ('Object' === x.constructor.name) {
      var keys = Object.keys(x)
        , i = keys.length
        , key
      while (i--) {
        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 (Array.isArray(x[key])) {
            x[key] = x[key].map(function (o) {
              return format(o, true)
            });
          }
        }
      }
    }
    if (sub) return x;
  }

  return require('util')
    .inspect(x, false, 10, true)
    .replace(/\n/g, '')
    .replace(/\s{2,}/g, ' ')
}

/**
 * Implement getIndexes
 *
 * @param {Function} callback
 * @api private
 */

MongooseCollection.prototype.getIndexes =
MongooseCollection.prototype.indexInformation;

/**
 * Override signature of ensureIndex. -native one is not standard.
 *
 * @param {Object} spec
 * @param {Object} options
 * @param {Function} callback
 * @api public
 */

var oldEnsureIndex = NativeCollection.prototype.ensureIndex;

function noop () {};

NativeCollection.prototype.ensureIndex = function(fields, options, fn){
  if (!this.buffer) {
    return oldEnsureIndex.apply(this, arguments);
  }
};

/**
 * Module exports.
 */

module.exports = MongooseCollection;