Support of CouchDB for Node.js
CAST supports CouchDB via its com.castsoftware.nodejs extension. Details about how this support is provided for Node.js source code is discussed below.
Objects
Icon | Description |
---|---|
Node.js CouchDB database |
What results can you expect?
Some example scenarios are shown below:
node-couchdb module
Database creation “myDatabase”
const NodeCouchDb = require('node-couchdb');
// node-couchdb instance with default options
const couch = new NodeCouchDb();
dbName = 'myDatabase';
couch.createDatabase(dbName).then(() => { }, err => {
// request error occured
});
useSelectLink from code to database “myDatabase”
couch.get("myDatabase", "some_document_id").then(({data, headers, status}) => {
// data is json response
// headers is an object with all response headers
// status is statusCode number
}, err => {
// either request error occured
// ...or err.code=EDOCMISSING if document is missing
// ...or err.code=EUNKNOWN if statusCode is unexpected
});
useInsertLink from code to database “myDatabase”
couch.insert("myDatabase", {
_id: "document_id",
field: ["sample", "data", true]
}).then(({data, headers, status}) => {
// data is json response
// headers is an object with all response headers
// status is statusCode number
}, err => {
// either request error occured
// ...or err.code=EDOCCONFLICT if document with the same id already exists
});
useUpdateLink from code to database “myDatabase”
couch.update("myDatabase", {
_id: "document_id",
_rev: "1-xxx"
field: "new sample data",
field2: 1
}).then(({data, headers, status}) => {
// data is json response
// headers is an object with all response headers
// status is statusCode number
}, err => {
// either request error occured
// ...or err.code=EFIELDMISSING if either _id or _rev fields are missing
});
useDeleteLink from code to database “myDatabase”
couch.del("myDatabase", "some_document_id", "document_revision").then(({data, headers, status}) => {
// data is json response
// headers is an object with all response headers
// status is statusCode number
}, err => {
// either request error occured
// ...or err.code=EDOCMISSING if document does not exist
// ...or err.code=EUNKNOWN if response status code is unexpected
});
couchdb module
Database creation “myDatabase”
var myCouchDB = require('couch-db').CouchDB,
server = new myCouchDB('http://localhost:5984');
server.auth(username, password);
server.bind('myDatabase');
var db = server.myDatabase;
useInsertLink from code to database “myDatabase”
// new document
var doc = db.testdb.doc({});
doc.attach([{
name: 'place.css',
content_type: 'text/css',
data: 'body { font-size: 12px; }'
}, {
name: 'script.js',
content_type: 'script/javascript',
data: 'window.onload(function() {})'
}]).create(function(err) {
});
useInsertLink from code to database “myDatabase”
Connector “couch-db”
db.destroy(function(err) {
// create a new database
db.create(function(err) {
// insert a document with id 'jack johns'
db.insert({ _id: 'jack johns', name: 'jack' }, function(err, body) {
if (err) {
console.log('insertion failed ', err.message);
return;
}
console.log(body);
// body will like following:
// { ok: true,
// id: 'jack johns',
// rev: '1-610953b93b8bf1bae12427e2de181307' }
});
});
});
useUpdateLink from code to database “myDatabase”
var doc = db.testdb.doc({});
// open to get revision or assign revision to the document
doc.open(function(err) {
doc.attach('plain.css', 'body { font-size:12pt; }', 'text/css');
// save the doc
doc.save(function(err, rs) {
var plain = doc.attachment('plain.txt');
// retrieve attachment
plain.get(function(err, body) {
assert.equal(body, 'body { font-size:12pt; }');
// update
plain.update('body { font-size:14pt; }', 'text/css', function(err) {
plain.get(function(err, body) {
assert.equal(body, 'body { font-size:14pt; }');
});
});
});
});