addywaddy

addywaddy

addywaddy  //  

Sep 17 / 8:34am

CouchDB on Merb

I recently stumbled across this series of blog posts regarding couchdb and rails and, having a couple of small merb projects behind me, thought I'd find out how to integrate couchdb into a merb application.

NOTE: The following example application is built on merb 0.96 and DM 0.96.

So let's generate our new app and call it .. er .. 'cd_collection'.

    merb-gen app cd_collection
cd cd_collection

We're going to use datamapper for our ORM so first things first, in init.rb:

    use_orm :datamapper

Now run 'rake dm:db:database_yaml' on the command line and rename the template created in config/ database.yml. Edit this as follows:

    adapter:  couchdb
database: cd_collection
host:     127.0.0.1
port:     5984

The adapter tells datamapper to use the dm-couchdb-adapter gem which you should have if you installed dm-more.

 

Aimee's blog covers how to install couchdb but if you're on a mac and running leopard, there is a nice installer available here.
Start couchdb, click on the magnifying glass and a tab should open in your browser with the admin interface for couchdb. Create a database 'cd_collection'.

Let's generate a cd resource for our merb app:

    merb-gen resource cd

and edit the cd.rb model:

    property :id, String, :serial => true, :key => true, :field => :_id
property :rev, String, :field => :_rev
property :title, String

The RDoc for dm-couchdb-adapter mentions only 'property :name, String' but I couldn't get it properly working without the :key and :field attributes.

Now lets make our merb app aware of our cd resource in router.rb:

    r.resources :cds

Before we start our app, we need to add our fields to the forms created my our merb-gen resource command. Edit the 'new' and 'edit' views in cds/ to include:

    <%= partial(:form) %>

and then create a '_form.html.erb' containing:

    <%= text_field :title %>

While we're at it, let's change the index and show views to display our CDs' titles (I'll leave that up to you:-)). One more thing before we start our app. The views added by our resource generation contain methods (specifically the link, form and error helpers) that merb won't understand out of the box. Back to init.rb:

    dependencies "merb-assets", "merb_helpers", "dm-validations"

So let's start up our merb app and navigate to /cds. New -> Enter title -> Save ... Striker! Our first CD has been saved in couchdb. Couchdb uses uuids instead of autoincrement by default which may disappoint those of you wanting to see cd/1 after saving in the address bar.

 

So there we go. I've added 10 CDs now, and I'd like to differentiate them by artist. An artist can have many CDs ... Here's something quit cool about Datamapper. Lets say that you want to keep your CDs in couchdb but would rather have your artists in an relational database. Firstly we need to inform Datamapper about our additional 'repository' in database.yml:

    repositories:
sqlite:
adapter:  sqlite3
database: /path/to/cd_artists.sqlite3
host:     localhost

We can now generate an artist resource as we did for our cds previously. Open up artist.rb and add the following:

    property :id, Integer, :serial => true
property :name, String
has 0..n, :cds
def self.default_repository_name
:sqlite
end

We're telling our Artist model to use the repository named :sqlite. Our association however will look in the default repository which in our case is couchdb. Lets edit the views again for our artists and add an extra field to our cds '_form' partial:

    <%= text_field :title %>
<%= select :artist_id, :collection => Artist.all.map{|a| [a.id, a.name]} %>

So we can associate a CD to a particular artist. Likewise, we can edit our 'show' view for artists to list their CDs:

    <ul>
<% @artist.cds.each do |cd| %>
<li>
<%= cd.title %>
</li>
<% end %>
</ul>

We didn't need to migrate our CD model as couchdb doesn't require a set schema. We will need to now though to get our Artist model up and running so on the command line run 'rake dm:db:automigrate'. You should see CREATE TABLE "artists" somewhere there in the output.
OK. Lets start our app again. You should now be able to add artists and associate CDs to a particular artist. Me like!

Download CD Collection here

1 comment

Oct 10, 2008
Fronx said...
me like, too!

Leave a comment...

 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    Connect    twitter