added raw function to query views
[outofuni/jsutils.git] / couchdb.js
1 /*
2  * couchdb.js - couchdb interface
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  */
7
8 var couchdb = {
9         type: "http",
10         host: "cdbsrv",
11         port: "80",
12         prefix: "",
13         init: function(type,host,port,prefix) {
14                 if(type!==undefined)
15                         couchdb.type=type;
16                 if(host!==undefined)
17                         couchdb.host=host;
18                 if(port!==undefined)
19                         couchdb.port=port;
20                 if(prefix!==undefined)
21                         couchdb.prefix=prefix;
22         },
23         cdb_xhr: function(type,url,data,cb) {
24                 var ao={
25                         url: url,
26                         type: type,
27                         success: function(ret) {
28                                 var ro=JSON.parse(ret);
29                                 if('error' in ro) {
30                                         switch(ro.error) {
31                                                 case "file_exists":
32                                                         cl("couchdb: exists - "+
33                                                            url);
34                                                         break;
35                                                 case "not_found":
36                                                         cl("couchdb: not found"+
37                                                            " - "+url);
38                                                         break;
39                                                 default:
40                                                         cl("couchdb: error - "+
41                                                            url);
42                                                         cl(ret);
43                                         }
44                                 }
45                                 else {
46                                         cl("couchdb: xhr success - "+url);
47                                 }
48                                 if(cb!==undefined)
49                                         cb(ro);
50                         },
51                         error: function(xhr,stat,err) {
52                                 cl("conn: "+url+", error: "+err+
53                                    ", stat: "+stat);
54                                 cl("executing callback anyways ...");
55                                 cb();
56                         }
57                 };
58                 if((data!==undefined)&&(data!==null))
59                         ao.data=JSON.stringify(data);
60                 $.ajax(ao);
61         },
62         item_count: function(db,callback) {
63                 var url=couchdb.type+"://"+couchdb.host+":"+couchdb.port+"/"+
64                         couchdb.prefix+db+"/_all_docs?limit=0";
65                 couchdb.cdb_xhr('GET',url,null,function(ret) {
66                         callback(ret.total_rows);
67                 });
68         },
69         get_item: function(db,key,callback) {
70                 var url=couchdb.type+"://"+couchdb.host+":"+couchdb.port+"/"+
71                         couchdb.prefix+db+"/"+key;
72                 couchdb.cdb_xhr('GET',url,null,function(ret) {
73                         callback(ret);
74                 });
75         },
76         get_db: function(db,callback) {
77                 var url=couchdb.type+"://"+couchdb.host+":"+couchdb.port+"/"+
78                         couchdb.prefix+db+"/_all_docs?include_docs=true";
79                 couchdb.cdb_xhr('GET',url,null,function(ret) {
80                         callback(ret);
81                 });
82         },
83         add_item: function(db,key,data,callback) {
84                 var url=couchdb.type+"://"+couchdb.host+":"+couchdb.port+"/"+
85                         couchdb.prefix+db+"/"+k;
86                 couchdb.cdb_xhr('PUT',url,data,function(ret) {
87                         callback(ret);
88                 });
89         }
90 };