Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.