Comparing version 3.1.3 to 3.2.0
# CHANGELOG | ||
*versions follow [SemVer](http://semver.org)* | ||
## 3.2.0 - 2017-01-23 | ||
* Added [`db.listRevs`](https://github.com/maxlath/blue-cot#listrevs) | ||
* Added [`db.revertLastChange`](https://github.com/maxlath/blue-cot#revertlastchange) | ||
* Added `db.revertLastChange` | ||
* `db.get` accepts a `rev` id as second parameter | ||
## 3.1.0 - 2017-01-14 | ||
* Added [`db.fetch`](https://github.com/maxlath/blue-cot#fetch) | ||
* Added `db.fetch` | ||
@@ -6,0 +13,0 @@ |
@@ -21,4 +21,6 @@ const querystring = require('querystring') | ||
}, | ||
get: function (docId) { | ||
return jsonRequest('GET', API.docUrl(docId)) | ||
get: function (docId, revId) { | ||
var url = API.docUrl(docId) | ||
if (typeof revId === 'string') url += `?rev=${revId}` | ||
return jsonRequest('GET', url) | ||
.then(function (res) { | ||
@@ -185,2 +187,31 @@ if (res.statusCode !== 200) { | ||
}, | ||
listRevs: function (docId) { | ||
const url = API.docUrl(docId) + '?revs_info=true' | ||
return jsonRequest('GET', url) | ||
.then(res => res.body._revs_info) | ||
}, | ||
revertLastChange: function (docId) { | ||
return API.listRevs(docId) | ||
.then(function (revsInfo) { | ||
const currentRevInfo = revsInfo[0] | ||
const previousRevInfo = revsInfo[1] | ||
if (previousRevInfo == null) { | ||
const err = new Error('no previous version could be found') | ||
err.statusCode = 400 | ||
err.context = { doc: docId } | ||
throw err | ||
} | ||
if (previousRevInfo.status !== 'available') { | ||
const err = new Error('previous version isnt available') | ||
err.statusCode = 400 | ||
err.context = { doc: docId, rev_info: previousRevInfo } | ||
throw err | ||
} | ||
return API.get(docId, previousRevInfo.rev) | ||
.then(function (lastVersion) { | ||
lastVersion._rev = currentRevInfo.rev | ||
return API.put(lastVersion) | ||
}) | ||
}) | ||
}, | ||
changes: function (query = {}) { | ||
@@ -187,0 +218,0 @@ const q = {} |
@@ -18,3 +18,3 @@ { | ||
}, | ||
"version": "3.1.3", | ||
"version": "3.2.0", | ||
"main": "lib/cot.js", | ||
@@ -21,0 +21,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
[CouchDB](http://couchdb.org/) library with a simple, functional-programing-friendly API, returning [Bluebird](https://github.com/petkaantonov/bluebird) promise. | ||
[CouchDB](http://couchdb.org/) library with a simple, functional-programing-friendly API, returning [Bluebird](https://github.com/petkaantonov/bluebird) promises. | ||
@@ -13,6 +13,17 @@ Forked from [Cot](https://github.com/willconant/cot-node) | ||
- [Initialization](#initialization) | ||
- [API](#api) | ||
- [Common API](#common-api) | ||
- [Specific API](#specific-api) | ||
- [Extended functions](#extended-functions) | ||
- [get](#get) | ||
- [Additional database functions](#additional-database-functions) | ||
- [fetch](#fetch) | ||
- [listRevs](#listrevs) | ||
- [revertLastChange](#revertlastchange) | ||
- [View functions](#view-functions) | ||
- [viewCustom](#viewcustom) | ||
- [viewByKeysCustom](#viewbykeyscustom) | ||
- [viewByKey](#viewbykey) | ||
- [viewFindOneByKey](#viewfindonebykey) | ||
- [viewByKeys](#viewbykeys) | ||
@@ -33,3 +44,3 @@ <!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
* `4xx` and `5xx` responses will return rejected promises (should be handled with `.catch`) | ||
* Adds [some view functions goodies](https://github.com/inventaire/blue-cot/blob/master/lib/view_functions.js) | ||
* Adds [a few new functions](#specific-api), notably [some view functions goodies](https://github.com/inventaire/blue-cot/blob/master/lib/view_functions.js) | ||
@@ -89,6 +100,21 @@ ## Initialization | ||
#### Extended functions | ||
##### get | ||
Takes a document id and optionaly a rev id to get a specific version: | ||
```js | ||
db.get('doc-1') | ||
.then(function (lastDocVersion) { | ||
// do something | ||
}) | ||
db.get('doc-1', '2-b8476e8877ff5707de9e62e70a8e0aeb') | ||
.then(function (specificVersion) { | ||
// doc._rev === '2-b8476e8877ff5707de9e62e70a8e0aeb' | ||
}) | ||
``` | ||
#### Additional database functions | ||
##### fetch | ||
takes doc ids, returns docs | ||
Takes doc ids, returns docs | ||
```js | ||
@@ -103,2 +129,33 @@ db.fetch([ 'doc-1', 'doc-2', 'doc-3' ]) | ||
##### listRevs | ||
Takes a doc id, returns the doc's rev infos | ||
```js | ||
db.listRevs('doc-1') | ||
.then(function (revsInfo) { | ||
// do your thing | ||
}) | ||
``` | ||
`revsInfo` will look something like: | ||
``` | ||
[ | ||
{ rev: '3-6a8869bc7fff815987ff9b7fda3e10e3', status: 'available' }, | ||
{ rev: '2-88476e8877ff5707de9e62e70a8e0aeb', status: 'available' }, | ||
{ rev: '1-a8bdf0ef0b7049d35c781210723b9ff9', status: 'available' } | ||
] | ||
``` | ||
##### revertLastChange | ||
Takes a doc id and reverts its last change, recovering the previous version. | ||
Only works if there is a previous version and if it is still available in the database (that is, if it wasn't deleted by a database compaction). | ||
It doesn't delete the last version, it simply creates a new version that is exactly like the version before the current one. | ||
```js | ||
db.revertLastChange('doc-1') | ||
.then(function (res) { | ||
// celebrate | ||
}) | ||
``` | ||
#### View functions | ||
@@ -105,0 +162,0 @@ |
@@ -44,2 +44,4 @@ require('should') | ||
'fetch', | ||
'listRevs', | ||
'revertLastChange', | ||
'changes', | ||
@@ -46,0 +48,0 @@ 'jsonRequest' |
@@ -93,2 +93,14 @@ require('should') | ||
}) | ||
it('should return a specific version when passed a rev', function (done) { | ||
db.get('person-1') | ||
.then(function (firstVersion) { | ||
db.update('person-1', randomUpdate) | ||
.then(() => db.get('person-1', firstVersion._rev)) | ||
.then(function (specificVersion) { | ||
specificVersion.should.deepEqual(firstVersion) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
@@ -242,2 +254,57 @@ | ||
}) | ||
describe('#list-revs', function () { | ||
it('should return all the doc revs', function (done) { | ||
db.listRevs.should.be.a.Function() | ||
db.update('person-1', randomUpdate) | ||
.then(() => db.update('person-1', randomUpdate)) | ||
.then(function (res) { | ||
db.listRevs('person-1') | ||
.then(function (res) { | ||
res.should.be.an.Array() | ||
res[0].rev.split('-')[0].should.equal('3') | ||
res[0].status.should.equal('available') | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('#revertLastChange', function () { | ||
it('should revert to the previous version', function (done) { | ||
db.revertLastChange.should.be.a.Function() | ||
const getCurrentDoc = () => db.get('person-1') | ||
db.update('person-1', randomUpdate) | ||
.then(getCurrentDoc) | ||
.then(function (previousVersion) { | ||
db.update('person-1', randomUpdate) | ||
.then(getCurrentDoc) | ||
.then(function (lastVersion) { | ||
lastVersion._rev.should.not.equal(previousVersion._rev) | ||
lastVersion.foo.should.not.equal(previousVersion.foo) | ||
db.revertLastChange('person-1') | ||
.then(getCurrentDoc) | ||
.then(function (restoredVersion) { | ||
lastVersion.foo.should.not.equal(restoredVersion.foo) | ||
restoredVersion.foo.should.equal(previousVersion.foo) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
it('should reject when no previous rev can be found', function (done) { | ||
db.revertLastChange('person-1') | ||
.catch(function (err) { | ||
err.message.should.equal('no previous version could be found') | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
const randomUpdate = function (doc) { | ||
doc.foo = Math.random() | ||
return doc | ||
} |
@@ -73,2 +73,3 @@ const should = require('should') | ||
should(err.statusCode).be.ok() | ||
should(err.context).be.ok() | ||
done() | ||
@@ -75,0 +76,0 @@ }) |
Sorry, the diff of this file is not supported yet
72909
820
173