Last night I began my flirtation with node.js (with a decided bias towards CouchDB). I had based some of my exploration on the node-couch, which I found easy to follow along while trying to figure things out. Several folks suggested checking out node-couchdb, so let's give it a whirl...
I clone the repository into
~/repos
like all of my other code repositories:cstrom@whitefall:~/repos$ git clone git://github.com/felixge/node-couchdb.gitThe instructions for node-couchdb introduce me to the node.js location for libraries, which, for local users, are stored in :
Initialized empty Git repository in /home/cstrom/repos/node-couchdb/.git/
...
cstrom@whitefall:~$ mkdir ~/.node_librariesTo use that library in a node.js script, I need to assign a local variable to the result of a require statement:
cstrom@whitefall:~$ cd !$
cd ~/.node_libraries
cstrom@whitefall:~/.node_libraries$ ln -s ../repos/node-couchdb
// ...The
couchdb = require('node-couchdb/lib/couchdb'),
//...
node-couchdb
is the repository I just symlinked and lib/couchdb
point to a couchdb.js
in that directory. I am eager to explore that library to figure out how it returns an object assigned to the couchdb
variable, but I will likely leave that for another day. For now, I try to use the couchdb
variable to print out a list of all DBs, just as I did manually last night:varThe
sys = require('sys'),
couchdb = require('node-couchdb/lib/couchdb'),
client = couchdb.createClient(5984, 'localhost');
sys.puts(client.allDbs());
allDbs()
method definition does not contain a callback in the documentation, so I wonder if it will work like this:cstrom@whitefall:~$ ./local/bin/node ./tmp/node-couch.jsAh, it does not. It is beginning to dawn on me that everything needs a callback in node.js. I am not sure how many arguments are needed for
undefined
allDbs()
, but I would guess just a callback:varThe other request methods in node-couch have callbacks with two arguments: an error object (set only if an error occurs) and a data object. Above, I am assuming no errors. Running this script, I find:
sys = require('sys'),
couchdb = require('node-couchdb/lib/couchdb'),
client = couchdb.createClient(5984, 'localhost');
client.allDbs(function (er, data) {
sys.puts(data);
});
cstrom@whitefall:~$ ./local/bin/node ./tmp/node-couch.jsCool! That is a lot less code than I had to use last night, so node-couchdb is already a win.
eee,test,seed
Ooh! There is a
replicate()
method in node-couchdb! And it accepts options like {create_target:true}
? I cannot resist replication, but I was not even aware there was a {create_target:true}
—now I have to try this out. So I boot three of my VMs, then I modify my script to output all DBs on my localhost CouchDB instance and on one of my VMs:varRunning that script I find that the VM does not have a "seed" DB:
sys = require('sys'),
couchdb = require('node-couchdb/lib/couchdb'),
client = couchdb.createClient(5984, 'localhost'),
clienta = couchdb.createClient(5984, 'couch-011a.local'),
clientb = couchdb.createClient(5984, 'couch-011b.local'),
clientc = couchdb.createClient(5984, 'couch-011c.local');
client.allDbs(function (er, data) {
sys.puts(data);
});
clientc.allDbs(function (er, data) {
sys.puts(data);
});
cstrom@whitefall:~$ ./local/bin/node ./tmp/node-couch.jsNow, if
eee,test,seed
test
{create_target:true}
does what I think it does, then I ought to be able to replicate the "seed" DB onto clienta, then replicate it from clienta to clientb, and finally from clientb to clientc. Will this work?varIf it does work, the last line should include a "seed" DB in the output:
sys = require('sys'),
couchdb = require('node-couchdb/lib/couchdb'),
client = couchdb.createClient(5984, 'localhost'),
clienta = couchdb.createClient(5984, 'couch-011a.local'),
clientb = couchdb.createClient(5984, 'couch-011b.local'),
clientc = couchdb.createClient(5984, 'couch-011c.local');
client.allDbs(function (er, data) {
sys.puts(data);
});
clientc.allDbs(function (er, data) {
sys.puts(data);
});
client.replicate("seed", "http://couch-011a.local:5984/seed", {create_target:true});
clienta.replicate("seed", "http://couch-011b.local:5984/seed", {create_target:true});
clientb.replicate("seed", "http://couch-011c.local:5984/seed", {create_target:true});
clientc.allDbs(function (er, data) {
sys.puts(data);
});
cstrom@whitefall:~$ ./local/bin/node ./tmp/node-couch.jsDang. Checking the localhost log, I see:
eee,test,seed
test
test
[Fri, 26 Mar 2010 02:43:56 GMT] [debug] [<0.2440.0>] httpd 404 error response:I am not positive, but that trailing slash on "seed" seems wrong. I am not sure if the problem is that or just that the
{"error":"db_not_found","reason":"could not open http://couch-011a.local:5984/seed/"}
{create_target:true}
is not being honored. I'm at a bit of a loss at this point, so I will pick it back up starting here tomorrow.
Day #53
No comments:
Post a Comment