forked from klipfolio-decode/project-apr-2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.js
73 lines (62 loc) · 1.92 KB
/
schema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var papa = require('papaparse');
var mongo = require('mongodb').MongoClient;
var DBURL = "mongodb://localhost:27017/klipfolioDecodeDB"
module.exports = {
schema_id_to_json: function(data) { //pass data from schema object
return papa.parse("one, two, three \n left, middle, right");
},
data_to_header_array: function(data){
var parsed_data = papa.parse(data);
var header_list = parsed_data.data[0];
console.log("header list: " + header_list);
return header_list;
},
data_to_columns: function(data_csv) {
var json_data = papa.parse(data_csv);
column_array = json_data.data;
column_array.shift();
return column_array; //2D Array with columns and no headers
},
get_requested_columns: function(headers, csv_unparsed){
var csv_data = papa.parse(csv_unparsed).data;
// Need to pivot the data.
var full = [], row, column, values, result = [];
for (row = 0; row < csv_data.length; row++) {
values = csv_data[row];
for (column = 0; column < values.length; column++) {
if (row == 0) {
full.push([values[column]]);
} else {
full[column].push(values[column]);
}
}
}
// filter the list of selected headers.
for (var i = 0; i < full.length; i++) {
if (headers.indexOf(full[i][0]) >=0) {
result.push(full[i]);
}
}
console.log("result: " + result);
return result;
},
create_schema: function(column_data){ //column data created from get_requested_columns
return {
data: column_data,
type: "table",
style: {}
}
},
get_schema: function(id){
mongo.connect(DBURL,function(err,db){
db.collection("schemas").findOne({_id:id},function(err,rec){
if (err){
console.log(err);
}
console.log(rec.schema);
return rec.schema;
})
});
}
//get schema by id, return schema json object
};