Writing CouchDB Views using ClojureScript
My motivations for doing this were manifold:
- I quite enjoy using CouchDB, as its model and general philosophy meshes very naturally with my (and my tools’) disposition and the data I work with most often.
- The operational hassle associated with maintaining a Clojure view server (which Clutch provides) configuration alongside my CouchDB installs was always a hassle.
- I’ve been wanting to do more and more with Cloudant, but a Clojure view server is just not an option with a hosted database-as-a-service like that.
Feel free to go check out clutch-clojurescript: beat on it some, and let me know if it breaks on you. I would eventually like to fold it into Clutch proper. Beware some limitations though — repeated here from the README in part to draw attention to them:
- ClojureScript / Google Closure produces a very large code footprint, even for the simplest of view functions. This is apparently an item of active development in ClojureScript.
- To my surprise (and shock/horror), the version of Spidermonkey that is used by CouchDB (and Couchbase Single, and Cloudant) does not treat regular expression literals properly — they work fine as arguments, e.g.
string.match(/foo/), but e.g.
/foo/.exec("string")fails. Using the
RegExp()function with a string argument *does* work. This was reported a long time ago, but has had little attention (though I’m trying to stir it up a bit).
I’m hoping to get to the bottom of this sooner or later, but I wonder if it’d be worthwhile to change the ClojureScript reader to emit
(js/RegExp "foo")calls instead of
/foo/literals (and hope that gClosure doesn’t optimize the former into the latter)? After all, there’s lots of CouchDB deployments out there with apparently broken spidermonkey installs/configurations, and likely lots of other apps/servers/environments in similarly dire straits.
Finally, here are the slides from my talk at the BACUG (download/view PDF):
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)