_temp_view support for alternate query languages, using content-type
[couchdbimport.git] / dist / common / ht_utils / couch.js
blobb203d5a0282629a80916804c739851475a81bdd3
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);
12 this.req.send(null);
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);
21 this.req.send(null);
22 if (this.req.status == 404)
23 return false;
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);
33 else
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.
40 doc._id = result._id;
41 doc._rev = result._rev;
42 return result;
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);
48 this.req.send(null);
49 if (this.req.status == 404)
50 return null;
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);
59 this.req.send(null);
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
64 doc._deleted = true;
65 return result;
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);
92 this.req.send("");
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);
100 this.req.send(null);
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) {
109 var query_str = ""
110 if (typeof optionsobj == "object") {
111 for(var option in optionsobj) {
112 if (typeof optionsobj[option] != "function") { //skip any methods
113 if (query_str == "")
114 query_str = "?" + option + "=" + this._json(optionsobj[option])
115 else
116 query_str = query_str + "&" + option + "=" + this._json(optionsobj[option])
120 return query_str;
123 this._json = function(obj) {
124 if (obj === null)
125 return "null"
126 else
127 return obj.toJSONString()