Skip to content

Instantly share code, notes, and snippets.

@tbranyen
Created May 16, 2012 19:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbranyen/2713395 to your computer and use it in GitHub Desktop.
Save tbranyen/2713395 to your computer and use it in GitHub Desktop.
Miso Project - Dataset: CouchDB Parser
// Underscore utility
var _ = require("underscore");
// Miso library
var Miso = require("miso.dataset");
Miso.Parsers.Couchdb = function(data, options) {};
_.extend(Miso.Parsers.Couchdb.prototype, {
parse: function(rows) {
var columns, valueIsObject;
var data = {};
// No data to process
if (!rows.length) {
return { columns: [], data: {} };
}
// If doc property is present, use this as the value
if (_.isObject(rows[0].doc)) {
// Iterate over every row and swap the doc for the value
_.each(rows, function(row) {
// Swap the value for the document
row.value = row.doc;
});
}
// Set columns based off the first row.
if (_.isObject(rows[0].value)) {
columns = _.keys(rows[0].value);
// Set this flag for assignment later on
valueIsObject = true;
// If the first row is not an object, use key/val
} else {
columns = ["key", "value"];
}
// Ensure each column has an array for data.
_.each(columns, function(column) {
data[column] = [];
});
// Iterate over every row and column and insert the data fetched
// correctly.
_.each(rows, function(row) {
_.each(columns, function(column) {
// Add to the respective column, if its not an object, use key/val
data[column].push(valueIsObject ? row.value[column] : row[column]);
});
});
// Expected format for dataset.
return {
columns: columns,
data: data
};
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment