We have a new wiki. The migration is not 100% complete. You can help out by moving pages across. This wiki will exist for as long as there are pages left.

The official documentation has moved to http://docs.couchdb.org — The transition is not 100% complete, but http://docs.couchdb.org should be seen as having the latest info. In some cases, the wiki still has some more or older info on certain topics inside CouchDB.

You need to be added to the ContributorsGroup to edit the wiki. But don't worry! Just email any Mailing List or grab us on IRC and let us know your user name.

Getting started with OCaml and the CouchDB API

For making the HTTP calls required by the RESTful API, it's hard to beat Gerd Stolpmann's Http_client.Convenience module (see http://docs.camlcity.org/docs/godipkg/3.11/godi-ocamlnet/doc/godi-ocamlnet/html/Http_client.Convenience.html ), shipped with the ocamlnet-2 library (see http://docs.camlcity.org/docs/godipkg/3.11/godi-ocamlnet ). The examples below assume your database is running on the localhost, port 5984--and that your database is named "mydatabase". Naturally, you're encouraged to change these to fit your specific needs.

Creating a Database

let result = 
  let server_url = "http://localhost:5984" in 
    let db_name = "mydatabase" in 
      Http_client.Convenience.http_put (server_url ^ "/" ^ db_name) "";;

print_endline result

Deleting a Database

let result = 
  let server_url = "http://localhost:5984" in 
    let db_name = "mydatabase" in 
      Http_client.Convenience.http_delete (server_url ^ "/" ^ db_name);;

print_endline result

Creating a Document

let result = 
  let url_doc = "http://localhost:5984/mydatabase/newdoc" in 
    let my_doc_json = "{ \"subject\":\"Plankton\", \"author\":\"Rusty\"," ^
                      "\"body\":\"I decided today I like plankton and OCaml equally well.\" }" in
      Http_client.Convenience.http_put url_doc my_doc_json;;

print_endline result

Retrieving a Document

The example below shows a bit more sophistication, illustrating how to use the json-wheel library (located at http://martin.jambon.free.fr/json-wheel.html).

type json doc = < subject:string; author:string; body:string > 
let url_doc = "http://localhost:5984/mydatabase/newdoc"
let get url =
       (Http_client.Convenience.http_get (url)))

let my_doc = (get url_doc)
let _= print_string ("Author: "  ^ my_doc#author  ^ "\n" ^ 
                     "Subject: " ^ my_doc#subject ^ "\n" ^ 
                     "Body: "    ^ my_doc#body    ^ "\n")

$ ./retrieve_doc

Author: Rusty
Subject: Plankton
Body: I decided today I like plankton and OCaml equally well.

Note: These examples can be compiled with the following:

ocamlfind ocamlopt -o dest-file -linkpkg -package json-static,netclient -syntax camlp4o source-file.ml

Getting_started_with_Objective Caml (last edited 2011-11-23 14:48:09 by JanLehnardt)