- no dependencies
- as simple as possible
All Functions return a Promise object that is resolved or rejected with an object of 4 properties:
- headers: {Object} - HTTP response headers
- data: {Object} - DB response object
- status: {Number} - HTTP status code
- message: {String} - description of the status code
The promise is resolved if the status code is < 400 otherwise rejected.
npm install couchdb-promises
const db = require('couchdb-promises')
const baseUrl = 'http://localhost:5984' // https://[user:password@]server[:port]
const dbName = 'testdb'
db.getInfo(baseUrl)
.then(console.log)
// { headers: { ... },
// data:
// { couchdb: 'Welcome',
// uuid: 'bce82829daa02c49fe5b57c542ea95a3',
// version: '1.6.1',
// vendor: { name: 'The Apache Software Foundation', version: '1.6.1' } },
// status: 200,
// message: 'OK - Request completed successfully' }
db.createDatabase(baseUrl, dbName)
.then(console.log)
// { headers: { ... },
// data: { ok: true },
// status: 201,
// message: 'Created - Database created successfully' }
.then(() => db.getDatabaseHead(baseUrl, dbName))
.then(console.log)
// { headers: { ... },
// data: {},
// status: 200,
// message: 'OK - Database exists' }
.then(() => db.listDatabases(baseUrl))
.then(console.log)
// { headers: { ... },
// data: [ '_replicator', '_users', 'testdb' ],
// status: 200,
// message: 'OK - Request completed successfully' }
.then(() => db.createDocument(baseUrl, dbName, {name: 'Bob'}))
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: 'daae0752c6909d7ca4cd833f46014605',
// rev: '1-5a26fa4b20e40bc9e2d3e47b168be460' },
// status: 201,
// message: 'Created – Document created and stored on disk' }
.then(() => db.createDocument(baseUrl, dbName, {name: 'Alice'}, 'doc2'))
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: 'doc2',
// rev: '1-88b10f13383b5d1e34d1d66d296f061f' },
// status: 201,
// message: 'Created – Document created and stored on disk' }
.then(() => db.getDocumentHead(baseUrl, dbName, 'doc2'))
.then(console.log)
// { headers:
// { server: 'CouchDB/1.6.1 (Erlang OTP/18)',
// etag: '"1-88b10f13383b5d1e34d1d66d296f061f"',
// date: 'Sat, 01 Oct 2016 09:46:09 GMT',
// 'content-type': 'application/json',
// 'content-length': '74',
// 'cache-control': 'must-revalidate' },
// data: {},
// status: 200,
// message: 'OK - Document exists' }
.then(() => db.getDocument(baseUrl, dbName, 'doc2'))
.then(response => { console.log(response); return response.data })
// { headers: { ... },
// data:
// { _id: 'doc2',
// _rev: '1-88b10f13383b5d1e34d1d66d296f061f',
// name: 'Alice' },
// status: 200,
// message: 'OK - Request completed successfully' }
.then((doc) => {
doc.age = 42
return db.createDocument(baseUrl, dbName, doc, 'doc2')
})
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: 'doc2',
// rev: '2-ee5ea9ac0bb1bec913a9b5e7bc11b113' },
// status: 201,
// message: 'Created – Document created and stored on disk' }
.then(() => db.getAllDocuments(baseUrl, dbName, {
descending: true,
include_docs: true
}))
.then(console.log)
// { headers: { ... },
// data: { total_rows: 2, offset: 0, rows: [ [Object], [Object] ] },
// status: 200,
// message: 'OK - Request completed successfully' }
.then(() => db.deleteDocument(baseUrl, dbName, 'doc2', '2-ee5ea9ac0bb1bec913a9b5e7bc11b113'))
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: 'doc2',
// rev: '3-ec0a86a95c5e98a5cd52c29b79b66372' },
// status: 200,
// message: 'OK - Document successfully removed' }
.then(() => db.createBulkDocuments(baseUrl, dbName, [
{name: 'Tick'}, {name: 'Trick'}, {name: 'Track'}
], {all_or_nothing: true}))
.then(console.log)
// { headers: { ... },
// data:
// [ { ok: true,
// id: '5413cf41edaedaec6b63aee93db86e1f',
// rev: '1-d7f23e94e65978ea9252d753fe5dc3f6' },
// { ok: true,
// id: '5413cf41edaedaec6b63aee93db877cc',
// rev: '1-646cd5f84634632f42fee2bdf4ff753a' },
// { ok: true,
// id: '5413cf41edaedaec6b63aee93db87c3d',
// rev: '1-9cc8cf1e775b686ca337f69cd39ff772' } ],
// status: 201,
// message: 'Created – Document(s) have been created or updated' }
.then(() => db.deleteDatabase(baseUrl, dbName))
.then(console.log)
// { headers: { ... },
// data: { ok: true },
// status: 200,
// message: 'OK - Database removed successfully' }
.then(() => db.getUuids(baseUrl, 3))
.then(console.log)
// { headers: { ... },
// data:
// { uuids:
// [ 'daae0752c6909d7ca4cd833f46014c47',
// 'daae0752c6909d7ca4cd833f460150c5',
// 'daae0752c6909d7ca4cd833f460156c5' ] },
// status: 200,
// message: 'OK - Request completed successfully' }
.then(() => db.getDocument(baseUrl, dbName, 'doc1'))
.catch(console.error)
// { headers: { ... },
// data: { error: 'not_found', reason: 'no_db_file' },
// status: 404,
// message: 'Not Found - Document not found' }
const ddoc = {
language: 'javascript',
views: { view1: { map: 'function (doc) {emit(doc.name, doc.number)}' } }
}
const docId = 'ddoc1'
db.createDesignDocument(baseUrl, dbName, ddoc, docId)
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: '_design/ddoc1',
// rev: '1-548c68d8cc2c1fac99964990a58f66fd' },
// status: 201,
// message: 'Created – Document created and stored on disk' }
db.getDesignDocument(baseUrl, dbName, docId)
.then(console.log)
// { headers: { ... },
// data:
// { _id: '_design/ddoc1',
// _rev: '1-548c68d8cc2c1fac99964990a58f66fd',
// language: 'javascript',
// views: { view1: [Object] } },
// status: 200,
// message: 'OK - Request completed successfully' }
db.getDesignDocumentInfo(baseUrl, dbName, docId)
.then(console.log)
// { headers: { ... },
// data:
// { name: 'ddoc1',
// view_index:
// { updates_pending: [Object],
// waiting_commit: false,
// waiting_clients: 0,
// updater_running: false,
// update_seq: 0,
// sizes: [Object],
// signature: '09da8e42090600707a71a85434663e4f',
// purge_seq: 0,
// language: 'javascript',
// disk_size: 408,
// data_size: 0,
// compact_running: false } },
// status: 200,
// message: 'OK - Request completed successfully' }
db.deleteDesignDocument(baseUrl, dbName, docId, rev)
.then(console.log)
// { headers: { ... },
// data:
// { ok: true,
// id: '_design/ddoc1',
// rev: '2-fd68157ec3c1915ebe0b248343292d34' },
// status: 200,
// message: 'OK - Document successfully removed' }
db.getView(baseUrl, dbName, docId, viewName, {limit: 3})
.then(console.log)
// { headers: { ... },
// data:
// { total_rows: 12,
// offset: 0,
// rows: [ [Object], [Object], [Object] ] },
// status: 200,
// message: 'OK' }
db.getTimeout()
// -> 10000 (no Promise)
db.setTimeout(3000)
// -> 3000 (no Promise)
--
See examples for details.
- createDatabase()
- deleteDatabase()
- getDatabase()
- getDatabaseHead()
- listDatabases()
- getAllDocuments()
- createDocument()
- deleteDocument()
- getDocument()
- getDocumentHead()
- getAttachment()
- getAttachmentHead()
- addAttachment()
- deleteAttachment()
- createDesignDocument()
- deleteDesignDocument()
- getDesignDocument()
- getDesignDocumentInfo()
- getView()
- createBulkDocuments()
- setTimeout()
- getTimeout()
- getInfo()
- getUuids()
- bulkDocs() -> createBulkDocuments()
- getAllDocs() -> getAllDocuments()