-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb-init.js
58 lines (50 loc) · 1.41 KB
/
db-init.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
"use strict";
const papa = require('papaparse');
const fs = require('fs');
//
// Read a text file form the file system.
//
function read (fileName) {
return new Promise((resolve, reject) => {
fs.readFile(fileName, "utf8",
function (err, textFileData) {
if (err) {
reject(err);
return;
}
resolve(textFileData);
}
);
});
};
//
// Helper function to import a CSV file.
//
function importCsvFile (filePath) {
return read(filePath)
.then(textFileData => {
const result = papa.parse(textFileData, {
header: true,
dynamicTyping: true,
});
return result.data;
});
};
function exportToMongoDB (db, collectionName, data) {
return data.reduce((prevPromise, row) => {
return prevPromise.then(() => {
return db[collectionName].insert(row);
});
}, Promise.resolve());
};
const mongo = require('promised-mongo');
const db = mongo("localhost:27017/weather_stations", ["daily_readings"]);
importCsvFile("/code/data/weather-stations.csv")
.then(data => exportToMongoDB(db, "daily_readings", data))
.then(() => db.daily_readings.createIndex({ Year: 1 }))
.then(() => db.daily_readings.createIndex({ Year: -1 }))
.then(() => db.close())
.catch(err => {
console.error("An error occurred.");
console.error(err.stack);
});