Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

support for rolling back to revision

  • Loading branch information...
commit 2e5311f899643b1da717d89e5fc834506e4bf851 1 parent cf4c350
@pgte authored
Showing with 67 additions and 9 deletions.
  1. +27 −0 index.js
  2. +2 −2 package.json
  3. +38 −7 test.js
View
27 index.js
@@ -84,8 +84,35 @@ module.exports = function(config) {
db.insert.apply(db, insertArgs);
}
+ function backToRevision(docId, revision, done, dontRepeat) {
+ if (typeof(docId) === 'object') { docId = docId.id || docId._id; }
+ db.get(docId, {rev: revision}, function(err, oldVer) {
+ if (err) {
+ if (err.message == 'no_db_file' && ! dontRepeat) {
+ return createDB(function(err) {
+ if (err) { return done(err); }
+ return backToRevision(docId, revision, done, true);
+ })
+ }
+ return done(err);
+ }
+ if (err) { return done(err); }
+ db.get(docId, function(err, doc) {
+ if (err) { return done(err); }
+ oldVer._rev = doc._rev;
+ db.insert(oldVer, function(err, response) {
+ if (err) { return done(err); }
+ if (response.id) { oldVer._id = response.id; }
+ if (response.rev) { oldVer._rev = response.rev; }
+ done(null, oldVer);
+ });
+ });
+ });
+ }
+
return {
load: load
, save: save
+ , backToRevision: backToRevision
};
};
View
4 package.json
@@ -1,7 +1,7 @@
{ "name" : "banzai-couchdb-store"
, "description" : "CouchDB plugin module for Banzai document store."
, "tags" : ["ETL", "pipeline", "document", "state machine"]
-, "version" : "1.0.3"
+, "version" : "1.1.0"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>"
, "repository" :
{ "type" : "git"
@@ -11,7 +11,7 @@
{ "web" : "http://github.com/pgte/banzai-docstore-couchdb/issues" }
, "engines" : ["node >= 0.4.10"]
, "dependencies" : {
- "nano": "0.8.x"
+ "nano": "0.9.x"
}
, "main" : "./index"
, "scripts": { "test": "expresso test.js" }
View
45 test.js
@@ -19,7 +19,7 @@ module.exports.test_1 = function(beforeExit) {
assert.ok(! cb1);
cb1 = true;
assert.isNull(err);
- assert.isNotNull(doc.id);
+ assert.isNotNull(doc._id);
doc_id = doc._id;
delete doc._id;
delete doc._rev;
@@ -29,9 +29,9 @@ module.exports.test_1 = function(beforeExit) {
assert.ok(! cb2);
cb2 = true;
assert.isNull(err);
- assert.isNotNull(doc.id);
- assert.eql(doc.id, doc_id);
- delete doc.id;
+ assert.isNotNull(doc._id);
+ assert.eql(doc._id, doc_id);
+ delete doc._id;
delete doc._rev;
assert.eql(doc, {a:1, b:2});
});
@@ -56,7 +56,7 @@ module.exports.test_2 = function(beforeExit) {
cb1 = true;
assert.isNull(err);
assert.isNotNull(doc.id);
- doc_id = doc._id;
+ doc_id = doc._id;-
delete doc._id;
delete doc._rev;
assert.eql(doc, {c:1, d:2});
@@ -66,8 +66,8 @@ module.exports.test_2 = function(beforeExit) {
cb2 = true;
assert.isNull(err);
assert.isNotNull(doc.id);
- assert.eql(doc.id, doc_id);
- delete doc.id;
+ assert.eql(doc._id, doc_id);
+ delete doc._id;
delete doc._rev;
assert.eql(doc, {c:1, d:2});
});
@@ -79,4 +79,35 @@ module.exports.test_2 = function(beforeExit) {
assert.ok(cb1);
assert.ok(cb2);
});
+};
+
+module.exports.test_back_to_revision = function(beforeExit) {
+ var cb1 = false
+ , cb2 = false;
+
+ done.once('done', function() {
+ store.save({c:1, d:2}, function(err, origDoc) {
+ if (err) { throw err; }
+ var rev = origDoc._rev;
+ origDoc.d = 3;
+ store.save(origDoc, function(err, doc) {
+ if (err) { throw err; }
+ store.backToRevision(doc, rev, function(err, doc) {
+ cb1 = true;
+ if (err) { throw err; }
+ store.load(doc.id || doc._id, function(err, doc) {
+ cb2 = true;
+ delete doc._rev;
+ delete doc._id;
+ assert.eql({c:1,d:2}, doc);
+ });
+ });
+ });
+ });
+ });
+
+ beforeExit(function() {
+ assert.ok(cb1);
+ assert.ok(cb2);
+ });
};
Please sign in to comment.
Something went wrong with that request. Please try again.