/* * couchdb.js - couchdb interface * * author: hackbard@hackdaworld.org * */ var couchdb = { type: "http", host: "cdbsrv", port: "80", prefix: "", init: function(type,host,port,prefix) { if(type!==undefined) couchdb.type=type; if(host!==undefined) couchdb.host=host; if(port!==undefined) couchdb.port=port; if(prefix!==undefined) couchdb.prefix=prefix; }, cdb_xhr: function(type,url,data,cb) { var ao={ url: url, type: type, success: function(ret) { var ro=JSON.parse(ret); if('error' in ro) { switch(ro.error) { case "file_exists": cl("couchdb: exists - "+ url); break; case "not_found": cl("couchdb: not found"+ " - "+url); break; default: cl("couchdb: error - "+ url); cl(ret); } } else { cl("couchdb: xhr success - "+url); } if(cb!==undefined) cb(ro); }, error: function(xhr,stat,err) { cl("conn: "+url+", error: "+err+ ", stat: "+stat); cl("executing callback anyways ..."); cb(); } }; if((data!==undefined)&&(data!==null)) ao.data=JSON.stringify(data); $.ajax(ao); }, item_count: function(db,callback) { var url=couchdb.type+"://"+couchdb.host+":"+couchdb.port+"/"+ couchdb.prefix+db+"/_all_docs?limit=0"; couchdb.cdb_xhr('GET',url,null,function(ret) { callback(ret.total_rows); }); }, get_item: function(db,key,callback) { var url=couchdb.type+"://"+couchdb.host+":"+couchdb.port+"/"+ couchdb.prefix+db+"/"+key; couchdb.cdb_xhr('GET',url,null,function(ret) { callback(ret); }); }, get_db: function(db,callback) { var url=couchdb.type+"://"+couchdb.host+":"+couchdb.port+"/"+ couchdb.prefix+db+"/_all_docs?include_docs=true"; couchdb.cdb_xhr('GET',url,null,function(ret) { callback(ret); }); }, add_item: function(db,key,data,callback) { var url=couchdb.type+"://"+couchdb.host+":"+couchdb.port+"/"+ couchdb.prefix+db+"/"+k; couchdb.cdb_xhr('PUT',url,data,function(ret) { callback(ret); }); } };