Skip to content

Instantly share code, notes, and snippets.

@MoreOutput
Created June 26, 2013 03:31
Show Gist options
  • Save MoreOutput/5864557 to your computer and use it in GitHub Desktop.
Save MoreOutput/5864557 to your computer and use it in GitHub Desktop.
Playing around with nodes process -- a not very robust CLI script that will export CouchDB view as a static JSON file
/**
* View-to-JSON CLI script for Node, turns a CouchDB view into a static JSON file.
* Version 0.0.1
* Example Useage: node vwtojson.js username password view filename directory server port
*/
var fs = require('fs'),
cradle = require('cradle'),
cradleConnection,
database,
username = process.argv[2],
password = process.argv[3],
databaseName = process.argv[4],
view = process.argv[5],
outputFileName = process.argv[6], // Defaults to database_view.json
outputFileDir = process.argv[7], // Defaults to same directory as script
location = process.argv[8], // Server location for requests, defaults to 127.0.0.1
locationPort = process.argv[9]; // Defaults to the typical couch port, 5984
if (!location) {
location = '127.0.0.1';
}
if (!locationPort) {
locationPort = '5984'
}
cradleConnection = new(cradle.Connection)(location, locationPort, {
auth: {
username: username,
password: password
}
});
database = cradleConnection.database(databaseName);
database.view(view, function (e, res) {
if (!outputFileDir) {
outputFileDir = '';
}
if (!outputFileName) {
outputFileName = databaseName + '_' + view.replace('/', '_');
}
fs.writeFile(outputFileDir + outputFileName + '.json', JSON.stringify(res, null), function (err) {
if (outputFileDir === '') {
console.log('View Saved as JSON in scripts current directory');
} else {
console.log('View Saved as JSON in ' + outputFileDir);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment