Skip to content

Commit

Permalink
Merge pull request #7 from jylauril/support-doc-meta-with-query
Browse files Browse the repository at this point in the history
Add support for querying sharedb meta fields
  • Loading branch information
ericyhwang authored Sep 28, 2018
2 parents 303de07 + 5157313 commit 81b5c3b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
37 changes: 34 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ function extendMemoryDB(MemoryDB) {

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

ShareDBMingo.prototype.query = function(collection, query, fields, options, callback) {
var includeMetadata = options && options.metadata;
var db = this;
if (typeof callback !== 'function') throw new Error('Callback required');
process.nextTick(function() {
var collectionDocs = db.docs[collection];
var snapshots = [];
// Include metadata for the snapshots we are about to query, so the metadata
// can be used to filter the results
var includeMetadataForQuery = true;
for (var id in collectionDocs || {}) {
var snapshot = db._getSnapshotSync(collection, id, includeMetadataForQuery);
snapshots.push(snapshot);
}
try {
var result = db._querySync(snapshots, query, options);
// If metadata was not explicitly defined in the original options, we want
// to remove the metadata from the snapshots to match ShareDB's behavior
if (result.snapshots && !includeMetadata) {
result.snapshots.forEach(function(snapshot) { snapshot.m = null; });
}
callback(null, result.snapshots, result.extra);
} catch (err) {
callback(err);
}
});
};

ShareDBMingo.prototype._querySync = function(snapshots, query, options) {
var parsed = parseQuery(query);
var mingoQuery = new Mingo.Query(castToSnapshotQuery(parsed.query));
Expand Down Expand Up @@ -85,13 +113,16 @@ function extendMemoryDB(MemoryDB) {

// Build a query object that mimics how the query would be executed if it were
// made against snapshots persisted with `sharedb-mongo`
// FIXME: This doesn't handle nested doc properties with dots like: {'_m.mtime': 12300}
function castToSnapshotQuery(query) {
var snapshotQuery = {};
var propertySegments;
for (var property in query) {
propertySegments = property.split('.');

// Mongo doc property
if (MONGO_DOC_PROPERTIES[property]) {
snapshotQuery[MONGO_DOC_PROPERTIES[property]] = query[property];
if (MONGO_DOC_PROPERTIES[propertySegments[0]]) {
propertySegments[0] = MONGO_DOC_PROPERTIES[propertySegments[0]];
snapshotQuery[propertySegments.join('.')] = query[property];

// top-level boolean operator
} else if (property[0] === '$' && Array.isArray(query[property])) {
Expand Down
7 changes: 1 addition & 6 deletions test/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ module.exports = function() {
});
});

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

0 comments on commit 81b5c3b

Please sign in to comment.