forked from mongo-js/mongojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
351 lines (282 loc) · 9.58 KB
/
index.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
var mongodb = require('mongodb');
var thunky = require('thunky');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Readable = require('stream').Readable || require('readable-stream');
var DRIVER_COLLECTION_PROTO = mongodb.Collection.prototype;
var DRIVER_CURSOR_PROTO = mongodb.Cursor.prototype;
var DRIVER_DB_PROTO = mongodb.Db.prototype;
var noop = function() {};
var forEachMethod = function(oldProto, newProto, fn) {
Object.keys(oldProto).forEach(function(methodName) {
if (oldProto.__lookupGetter__(methodName) || newProto[methodName]) return;
if (methodName[0] === '_' || typeof oldProto[methodName] !== 'function') return;
fn(methodName, oldProto[methodName]);
});
};
var ensureCallback = function(args) {
if (getCallback(args) !== noop) return args;
args = Array.prototype.slice.call(args);
args.push(noop);
return args;
};
var getCallback = function(args) {
var callback = args[args.length-1];
return typeof callback === 'function' ? callback : noop;
};
// Proxy for the native cursor prototype that normalizes method names and
// arguments to fit the mongo shell.
var Cursor = function(oncursor) {
Readable.call(this, {objectMode:true, highWaterMark:0});
this._get = oncursor;
};
util.inherits(Cursor, Readable);
Cursor.prototype.toArray = function() {
this._apply(DRIVER_CURSOR_PROTO.toArray, arguments);
};
Cursor.prototype.next = function() {
this._apply(DRIVER_CURSOR_PROTO.nextObject, arguments);
};
Cursor.prototype.forEach = function() {
this._apply(DRIVER_CURSOR_PROTO.each, arguments);
};
Cursor.prototype.count = function() {
this._apply(DRIVER_CURSOR_PROTO.count, arguments);
};
Cursor.prototype.explain = function() {
this._apply(DRIVER_CURSOR_PROTO.explain, arguments);
};
Cursor.prototype.limit = function() {
return this._config(DRIVER_CURSOR_PROTO.limit, arguments);
};
Cursor.prototype.skip = function() {
return this._config(DRIVER_CURSOR_PROTO.skip, arguments);
};
Cursor.prototype.batchSize = function() {
return this._config(DRIVER_CURSOR_PROTO.batchSize, arguments);
};
Cursor.prototype.sort = function() {
return this._config(DRIVER_CURSOR_PROTO.sort, arguments);
};
Cursor.prototype.destroy = function() {
this._apply(DRIVER_CURSOR_PROTO.close, arguments);
this.push(null);
};
Cursor.prototype._apply = function(fn, args) {
this._get(function(err, cursor) {
if (err) return getCallback(args)(err);
fn.apply(cursor, args);
});
return this;
};
Cursor.prototype._read = function() { // 0.10 stream support (0.8 compat using readable-stream)
var self = this;
this.next(function(err, data) {
if (err) return self.emit('error', err);
self.push(data);
});
};
Cursor.prototype._config = function(fn, args) {
if (typeof args[args.length-1] !== 'function') return this._apply(fn, args);
args = Array.prototype.slice.call(args);
var callback = args.pop();
return this._apply(fn, args).toArray(callback);
};
// Proxy for the native collection prototype that normalizes method names and
// arguments to fit the mongo shell.
var Collection = function(oncollection) {
this._get = oncollection;
};
Collection.prototype.find = function() {
var args = Array.prototype.slice.call(arguments);
var oncollection = this._get;
var oncursor = thunky(function(callback) {
args.push(callback);
oncollection(function(err, collection) {
if (err) return callback(err);
collection.find.apply(collection, args);
});
});
if (typeof args[args.length-1] === 'function') {
var callback = args.pop();
oncursor(function(err, cursor) {
if (err) return callback(err);
cursor.toArray(callback);
});
}
return new Cursor(oncursor);
};
Collection.prototype.findOne = function() { // see http://www.mongodb.org/display/DOCS/Queries+and+Cursors
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
this.find.apply(this, args).limit(1).next(callback);
};
Collection.prototype.findAndModify = function(options, callback) {
this._apply(DRIVER_COLLECTION_PROTO.findAndModify, [options.query, options.sort || [], options.update || {}, {
new:!!options.new,
remove:!!options.remove,
upsert:!!options.upsert,
fields:options.fields
}, callback || noop]);
};
Collection.prototype.group = function(group, callback) {
this._apply(DRIVER_COLLECTION_PROTO.group, [group.key ? group.key : group.keyf, group.cond, group.initial, group.reduce, group.finalize, true, callback]);
};
Collection.prototype.remove = function() {
var self = this;
var callback = getCallback(arguments);
if (arguments.length > 1 && arguments[1] === true) { // the justOne parameter
this.findOne(arguments[0], function(err, doc) {
if (err) return callback(err);
if (!doc) return callback(null, 0);
self._apply(DRIVER_COLLECTION_PROTO.remove, [doc, callback]);
});
return;
}
this._apply(DRIVER_COLLECTION_PROTO.remove, arguments.length === 0 ? [{}, noop] : ensureCallback(arguments));
};
Collection.prototype.getIndexes = function() {
this._apply(DRIVER_COLLECTION_PROTO.indexes, arguments);
};
Collection.prototype.runCommand = function(cmd, opts, callback) {
callback = callback || noop;
opts = opts || {};
if (typeof opts === 'function') {
callback = opts;
};
this._get(function(err, collection) {
if (err) return callback(err);
var commandObject = {};
commandObject[cmd] = collection.collectionName;
Object.keys(opts).forEach(function(key) {
commandObject[key] = opts[key];
});
collection.db.command(commandObject, callback);
});
};
forEachMethod(DRIVER_COLLECTION_PROTO, Collection.prototype, function(methodName, fn) {
Collection.prototype[methodName] = function() { // we just proxy the rest of the methods directly
this._apply(fn, ensureCallback(arguments));
};
});
Collection.prototype._apply = function(fn, args) {
this._get(function(err, collection) {
if (err) return getCallback(args)(err);
if (!collection.opts || getCallback(args) === noop) return fn.apply(collection, args);
var safe = collection.opts.safe;
collection.opts.safe = true;
fn.apply(collection, args);
collection.opts.safe = safe;
});
};
var toConnectionString = function(conf) { // backwards compat config map (use a connection string instead)
var options = [];
var hosts = conf.replSet ? conf.replSet.members || conf.replSet : [conf];
var auth = conf.username ? (conf.username+':'+conf.password+'@') : '';
hosts = hosts.map(function(server) {
if (typeof server === 'string') return server;
return (server.host || '127.0.0.1') + ':' + (server.port || 27017);
}).join(',');
if (conf.slaveOk) options.push('slaveOk=true');
return 'mongodb://'+auth+hosts+'/'+conf.db+'?'+options.join('&');
};
var parseConfig = function(cs) {
if (typeof cs === 'object' && cs) return toConnectionString(cs);
if (typeof cs !== 'string') throw new Error('connection string required'); // to avoid undef errors on bad conf
cs = cs.replace(/^\//, '');
if (cs.indexOf('/') < 0) return parseConfig('127.0.0.1/'+cs);
if (cs.indexOf('mongodb://') !== 0) return parseConfig('mongodb://'+cs);
return cs;
};
var Database = function(ondb) {
EventEmitter.call(this);
this._get = ondb;
};
util.inherits(Database, EventEmitter);
Database.prototype.runCommand = function(opts, callback) {
callback = callback || noop;
if (typeof opts === 'string') {
var tmp = opts;
opts = {};
opts[tmp] = 1;
}
this._get(function(err, db) {
if (err) return callback(err);
db.command(opts, callback);
});
};
Database.prototype.open = function(callback) {
this._get(callback); // a way to force open the db, 99.9% of the times this is not needed
};
Database.prototype.getCollectionNames = function(callback) {
this.collections(function(err, cols) {
if (err) return callback(err);
callback(null, cols.map(function(c) {
return c.collectionName;
}));
});
};
Database.prototype.createCollection = function(name, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = opts || {};
opts.strict = opts.strict !== false;
this._apply(DRIVER_DB_PROTO.createCollection, [name, opts, callback || noop]);
};
Database.prototype.collection = function(name) {
var self = this;
var oncollection = thunky(function(callback) {
self._get(function(err, db) {
if (err) return callback(err);
db.collection(name, callback);
});
});
return new Collection(oncollection);
};
Database.prototype._apply = function(fn, args) {
this._get(function(err, db) {
if (err) return getCallback(args)(err);
fn.apply(db, args);
});
};
forEachMethod(DRIVER_DB_PROTO, Database.prototype, function(methodName, fn) {
Database.prototype[methodName] = function() {
this._apply(fn, arguments);
};
});
var connect = function(config, collections) {
var connectionString = parseConfig(config);
var ondb = thunky(function(callback) {
mongodb.Db.connect(connectionString, function(err, db) {
if (err) return callback(err);
that.client = db;
db.on('error', function(err) {
process.nextTick(function() {
that.emit('error', err);
});
});
callback(null, db);
});
});
var that = new Database(ondb);
that.bson = mongodb.BSONPure; // backwards compat (require('bson') instead)
that.ObjectId = mongodb.ObjectID; // backwards compat
collections = collections || config.collections || [];
collections.forEach(function(colName) {
var parts = colName.split('.');
var last = parts.pop();
var parent = parts.reduce(function(parent, prefix) {
return parent[prefix] = parent[prefix] || {};
}, that);
parent[last] = that.collection(colName);
});
return that;
};
connect.connect = connect; // backwards compat
connect.ObjectId = mongodb.ObjectID;
connect.Cursor = Cursor;
connect.Collection = Collection;
connect.Database = Database;
module.exports = connect;