Prerequisites:
var Hapi = require('hapi'),
server = new Hapi.Server();
server.connection({
host: '0.0.0.0',
port: 8080
});
// Register plugin with some options
server.register({
plugin: require('hapi-couchdb'),
options: {
url: 'http://username:password@localhost:5984',
db: 'mycouchdb'
}
}, function (err) {
if(err) {
console.log('Error registering hapi-couchdb', err);
} else {
console.log('hapi-couchdb registered');
}
});
// Example of accessing CouchDb within a route handler
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
var CouchDb = request.server.plugins['hapi-couchdb'];
// Get a document from the db
CouchDb.Db.get('rabbit', { revs_info: true }, function(err, body) {
if (err) {
throw new Error(CouchDb.Error(error); // Using error decoration convenience method
} else {
reply(body);
});
}
});
server.start(function() {
console.log('Server running on host: ' + server.info.uri);
});
exports.register = function (server, options, next) {
var CouchDb = server.plugins['hapi-couchdb'];
// Do something cool with CouchDb here...
next();
};
var CouchDb = require('hapi-couchdb').init(options);
For example:
var CouchDb = require('hapi-couchdb').init({db: 'mycouchdb'});
// Do something cool with CouchDb here...
The configured database interface. More information available here.
Db.update(/** document id **/ id, /** updated document **/ doc, /** callback **/ callback);
A document update convenience method. It will create the document in the database if it doesn't exist.
Example usage:
Db.update(id, doc, function(error, body) {
if(error) {
console.log(error);
} else {
console.log('Document Updated! Current revision:', body._rev);
}
});
The full Nano interface. More information available here.
CouchDb.Error(/* CouchDb error response */ error);
A convenience method that will decorate an error returned by CouchDb using Boom for Hapi compatability. This function is only exposed if the module if used as a Hapi plugin.
// Example options
{
url: 'http://myusername:mypassword@localhost:5984', // CouchDb host
db: 'mycouchdb', // Database to use. *Required*
}
The CouchDb host to use. Defaults to http://localhost:5484
The name of the database you wish to use. This option is required.
Nano uses Request under the hood. You can use this configuration option to fine tune requests made by Nano to CouchDb. A full list of request options can be found here
MIT