Skip to content

Instantly share code, notes, and snippets.

@phred
Created October 5, 2009 19:18
Show Gist options
  • Save phred/202356 to your computer and use it in GitHub Desktop.
Save phred/202356 to your computer and use it in GitHub Desktop.
//
// Output a Couch view as CSV, fields ordered per the 'fields' local variable.
// This is designed for use with FoxyCouch, the RESTful Foxycart order manager.
//
// To install in a FoxyCouch[1] instance, use CouchApp to 'pull' the design documents
// and then make a 'lists' directory inside 'manager_screen'; then put the contents
// of this into 'orders_as_csv.js'. Then do a 'couchapp push'.
//
// (Yeah, I realize now that I should move the rest of FoxyCouch to CouchApp, because
// it rocks.)
//
// Invoke this list function for great justice as follows:
// http://localhost:5984/foxy-orders/_design/manager_screen/_list/orders_as_csv/orders_by_id
//
// You can tack view query parameters on the end of the URL, and this function will
// output the results of that query as CSV. Pretty neat, eh?
//
// See for more information:
// http://wiki.apache.org/couchdb/Formatting_with_Show_and_List
//
// [1] Foxy-on-the-couch is here:
// http://github.com/themancan/foxycouch/
//
function(head, row, req, row_info) {
var endl = String.fromCharCode(15) + String.fromCharCode(10); // Couch doesn't like \r\n
var fields = ["id", "customer_address1", "customer_city", "customer_country", "customer_email", "customer_first_name", "customer_ip", "customer_last_name", "customer_postal_code", "customer_state", "order_total", "processor_response", "product_total", "receipt_url", "ship_to_billing", "shipping_total", "subscription_end_date", "tax_total", "transaction_date"];
var join_field_name = "order_items";
var join_field_cols = ["id", "name", "price", "quantity", "weight", "code", "subscription_startdate", "category_description", "category_code", "delivery_type", "options"];
var format_csv_row = function (input_row) {
var to_string = function (val) {
return (typeof val == 'undefined' ? '' : '' + val);
};
return input_row.map(function (val) {
return '"' + to_string(val).replace(/(")/g, "$1$1") + '"';
}).join(",");
};
if (head) {
var output = [];
var format_header = function (val) {
return val.replace(/_/g, ' ');
}
for (var field in fields) {
output.push(fields[field]);
}
for (var col in join_field_cols) {
output.push(join_field_name.substr(0, join_field_name.length-1) + '_' + join_field_cols[col]);
}
return {body: ([output.map(format_header)].map(format_csv_row) + endl),
headers: {"Content-Type": "text/csv",
"Content-Disposition": "attachment; filename=orders.csv"}};
}
else if (row) {
var output = [], output_rows = [];
var doc = row.value;
var format_options = function(options) {
var options_output = [];
for (var option_key in options) {
var option = options[option_key];
options_output.push(option.name + ' = ' + option.value);
}
return options_output.join("; ");
}
for (var field in fields) {
output.push(doc[fields[field]]);
}
if (doc[join_field_name].length > 0) {
for (var ndx = 0; ndx < doc[join_field_name].length; ndx++) {
var join_output = [].concat(output);
var join_row = doc[join_field_name][ndx];
join_row['id'] = ndx;
join_row['options'] = format_options(join_row.options);
for (var col in join_field_cols) {
join_output.push(join_row[join_field_cols[col]]);
}
output_rows.push(join_output);
}
}
else {
output_rows.push(output);
}
return output_rows.map(format_csv_row).join(endl) + endl;
}
return {body: ''};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment