public
Created

node.js script to process couchdb output

  • Download Gist
coucheval.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#!/usr/bin/env node
 
// Sample usage:
//
// $ curl -s 'http://127.0.0.1:15984/seeds/_all_docs?limit=10&include_docs=true' | coucheval.js 'console.log(JSON.stringify(row.doc));'
//
 
var carrier = require('carrier');
 
var stdin = process.openStdin();
stdin.setEncoding('utf8');
 
var couchparse = function(callback) {
var firstline = true;
carrier.carry(stdin, function(line) {
// skip first line
if (firstline) {
firstline = false;
return;
}
// skip last line
if (line.substr(0, 2) == "]}") {
return;
}
 
if (line[line.length-1] == ",")
line = line.substr(0, line.length-1);
callback(line);
});
};
 
couchparse(function(line) {
var row = JSON.parse(line);
eval(process.argv[2]);
});

nice work!

A small addition: on Mac OS X, the line that is parsed contains a '\r' at the end of each line. To make it work, I added:

    if (line[line.length-1] == "\r") {
        line = line.substr(0, line.length-1);
    }

just before the 'if (line[line.length-1] == ",")' part...

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.