Basic CouchDB Operations

Require the leanback Gem

require 'leanback'

Default bind address and port is localhost and 5984 respectively.
To login to CouchDB as an admin or non-admin user

hash = Couchdb.login(username = 'samson',password ='sam123') 
auth_session =  hash["AuthSession"]

Leanback uses CouchDB’s cookie authentication.

 Couchdb.login(username = 'samson',password ='sam123') 
#=>{"AuthSession"=>"b2JpOjRFQzgyQ0U0OlA54YsdHNo8altYRIkLVVeyvwah",
#       "Version"=>"1", "Path"=>"%2F"}

auth_session is the authentication token generated by CouchDB, to be used for making future requests on behalf of the authenticated user.

auth_session.inspect
#=> "b2JpOjRFQzgyQ0U0OlA54YsdHNo8altYRIkLVVeyvwah"

To add users to CouchDB see here.

To create a CouchDB database

Couchdb.create 'contacts',auth_session
# => {"ok"=>true}

If you are running an admin party (i.e. you have no admins) simply use the same method with no auth_session parameter. This works for all Leanback methods except when otherwise stated.
To create a database in admin party mode:

Couchdb.create 'contacts'
# => {"ok"=>true}

Delete a database

Couchdb.delete 'contacts',auth_session
# => {"ok"=>true}

Return a list of all Databases

Couchdb.all(auth_session)
# => ["maps", "inventory", "monitors", "contacts", "books"]

Create a new document

data = {:firstname => 'Linda', 
        :lastname =>'smith', 
        :phone => '212-234-1234',
        :email =>'john@mail.com'}
 
doc = {:database => 'contacts', :doc_id => 'Linda', :data => data}
Couchdb.create_doc doc,auth_session
 
# => {"ok"=>true, "id"=>"Linda", "rev"=>"1-6f16274513f51e922ff1f745452a92b6"}

This will create a new document in the ‘contacts’ database, with document id ‘Linda’.

Let’s update that document to change the email address

data = {:email => "linda@mail.com" }
doc = { :database => 'contacts', :doc_id => 'Linda', :data => data}   
Couchdb.update_doc doc,auth_session

Let’s add gender and age

data = {:age => "32", :gender => "female" }
doc = { :database => 'contacts', :doc_id => 'Linda', :data => data}   
Couchdb.update_doc doc,auth_session

To change phone# and add a fax number

data = {:phone => "718-234-2904",:fax => "646-309-4049" }
doc = { :database => 'contacts', :doc_id => 'Linda', :data => data}   
Couchdb.update_doc doc,auth_session

Let’s add twitter account, facebook account and website url

data = { :twitter => "http://twitter.com/#!/linda", 
          :faceboook => "http://facebook.com/linda", 
           :website => "http://linda-blogs.com" }
 
 doc = { :database => 'contacts', :doc_id => 'Linda', :data => data}   
 Couchdb.update_doc doc,auth_session

Retrieve/view the document

doc = {:database => 'contacts', :doc_id => 'Linda'}
Couchdb.view doc,auth_session
 
#=> {"_id"=>"Linda", "_rev"=>"5-f99fd63f2c784b5e2f7b7d92b2df9a1e", "firstname"=>"Linda", "lastname"=>"smith", 
# "phone"=>"718-234-2904", "email"=>"linda@mail.com", "age"=>"32", "gender"=>"female", "fax"=>"646-309-4049", "twitter"=>"http://twitter.com/#!/linda",
# "faceboook"=>"http://facebook.com/linda", "website"=>"http://linda-blogs.com"}

To edit the document and replace it with a new one

data = {:firstname => 'Linda', 
        :lastname =>'smith', 
        :email => 'linda@mail.com',
        :gender=>'female',
        :phone =>'718-245-5611',
        :_rev=>'5-f99fd63f2c784b5e2f7b7d92b2df9a1e'}
 
doc = {:database => 'contacts', :doc_id => 'Linda', :data => data}
Couchdb.edit_doc doc,auth_session
 
# => {"ok"=>true, "id"=>"Linda", "rev"=>"6-211ebb68bdd4ba8799387214b4a3b445"}

This replaces the existing document with a new one, the _rev number of the document must be included in the data.

Retrieve/view the document

doc = {:database => 'contacts', :doc_id => 'Linda'}
Couchdb.view doc,auth_session
 
#=> {"_id"=>"Linda", "_rev"=>"6-211ebb68bdd4ba8799387214b4a3b445", "firstname"=>"Linda", 
#  "lastname"=>"smith", "email"=>"linda@mail.com", "gender"=>"female", "phone"=>"718-245-5611"}

Delete the document

 doc = {:database => 'contacts', :doc_id => 'Linda'}
 Couchdb.delete_doc doc,auth_session
 
#=> {"ok"=>true, "id"=>"Linda", "rev"=>"7-a2cfbbb607a668c26256bc19ff270ecf"}

Delete the document using it’s revision number (_rev):

doc = {:database => 'contacts', 
        :doc_id => 'Linda', :rev => '2-e813a0e902e3ac114400ff3959a2adde'}
 
Couchdb.delete_rev doc,auth_session
 
#=> {"ok"=>true, "id"=>"Linda", "rev"=>"3-48580d1806983b32cb03f114efb064e3"}

Retrieve all documents in a database

docs = Couchdb.docs_from 'contacts',auth_session  
docs.each do |d| 
 puts d["_rev"]
 puts d["_id"]    
 puts d["firstname"]
 puts d["lastname"]
 puts d["email"] 
 puts d["phone"]
end
Premium Wordpress Plugin