-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdbSetup.js
executable file
·96 lines (93 loc) · 2.24 KB
/
dbSetup.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const mongoose = require('mongoose');
const volunteer = require('./model/volunteer');
const election = require('./model/election');
const training = require('./model/training');
const position = require('./model/position');
const CONFIG = require('./config.json');
const bluebird = require('bluebird');
const connection = mongoose.connect(CONFIG.MONGO_URL);
const csv = require('fast-csv');
var electionData = [];
var trainingData = [];
var positionData = [];
csv
.fromPath("./csv/sites.csv")
.on("data", function(data){
electionData.push({
district:data[0],
site:data[1],
address:data[2]});
});
csv
.fromPath("./csv/training.csv")
.on("data", function(data){
trainingData.push({
day: data[0],
date: data[1],
area: data[2],
training: data[3],
site: data[4],
address: data[5],
city: data[6],
zip: data[7],
time: data[8],
isoTime: {
startTime: data[9],
endTime: data[10]
},
district: data[11],
coordinates: {
lat: data[12],
long: data[13]
}
});
});
csv
.fromPath("./csv/position.csv")
.on("data", function(data){
positionData.push({
position: data[0],
volunteerSite: data[1],
volunteerCount: data[2],
trainingNeeded: data[3]
});
});
mongoose.connection.once('open', function() {
Promise.all([
election.insertMany(electionData.map((element, index, array) =>{
return {
district: element.district,
site:element.site,
address: element.address
}
})),
position.insertMany(positionData.map((element, index, array) =>{
return {
position: element.position,
volunteerSite: element.volunteerSite,
volunteerCount: element.volunteerCount,
trainingNeeded: element.trainingNeeded
}
})),
training.insertMany(trainingData.map((element, index, array) =>{
return {
day: element.day,
date: element.date,
area: element.area,
training: element.training,
site: element.site,
address: element.address,
city: element.city,
zip: element.zip,
time: element.time,
isoTime: element.isoTime,
district: element.district,
coordinates: element.coordinates
}
}))
])
.then(function() {
mongoose.connection.close();
process.exit();
});
});