2 // A simple class to represent a database. Uses XMLHttpRequest
3 // to interface with the CouchDb server.
5 function CouchDb(dbname
) {
6 this.dbUrl
= "/" + dbname
+"/";
7 this.req
= new XMLHttpRequest();
9 // Creates the database on the server
10 this.createDb = function() {
11 this.req
.open("PUT", this.dbUrl
, false);
13 if (this.req
.status
!= 201)
14 throw this.req
.responseText
.parseJSON();
15 return this.req
.responseText
.parseJSON();
18 // Deletes the database on the server
19 this.deleteDb = function() {
20 this.req
.open("DELETE", this.dbUrl
, false);
22 if (this.req
.status
== 404)
24 if (this.req
.status
!= 202)
25 throw this.req
.responseText
.parseJSON();
26 return this.req
.responseText
.parseJSON();
29 // Save a document to the database
30 this.save = function(doc
, options
) {
31 if (doc
._id
== undefined)
32 this.req
.open("POST", this.dbUrl
+ this.options_to_str(options
), false);
34 this.req
.open("PUT", this.dbUrl
+ doc
._id
+ this.options_to_str(options
), false);
35 this.req
.send(doc
.toJSONString());
36 if (this.req
.status
!= 201)
37 throw this.req
.responseText
.parseJSON();
38 var result
= this.req
.responseText
.parseJSON();
39 // set the _id and _rev members on the input object, for caller convenience.
41 doc
._rev
= result
._rev
;
45 // Open a document from the database
46 this.open = function(docid
, options
) {
47 this.req
.open("GET", this.dbUrl
+ docid
+ this.options_to_str(options
), false);
49 if (this.req
.status
== 404)
51 if (this.req
.status
!= 200)
52 throw this.req
.responseText
.parseJSON();
53 return this.req
.responseText
.parseJSON();
56 // Deletes a document from the database.
57 this.deleteDoc = function(doc
) {
58 this.req
.open("DELETE", this.dbUrl
+ doc
._id
+ "?rev=" + doc
._rev
, false);
60 if (this.req
.status
!= 202)
61 throw this.req
.responseText
.parseJSON();
62 var result
= this.req
.responseText
.parseJSON();
63 doc
._rev
= result
._rev
; //record rev in input document
68 this.bulk_save = function(docs
, options
) {
69 this.req
.open("POST", this.dbUrl
+ this.options_to_str(options
), false);
70 this.req
.send({_bulk_docs
:docs
}.toJSONString());
71 if (this.req
.status
!= 201)
72 throw this.req
.responseText
.parseJSON();
73 return this.req
.responseText
.parseJSON();
76 // Applies the map function to the contents of database and returns the results.
77 this.query = function(mapFun
, options
) {
78 this.req
.open("POST", this.dbUrl
+ "_temp_view" + this.options_to_str(options
), false);
79 this.req
.send(mapFun
.toSource());
80 if (this.req
.status
!= 200)
81 throw this.req
.responseText
.parseJSON();
82 return this.req
.responseText
.parseJSON();
85 this.view = function(viewname
, options
) {
86 return this.open(viewname
, options
);
89 // gets information about the database
90 this.info = function() {
91 this.req
.open("GET", this.dbUrl
, false);
93 if (this.req
.status
!= 200)
94 throw this.req
.responseText
.parseJSON();
95 return this.req
.responseText
.parseJSON();
98 this.all_docs = function(options
) {
99 this.req
.open("GET", this.dbUrl
+ "_all_docs" + this.options_to_str(options
), false);
101 if (this.req
.status
!= 200)
102 throw this.req
.responseText
.parseJSON();
103 return this.req
.responseText
.parseJSON();
106 // Convert a options object to an url query string.
107 // ex: {key:'value',key2:'value2'} becomes '?key="value"&key2="value2"'
108 this.options_to_str = function(optionsobj
) {
110 if (typeof optionsobj
== "object") {
111 for(var option
in optionsobj
) {
112 if (typeof optionsobj
[option
] != "function") { //skip any methods
114 query_str
= "?" + option
+ "=" + this._json(optionsobj
[option
])
116 query_str
= query_str
+ "&" + option
+ "=" + this._json(optionsobj
[option
])
123 this._json = function(obj
) {
127 return obj
.toJSONString()