Example #1
0
function getDBConnectionFromConfig(dbConf, callback){
  var connection = new(cradle.Connection)(dbConf.host,dbConf.port, dbConf.options);

  connection.info(function (err, json) {
    if (err === null){
      console.log("Couchdb ("+json.version+") at "+dbConf.host+" connected.", json);
      var forenDb = connection.database(dbConf.dbname); 
      // Create the db if it doesn't exist.
      forenDb.exists(function (error, exists) {
              if (!exists) {
                forenDb.create( 
                  function(error, res){
                    if(error === null){
                      callback(null, forenDb, connection);
                    }else{
                      callback(error, null,null);
                    }
                  });
              }else callback(null, forenDb, connection);
      });
    }
    else{
      err.host = dbConf.host;
      callback(err, null);
      //console.log("info() failed to connect to couchdb : ", err);
    }
  });


}