Ihavegotyou 发表于 2013-1-23 02:27:19

CouchDB's RESTful API

实验环境:   Ubuntu 7.10,  CouchDB 0.8.0, Curl in shell, Httpheader plugin for firefox
 
 

[*]CouchDB以及商业应用, Reference URL:  http://couchdb.apache.org/ &&http://wiki.apache.org/couchdb/CouchDB_in_the_wild
[*]启动和打开后台管理

[*]$ sudo /usr/local/bin/couchdb start
Apache CouchDB 0.8.0-incubating (LogLevel=info)
Apache CouchDB is starting.
[*]在browser(i.e. FF) 输入 http://localhost:5984/_utils/ http://dl.iteye.com/upload/attachment/155634/e3ae6d65-043a-3895-939d-b0d1cd32cc39.png


[*]www目录(作简单的测试,你可以在这里):  $  /usr/local/share/couchdb/www,   $ ls;
browse  CouchDbToDo  couch_tests.html  favicon.ico  grid.css  image  index.html  jquery.editable-1.3.3.js  myTodo.html  replicator.html  script  style  todo.css  todo.html

[*]CouchDB's javascript API
// Licensed under the Apache License, Version 2.0 (the "License"); you may not// use this file except in compliance with the License.You may obtain a copy// of the License at////   http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the// License for the specific language governing permissions and limitations under// the License.(function($) {$.couch = $.couch || {};$.fn.extend($.couch, {    allDbs: function(options) {      options = options || {};      $.ajax({      type: "GET", url: "/_all_dbs",      complete: function(req) {          var resp = $.httpData(req, "json");          if (req.status == 200) {            if (options.success) options.success(resp);          } else if (options.error) {            options.error(req.status, resp.error, resp.reason);          } else {            alert("An error occurred retrieving the list of all databases: " +            resp.reason);          }      }      });    },    db: function(name) {      return {      name: name,      uri: "/" + encodeURIComponent(name) + "/",      compact: function(options) {          options = options || {};          $.ajax({            type: "POST", url: this.uri + "_compact",            contentType: "application/json",            dataType: "json", data: "", processData: false,             complete: function(req) {            var resp = $.httpData(req, "json");            if (req.status == 202) {                if (options.success) options.success(resp);            } else if (options.error) {                options.error(req.status, resp.error, resp.reason);            } else {                alert("The database could not be compacted: " + resp.reason);            }            }          });      },      create: function(options) {          options = options || {};          $.ajax({            type: "PUT", url: this.uri, contentType: "application/json",            dataType: "json", data: "", processData: false,             complete: function(req) {            var resp = $.httpData(req, "json");            if (req.status == 201) {                if (options.success) options.success(resp);            } else if (options.error) {                options.error(req.status, resp.error, resp.reason);            } else {                alert("The database could not be created: " + resp.reason);            }            }          });      },      drop: function(options) {          options = options || {};          $.ajax({            type: "DELETE", url: this.uri, dataType: "json",            complete: function(req) {            var resp = $.httpData(req, "json");            if (req.status == 200) {                if (options.success) options.success(resp);            } else if (options.error) {                options.error(req.status, resp.error, resp.reason);            } else {                alert("The database could not be deleted: " + resp.reason);            }            }          });      },      info: function(options) {          options = options || {};          $.ajax({            type: "GET", url: this.uri, dataType: "json",            complete: function(req) {            var resp = $.httpData(req, "json");            if (req.status == 200) {                if (options.success) options.success(resp);            } elseif (options.error) {                options.error(req.status, resp.error, resp.reason);            } else {                alert("Database information could not be retrieved: " +                  resp.reason);            }            }          });      },      allDocs: function(options) {          options = options || {};          $.ajax({            type: "GET", url: this.uri + "_all_docs" + encodeOptions(options),            dataType: "json",            complete: function(req) {            var resp = $.httpData(req, "json");            if (req.status == 200) {                if (options.success) options.success(resp);            } else if (options.error) {                options.error(req.status, resp.error, resp.reason);            } else {                alert("An error occurred retrieving a list of all documents: " +                  resp.reason);            }            }          });      },      openDoc: function(docId, options) {          options = options || {};          $.ajax({            type: "GET",            url: this.uri + encodeURIComponent(docId) + encodeOptions(options),            dataType: "json",            complete: function(req) {            var resp = $.httpData(req, "json");            if (req.status == 200) {                if (options.success) options.success(resp);            } else if (options.error) {                options.error(req.status, resp.error, resp.reason);            } else {                alert("The document could not be retrieved: " + resp.reason);            }            }          });      },      saveDoc: function(doc, options) {          options = options || {};          if (doc._id === undefined) {            var method = "POST";            var uri = this.uri;          } else {            var method = "PUT";            var uri = this.uri+ encodeURIComponent(doc._id);          }          $.ajax({            type: method, url: uri + encodeOptions(options),            contentType: "application/json",            dataType: "json", data: toJSON(doc),            complete: function(req) {            var resp = $.httpData(req, "json")            doc._id = resp.id;            doc._rev = resp.rev;            if (req.status == 201) {                if (options.success) options.success(resp);            } else if (options.error) {                options.error(req.status, resp.error, resp.reason);            } else {                alert("The document could not be saved: " + resp.reason);            }            }          });      },      removeDoc: function(doc, options) {          options = options || {};          $.ajax({            type: "DELETE",            url: this.uri + encodeURIComponent(doc._id) + encodeOptions({rev: doc._rev}),            dataType: "json",            complete: function(req) {            var resp = $.httpData(req, "json");            if (req.status == 200) {                if (options.success) options.success(resp);            } else if (options.error) {                options.error(req.status, resp.error, resp.reason);            } else {                alert("The document could not be deleted: " + resp.reason);            }            }          });      },      query: function(mapFun, reduceFun, language, options) {          options = options || {};          language = language || "javascript"          if (typeof(mapFun) != "string") {            mapFun = mapFun.toSource ? mapFun.toSource() : "(" + mapFun.toString() + ")";          }          var body = {language: language, map: mapFun};          if (reduceFun != null) {            if (typeof(reduceFun) != "string")            reduceFun = reduceFun.toSource ? reduceFun.toSource() : "(" + reduceFun.toString() + ")";            body.reduce = reduceFun;          }          $.ajax({            type: "POST", url: this.uri + "_temp_view" + encodeOptions(options),            contentType: "application/json",            data: toJSON(body), dataType: "json",            complete: function(req) {            var resp = $.httpData(req, "json");            if (req.status == 200) {                if (options.success) options.success(resp);            } else if (options.error) {                options.error(req.status, resp.error, resp.reason);            } else {                alert("An error occurred querying the database: " + resp.reason);            }            }          });      },      view: function(name, options) {          options = options || {};          $.ajax({            type: "GET", url: this.uri + "_view/" + name + encodeOptions(options),            dataType: "json",            complete: function(req) {            var resp = $.httpData(req, "json");            if (req.status == 200) {                if (options.success) options.success(resp);            } else if (options.error) {                options.error(req.status, resp.error, resp.reason);            } else {                alert("An error occurred accessing the view: " + resp.reason);            }            }          });      }      };    },    info: function(options) {      options = options || {};      $.ajax({      type: "GET", url: "/", dataType: "json",      complete: function(req) {          var resp = $.httpData(req, "json");          if (req.status == 200) {            if (options.success) options.success(resp);          } else if (options.error) {            options.error(req.status, resp.error, resp.reason);          } else {            alert("Server information could not be retrieved: " + resp.reason);          }      }      });    },    replicate: function(source, target, options) {      options = options || {};      $.ajax({      type: "POST", url: "/_replicate", dataType: "json",      data: JSON.stringify({source: source, target: target}),      contentType: "application/json",      complete: function(req) {          var resp = $.httpData(req, "json");          if (req.status == 200) {            if (options.success) options.success(resp);          } else if (options.error) {            options.error(req.status, resp.error, resp.reason);          } else {            alert("Replication failed: " + resp.reason);          }      }      });    }});// Convert a options object to an url query string.// ex: {key:'value',key2:'value2'} becomes '?key="value"&key2="value2"'function encodeOptions(options) {    var buf = []    if (typeof(options) == "object" && options !== null) {      for (var name in options) {      if (name == "error" || name == "success") continue;      var value = options;      if (name == "key" || name == "startkey" || name == "endkey") {          value = toJSON(value);      }      buf.push(encodeURIComponent(name) + "=" + encodeURIComponent(value));      }    }    return buf.length ? "?" + buf.join("&") : "";}function toJSON(obj) {    return obj !== null ? JSON.stringify(obj) : null;}})(jQuery);

