forked from Automattic/mongoose
/
collection.js
104 lines (83 loc) · 1.96 KB
/
collection.js
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
/**
* Module dependencies.
*/
var Collection = require('../../collection')
, MongoCollection = require('mongodb').Collection;
/**
* Native collection
*
* @api private
*/
function NativeCollection () {
Collection.apply(this, arguments);
};
/**
* Inherit from Collection.
*/
NativeCollection.prototype.__proto__ = Collection.prototype;
/**
* Called when the connection opens
*
* @api private
*/
NativeCollection.prototype.onOpen = function () {
var self = this;
this.conn.db.collection(this.name, function(err, collection){
if (!err){
self.collection = collection;
Collection.prototype.onOpen.call(self);
}
});
};
/**
* Called when the connection closes
*
* @api private
*/
NativeCollection.prototype.onClose = function () {
Collection.prototype.onClose.call(this);
};
/**
* Copy the collection methods and make them subject to queues
*/
for (var i in MongoCollection.prototype)
(function(i){
NativeCollection.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(){
collection[i].apply(collection, args);
});
} else
this.addQueue(i, arguments);
};
})(i);
/**
* Implement getIndexes
*
* @param {Function} callback
* @api private
*/
NativeCollection.prototype.getIndexes = NativeCollection.prototype.indexInformation;
/**
* Override signature of ensureIndex. -native one is not standard.
*
* @param {Object} spec
* @param {Object} options
* @param {Function} callback
* @api public
*/
var oldEnsureIndex = MongoCollection.prototype.ensureIndex;
function noop () {};
MongoCollection.prototype.ensureIndex = function(fields, options, fn){
if (!this.buffer) {
return oldEnsureIndex.apply(this, arguments);
}
};
/**
* Module exports.
*/
module.exports = NativeCollection;