Comparing version 3.3.0 to 3.4.0
@@ -121,2 +121,28 @@ const querystring = require('querystring') | ||
}, | ||
// Based on http://stackoverflow.com/a/16827094/3324977 | ||
undelete: function (docId) { | ||
// Verify that it's indeed a deleted document | ||
return API.get(docId) | ||
.then(function (res) { | ||
throw errors_.new("can't undelete an non-deleted document", 400, docId) | ||
}) | ||
.catch(function (err) { | ||
if (err.statusCode !== 404 || err.body.reason !== 'deleted') throw err | ||
var url = API.docUrl(docId) + '?revs=true&open_revs=all' | ||
return jsonRequest('GET', url) | ||
.then(function (res) { | ||
const data = res.body[0].ok | ||
const currentRev = data._rev | ||
const preDeleteRevNum = data._revisions.start - 1 | ||
const preDeleteRevId = data._revisions.ids[1] | ||
const preDeleteRev = preDeleteRevNum + '-' + preDeleteRevId | ||
return API.get(docId, preDeleteRev) | ||
.then(function (preDeleteDoc) { | ||
preDeleteDoc._rev = currentRev | ||
return API.put(preDeleteDoc) | ||
}) | ||
}) | ||
}) | ||
}, | ||
bulk: function (docs) { | ||
@@ -123,0 +149,0 @@ const url = `/${dbName}/_bulk_docs` |
@@ -18,3 +18,3 @@ { | ||
}, | ||
"version": "3.3.0", | ||
"version": "3.4.0", | ||
"main": "lib/cot.js", | ||
@@ -21,0 +21,0 @@ "dependencies": { |
@@ -23,2 +23,3 @@ [CouchDB](http://couchdb.org/) library with a simple, functional-programing-friendly API, returning [Bluebird](https://github.com/petkaantonov/bluebird) promises. | ||
- [revertToLastVersionWhere](#reverttolastversionwhere) | ||
- [undelete](#undelete) | ||
- [View functions](#view-functions) | ||
@@ -167,2 +168,12 @@ - [viewCustom](#viewcustom) | ||
##### undelete | ||
Mistakes happen | ||
```js | ||
db.delete(docId, docRev) | ||
.then(res => db.undelete(docId)) | ||
.then(res => db.get(docId)) | ||
.then(restoredDoc => // celebrate) | ||
``` | ||
:alert: this will obviously not work if the version before deletion isn't in the database (because the database was compressed or it's a freshly replicated database), or if the database was purged from deleted documents | ||
#### View functions | ||
@@ -169,0 +180,0 @@ |
@@ -35,2 +35,3 @@ require('should') | ||
'delete', | ||
'undelete', | ||
'bulk', | ||
@@ -37,0 +38,0 @@ 'buildQueryString', |
@@ -107,2 +107,74 @@ require('should') | ||
describe('#delete', function () { | ||
it('should delete a document', function (done) { | ||
db.get('person-1') | ||
.then(function (doc) { | ||
db.delete('person-1', doc._rev) | ||
.then(function () { | ||
db.get('person-1') | ||
.catch(function (err) { | ||
err.statusCode.should.equal(404) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('#undelete', function () { | ||
it('should undelete a document', function (done) { | ||
db.undelete.should.be.a.Function() | ||
const docId = 'person-1' | ||
db.get(docId) | ||
.then(function (originalDoc) { | ||
db.delete(docId, originalDoc._rev) | ||
.then(res => db.undelete(docId)) | ||
.then(res => db.get(docId)) | ||
.then(function (restoredDoc) { | ||
delete originalDoc._rev | ||
delete restoredDoc._rev | ||
originalDoc.should.deepEqual(restoredDoc) | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('should throw when asked to delete a non deleted document', function (done) { | ||
const docId = 'person-1' | ||
// updating so that there is more than one rev | ||
db.update(docId, randomUpdate) | ||
.then((res) => db.undelete(docId)) | ||
.catch(function (err) { | ||
err.message.should.equal("can't undelete an non-deleted document") | ||
done() | ||
}) | ||
}) | ||
it('should be able to undelete several times the same doc', function (done) { | ||
db.undelete.should.be.a.Function() | ||
const docId = 'person-1' | ||
db.get(docId) | ||
.then(function (originalDoc) { | ||
// First delete | ||
db.delete(docId, originalDoc._rev) | ||
// First undelete | ||
.then(res => db.undelete(docId)) | ||
.then(res => db.get(docId)) | ||
.then(function (restoredDoc) { | ||
const currentRev = restoredDoc._rev | ||
delete originalDoc._rev | ||
delete restoredDoc._rev | ||
originalDoc.should.deepEqual(restoredDoc) | ||
// Second delete | ||
db.delete(docId, currentRev) | ||
// Second undelete | ||
.then(res => db.undelete(docId)) | ||
.then(res => db.get(docId)) | ||
.then(function (restoredDoc) { | ||
delete originalDoc._rev | ||
delete restoredDoc._rev | ||
originalDoc.should.deepEqual(restoredDoc) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('#view', function () { | ||
@@ -109,0 +181,0 @@ it('should return a single row', function (done) { |
Sorry, the diff of this file is not supported yet
79111
967
193