Skip to content

Commit 5157313

Browse files
committed
Add support for querying sharedb meta fields
1 parent 303de07 commit 5157313

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

index.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,34 @@ function extendMemoryDB(MemoryDB) {
1717

1818
ShareDBMingo.prototype = Object.create(MemoryDB.prototype);
1919

20+
ShareDBMingo.prototype.query = function(collection, query, fields, options, callback) {
21+
var includeMetadata = options && options.metadata;
22+
var db = this;
23+
if (typeof callback !== 'function') throw new Error('Callback required');
24+
process.nextTick(function() {
25+
var collectionDocs = db.docs[collection];
26+
var snapshots = [];
27+
// Include metadata for the snapshots we are about to query, so the metadata
28+
// can be used to filter the results
29+
var includeMetadataForQuery = true;
30+
for (var id in collectionDocs || {}) {
31+
var snapshot = db._getSnapshotSync(collection, id, includeMetadataForQuery);
32+
snapshots.push(snapshot);
33+
}
34+
try {
35+
var result = db._querySync(snapshots, query, options);
36+
// If metadata was not explicitly defined in the original options, we want
37+
// to remove the metadata from the snapshots to match ShareDB's behavior
38+
if (result.snapshots && !includeMetadata) {
39+
result.snapshots.forEach(function(snapshot) { snapshot.m = null; });
40+
}
41+
callback(null, result.snapshots, result.extra);
42+
} catch (err) {
43+
callback(err);
44+
}
45+
});
46+
};
47+
2048
ShareDBMingo.prototype._querySync = function(snapshots, query, options) {
2149
var parsed = parseQuery(query);
2250
var mingoQuery = new Mingo.Query(castToSnapshotQuery(parsed.query));
@@ -85,13 +113,16 @@ function extendMemoryDB(MemoryDB) {
85113

86114
// Build a query object that mimics how the query would be executed if it were
87115
// made against snapshots persisted with `sharedb-mongo`
88-
// FIXME: This doesn't handle nested doc properties with dots like: {'_m.mtime': 12300}
89116
function castToSnapshotQuery(query) {
90117
var snapshotQuery = {};
118+
var propertySegments;
91119
for (var property in query) {
120+
propertySegments = property.split('.');
121+
92122
// Mongo doc property
93-
if (MONGO_DOC_PROPERTIES[property]) {
94-
snapshotQuery[MONGO_DOC_PROPERTIES[property]] = query[property];
123+
if (MONGO_DOC_PROPERTIES[propertySegments[0]]) {
124+
propertySegments[0] = MONGO_DOC_PROPERTIES[propertySegments[0]];
125+
snapshotQuery[propertySegments.join('.')] = query[property];
95126

96127
// top-level boolean operator
97128
} else if (property[0] === '$' && Array.isArray(query[property])) {

test/query.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,7 @@ module.exports = function() {
9191
});
9292
});
9393

94-
// The simpler query-casting approach doesn't handle queries that filter on
95-
// sub-properties of the Share metadata object. An alternative would be to
96-
// rewrite this module to cast Share snapshots to Mongo docs and vice versa,
97-
// the same way that sharedb-mongo does:
98-
// https://github.com/share/sharedb-mingo-memory/pull/3#pullrequestreview-99017385
99-
it.skip('condition on sub-property under Share metadata', function(done) {
94+
it('condition on sub-property under Share metadata', function(done) {
10095
this.db.query('testcollection', {'_m.mtime': 1001}, null, null, function(err, results, extra) {
10196
if (err) throw err;
10297
expect(results).eql([snapshotsNoMeta[1]]);

0 commit comments

Comments
 (0)