forked from share/sharedb-mingo-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.js
350 lines (314 loc) · 12.2 KB
/
query.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
var expect = require('chai').expect;
var async = require('async');
var sortSnapshot = function(snapshots) {
return snapshots.sort(function(a, b) {
return (a.id > b.id) ? 1 : ((b.id > a.id) ? -1 : 0);
});
};
// Call this function inside a `describe` block. Assumes that
// `this.db` is set to be a ShareDB instance that supports certain
// Mongo queries.
module.exports = function() {
it('$count should count documents', function(done) {
var snapshots = [
{type: 'json0', id: 'test1', v: 1, data: {x: 1, y: 1}},
{type: 'json0', id: 'test2', v: 1, data: {x: 2, y: 2}},
{type: 'json0', id: 'test3', v: 1, data: {x: 3, y: 2}}
];
var query = {$count: true, y: 2};
var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, function(err) {
if (err) return done(err);
db.query('testcollection', query, null, null, function(err, results, extra) {
if (err) return done(err);
expect(results).eql([]);
expect(extra).eql(2);
done();
});
});
});
it('supports regex in a simple query', function(done) {
var snapshots = [
{type: 'json0', id: 'test1', v: 1, data: {x: 1, y: 1, foo: '1foo'}},
{type: 'json0', id: 'test2', v: 1, data: {x: 2, y: 2, foo: 'foo1'}},
{type: 'json0', id: 'test3', v: 1, data: {x: 3, y: 2, foo: 'foo2'}}
];
var query = {$count: true, foo: /^foo/};
var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, function(err) {
if (err) return done(err);
db.query('testcollection', query, null, null, function(err, results, extra) {
if (err) return done(err);
expect(results).eql([]);
expect(extra).eql(2);
done();
});
});
});
it('supports regex in a $all query', function(done) {
var snapshots = [
{type: 'json0', id: 'test1', v: 1, data: {x: 1, y: 1, foo: ['1foo', 'bar']}},
{type: 'json0', id: 'test1', v: 1, data: {x: 1, y: 1, foo: ['foo', 'barz']}},
{type: 'json0', id: 'test2', v: 1, data: {x: 2, y: 2, foo: ['foo1', 'bar']}},
{type: 'json0', id: 'test3', v: 1, data: {x: 3, y: 2, foo: ['foo2', 'bar']}}
];
var query = {$count: true, foo: {$all: [/^foo/, 'bar']}};
var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, function(err) {
if (err) return done(err);
db.query('testcollection', query, null, null, function(err, results, extra) {
if (err) return done(err);
expect(results).eql([]);
expect(extra).eql(2);
done();
});
});
});
it('$sort, $skip and $limit should order, skip and limit', function(done) {
var snapshots = [
{type: 'json0', v: 1, data: {x: 1}, id: 'test1', m: null},
{type: 'json0', v: 1, data: {x: 3}, id: 'test2', m: null}, // intentionally added out of sort order
{type: 'json0', v: 1, data: {x: 2}, id: 'test3', m: null}
];
var query = {$sort: {x: 1}, $skip: 1, $limit: 1};
var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, function(err) {
if (err) return done(err);
db.query('testcollection', query, null, null, function(err, results) {
if (err) throw err;
expect(results).eql([snapshots[2]]);
done();
});
});
});
it('$comment and $hint should be ignored', function(done) {
var snapshots = [
{type: 'json0', v: 1, data: {x: 1}, id: 'test1', m: null},
{type: 'json0', v: 1, data: {x: 3}, id: 'test2', m: null},
{type: 'json0', v: 1, data: {x: 2}, id: 'test3', m: null}
];
// $comment and $hint should be ignored.
var query = {x: 2, $comment: 'Hello', $hint: 'x_1'};
var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, function(err) {
if (err) return done(err);
db.query('testcollection', query, null, null, function(err, results) {
if (err) return done(err);
expect(results).eql([snapshots[2]]);
// queryPollDoc should match, otherwise subscriptions won't update properly.
db.queryPollDoc('testcollection', snapshots[2].id, query, null, function(err, result) {
if (err) return done(err);
expect(result).eql(true);
done();
});
});
});
});
describe('$aggregate', function() {
it('supports basic $match $group $sort', function(done) {
var snapshots = [
{type: 'json0', id: 'test1', v: 1, data: {day: 1, name: 'a', amount: 1}},
{type: 'json0', id: 'test2', v: 1, data: {day: 1, name: 'b', amount: 3}},
{type: 'json0', id: 'test3', v: 1, data: {day: 2, name: 'a', amount: 5}},
{type: 'json0', id: 'test4', v: 1, data: {day: 2, name: 'b', amount: 7}},
{type: 'json0', id: 'test5', v: 1, data: {day: 2, name: 'a', amount: 13}}
];
var query = {
$aggregate: [
{$match: {day: 2}},
{$group: {_id: '$name', total: {$sum: '$amount'}}},
{$sort: {_id: 1}}
]
};
var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, function(err) {
if (err) return done(err);
db.query('testcollection', query, null, null, function(err, results, extra) {
if (err) return done(err);
expect(results).eql([]);
expect(extra).eql([{_id: 'a', total: 18}, {_id: 'b', total: 7}]);
done();
});
});
});
it('supports $lookup', function(done) {
var users = [
{type: 'json0', id: 'user1', v: 1, data: {name: 'Alice', age: 30}},
{type: 'json0', id: 'user2', v: 1, data: {name: 'Bob', age: 25}}
];
var orders = [
{type: 'json0', id: 'order1', v: 1, data: {userId: 'user1', item: 'Book'}},
{type: 'json0', id: 'order2', v: 1, data: {userId: 'user2', item: 'Pen'}},
{type: 'json0', id: 'order3', v: 1, data: {userId: 'user1', item: 'Notebook'}}
];
var query = {
$aggregate: [
{$lookup: {
from: 'users',
localField: 'userId',
foreignField: '_id',
as: 'userDetails'
}},
{$unwind: '$userDetails'},
{$project: {
_id: 1,
item: 1,
userName: '$userDetails.name',
userAge: '$userDetails.age'
}},
{$sort: {userName: 1, item: 1}}
]
};
var db = this.db;
async.series([
function(cb) {
async.each(users, function(user, innerCb) {
db.commit('users', user.id, {v: 0, create: {}}, user, null, innerCb);
}, cb);
},
function(cb) {
async.each(orders, function(order, innerCb) {
db.commit('orders', order.id, {v: 0, create: {}}, order, null, innerCb);
}, cb);
},
function(cb) {
db.query('orders', query, null, null, function(err, results, extra) {
if (err) return cb(err);
expect(results).eql([]);
expect(extra).eql([
{_id: 'order1', item: 'Book', userName: 'Alice', userAge: 30},
{_id: 'order3', item: 'Notebook', userName: 'Alice', userAge: 30},
{_id: 'order2', item: 'Pen', userName: 'Bob', userAge: 25}
]);
cb();
});
}
], done);
});
});
describe('filtering on special Share properties', function() {
// When sharedb-mongo persists a snapshot into Mongo, any properties
// underneath `data` get "promoted" to top-level, and Share properties
// get underscore-prefixed to avoid name conflicts, like `v` to `_v`.
//
// Query conditions don't undergo this transformation, so if you wanted
// to filter on snapshot version, you'd query with `{_v: 12}`. These tests
// check that sharedb-mingo-memory is consistent with sharedb-mongo for
// queries like those that filter on non-data Share properties.
var snapshots = [
{type: 'json0', v: 1, data: {x: 1, y: 1}, id: 'test1', m: {mtime: 1000}},
{type: 'json0', v: 1, data: {x: 1, y: 2}, id: 'test2', m: {mtime: 1001}},
{type: 'json0', v: 1, data: {x: 2, y: 2}, id: 'test3', m: {mtime: 1002}}
];
var snapshotsNoMeta = snapshots.map(function(snapshot) {
var snapshotCopy = JSON.parse(JSON.stringify(snapshot));
snapshotCopy.m = null;
return snapshotCopy;
});
beforeEach(function(done) {
var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, done);
});
it('condition on Mongo _id (Share id)', function(done) {
this.db.query('testcollection', {_id: 'test1'}, null, null, function(err, results) {
if (err) throw err;
expect(results).eql([snapshotsNoMeta[0]]);
done();
});
});
it('condition on sub-property under Share metadata', function(done) {
this.db.query('testcollection', {'_m.mtime': 1001}, null, null, function(err, results) {
if (err) throw err;
expect(results).eql([snapshotsNoMeta[1]]);
done();
});
});
it('condition on Mongo _id and Share data', function(done) {
this.db.query('testcollection', {y: 2, _id: {$nin: ['test2']}}, null, null, function(err, results) {
if (err) throw err;
expect(results).eql([snapshotsNoMeta[2]]);
done();
});
});
it('top-level boolean operator', function(done) {
this.db.query('testcollection', {$or: [{y: 1}, {_id: 'test2'}]}, null, null, function(err, results) {
if (err) throw err;
expect(sortSnapshot(results)).eql(sortSnapshot([snapshotsNoMeta[0], snapshotsNoMeta[1]]));
done();
});
});
});
it('filters with null condition', function(done) {
var snapshots = [
{type: 'json0', v: 1, data: {x: 1, y: 1}, id: 'test1', m: null},
{type: 'json0', v: 1, data: {x: 1}, id: 'test2', m: null}, // y value intentionally omitted
{type: 'json0', v: 1, data: {x: 2, y: 2}, id: 'test3', m: null}
];
var query = {y: null};
var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, function(err) {
if (err) return done(err);
db.query('testcollection', query, null, null, function(err, results) {
if (err) throw err;
expect(results).eql([snapshots[1]]);
done();
});
});
});
it('throws when the query is not an valid query', function(done) {
var db = this.db;
var query = {
$and: [
123,
{y: 1}
]
};
db.query('testcollection', query, null, null, function(err) {
expect(err.message).to.be.equal('Invalid mongo query format');
done();
});
});
describe('top-level boolean operator', function() {
var snapshots = [
{type: 'json0', v: 1, data: {x: 1, y: 1}, id: 'test1', m: null},
{type: 'json0', v: 1, data: {x: 1, y: 2}, id: 'test2', m: null},
{type: 'json0', v: 1, data: {x: 2, y: 2}, id: 'test3', m: null}
];
beforeEach(function(done) {
var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, done);
});
it('$and', function(done) {
this.db.query('testcollection', {$and: [{x: 1}, {y: 1}], $sort: {_id: 1}}, null, null, function(err, results) {
if (err) throw err;
expect(results).eql([snapshots[0]]);
done();
});
});
it('$or', function(done) {
this.db.query('testcollection', {$or: [{x: 1}, {y: 1}], $sort: {_id: 1}}, null, null, function(err, results) {
if (err) throw err;
expect(sortSnapshot(results)).eql(sortSnapshot([snapshots[0], snapshots[1]]));
done();
});
});
});
};