-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
405 lines (342 loc) · 11.2 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
var assert = require('assert');
var async = require('async');
var PouchDB = require('pouchdb');
var metaOperators = {
$comment: true
, $explain: true
, $hint: true
, $maxScan: true
, $max: true
, $min: true
, $orderby: true
, $returnKey: true
, $showDiskLoc: true
, $snapshot: true
, $count: true
};
var cursorOperators = {
$limit: 'limit'
, $skip: 'skip'
};
/* There are two ways to instantiate a livedb-mongo wrapper.
*
* 1. The simplest way is to just invoke the module and pass in your mongoskin
* arguments as arguments to the module function. For example:
*
* var db = require('livedb-mongo')('localhost:27017/test?auto_reconnect', {safe:true});
*
* 2. If you already have a mongoskin instance that you want to use, you can
* just pass it into livedb-mongo:
*
* var skin = require('mongoskin')('localhost:27017/test?auto_reconnect', {safe:true});
* var db = require('livedb-mongo')(skin);
*/
exports = module.exports = function(options) {
return new LiveDbPouch(options);
};
function LiveDbPouch(options) {
this.dbs = {};
this.options = options;
this.closed = false;
}
LiveDbPouch.prototype._open = function(dbName) {
this.dbs[dbName] = this.dbs[dbName] || new PouchDB(dbName, this.options);
return this.dbs[dbName];
};
LiveDbPouch.prototype.close = function(callback) {
if (this.closed) return callback('db already closed');
this.dbs = {}
if (typeof callback === 'function') {
callback();
}
this.closed = true;
};
// **** Snapshot methods
LiveDbPouch.prototype.getSnapshot = function(dbName, docName, callback) {
if (this.closed) return callback('db already closed');
if (/_ops$/.test(dbName)) return callback('Invalid collection name');
this._open(dbName).get(docName, function(err, doc) {
if (err) {
if (err.name == 'not_found') {
return callback(null, null);
}
return callback(err);
}
callback(null, castToSnapshot(doc));
});
};
LiveDbPouch.prototype.bulkGetSnapshot = function(requests, callback) {
if (this.closed) return callback('db already closed');
var db = this._open.bind(this);
var results = {};
var getSnapshots = function(cName, callback) {
if (/_ops$/.test(cName)) return callback('Invalid collection name');
var cResult = results[cName] = {};
var docNames = requests[cName];
var query = {
keys: docNames
, include_docs: true
};
db(cName).allDocs(query, function(err, response) {
if (err) return callback(err);
var rows = response.rows;
for (var i = 0; i < rows.length; i++) {
if (rows[i].doc != void 0) {
var snapshot = castToSnapshot(rows[i].doc);
cResult[snapshot.docName] = snapshot;
}
}
callback();
});
};
async.each(Object.keys(requests), getSnapshots, function(err) {
callback(err, err ? null : results);
});
};
LiveDbPouch.prototype.writeSnapshot = function(cName, docName, data, callback) {
if (this.closed) return callback('db already closed');
var doc = castToDoc(docName, data);
/* XXX
This is nasty. We should be able to round trip _rev through livedb somehow
but the test suite does a bunch of deepEquals and tries to delete a doc
by specifying an object with only a version number.
If livedb allowed for non-integer versions we could map it directly to
revs but it doesn't. So we deal. It's not ideal.
*/
var db = this._open(cName);
db.get(doc._id, function (err, existing) {
if (existing) doc._rev = existing._rev;
db.put(doc, callback);
});
};
// ******* Oplog methods
// Overwrite me if you want to change this behaviour.
LiveDbPouch.prototype.getOplogCollectionName = function(cName) {
// Using an underscore to make it easier to see whats going in on the shell
return cName + '_ops';
};
// Get and return the op collection.
LiveDbPouch.prototype._opCollection = function(cName) {
return this._open(this.getOplogCollectionName(cName));
};
LiveDbPouch.prototype.writeOp = function(cName, docName, opData, callback) {
assert(opData.v !== null);
if (this.closed) return callback('db already closed');
if (/_ops$/.test(cName)) return callback('Invalid collection name');
var self = this;
var data = shallowClone(opData);
data._id = docName + ' v' + opData.v;
data.name = docName;
this._opCollection(cName).put(data, function (err) {
if (err && err.status != 409) return callback(err);
callback();
});
};
LiveDbPouch.prototype.getVersion = function(cName, docName, callback) {
if (this.closed) return callback('db already closed');
if (/_ops$/.test(cName)) return callback('Invalid collection name');
var query = {
startkey: docName + ' v\ufff0'
, endkey: docName + ' v'
, limit: 1
, descending: true
, include_docs: true
};
this._opCollection(cName).allDocs(query, function(err, data) {
if (err) return callback(err);
var op = data.rows[0];
if (op === void 0) {
callback(null, 0);
} else {
callback(null, op.doc.v + 1);
}
});
};
LiveDbPouch.prototype.getOps = function(cName, docName, start, end, callback) {
if (this.closed) return callback('db already closed');
if (/_ops$/.test(cName)) return callback('Invalid collection name');
if (start == end) return callback(null, []);
if (end == null) end = '\ufff0';
var query = {
startkey: docName + ' v' + start
, endkey: docName + ' v' + end
, include_docs: true
};
this._opCollection(cName).allDocs(query, function (err, data) {
if (err) return callback(err);
var ops = data.rows.map(function (row) {
delete row.doc._id;
delete row.doc._rev;
delete row.doc.name;
return row.doc;
});
callback(null, ops);
});
};
// ***** Query methods
// Internal method to actually run the query.
// LiveDbPouch.prototype._query = function(mongo, cName, query, callback) {
// // For count queries, don't run the find() at all.
// if (query.$count) {
// delete query.$count;
// mongo.collection(cName).count(query.$query || {}, function(err, count) {
// if (err) return callback(err);
// // This API is kind of awful. FIXME in livedb.
// callback(err, {results:[], extra:count});
// });
// } else {
// var cursorMethods = extractCursorMethods(query);
// mongo.collection(cName).find(query, function(err, cursor) {
// if (err) return callback(err);
// for (var i = 0; i < cursorMethods.length; i++) {
// var item = cursorMethods[i];
// var method = item[0];
// var arg = item[1];
// cursor[method](arg);
// }
// cursor.toArray(function(err, results) {
// results = results && results.map(castToSnapshot);
// callback(err, results);
// });
// });
// }
// };
// LiveDbPouch.prototype.query = function(livedb, cName, inputQuery, opts, callback) {
// if (this.closed) return callback('db already closed');
// if (/_ops$/.test(cName)) return callback('Invalid collection name');
// // To support livedb <=0.2.8
// if (typeof opts === 'function') {
// callback = opts;
// opts = {};
// }
// var query = normalizeQuery(inputQuery);
// // Use this.mongoPoll if its a polling query.
// if (opts.mode === 'poll' && this.mongoPoll) {
// var self = this;
// // This timeout is a dodgy hack to work around race conditions replicating the
// // data out to the polling target replica.
// setTimeout(function() {
// self._query(self.mongoPoll, cName, query, callback);
// }, 300);
// } else {
// this._query(this.mongo, cName, query, callback);
// }
// };
// LiveDbPouch.prototype.queryDoc = function(livedb, index, cName, docName, inputQuery, callback) {
// if (this.closed) return callback('db already closed');
// if (/_ops$/.test(cName)) return callback('Invalid collection name');
// var query = normalizeQuery(inputQuery);
// // Run the query against a particular mongo document by adding an _id filter
// var queryId = query.$query._id;
// if (queryId) {
// delete query.$query._id;
// query.$query.$and = [{_id: docName}, {_id: queryId}];
// } else {
// query.$query._id = docName;
// }
// this.mongo.collection(cName).findOne(query, function(err, doc) {
// callback(err, castToSnapshot(doc));
// });
// };
// // Test whether an operation will make the document its applied to match the
// // specified query. This function doesn't really have enough information to know
// // in all cases, but if we can determine whether a query matches based on just
// // the operation, it saves doing extra DB calls.
// //
// // currentStatus is true or false depending on whether the query currently
// // matches. return true or false if it knows, or null if the function doesn't
// // have enough information to tell.
// LiveDbPouch.prototype.willOpMakeDocMatchQuery = function(currentStatus, query, op) {
// return null;
// };
// // Does the query need to be rerun against the database with every edit?
// LiveDbPouch.prototype.queryNeedsPollMode = function(index, query) {
// return query.hasOwnProperty('$orderby') ||
// query.hasOwnProperty('$limit') ||
// query.hasOwnProperty('$skip') ||
// query.hasOwnProperty('$count');
// };
// // Utility methods
// function extractCursorMethods(query) {
// var out = [];
// for (var key in query) {
// if (cursorOperators[key]) {
// out.push([cursorOperators[key], query[key]]);
// delete query[key];
// }
// }
// return out;
// }
// function normalizeQuery(inputQuery) {
// // Box queries inside of a $query and clone so that we know where to look
// // for selctors and can modify them without affecting the original object
// var query;
// if (inputQuery.$query) {
// query = shallowClone(inputQuery);
// query.$query = shallowClone(query.$query);
// } else {
// query = {$query: {}};
// for (var key in inputQuery) {
// if (metaOperators[key] || cursorOperators[key]) {
// query[key] = inputQuery[key];
// } else {
// query.$query[key] = inputQuery[key];
// }
// }
// }
// // Deleted documents are kept around so that we can start their version from
// // the last version if they get recreated. When they are deleted, their type
// // is set to null, so don't return any documents with a null type.
// if (!query.$query._type) query.$query._type = {$ne: null};
// return query;
// }
function castToDoc(docName, data) {
var doc = (
typeof data.data === 'object' &&
data.data !== null &&
!Array.isArray(data.data)
) ?
shallowClone(data.data) :
{data: (data.data === void 0) ? null : data.data};
doc.type = data.type || null;
doc.m = data.m;
doc.v = data.v;
doc._id = docName;
return doc;
}
function castToSnapshot(doc) {
if (!doc) return;
var type = doc.type;
var v = doc.v;
var docName = doc._id;
var data = doc.data;
var meta = doc.m;
if (data === void 0) {
doc = shallowClone(doc);
delete data.type;
delete data.v;
delete data._rev;
delete data._id;
return {
data: doc
, type: type
, v: v
, docName: docName
, m: meta
};
}
return {
data: data
, type: type
, v: v
, docName: docName
, m: meta
};
}
function shallowClone(object) {
var out = {};
for (var key in object) {
out[key] = object[key];
}
return out;
}