Created
May 16, 2012 19:46
-
-
Save tbranyen/2713395 to your computer and use it in GitHub Desktop.
Miso Project - Dataset: CouchDB Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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