CouchDB with CouchRest in 5 minutes

The other night, during our monthly SDRuby meetup, lots of people were very interested in learning more about CouchDB and Ruby. I tried to show what Couch was all about but I didn’t have time to show how to use CouchDB with Ruby.
Here is me trying to do that in 10 minutes or less. I’ll assume you don’t have CouchDB installed.

Install CouchDB, if you are on MacOSX, you are in luck, download and unzip the standalone package called CouchDBX.
That’s it you have couch ready to go, press play and play with the web interface.

Next, let’s write a quick script. Let’s say we want to write a script that manages your contacts.

First, let’s install CouchRest:

$ sudo gem install couchrest

Now, let’s open a new file and write our script.

In line 4 and 5 we are just setting up the server(by default, localhost is being used). If the database doesn’t exist, it will get created.


DB = SERVER.database!('contact-manager')

Then, we define your ‘model’, we set the default database to use and define a list of properties. Properties are not required, but they generate getters and setters for you. They are also used to set default values and validate your model. Line 11 shows how to use an alias that will provider a getter and a setter for the property name and the alias name:

property :last_name, :alias => :family_name

Line 14 does something that might seem strange at first. We are casting the address property as an instance of the Address class. Here is what the implementation of the Address class could look like:

Address is just an instance of Hash with some extra methods provided by the CouchRest::CastedModel module. (If you wonder why it’s called CastedModel instead of the more grammatically correct CastModel, the answer is simple: I suck at English grammar :p )

So here is a quick example of how to use a ‘CastedModel’:

That’s part of what’s great with CouchDB, you don’t need to worry too much about storage. Just define your properties, cast to models if needed and save everything as a document.

For more examples checkout the CouchRest spec fixtures and the examples.

To learn more about couchdb, read the (free) online draft of the CouchDB book and of course you probably should read the CouchRest source on GitHub.

Similar Posts

, ,

  1. #1 by Sebastian - May 18th, 2009 at 01:05

    Seems simple and clean.

  2. #2 by Luigi Montanez - May 19th, 2009 at 13:34

    I’ve been thinking about using CouchDB for a CRM. A powerful feature of advanced CRMs is where the user is able to create custom fields (like for custom IDs from external sources). In the relational model, this is usually accomplished with a messy “custom_fields” table which inevitably results in funky behavior.

    But with CouchDB, it’s as simple as creating that new custom field on whichever document it belongs in. What’s the best way to accomplish that via CouchRest, when the ‘property’ directive seems to be somewhat static?

  3. #3 by Matt Aimonetti - May 19th, 2009 at 23:04

    @luigi, you don’t have to use the properties, you can just add/access the document as a hash and do something like:

    object['custom_field'] = ‘test’

  1. No trackbacks yet.

Comments are closed.