Comparing version 4.1.5 to 5.0.0
@@ -87,6 +87,8 @@ var DB = require('./index'); | ||
var opLog = db._getOpLogSync(collection, id); | ||
if (to == null) { | ||
to = opLog.length; | ||
if (!from) from = 0; | ||
if (to == null) to = opLog.length; | ||
var ops = clone(opLog.slice(from, to).filter(Boolean)); | ||
if (ops.length < to - from) { | ||
return callback(new Error('Missing ops')); | ||
} | ||
var ops = clone(opLog.slice(from, to)); | ||
if (!includeMetadata) { | ||
@@ -101,2 +103,14 @@ for (var i = 0; i < ops.length; i++) { | ||
MemoryDB.prototype.deleteOps = function(collection, id, from, to, options, callback) { | ||
if (typeof callback !== 'function') throw new Error('Callback required'); | ||
var db = this; | ||
util.nextTick(function() { | ||
var opLog = db._getOpLogSync(collection, id); | ||
if (!from) from = 0; | ||
if (to == null) to = opLog.length; | ||
for (var i = from; i < to; i++) opLog[i] = null; | ||
callback(null); | ||
}); | ||
}; | ||
// The memory database query function returns all documents in a collection | ||
@@ -103,0 +117,0 @@ // regardless of query by default |
{ | ||
"name": "sharedb", | ||
"version": "4.1.5", | ||
"version": "5.0.0", | ||
"description": "JSON OT database backend", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -148,3 +148,3 @@ var Backend = require('../../lib/backend'); | ||
backend.connect().fetchSnapshot('books', 'don-quixote', 4, function(error, snapshot) { | ||
expect(error.code).to.equal('ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT'); | ||
expect(error).to.be.ok; | ||
expect(snapshot).to.equal(undefined); | ||
@@ -155,2 +155,13 @@ done(); | ||
it('errors if asking for a version that does not exist if the DB adapter does not error', function(done) { | ||
sinon.stub(MemoryDb.prototype, 'getOps').callsFake(function(collection, id, from, to, options, callback) { | ||
callback(null, []); | ||
}); | ||
backend.connect().fetchSnapshot('books', 'don-quixote', 4, function(error, snapshot) { | ||
expect(error).to.be.ok; | ||
expect(snapshot).to.equal(undefined); | ||
done(); | ||
}); | ||
}); | ||
it('returns an empty snapshot if trying to fetch a non-existent document', function(done) { | ||
@@ -157,0 +168,0 @@ backend.connect().fetchSnapshot('books', 'does-not-exist', 0, function(error, snapshot) { |
var expect = require('chai').expect; | ||
var Backend = require('../lib/backend'); | ||
var DB = require('../lib/db'); | ||
var BasicQueryableMemoryDB = require('./BasicQueryableMemoryDB'); | ||
var async = require('async'); | ||
@@ -57,15 +59,81 @@ describe('DB base class', function() { | ||
// Run all the DB-based tests against the BasicQueryableMemoryDB. | ||
require('./db')({ | ||
create: function(options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = null; | ||
describe('MemoryDB', function() { | ||
// Run all the DB-based tests against the BasicQueryableMemoryDB. | ||
require('./db')({ | ||
create: function(options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = null; | ||
} | ||
var db = new BasicQueryableMemoryDB(options); | ||
callback(null, db); | ||
}, | ||
getQuery: function(options) { | ||
return {filter: options.query, sort: options.sort}; | ||
} | ||
var db = new BasicQueryableMemoryDB(options); | ||
callback(null, db); | ||
}, | ||
getQuery: function(options) { | ||
return {filter: options.query, sort: options.sort}; | ||
} | ||
}); | ||
describe('deleteOps', function() { | ||
describe('with some ops', function() { | ||
var backend; | ||
var db; | ||
var connection; | ||
var doc; | ||
beforeEach(function(done) { | ||
backend = new Backend(); | ||
db = backend.db; | ||
connection = backend.connect(); | ||
doc = connection.get('dogs', 'fido'); | ||
async.waterfall([ | ||
doc.create.bind(doc, {name: 'Fido'}), | ||
doc.submitOp.bind(doc, [{p: ['tricks'], oi: ['fetch']}]), | ||
db.getOps.bind(db, 'dogs', 'fido', null, null, null), | ||
function(ops, next) { | ||
expect(ops).to.have.length(2); | ||
next(); | ||
} | ||
], done); | ||
}); | ||
it('deletes all ops', function(done) { | ||
async.waterfall([ | ||
db.deleteOps.bind(db, 'dogs', 'fido', null, null, null), | ||
function(next) { | ||
db.getOps('dogs', 'fido', null, null, null, function(error) { | ||
expect(error.message).to.equal('Missing ops'); | ||
next(); | ||
}); | ||
} | ||
], done); | ||
}); | ||
it('deletes some ops', function(done) { | ||
async.waterfall([ | ||
db.deleteOps.bind(db, 'dogs', 'fido', 0, 1, null), | ||
db.getOps.bind(db, 'dogs', 'fido', 1, 2, null), | ||
function(ops, next) { | ||
expect(ops).to.have.length(1); | ||
expect(ops[0].op).to.eql([{p: ['tricks'], oi: ['fetch']}]); | ||
db.getOps('dogs', 'fido', 0, 1, null, function(error) { | ||
expect(error.message).to.equal('Missing ops'); | ||
next(); | ||
}); | ||
} | ||
], done); | ||
}); | ||
it('submits more ops after deleting ops', function(done) { | ||
async.series([ | ||
db.deleteOps.bind(db, 'dogs', 'fido', null, null, null), | ||
doc.submitOp.bind(doc, [{p: ['tricks', 1], li: 'sit'}]), | ||
function(next) { | ||
expect(doc.data.tricks).to.eql(['fetch', 'sit']); | ||
next(); | ||
} | ||
], done); | ||
}); | ||
}); | ||
}); | ||
}); |
660863
17603