Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 722ec51

Browse files
committedMay 30, 2018
Improvements and update to chapter 3 code.
1 parent 061e38f commit 722ec51

26 files changed

+637
-83
lines changed
 

‎listing-1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var fs = require('fs');
3+
const fs = require('fs');
44
fs.readFile("./data/earthquakes.csv", "utf8",
55
(err, textFileData) => {
66
if (err) {

‎listing-9.js renamed to ‎listing-10.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var importCsvFile = require('./toolkit/importCsvFile.js');
3+
const importCsvFile = require('./toolkit/importCsvFile.js');
44

55
importCsvFile("./data/earthquakes.csv")
66
.then(data => {

‎listing-11.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎listing-12.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
const importCsvFromRestApi = require('./toolkit/importCsvFromRestApi.js');
4+
5+
const url = "https://earthquake.usgs.gov/fdsnws/event/1/query.csv?starttime=2017-01-01&endtime=2017-03-02";
6+
importCsvFromRestApi(url)
7+
.then(data => {
8+
console.log(data);
9+
})
10+
.catch(err => {
11+
console.error(err);
12+
});

‎listing-13.js renamed to ‎listing-14.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

3-
var mongo = require('promised-mongo');
4-
var importFromMongoDB = require('./toolkit/importFromMongoDB.js');
3+
const mongo = require('promised-mongo');
4+
const importFromMongoDB = require('./toolkit/importFromMongoDB.js');
55

6-
var db = mongo("localhost:6000/earthquakes", ["largest_earthquakes"]);
6+
const db = mongo("localhost:6000/earthquakes", ["largest_earthquakes"]);
77

88
importFromMongoDB(db, "largest_earthquakes")
99
.then(data => {

‎listing-15.js renamed to ‎listing-16.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

3-
var importFromMySql = require('./toolkit/importFromMySql.js');
4-
var mysql = require('nodejs-mysql').default;
3+
const importFromMySql = require('./toolkit/importFromMySql.js');
4+
const mysql = require('nodejs-mysql').default;
55

6-
var config = {
6+
const config = {
77
host: "localhost",
88
port: 5000,
99
user: "root",
@@ -13,7 +13,7 @@ var config = {
1313
debug: true
1414
};
1515

16-
var db = mysql.getInstance(config);
16+
const db = mysql.getInstance(config);
1717

1818
return importFromMySql(db, "largest_earthquakes")
1919
.then(data => {

‎listing-19.js renamed to ‎listing-20.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
var importCsvFile = require('./toolkit/importCsvFile.js');
4-
var exportJsonFile = require('./toolkit/exportJsonFile.js');
3+
const importCsvFile = require('./toolkit/importCsvFile.js');
4+
const exportJsonFile = require('./toolkit/exportJsonFile.js');
55

66
importCsvFile("./data/earthquakes.csv")
77
.then(data => exportJsonFile("./output/earthquakes.json", data))

‎listing-21.js renamed to ‎listing-22.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
var importCsvFile = require('./toolkit/importCsvFile.js');
4-
var exportCsvFile = require('./toolkit/exportCsvFile.js');
3+
const importCsvFile = require('./toolkit/importCsvFile.js');
4+
const exportCsvFile = require('./toolkit/exportCsvFile.js');
55

66
importCsvFile("./data/earthquakes.csv")
77
.then(data => exportCsvFile("./output/earthquakes-export.csv", data))

‎listing-23.js renamed to ‎listing-24.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use strict";
22

3-
var importCsvFile = require('./toolkit/importCsvFile.js');
4-
var exportToMongoDB = require('./toolkit/exportToMongoDB.js');
5-
var mongo = require('promised-mongo');
3+
const importCsvFile = require('./toolkit/importCsvFile.js');
4+
const exportToMongoDB = require('./toolkit/exportToMongoDB.js');
5+
const mongo = require('promised-mongo');
66

7-
var db = mongo("localhost:6000/earthquakes", ["largest_earthquakes_export"]);
7+
const db = mongo("localhost:6000/earthquakes", ["largest_earthquakes_export"]);
88

99
importCsvFile("./data/earthquakes.csv")
1010
.then(data => exportToMongoDB(db, "largest_earthquakes_export", data))

‎listing-25.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

‎listing-26.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"use strict";
22

3-
var importCsvFile = require('./toolkit/importCsvFile.js');
4-
var exportToMySql = require('./toolkit/exportToMySql.js');
5-
var mysql = require('nodejs-mysql').default;
3+
const mysql = require('nodejs-mysql').default;
64

7-
var config = {
5+
const config = {
86
host: "localhost",
97
port: 5000,
108
user: "root",
@@ -16,11 +14,16 @@ var config = {
1614
connectTimeout: 0,
1715
};
1816

19-
var db = mysql.getInstance(config);
17+
const db = mysql.getInstance(config);
2018

21-
importCsvFile("./data/earthquakes.csv")
22-
.then(data => exportToMySql(db, "largest_earthquakes_export", data))
19+
const createDbCmd =
20+
"create table largest_earthquakes_export ( Magnitude double, Time datetime, Latitude double, Longitude double, `Depth/Km` double )";
21+
22+
db.exec(createDbCmd)
23+
.then(() => {
24+
console.log("Database table created!");
25+
})
2326
.catch(err => {
24-
console.error("An error occurred.");
27+
console.error("Failed to create the database table.");
2528
console.error(err.stack);
2629
});

‎listing-27.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
3+
const importCsvFile = require('./toolkit/importCsvFile.js');
4+
const exportToMySql = require('./toolkit/exportToMySql.js');
5+
const mysql = require('nodejs-mysql').default;
6+
7+
const config = {
8+
host: "localhost",
9+
port: 5000,
10+
user: "root",
11+
password: "root",
12+
database: "earthquakes",
13+
dateStrings: true,
14+
debug: true,
15+
acquireTimeout: 0,
16+
connectTimeout: 0,
17+
};
18+
19+
const db = mysql.getInstance(config);
20+
21+
importCsvFile("./data/earthquakes.csv")
22+
.then(data => exportToMySql(db, "largest_earthquakes_export", data))
23+
.catch(err => {
24+
console.error("An error occurred.");
25+
console.error(err.stack);
26+
});

‎listing-3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var file = require('./toolkit/file.js');
3+
const file = require('./toolkit/file.js');
44

55
file.read("./data/earthquakes.csv")
66
.then(textFileData => {

‎listing-4.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const request = require('request-promise');
2+
3+
const url = "https://earthquake.usgs.gov" +
4+
"/earthquakes/feed/v1.0/summary/significant_month.geojson";
5+
request.get(url)
6+
.then(response => {
7+
console.log(response);
8+
})
9+
.catch(err => {
10+
console.error(err);
11+
});

‎listing-6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var importJsonFile = require('/toolkit/importJsonFile.js');
3+
const importJsonFile = require('./toolkit/importJsonFile.js');
44

55
importJsonFile("./data/earthquakes.json")
66
.then(data => {

‎listing-8.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
const importJsonFromRestApi = require('./toolkit/importJsonFromRestApi.js'); // Require our "importJsonFromRestApi" toolkit function.
4+
5+
const url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.geojson";
6+
7+
importJsonFromRestApi(url) // Use our toolkit function to import data from the REST API.
8+
.then(data => { // Callback to handle imported data.
9+
const earthquakes = data.features.map(feature => { // Restructure incoming data to the CDR.
10+
const earthquake = Object.assign({}, feature.properties, { id: feature.id });
11+
return earthquake;
12+
});
13+
console.log(earthquakes); // Print the data to the console so that we can verify it.
14+
})
15+
.catch(err => { // Handle any error that might have occurred.
16+
console.error("An error occurred.");
17+
console.error(err.stack);
18+
});

‎package-lock.json

Lines changed: 524 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dependencies": {
2020
"nodejs-mysql": "0.1.3",
2121
"papaparse": "4.3.3",
22+
"promised-mongo": "1.2.0",
2223
"request": "2.81.0",
2324
"request-promise": "4.2.1",
2425
"vagrant": "file:mongodb"

‎toolkit/exportCsvFile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use strict";
22

3-
var papa = require('papaparse');
4-
var file = require('./file.js');
3+
const papa = require('papaparse');
4+
const file = require('./file.js');
55

66
function exportCsvFile (fileName, data) {
7-
var json = papa.unparse(data);
7+
const json = papa.unparse(data);
88
return file.write(fileName, json);
99
};
1010

‎toolkit/exportJsonFile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

3-
var file = require('./file.js');
3+
const file = require('./file.js');
44

55
function exportJsonFile (fileName, data) {
6-
var json = JSON.stringify(data, null, 4);
6+
const json = JSON.stringify(data, null, 4);
77
return file.write(fileName, json);
88
};
99

‎toolkit/file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Toolkit functions for reading and writing files.
55
//
66

7-
var fs = require('fs');
7+
const fs = require('fs');
88

99
//
1010
// Read a text file form the file system.

‎toolkit/importCsvFile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"use strict";
22

3-
var papa = require('papaparse');
4-
var file = require('./file.js');
3+
const papa = require('papaparse');
4+
const file = require('./file.js');
55

66
//
77
// Helper function to import a CSV file.
88
//
99
function importCsvFile (filePath) {
1010
return file.read(filePath)
1111
.then(textFileData => {
12-
var result = papa.parse(textFileData, {
12+
const result = papa.parse(textFileData, {
1313
header: true,
1414
dynamicTyping: true,
1515
});

‎toolkit/importCsvFromRestApi.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"use strict";
22

3-
var request = require('request-promise');
4-
var papa = require('papaparse');
3+
const request = require('request-promise');
4+
const papa = require('papaparse');
55

66
function importCsvFromRestApi (url) {
77
return request.get({
88
uri: url,
99
json: false
1010
})
1111
.then(response => {
12-
var result = papa.parse(response, {
12+
const result = papa.parse(response, {
1313
header: true,
1414
dynamicTyping: true
1515
});

‎toolkit/importFromMongoDB.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var mongo = require('promised-mongo');
3+
const mongo = require('promised-mongo');
44

55
function importFromMongoDB (db, collectionName) {
66
return db[collectionName].find().toArray();

‎toolkit/importJsonFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var file = require('./file.js');
3+
const file = require('./file.js');
44

55
//
66
// Helper function to import a JSON file.

‎toolkit/importJsonFromRestApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var request = require('request-promise');
3+
const request = require('request-promise');
44

55
function importJsonFromRestApi (url) {
66
return request.get(url)

0 commit comments

Comments
 (0)
Please sign in to comment.