-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
369 lines (318 loc) · 10.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
module.exports = ShareDbMingo;
var Mingo = require('mingo');
var DB = require('sharedb').DB;
var clone = require('clone');
var uuid = require('uuid/v1')
var memoryStore = require('@js-code/memory-store')
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'
, $orderby: 'sort'
};
// Agenda
// cName - collection name
// docName - id of the specific document
// There are 3 different APIs a database can expose. A database adaptor does
// not need to implement all three APIs. You can pick and choose at will.
//
// The three database APIs are:
//
// - Snapshot API, which is used to store actual document data
// - Query API, which livedb wraps for live query capabilities.
// - Operation log for storing all the operations people have submitted. This
// is used if a user makes changes while offline and then reconnects. Its
// also really useful for auditing user actions.
//
// All databases should implement the close() method regardless of which APIs
// they expose.
function CollectionStore (store, prefix) {
this.store = store
this.prefix = prefix
}
CollectionStore.prototype.getCollectionDocs = function (collection) {
return this.store.getItemIds().then((ids) => {
const promises = ids.filter(id => {
const split = id.split('.')
return split.length === 3 && split[0] === this.prefix && split[1] === collection
}).map(key => {
const [prefix, collectionName, id] = key.split('.')
return this.getDoc(collection, id)
})
return Promise.all(promises)
})
}
CollectionStore.prototype.getDoc = function (collection, docId) {
return new Promise((resolve, reject) => {
this.store.getItem(`${this.prefix}.${collection}.${docId}`).then(value => {
if (typeof value === 'string' || value instanceof String) {
value = JSON.parse(value)
}
resolve(value)
}).catch(e => {
reject(e)
})
})
}
CollectionStore.prototype.setDoc = function (collection, docId, doc) {
return this.store.setItem(`${this.prefix}.${collection}.${docId}`, JSON.stringify(doc))
}
CollectionStore.prototype.removeDoc = function (collection, docId) {
return this.store.removeItem(`${this.prefix}.${collection}.${docId}`)
}
CollectionStore.prototype.getCollections = function () {
return new Promise((resolve, reject) => {
this.store.getItemIds().then((ids) => {
const collections = []
for (const key of ids) {
const [prefix, collection, id] = key.split('.')
if (prefix === this.prefix) {
if (!collections.includes(collection)) {
collections.push(collection)
}
}
}
resolve(collections)
}).catch(err => reject(err))
})
}
CollectionStore.prototype.getCollectionDocIds = function (collection) {
return new Promise((resolve, reject) => {
this.store.getItemIds().then((ids) => {
const docIds = []
for (const key of ids) {
const [prefix, collectionName, id] = key.split('.')
if (collection !== collectionName) continue
if (prefix === this.prefix) {
if (!docIds.includes(id)) {
docIds.push(id)
}
}
}
resolve(docIds)
}).catch(err => reject(err))
})
}
function ShareDbMingo(options) {
if (!(this instanceof ShareDbMingo)) return new ShareDbMingo();
options = options || {};
this.store = new CollectionStore(options.store || new memoryStore(), options.storePrefix || 'store')
this.allowJSQueries = options.allowAllQueries || options.allowJSQueries || false;
this.allowAggregateQueries = options.allowAllQueries || options.allowAggregateQueries || false;
this.closed = false;
}
ShareDbMingo.prototype = Object.create(DB.prototype);
ShareDbMingo.prototype.dropDatabase = function(callback) {
var db = this;
db.store.clean().then(function() {
callback && callback();
})
};
ShareDbMingo.prototype.commit = function(collection, id, op, snapshot, options, callback) {
var db = this;
this._writeOp(collection, id, op, snapshot, (error, opId) => {
if (error) return callback(error)
db.store.setDoc(collection, id, castToDoc(id, clone(snapshot), opId)).then(function () {
var succeeded = true;
callback(null, succeeded);
}).catch(err => callback(err))
})
}
ShareDbMingo.prototype._writeOp = function(collectionName, id, op, snapshot, callback) {
if (typeof op.v !== 'number') {
var err = 'Invalid op version ' + collectionName + '.' + id + ' ' + op.v
return callback(err);
}
const opCollectionName = this.getOplogCollectionName(collectionName)
var doc = shallowClone(op);
doc.d = id;
doc.o = snapshot._opLink
doc._id = uuid()
this.store.setDoc(opCollectionName, doc._id, doc).then(() => {
callback(null, doc._id)
}).catch((error) => {
callback(error)
})
}
ShareDbMingo.prototype.getOplogCollectionName = function(collectionName) {
return 'o_' + collectionName;
}
// Snapshot database API
// Get the named document from the database. The callback is called with (err,
// data). data may be null if the docuemnt has never been created in the
// database.
ShareDbMingo.prototype.getSnapshot = function(collectionName, id, fields, options, callback) {
var db = this;
db.store.getDoc(collectionName, id, fields).then((doc) => {
var snapshot = (doc) ? castToSnapshot(clone(doc)) : new MongoSnapshot(id, 0, null, undefined);
callback(null, snapshot);
}).catch(function(error) {
callback(error)
})
};
// ********* Oplog API
function getOpsQuery(id, from) {
return (from == null) ?
{d: id} :
{d: id, v: {$gte: from}};
}
ShareDbMingo.prototype.getOps = function(collection, id, from, to, options, callback) {
var db = this;
const opCollectionName = this.getOplogCollectionName(collection)
this.store.getCollectionDocs(opCollectionName).then(function (docs) {
docs = docs.map(function(doc) {
return clone(doc)
})
var query = getOpsQuery(id, from);
query['$sort'] = {v: 1}
try {
var data = db._querySync(docs, query, options);
callback(null, data.results || []);
} catch (err) {
callback(err);
}
}).catch(err => (callback(err)));
};
// ********** Query support API.
// The memory database query function returns all documents in a collection
// regardless of query by default
ShareDbMingo.prototype.query = function(collection, query, fields, options, callback) {
var db = this;
this.store.getCollectionDocs(collection).then(function (docs) {
docs = docs.map(function(doc) {
return clone(doc)
})
try {
var data = db._querySync(docs, clone(query), options);
callback(null, data.results || [], data.extra);
} catch (err) {
callback(err);
}
}).catch(err => (callback(err)));
};
// For testing, it may be useful to implement the desired query language by
// defining this function
ShareDbMingo.prototype._querySync = function(docs, query, options) {
if (query.$count) {
delete query.$count;
var cursor = Mingo.find(docs, query.$query);
return {results: [], extra: cursor.count()};
} else if (query.$aggregate) {
var aggregate = query.$aggregate;
if (!Array.isArray(aggregate)) aggregate = [aggregate];
var extra = Mingo.aggregate(docs, aggregate);
return {results: [], extra: extra};
} else {
query = normalizeQuery(clone(query));
var cursorMethods = extractCursorMethods(query);
var cursor = Mingo.find(docs, query.$query);
for (var i = 0; i < cursorMethods.length; i++) {
var item = cursorMethods[i];
var method = item[0];
var arg = item[1];
cursor[method](arg);
}
var results = cursor.all();
results = results.map(function(doc){
return castToSnapshot(doc);
});
return {results: results};
}
};
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(id, snapshot, opLink) {
var data = snapshot.data;
var doc =
(isObject(data)) ? shallowClone(data) :
(data === undefined) ? {} :
{_data: data};
doc._id = id;
doc._type = snapshot.type;
doc._v = snapshot.v;
doc._m = snapshot.m;
doc._o = opLink;
return doc;
}
function castToSnapshot(doc) {
var id = doc._id;
var version = doc._v;
var type = doc._type;
var data = doc._data;
var meta = doc._m;
var opLink = doc._o;
if (type == null) {
return new MongoSnapshot(id, version, null, undefined, meta, opLink);
}
if (doc.hasOwnProperty('_data')) {
return new MongoSnapshot(id, version, type, data, meta, opLink);
}
data = shallowClone(doc);
delete data._id;
delete data._v;
delete data._type;
delete data._m;
delete data._o;
return new MongoSnapshot(id, version, type, data, meta, opLink);
}
function MongoSnapshot(id, version, type, data, meta, opLink) {
this.id = id;
this.v = version;
this.type = type;
this.data = data;
if (meta) this.m = meta;
if (opLink) this._opLink = opLink;
}
function isObject(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
function shallowClone(object) {
var out = {};
for (var key in object) {
out[key] = object[key];
}
return out;
}