Skip to content

Commit 48c9a9e

Browse files
committed
Initial version
0 parents  commit 48c9a9e

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*~
2+
node_modules

index.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
var MemoryDB = require('sharedb/lib/db/memory');
2+
var Mingo = require('mingo');
3+
4+
function ShareDBMingo(options) {
5+
if (!(this instanceof ShareDBMingo)) return new ShareDBMingo(options);
6+
MemoryDB.call(this, options);
7+
}
8+
9+
ShareDBMingo.prototype = Object.create(MemoryDB.prototype);
10+
11+
ShareDBMingo.prototype._querySync = function(snapshots, query, options) {
12+
var query = shallowCopy(query);
13+
var orderby = query.$orderby;
14+
delete query.$orderby;
15+
var skip = query.$skip;
16+
delete query.$skip;
17+
var limit = query.$limit;
18+
delete query.$limit;
19+
20+
var filtered = filter(snapshots, query.$query || query);
21+
sort(filtered, orderby);
22+
if (skip) filtered.splice(0, skip);
23+
if (limit) filtered = filtered.slice(0, limit);
24+
return filtered;
25+
};
26+
27+
ShareDBMingo.prototype.queryPollDoc = function(collection, id, query, options, callback) {
28+
var mingoQuery = new Mingo.Query(query);
29+
this.getSnapshot(collection, id, null, function(err, snapshot) {
30+
if (err) return callback(err);
31+
if (snapshot.data) {
32+
callback(null, mingoQuery.test(snapshot.data));
33+
} else {
34+
callback(null, false);
35+
}
36+
});
37+
};
38+
39+
ShareDBMingo.prototype.canPollDoc = function(collection, query) {
40+
return !(
41+
query.hasOwnProperty('$orderby') ||
42+
query.hasOwnProperty('$limit') ||
43+
query.hasOwnProperty('$skip') ||
44+
query.hasOwnProperty('$count')
45+
);
46+
};
47+
48+
// Support exact key match filters only
49+
function filter(snapshots, query) {
50+
var mingoQuery = new Mingo.Query(query);
51+
return snapshots.filter(function(snapshot) {
52+
return snapshot.data && mingoQuery.test(snapshot.data);
53+
});
54+
}
55+
56+
// Support sorting with the Mongo $orderby syntax
57+
function sort(snapshots, orderby) {
58+
if (!orderby) return snapshots;
59+
snapshots.sort(function(snapshotA, snapshotB) {
60+
for (var key in orderby) {
61+
var value = orderby[key];
62+
if (value !== 1 && value !== -1) {
63+
throw new Error('Invalid $orderby value');
64+
}
65+
var a = snapshotA.data && snapshotA.data[key];
66+
var b = snapshotB.data && snapshotB.data[key];
67+
if (a > b) return value;
68+
if (b > a) return -value;
69+
}
70+
return 0;
71+
});
72+
}
73+
74+
function shallowCopy(object) {
75+
var out = {};
76+
for (var key in object) {
77+
out[key] = object[key];
78+
}
79+
return out;
80+
}
81+
82+
module.exports = ShareDBMingo;

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "sharedb-mingo-memory",
3+
"version": "0.1.0",
4+
"description": "In-memory database adapter with MongoDB API for ShareDB",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha",
8+
"test-cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git://github.com/share/sharedb-mingo-memory.git"
13+
},
14+
"devDependencies": {
15+
"coveralls": "^2.11.8",
16+
"expect.js": "^0.3.1",
17+
"istanbul": "^0.4.2",
18+
"mocha": "^2.3.3"
19+
},
20+
"author": "Avital Oliver",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/share/sharedb-mingo-memory/issues"
24+
},
25+
"homepage": "https://github.com/share/sharedb-mingo-memory#readme",
26+
"dependencies": {
27+
"sharedb": "^0.11.35"
28+
}
29+
}

test/mocha.opts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--reporter spec
2+
--check-leaks
3+
--globals Promise
4+
--timeout 5000

test/test.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var expect = require('expect.js');
2+
var ShareDBMingo = require('../index');
3+
4+
function create(callback) {
5+
var db = ShareDBMingo();
6+
callback(null, db);
7+
}
8+
9+
require('sharedb/test/db')(create);
10+

0 commit comments

Comments
 (0)