couchdbjs
couchdbjs is node.js client for couchdb.
Install
$ npm install couchdbjs --save
Create a couchdbjs object
const Couchdbjs = ;const db = dbname options callback;
Note : Here DB is the object of the class couchdbjs.
Here options
is an optional js object containing the configuration.
This is the default configuration.
protocol : 'http:' hostname: 'localhost' port: 5984
dbname
is the name of the database.
Generate a new uuid
Couchdbjs;
Here options
is optional configuration object.
This is the default configuration.
protocol : 'http:' hostname : 'localhost' port: 5984 count : 1
cb
is reqired callback function with two parameters err
and data
.
To avoid writing configurations for protocol, hostname and port again the function getConfig
can be called on existing database object.
db;
returns
protocol : 'http:' hostname : 'localhost' port: 5984 count : 1
Complete Example :
Couchdbjs;
Create a new database
// not recommendedconst db = 'dbname' options;
This creates a database when a database with name dbname
doesn't exist.
Database can also be created using Couchdbjs.createDB
function.
// recommendedCouchdbjs;
Delete a database
Database can be deleted using function Couchdbjs.deleteDB
similar to Couchdbjs.createDB
.
Couchdbjs;
List all documents in a database
db;
Create a new document
db;
id
is the id of the document to be used.
document
is the object to be stored in the document.
cb
is the callback function with parameters err
and data
.
Example
db;
Get document
db;
id
is the id of the document.
cb
is the callback function with parameters err
and data
.
Example
db;
Update a document
db;
id
, doc
, cb
are id of document to be updated, document object with filed to be updated and callback function with parameters err
and data
respectively.
Example
Suppose this a document already present
'id': 'id_doc' '_rev': '1-1357' 'a': 1 'b': 'asd'
If the following function is used to update the document
db;
then the final document will be
'id': 'id_doc' '_rev': '2-2468' 'a': 1 'b': 'cs' 'c': 5
After that if the following function is used to update the document
db;
then the final document will be
'id': 'id_doc' '_rev': '3-3579' 'b': 'cse' 'c': 5
To update whole document by overwriting the existing one use
db;
with _rev
field in the doc
document.
Delete a document
db;
Attach files to a document
db;
Example
db;
Here the file gets uploaded to http://localhost:5984/db_name/id_doc/pic.jpg.
Delete attachment from document
db;
Example
db;