CouchDB library with a simple, functional-programing-friendly API, returning Bluebird promises.
Forked from Cot
Summary
Installing
npm install blue-cot
Differences with Cot
- Returns Bluebird promises
- Class-less, thus a different initialization, but the rest of the API stays the same
- Consequently,
blue-cot
is this
-free: no need to bind functions contexts! 4xx
and 5xx
responses will return rejected promises (should be handled with .catch
)- Adds a few new functions, notably some view functions goodies
Initialization
const bluecot = require('blue-cot')
const config = {
hostname: 'localhost'
port: 5984,
ssl: true
auth: 'username:password'
user: 'username'
pass: 'password'
debug: true
}
const getDbApi = bluecot(config)
const db = getDbApi('some-db-name')
API
Common API
Cot API Documentation
Those are the same than for cot-node
. Just remember this difference in error handling: here, 4xx
and 5xx
responses from CouchDB will return rejected promises (should be handled with .catch
)
- docUrl
- info
- get
- exists
- put
- post
- batch
- update
- delete
- bulk
- buildQueryString
- viewQuery
- view
- allDocs
- viewKeysQuery
- viewKeys
- allDocsKeys
- changes
Specific API
Extended functions
get
Takes a document id and optionaly a rev id to get a specific version:
db.get('doc-1')
.then(function (lastDocVersion) {
})
db.get('doc-1', '2-b8476e8877ff5707de9e62e70a8e0aeb')
.then(function (specificVersion) {
})
Additional database functions
fetch
Takes doc ids, returns docs
db.fetch([ 'doc-1', 'doc-2', 'doc-3' ])
.then(function (docs) {
docs[0]._id === 'doc-1'
docs[1]._id === 'doc-2'
docs[2]._id === 'doc-3'
})
listRevs
Takes a doc id, returns the doc's rev infos
db.listRevs('doc-1')
.then(function (revsInfo) {
})
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.
db.revertLastChange('doc-1')
.then(function (res) {
})
View functions
To access those, pass a design doc name as second argument
const db = getDbApi('some-db-name', 'some-design-doc-name')
viewCustom
viewByKeysCustom
viewByKey
viewFindOneByKey
viewByKeys
see lib/view_functions
If you find this module useful, consider making a PR to improve the documentation