Sunday, April 11, 2010

Spike node.couchapp.js

‹prev | My Chain | next›

Thanks to node.couch.js, I think I have finally exhausted the subject of the CouchDB changes API. Interesting stuff, especially coupled with node.js. At this point, I think I am just about ready to get started with actual work on my chain's purpose (a mechanism for maintaining recipes from last year's chain). But before I do...

I think I will spike on node.couchapp.js. Node.couchapp.js is aiming to be a couchapp implementation in node.js. I have no idea how complete an implementation it is, but it has one major advantage in my eyes over couchapp: it's written in javascript/node.js vs. python.

I clone the repository:
cstrom@whitefall:~/repos$ git clone git://
Initialized empty Git repository in /home/cstrom/repos/node.couchapp.js/.git/
remote: Counting objects: 70, done.
remote: Compressing objects: 100% (68/68), done.
remote: Total 70 (delta 22), reused 0 (delta 0)
Receiving objects: 100% (70/70), 14.81 KiB, done.
Resolving deltas: 100% (22/22), done.
It takes me a little bit of rooting about, but I finally determine that the script that needs to be executed is lib/bin.js:
cstrom@whitefall:~/tmp/test_node_couchapp_js$ node ~/repos/node.couchapp.js/lib/bin.js -h
Invalid Option: -h
cstrom@whitefall:~/tmp/test_node_couchapp_js$ node ~/repos/node.couchapp.js/lib/bin.js help
-d, --design :: File or directory for design document(s)
-t, --test :: Run tests.
-s, --sync :: Sync with CouchDB.
-c, --couch :: Url to couchdb.
The entire package appears to be installable via something called NPM (Node.js package Manager). I opt to avoid this (for now) as it seems to be very much a work in progress. Besides I am able to get the help output described in the node.couchapp.js README without NPM installation.

At this point, I am very much flying blind. I do not know which, if any of those options are required. I am not sure what the format of the design documents should be. I am not entirely clear what --sync should do, though I'd guess that it is analogous to couchapp's "push".

Well, what happens when I run the command without any options:
cstrom@whitefall:~/tmp/test_node_couchapp_js$ node ~/repos/node.couchapp.js/lib/bin.js
Hmm... It would seem nothing. A quick scan of the CouchDB log confirms this.

What happens when I supply a CouchDB url?:
cstrom@whitefall:~/tmp/test_node_couchapp_js$ node ~/repos/node.couchapp.js/lib/bin.js -c http://localhost:5984/seed
Nothing again.

Well what happens when I supply an empty design document directory along with a CouchDB URL?
cstrom@whitefall:~/tmp/test_node_couchapp_js$ node ~/repos/node.couchapp.js/lib/bin.js -d . -c http://localhost:5984/seed
Error: Cannot find module '/home/cstrom/tmp/test_node_couchapp_js'
at loadModule (node.js:574:15)
at require (node.js:700:12)
at Object.<anonymous> (/home/cstrom/repos/node.couchapp.js/lib/bin.js:22:16)
at Module._compile (node.js:721:23)
at node.js:749:20
at fs:51:23
at node.js:810:9
Hrm... Well how about an empty dot-JS file?
cstrom@whitefall:~/tmp/test_node_couchapp_js$ node ~/repos/node.couchapp.js/lib/bin.js -d foo.js -c http://localhost:5984/seed
Back to nothing.

Perhaps I need to tell it to sync?
cstrom@whitefall:~/tmp/test_node_couchapp_js$ node ~/repos/node.couchapp.js/lib/bin.js -d foo.js -c http://localhost:5984/seed -s
No design docs in provided modules!
Ah, now we're getting somewhere.

After a little fiddling around, I create a "design document" that looks like this:
show: "function() { return 'foo'; }"
Even with that, I still get:
cstrom@whitefall:~/tmp/test_node_couchapp_js$ cat foo.js 
show: "function() { return 'foo'; }"
cstrom@whitefall:~/tmp/test_node_couchapp_js$ node ~/repos/node.couchapp.js/lib/bin.js -s -d foo -c http://localhost:5984/seed
No design docs in provided modules!
I have to call it a night at this point. Hopefully I will be able to make more progress on this tomorrow.

Day #70


  1. if you are hacking on this, I'd really like to see Node.js code that can push existing CouchApp source trees to CouchDB.

    Mikeal's node library has some cool features, but isn't aiming for direct interoperability with the filesystem format. Which is cool. But I'd also be really excited to see a node js version of

  2. Here are some example apps that written for node.couchapp.js

  3. @jchris I don't know that I'd really need that kind of interop. Still, seems like a good challenge, so who knows?

    @mikeal Thanks a ton. Those examples *really* helped the next night!