CouchDB and data storage

Alexander Lang has written a great article about why CouchDB is not compatible with ActiveRecord, and why you should not try to coerce CouchDB into mimicking a relational database. It really is a very different thing altogether: The case of ActiveRecord vs. CouchDB

In my experience of CouchDB i first tried out ActiveCouch because of my familiarity with ActiveRecord. I soon came across problems because it was trying to make CouchDB something that it is not. As i exclaimed at the time, “LOL. ActiveRecord this is not!”

I had far more success with couchrest which is a much closer CouchDB wrapper, enabling CouchDB to be used as it’s intended: as a RESTful interface with map/reduce views.

Domain-Driven Design

Recently i have been reading Domain-Driven Design by Eric Evans. Through reading it my understanding of Rails – and web programming – has completely turned around. My thinking used to be entirely database-centric. I saw Rails as little more than an easy access into the database. For ages i didn’t even realise that you could have models that weren’t connected to a database table!

Now my thinking has changed and i consider the primary focus to be the domain model. I think about the classes and the design patterns that apply to them. I consider how they fit together, how they communicate with each other, and the boundaries between the core domain and subdomains. In my mind, the database has gone from being the most important thing to being just a method of persistence for the data in the domain model.

When you think of things this way round you are less likely to get hung up on the differences between ActiveRecord and CouchDB. You work out your domain, design the classes and then think about the most appropriate database platform to support your model.

Update: I’ve just come across a useful article that provides three methods to achieve a has_many relational structure in CouchDB. CouchDB “Joins” by Christopher Lenz.

Christmas cards: DONE!

Wow, what a relief! I finished writing my Christmas cards at 18:30 this evening. I am so glad of the idea to print labels. Thanks to Rails, CouchDB and my pdf_label_maker library, it was much easier than usual. I just sat down and forced myself to get through them all, pausing only once to take a photo:

Half way through

Note the chocolate, ruler and calculator, heheh! I actually bought some cheapo labels from the £1 shop … they weren’t even on an A4 sheet, which meant i had to do quite a lot of measuring and trial and error. But i’m so pleased with the result – they look so neat, as opposed to my handwriting which gets terrible whenever i have to write a lot in one go.

Christmas cards: DONE!

Yes, i actually did write a Christmas card to topfunky! ;)

The best thing is … if i keep the database updated during the year, i can print them all out again next year! I’m definitely going to keep developing it, and learning more about CouchDB on Rails in the process.

A hurrah moment!

Not really part of the “CouchDB on Rails” series, but i thought i’d post it anyway …

I have successfully entered the addresses for Christmas cards into my database and generated PDF output that should be of the right layout to print onto Avery labels. Sweet! The really awesome thing was, i started to forget that i was working with CouchDB and it just felt like any normal Rails project.

I only know two label formats right now, and neither of them are the one that i actually want to use.

L7162 – too big:
L7162 labels

J8651 – too small:
J8651 labels

But the cool thing is, i made a little library, pushed to GitHub of course! It is not CouchDB-specific. It’s probably not even Rails-specific, although the examples are. Perhaps some other people will contribute the dimensions of other label formats for me.

For those who want to know how i implemented this in the CouchDB application, here’s the commit.

For those who are interested in the PdfLabelMaker library, here it is: pdf_label_maker.

My thanks to Nick Sieger for giving me the example code to begin from.

Update 14th December 2008: Success! Labels printed and Christmas cards written! :) See this post for more.

CouchDB on Rails (part 8 of ?)

  1. An introduction to CouchDB
  2. Installing CouchDB
  3. Experimenting with CouchDB’s web interface
  4. Integrating with Rails using ActiveCouch
  5. Integrating with Rails using RelaxDB
  6. Getting to scaffolding using RelaxDB
  7. Installing CouchDB from Subversion source code
  8. Trying out couchrest and topfunky’s basic_model

Some time has gone by since i last used CouchDB and one method seems to have bubbled up to the top as a good one to use. It is couchrest, helped along by topfunky‘s basic_model wrapper. Today i am going to have a little play with them.

I have been meaning to get back into CouchDB but to be honest, i was tired of the old CD Collection database, and i was waiting for some inspiration. Today that inspiration arrived! I need to write my Christmas cards by the end of the week. I was impressed this afternoon by my auntie’s label printing system, and i decided i should do the same. I did some label printing last week at work, and i quite fancy making a nice little Ruby library for it.

I also think an address book is a pretty neat application for CouchDB as a document oriented database. It’ll work quite well for people who have multiple email addresses, or multiple telephone numbers. The document for each person can expand as big as it needs to be without wasting database space for the people who only have one phone number.

I need to print these address labels by the end of the week. You know that if a programmer has 100 hours to do something, they will generally spend 99 hours figuring out how to do it in 1 hour! Let’s go then! :D

Continue reading

CouchDB on Rails (part 7 of ?)

  1. An introduction to CouchDB
  2. Installing CouchDB
  3. Experimenting with CouchDB’s web interface
  4. Integrating with Rails using ActiveCouch
  5. Integrating with Rails using RelaxDB
  6. Getting to scaffolding using RelaxDB
  7. Installing CouchDB from Subversion source code
  8. Trying out couchrest and topfunky’s basic_model

Yesterday i started using the couchrest plugin, only to run into problems with the spec tests because it is built for latest latest CouchDB. So i had to install from Subversion source, but i got into a terrible mess, and to cut a long story short, i reinstalled the operating system today! I am now on shiny new Debian Lenny, woohoo! :D

So hopefully today i will successfully show you how to install from source!

Continue reading

CouchDB on Rails (part 6 of ?)

  1. An introduction to CouchDB
  2. Installing CouchDB
  3. Experimenting with CouchDB’s web interface
  4. Integrating with Rails using ActiveCouch
  5. Integrating with Rails using RelaxDB
  6. Getting to scaffolding using RelaxDB
  7. Installing CouchDB from Subversion source code
  8. Trying out couchrest and topfunky’s basic_model

Okay, after my brief disappointment earlier, i’ve found a way that seems to work, at least for the time being. I have taken the RelaxDB lines out of environment.rb and put them into the application controller instead.

Continue reading

CouchDB on Rails (part 5 of ?)

  1. An introduction to CouchDB
  2. Installing CouchDB
  3. Experimenting with CouchDB’s web interface
  4. Integrating with Rails using ActiveCouch
  5. Integrating with Rails using RelaxDB
  6. Getting to scaffolding using RelaxDB
  7. Installing CouchDB from Subversion source code
  8. Trying out couchrest and topfunky’s basic_model

So today i’m going to try using the RelaxDB plugin. I am fortunate in that there is a tutorial already written, which might make it easier. However, the tutorial is for Merb, so it might not work at all on Rails. We’ll see.

Continue reading

CouchDB on Rails (part 4 of ?)

  1. An introduction to CouchDB
  2. Installing CouchDB
  3. Experimenting with CouchDB’s web interface
  4. Integrating with Rails using ActiveCouch
  5. Integrating with Rails using RelaxDB
  6. Getting to scaffolding using RelaxDB
  7. Installing CouchDB from Subversion source code
  8. Trying out couchrest and topfunky’s basic_model

A grand moment has come. It is time to attempt to get Rails talking to my CouchDB database.

I have come across four plugins that i intend to try out. ActiveCouch, RelaxDB, couchrest and couchobject. They are all hosted on GitHub because GitHub is made of love.

Today i am going to try ActiveCouch. My main reason for picking it first is that it seems to be inspired by ActiveRecord. It is quite evidently intended for Rails. It seems quite close to what i’m used to. RelaxDB may also be, but it is written for Merb so i may run into troubles that are simply differences between Rails and Merb.

Continue reading