Skip to content

Commit

Permalink
Merge pull request #230 from share/remove-mingo-dep
Browse files Browse the repository at this point in the history
Remove sharedb-mingo-memory circular dependency
  • Loading branch information
nateps committed Jul 25, 2018
2 parents 5f4bd90 + 7913314 commit 721a0ab
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ node_js:
- "10"
- "8"
- "6"
script: "ln -s .. node_modules/sharedb; npm run jshint && npm run test-cover"
script: "npm run jshint && npm run test-cover"
# Send coverage data to Coveralls
after_script: "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"expect.js": "^0.3.1",
"istanbul": "^0.4.2",
"jshint": "^2.9.2",
"mocha": "^5.2.0",
"sharedb-mingo-memory": "^1.0.0-beta"
"mocha": "^5.2.0"
},
"scripts": {
"test": "./node_modules/.bin/mocha && npm run jshint",
Expand Down
3 changes: 1 addition & 2 deletions test/client/query-subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,13 @@ describe('client query subscribe', function() {

it('changing a sorted property moves in a subscribed query', function(done) {
var connection = this.backend.connect();
var matchAllDbQuery = this.matchAllDbQuery;

async.parallel([
function(cb) { connection.get('dogs', 'fido').create({age: 3}, cb); },
function(cb) { connection.get('dogs', 'spot').create({age: 5}, cb); }
], function(err) {
if (err) return done(err);
var dbQuery = getQuery({query: matchAllDbQuery, sort: [['age', 1]]});
var dbQuery = getQuery({query: {}, sort: [['age', 1]]});
var query = connection.createSubscribeQuery(
'dogs',
dbQuery,
Expand Down
75 changes: 68 additions & 7 deletions test/db-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ var expect = require('expect.js');
var DB = require('../lib/db');
var MemoryDB = require('../lib/db/memory');

// Extend from MemoryDB as defined in this package, not the one that
// sharedb-mingo-memory depends on.
var ShareDbMingo = require('sharedb-mingo-memory').extendMemoryDB(MemoryDB);
var getQuery = require('sharedb-mingo-memory/get-query');

describe('DB base class', function() {
it('can call db.close() without callback', function() {
var db = new DB();
Expand Down Expand Up @@ -59,10 +54,76 @@ describe('DB base class', function() {
});
});


// Extension of MemoryDB that supports query filters and sorts on simple
// top-level properties, which is enough for the core ShareDB tests on
// query subscription updating.
function BasicQueryableMemoryDB() {
MemoryDB.apply(this, arguments);
}
BasicQueryableMemoryDB.prototype = Object.create(MemoryDB.prototype);
BasicQueryableMemoryDB.prototype.constructor = BasicQueryableMemoryDB;

BasicQueryableMemoryDB.prototype._querySync = function(snapshots, query, options) {
if (query.filter) {
snapshots = snapshots.filter(function(snapshot) {
for (var queryKey in query.filter) {
// This fake only supports simple property equality filters, so
// throw an error on Mongo-like filter properties with dots.
if (queryKey.includes('.')) {
throw new Error('Only simple property filters are supported, got:', queryKey);
}
if (snapshot.data[queryKey] !== query.filter[queryKey]) {
return false;
}
}
return true;
});
}

if (query.sort) {
if (!Array.isArray(query.sort)) {
throw new Error('query.sort must be an array');
}
if (query.sort.length) {
snapshots.sort(snapshotComparator(query.sort));
}
}

return {snapshots: snapshots};
};

// sortProperties is an array whose items are each [propertyName, direction].
function snapshotComparator(sortProperties) {
return function(snapshotA, snapshotB) {
for (var i = 0; i < sortProperties.length; i++) {
var sortProperty = sortProperties[i];
var sortKey = sortProperty[0];
var sortDirection = sortProperty[1];

var aPropVal = snapshotA.data[sortKey];
var bPropVal = snapshotB.data[sortKey];
if (aPropVal < bPropVal) {
return -1 * sortDirection;
} else if (aPropVal > bPropVal) {
return sortDirection;
} else if (aPropVal === bPropVal) {
continue;
} else {
throw new Error('Could not compare ' + aPropVal + ' and ' + bPropVal);
}
}
return 0;
};
}

// Run all the DB-based tests against the BasicQueryableMemoryDB.
require('./db')({
create: function(callback) {
var db = new ShareDbMingo();
var db = new BasicQueryableMemoryDB();
callback(null, db);
},
getQuery: getQuery
getQuery: function(options) {
return {filter: options.query, sort: options.sort};
}
});

0 comments on commit 721a0ab

Please sign in to comment.