[*]CouchDB's  RESTful  API
文档库操作

[*]

[*]查看所有库(描述信息)

[*]curl-X GET  "http://localhost:5984/_all_dbs" ;  Response:     ["survey","test_suite_db_b","test_suite_db","test_suite_db_a","fruit","test","todo"]

[*]查看单个库(描述信息)

[*]curl-X GET  "http://localhost:5984/test/";  Response: {"db_name":"test","doc_count":0,"doc_del_count":0,"update_seq":112,"compact_running":false,"disk_size":15889}

[*]创建

[*]curl-X PUT "http://localhost:5984/testdb";   Response:{"ok":true}

[*]删除

[*]curl-X DELETE "http://localhost:5984/testdb";  Response: {"ok":true}

[*]压缩

[*]curl-X POST http://localhost:5984/test/_compact  -H'Content-Type: application/json' -d '';   Response:{"ok":true}

[*]库同步或复制
[*][分布式]查询(采用Map/Reduce)
[*]视图(即文档可视化)
[*]文档操作(对当前库的)

[*]查看所有文档

[*]curl -X GET "http://localhost:5984/test/_all_docs";   Response:
{"total_rows":1,"offset":0,"rows":[
{"id":"test","key":"test","value":{"rev":"4100948714"}},
{"id":"test2","key":"test2","value":{"rev":"107550922"}}
]}

[*]打开文档

[*]打开最新版本: curl-X GET "http://localhost:5984/fruit/apple?revs_info=true"; Response:
{"_id":"apple","_rev":"2755175234","_revs_info":[{"rev":"2755175234","status":"available"}]}
[*]打开一个历史版本: curl-X GET "http://localhost:5984/survey/_design/company?rev=2543967448";Response: {"_id":"_design\/company","_rev":"2543967448"}

[*]新增(更新)文档
[*]删除文档


页: [1]
查看完整版本: CouchDB's RESTful API