Skip to content

Instantly share code, notes, and snippets.

@jmakeig
Created March 28, 2017 23:33
Show Gist options
  • Save jmakeig/287482ce91cd97519d3ddeeb122548f7 to your computer and use it in GitHub Desktop.
Save jmakeig/287482ce91cd97519d3ddeeb122548f7 to your computer and use it in GitHub Desktop.
const table = fn.head(xdmp.documentGet(CSV_FILE)).toObject(); // string
const rows = table.split('\r\n'); // Parse lines into Array<string>
const headers = rows.shift().split(','); // Array<string>
// Array of functions used to parse column values
// Only need to specify the left-most columns the others will be skipped
const types = [
function(columnValue) {
return; /* parsed representation or undefined to skip */
}
// …one for each column
];
// Given an entity instance, what's its unique identifier?
const id = obj => obj.id;
for (const row of rows) {
const columns = row.split(',');
if (columns.length > 1) {
const obj = columns
// Allow for only specifying the left-most columns in the headers, ignore the rest
.slice(0, headers.length - 1)
// Aggregate a object with headers as keys and parsed columns as values
.reduce(
(obj, c, i) => Object.assign(obj, { [headers[i]]: types[i](c) }),
{}
);
// Multiple rows for each order to represent the order lines
xdmp.documentInsert(`/${entity}/${id(obj)}.json`, obj, {
collections: ['staging', entity]
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment