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:
1 2 3 4 5 6 |
|
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?