sharedb-mingo-memory
Advanced tools
Comparing version
@@ -13,3 +13,3 @@ var cloneDeep = require('lodash.clonedeep'); | ||
// Convert sort order to Mongo's expected structure | ||
if (!Array.isArray(sort)) throw new Error("invalid sort order"); | ||
if (!Array.isArray(sort)) throw new Error('invalid sort order'); | ||
if (sort.length > 0) { | ||
@@ -19,3 +19,3 @@ var mongoSort = {}; | ||
if (!Array.isArray(sort[i]) || sort[i].length !== 2) { | ||
throw new Error("invalid sort order"); | ||
throw new Error('invalid sort order'); | ||
} | ||
@@ -22,0 +22,0 @@ mongoSort[sort[i][0]] = sort[i][1]; |
34
index.js
var Mingo = require('mingo'); | ||
var cloneDeep = require('lodash.clonedeep'); | ||
var isObject = require('lodash.isobject'); | ||
@@ -9,7 +10,7 @@ // This is designed for use in tests, so load all Mingo query operators | ||
var MONGO_DOC_PROPERTIES = { | ||
'_id': 'id', | ||
'_v': 'v', | ||
'_type': 'type', | ||
'_m': 'm', | ||
'_o': 'o' | ||
_id: 'id', | ||
_v: 'v', | ||
_type: 'type', | ||
_m: 'm', | ||
_o: 'o' | ||
}; | ||
@@ -19,4 +20,4 @@ | ||
var STRIPPED_QUERY_KEYS = { | ||
'$comment': true, | ||
'$hint': true | ||
$comment: true, | ||
$hint: true | ||
}; | ||
@@ -51,3 +52,5 @@ | ||
if (result.snapshots && !includeMetadata) { | ||
result.snapshots.forEach(function(snapshot) { snapshot.m = null; }); | ||
result.snapshots.forEach(function(snapshot) { | ||
snapshot.m = null; | ||
}); | ||
} | ||
@@ -61,3 +64,3 @@ callback(null, result.snapshots, result.extra); | ||
ShareDBMingo.prototype._querySync = function(snapshots, query, options) { | ||
ShareDBMingo.prototype._querySync = function(snapshots, query, _options) { | ||
if (Array.isArray(query.$aggregate)) { | ||
@@ -69,3 +72,3 @@ // sharedb-mongo passes the $aggregate pipeline straight to Mongo, so | ||
var aggResult = mingoAgg.run(mongoDocs); | ||
return { snapshots: [], extra: aggResult }; | ||
return {snapshots: [], extra: aggResult}; | ||
} | ||
@@ -121,4 +124,5 @@ | ||
if (inputQuery.$orderby) | ||
console.warn("Warning: query.$orderby deprecated. Use query.$sort instead."); | ||
if (inputQuery.$orderby) { | ||
console.warn('Warning: query.$orderby deprecated. Use query.$sort instead.'); | ||
} | ||
var sort = query.$sort || query.$orderby; | ||
@@ -149,2 +153,6 @@ delete query.$sort; | ||
function castToSnapshotQuery(query) { | ||
if (!isObject(query) || Array.isArray(query)) { | ||
throw new Error('Invalid mongo query format'); | ||
} | ||
var snapshotQuery = {}; | ||
@@ -175,3 +183,3 @@ var propertySegments; | ||
} else { | ||
snapshotQuery["data." + property] = query[property]; | ||
snapshotQuery['data.' + property] = query[property]; | ||
} | ||
@@ -178,0 +186,0 @@ } |
{ | ||
"name": "sharedb-mingo-memory", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "In-memory database adapter with MongoDB API for ShareDB", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "./node_modules/.bin/eslint --ignore-path .gitignore '**/*.js'", | ||
"lint:fix": "npm run lint -- --fix", | ||
"test": "mocha", | ||
@@ -16,2 +18,3 @@ "test-cover": "node_modules/nyc/bin/nyc.js --temp-dir=coverage -r text -r lcov node_modules/mocha/bin/_mocha" | ||
"lodash.clonedeep": "^4.5.0", | ||
"lodash.isobject": "^3.0.2", | ||
"mingo": "^6.1.0" | ||
@@ -26,2 +29,4 @@ }, | ||
"coveralls": "^3.0.7", | ||
"eslint": "^7.32.0", | ||
"eslint-config-google": "^0.14.0", | ||
"mocha": "^6.2.2", | ||
@@ -28,0 +33,0 @@ "nyc": "^14.1.1", |
@@ -5,6 +5,6 @@ var expect = require('chai').expect; | ||
var sortSnapshot = function(snapshots) { | ||
return snapshots.sort(function (a, b) { | ||
return snapshots.sort(function(a, b) { | ||
return (a.id > b.id) ? 1 : ((b.id > a.id) ? -1 : 0); | ||
}); | ||
} | ||
}; | ||
@@ -66,5 +66,5 @@ // Call this function inside a `describe` block. Assumes that | ||
{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']}}, | ||
{type: 'json0', id: 'test3', v: 1, data: {x: 3, y: 2, foo: ['foo2', 'bar']}} | ||
]; | ||
var query = {$count: true, foo: {'$all': [/^foo/, 'bar']}}; | ||
var query = {$count: true, foo: {$all: [/^foo/, 'bar']}}; | ||
@@ -88,5 +88,5 @@ var db = this.db; | ||
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} | ||
{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} | ||
]; | ||
@@ -101,3 +101,3 @@ var query = {$sort: {x: 1}, $skip: 1, $limit: 1}; | ||
db.query('testcollection', query, null, null, function(err, results, extra) { | ||
db.query('testcollection', query, null, null, function(err, results) { | ||
if (err) throw err; | ||
@@ -112,5 +112,5 @@ expect(results).eql([snapshots[2]]); | ||
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} | ||
{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} | ||
]; | ||
@@ -126,3 +126,3 @@ // $comment and $hint should be ignored. | ||
db.query('testcollection', query, null, null, function(err, results, extra) { | ||
db.query('testcollection', query, null, null, function(err, results) { | ||
if (err) return done(err); | ||
@@ -183,5 +183,5 @@ expect(results).eql([snapshots[2]]); | ||
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}} | ||
{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}} | ||
]; | ||
@@ -202,3 +202,3 @@ var snapshotsNoMeta = snapshots.map(function(snapshot) { | ||
it('condition on Mongo _id (Share id)', function(done) { | ||
this.db.query('testcollection', {_id: 'test1'}, null, null, function(err, results, extra) { | ||
this.db.query('testcollection', {_id: 'test1'}, null, null, function(err, results) { | ||
if (err) throw err; | ||
@@ -211,3 +211,3 @@ expect(results).eql([snapshotsNoMeta[0]]); | ||
it('condition on sub-property under Share metadata', function(done) { | ||
this.db.query('testcollection', {'_m.mtime': 1001}, null, null, function(err, results, extra) { | ||
this.db.query('testcollection', {'_m.mtime': 1001}, null, null, function(err, results) { | ||
if (err) throw err; | ||
@@ -220,3 +220,3 @@ expect(results).eql([snapshotsNoMeta[1]]); | ||
it('condition on Mongo _id and Share data', function(done) { | ||
this.db.query('testcollection', {y: 2, _id: {$nin: ['test2']}}, null, null, function(err, results, extra) { | ||
this.db.query('testcollection', {y: 2, _id: {$nin: ['test2']}}, null, null, function(err, results) { | ||
if (err) throw err; | ||
@@ -229,3 +229,3 @@ expect(results).eql([snapshotsNoMeta[2]]); | ||
it('top-level boolean operator', function(done) { | ||
this.db.query('testcollection', {$or: [{y: 1}, {_id: 'test2'}]}, null, null, function(err, results, extra) { | ||
this.db.query('testcollection', {$or: [{y: 1}, {_id: 'test2'}]}, null, null, function(err, results) { | ||
if (err) throw err; | ||
@@ -240,5 +240,5 @@ expect(sortSnapshot(results)).eql(sortSnapshot([snapshotsNoMeta[0], snapshotsNoMeta[1]])); | ||
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} | ||
{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} | ||
]; | ||
@@ -253,3 +253,3 @@ var query = {y: null}; | ||
db.query('testcollection', query, null, null, function(err, results, extra) { | ||
db.query('testcollection', query, null, null, function(err, results) { | ||
if (err) throw err; | ||
@@ -262,7 +262,22 @@ expect(results).eql([snapshots[1]]); | ||
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} | ||
{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} | ||
]; | ||
@@ -278,3 +293,3 @@ | ||
it('$and', function(done) { | ||
this.db.query('testcollection', {$and: [{x: 1}, {y: 1}], $sort: {_id: 1}}, null, null, function(err, results, extra) { | ||
this.db.query('testcollection', {$and: [{x: 1}, {y: 1}], $sort: {_id: 1}}, null, null, function(err, results) { | ||
if (err) throw err; | ||
@@ -287,3 +302,3 @@ expect(results).eql([snapshots[0]]); | ||
it('$or', function(done) { | ||
this.db.query('testcollection', {$or: [{x: 1}, {y: 1}], $sort: {_id: 1}}, null, null, function(err, results, extra) { | ||
this.db.query('testcollection', {$or: [{x: 1}, {y: 1}], $sort: {_id: 1}}, null, null, function(err, results) { | ||
if (err) throw err; | ||
@@ -290,0 +305,0 @@ expect(sortSnapshot(results)).eql(sortSnapshot([snapshots[0], snapshots[1]])); |
@@ -6,3 +6,3 @@ var expect = require('chai').expect; | ||
function create(callback) { | ||
var db = ShareDBMingo(); | ||
var db = new ShareDBMingo(); | ||
callback(null, db); | ||
@@ -22,3 +22,3 @@ } | ||
it('unsupported', function() { | ||
this.db.query('testcollection', {$mapReduce: []}, null, null, function(err, results) { | ||
this.db.query('testcollection', {$mapReduce: []}, null, null, function(err) { | ||
expect(err).an('error'); | ||
@@ -38,3 +38,3 @@ }); | ||
.eql({foo: 2, $sort: {foo: 1, bar: -1}}); | ||
}) | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
25099
10.7%10
11.11%568
16.87%4
33.33%10
25%+ Added
+ Added