forked from kissjs/node-mongoskin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collection.js
227 lines (206 loc) · 5.26 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
bind these methods from Collection.prototype to Provider
methods:
insert
checkCollectionName
remove
rename
insertAll
save
update
distinct
count
drop
findAndModify
find
normalizeHintField
findOne
createIndex
ensureIndex
indexInformation
dropIndex
dropIndexes
mapReduce
group
options
*/
var __slice = Array.prototype.slice,
events = require('events'),
Collection = require('mongodb').Collection,
SkinCursor = require('./cursor').SkinCursor,
STATE_CLOSE = 0,
STATE_OPENNING = 1,
STATE_OPEN = 2;
/**
* Construct SkinCollection from SkinDb and collectionName
* use skinDb.collection('name') usually
*
* @param skinDb
* @param collectionName
*
*/
var SkinCollection = exports.SkinCollection = function(skinDb, collectionName) {
this.skinDb = skinDb;
this.ObjectID = this.skinDb.db.bson_serializer.ObjectID;
this.collectionName = collectionName;
this.collection;
this.state = STATE_CLOSE;
this.internalHint;
var that = this;
this.__defineGetter__('hint', function() { return this.internalHint; });
this.__defineSetter__('hint', function(value) {
this.internalHint = value;
this.open(function(err, collection) {
collection.hint = value;
that.internalHint = collection.hint;
});
});
this.emitter = new events.EventEmitter();
};
/**
* bind method of mongodb.Collection to mongoskin.SkinCollection
*/
var bindSkin = function(name, method) {
SkinCollection.prototype[name] = function() {
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
this.open(function(err, collection) {
if (err) {
args[args.length - 1](err);
} else {
method.apply(collection, args);
}
});
};
};
for (var name in Collection.prototype) {
var method = Collection.prototype[name];
bindSkin(name, method);
}
/*
* find is a special method, because it could return a SkinCursor instance
*/
SkinCollection.prototype._find = SkinCollection.prototype.find;
/**
* retrieve mongodb.Collection
*/
SkinCollection.prototype.open = function(fn) {
switch (this.state) {
case STATE_OPEN:
return fn(null, this.collection);
case STATE_OPENNING:
return this.emitter.addListener('open', fn);
case STATE_CLOSE:
default:
var that = this;
this.emitter.addListener('open', fn);
this.state = STATE_OPENNING;
this.skinDb.open(function(err, db) {
if (err) {
that.state = STATE_CLOSE;
return that.emitter.emit('open', err, null);
}
that.skinDb.db.collection(that.collectionName, function(err, collection) {
if (collection) {
that.state = STATE_OPEN;
that.collection = collection;
if (that.hint) {
that.collection.hint = that.hit;
}
}else {
that.state = STATE_CLOSE;
}
that.emitter.emit('open', err, collection);
});
});
}
};
SkinCollection.prototype.close = function(){
this.state = STATE_CLOSE;
};
SkinCollection.prototype.drop = function(fn) {
this.skinDb.dropCollection(this.collectionName, fn);
this.close();
};
/**
* same args as find, but use Array as callback result but not use Cursor
*
* findItems(args, function(err, items){});
*
* same as
*
* find(args, function(err, cursor){cursor.toArray(err, items){}});
*
*/
SkinCollection.prototype.findItems = function() {
var args = __slice.call(arguments),
fn = args[args.length - 1];
args[args.length - 1] = function(err, cursor) {
if (err) {
fn(err);
} else {
cursor.toArray(fn);
}
}
this._find.apply(this, args);
};
/**
* find and cursor.each
*/
SkinCollection.prototype.findEach = function() {
var args = __slice.call(arguments),
fn = args[args.length - 1];
args[args.length - 1] = function(err, cursor) {
if (err) {
fn(err);
} else {
cursor.each(fn);
}
}
this._find.apply(this, args);
};
/**
* @deprecated use SkinDb.id instead
*/
SkinCollection.prototype.id = function(hex) {
return this.skinDb.toId(hex);
};
/**
* use hex id as first argument, support ObjectID and String id
*
* @param {String/ObjectID} id
* @param {Function} callback
* @return {Object} cursor
* @api public
*/
SkinCollection.prototype.findById = function() {
var args = __slice.call(arguments);
args[0] = {_id: this.skinDb.toId(args[0])};
this.findOne.apply(this, args);
};
/**
* use hex id as first argument
*/
SkinCollection.prototype.updateById = function() {
var args = __slice.call(arguments);
args[0] = {_id: this.skinDb.toId(args[0])};
this.update.apply(this, args);
};
/**
* use hex id as first argument
*/
SkinCollection.prototype.removeById = function() {
var args = __slice.call(arguments);
args[0] = {_id: this.skinDb.toId(args[0])};
this.remove.apply(this, args);
};
/**
* if last argument is not a function, then returns a SkinCursor
*/
SkinCollection.prototype.find = function() {
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
if (args.length > 0 && typeof(args[args.length - 1]) === 'function') {
this._find.apply(this, args);
}else {
return new SkinCursor(null, this, args);
}
};