Thursday, December 31, 2009

Announcing EventMachine CouchDB (em-couchdb)

I thought it will be a good idea to end the year with a bang.. So here is the announcement for an awesome client for CouchDB based on EventMachine.

People who follow me on twitter (@sai_venkat) know that I am crazy about things like EventMachine, node.js, eventlet and NoSql databases. This is one of my attempts to dive into the NoSql world.

I was looking for clients for CouchDB in Ruby and found most to be using Net/Http and blocking in nature. So I began my quest of writing an asynchronous non blocking awesome EventMachine based CouchDB client inspired by EventMachine::Redis client.

Here is a sample code to enjoy... It creates a database, saves a document inside it, reads the doc, deletes it and then deletes the database.

require "rubygems"
require "eventmachine"
require "../lib/em-couchdb"

# Need to write test for updating doc...
# But before that write example test framework :)
# Also need to implement get all docs in db
EventMachine.run do
couch = EventMachine::Protocols::CouchDB.connect :host => 'localhost', :port => 5986
couch.get_all_dbs {|dbs| puts dbs}
couch.create_db("test-project")
couch.get_all_dbs {|dbs| puts dbs}
couch.get_db("test-project") do |db|
puts db
couch.save(db["db_name"], {:name => "couchd", "description" => "awesome"}) do |doc|
couch.get(db["db_name"], doc["id"]) do |doc|
puts doc
couch.delete(db["db_name"], doc) do
couch.delete_db(db["db_name"]){
EventMachine.stop
}
end
end
end
end
end


The current version supports database manipulation like creation and deletion and document manipulation.

Looks cool but still there are things to improve (a lot of things actually). One to start off with is the Continuation Passing Style.

So the current response to em-couchdb from one of the committers of CouchDb is ...

"em-couchdb looks right-on. Streaming JSON parsing would let large views and docs be processed without memory bloat."

Please feel free to fork and contribute and happy hacking :).

The code is available at http://github.com/saivenkat/em-couchdb

No comments: