Loading CouchDB Views from Source Files

by Calvin on July 28, 2010

I’ve been doing a lot work with CouchDB lately for Socialytics, and which means writing map/reduce functions in JavaScript for building views.  I was beginning to have a healthy set of views, it made sense to have this code in version control and be able to load these views to a CouchDB instance via the command line.  Not finding anything like this on the interwebs, I decided to come with something on my own.

My first pass at it was a Ruby implementation, but was fragile since it found the map and reduce functions via regular expressions.  This past weekend I decided to give a go with a new implementation using Node.js.  Since I’m now using JavaScript, I no longer had to use regular expressions to sniff out the map/reduce functions – I can just load the scripts up as code.  Another additional benefit is that now the view code got parsed, so I find syntax errors before the are loaded to Couch.

Here’s the code.  The script expects a designs/ folder at the same level as the loader script, with a subfolder for each design document underneath.  A design folder should have a .js file for each view.  A view file looks like this:

design.view = {
    map: function(doc) {
        emit(doc.created_at, null);
    },
    reduce: '_count'
};

CouchDB document update handlers are supported as well by assigning a function to design.update.  Once your view files are ready, just run loader.js with the CouchDB database URL as the parameter:

node db/couchdb/loader.js http://localhost:5984/myDatabase

So what do you think?  Does something like this exist already?  Is there a better way to do this?

{ 1 comment }

Introducing Rack::CORS

June 9, 2010

Recently, I’ve been working on an HTML5 project that needed to need to retrieve data from a different origin, and decided to look at using CORS. CORS, or Cross-Origin Resource Sharing is a specification that allows web applications to make AJAX calls cross-origin without resorting to workarounds such as JSONP. Searching around, I found an [...]

Read the full article →

Setting up Virtual Hosts on Mac OSX

March 26, 2010

I’ve been juggling a few different web projects lately, and decided to setup different virtual hosts on my Mac so that I can easily work with them. Googling around gave me a lot of different answers, none of which seem to work completely. This is what finally worked for me (on Snow Leopard). First, add [...]

Read the full article →

Installing Native Gems with Custom Library Paths

September 6, 2009

A few weeks ago, I started using the Patron Gem for Skribit and ran into an issue on our CentOS production servers which uses a very old version of libcurl.  I got it working by compiling a new version of libcurl and building the Gem against those binaries.  Since I didn’t want to overwrite the [...]

Read the full article →

Switching from Windows to Mac: A Retrospective

July 8, 2009
Thumbnail image for Switching from Windows to Mac: A Retrospective

Just recently done a 2 week stint working on Windows while Apple Geniuses were working on my MBP, I thought it would be a good time to blog on my own Skribit Suggestion and reflect on my switch to Mac 14 months ago. Reasons for Switching Prior to my MBP, I’ve was working on a [...]

Read the full article →

Problems Uploading Files to WordPress?

June 25, 2009

A Google search seems to indicate that problems uploading files on WordPress installs is a pretty common occurrence.  Unfortunately, I had the hardest time finding the solution to my particular ailment.  Hopefully, this post will find those others who run into this problem in the future. From the WP Admin pages, go to Settings > [...]

Read the full article →

Creating Static Pages w/ Rails ActionViews

May 24, 2009

Recently, I needed to create some static reporting pages for Skribit.  From a quick search, I got a lot of results that talk about Rails and static pages, but none did exactly what I needed: To be able to generate pages with different paths from one URL Pages to persist across Rails deployments Not seeing [...]

Read the full article →

Building Software to their Audiences

February 24, 2009

When building software, it’s a good idea to identify the needs of 3 different kinds of audiences. The first 2 kinds are obvious ones.  We know to listen to what our End Users ask for, but not necessarily build everything they ask.  And we want to balance what the Business wants with when they want [...]

Read the full article →

Turning a New Page

November 15, 2008

It’s been a month now since I left my contract at AutoTrader.com.  For the most part, I’ve enjoyed my time there, but the thought of celebrating my two year anniversary there was a little frightening.  While I am very adaptable to the corporate life, it isn’t for me.  When FlickStation (the last startup I was [...]

Read the full article →

Growl Notifications for Ant

October 18, 2008

It’s a real refreshing change to be doing developing on Mac these days.  Currently, our Ant builds at work are less than optimal, taking ten’s of minutes to do a full build.  Fixing it is something we definitely want to do, but because of the complexity of the build and existing deadlines, right now isn’t [...]

Read the full article →