Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added INIT
Empty file.
13 changes: 7 additions & 6 deletions solutions/earthquake.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { Client } = require('pg');
const { Client } = require("pg");

/**
* Does the heavy lifting.
*
*
* Function is async so we can use await.
*/
async function run(mag) {
Expand All @@ -26,14 +26,15 @@ async function run(mag) {
const res = await client.query(query, [mag]);

// Print the results
console.log(`Earthquakes with magnitudes greater than or equal to ${mag}:\n`);
console.log(
`Earthquakes with magnitudes greater than or equal to ${mag}:\n`
);

for (let row of res.rows) {
console.log(`${row.name}: ${row.magnitude}`);
console.log(`${row.name}: ${row.magnitude}`);
}

await client.end();

} catch (err) {
console.log(err);
}
Expand All @@ -60,4 +61,4 @@ if (isNaN(mag)) {
usageExit();
}

run(mag);
run(mag);
27 changes: 13 additions & 14 deletions solutions/maketable.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { Client } = require('pg');
const { Client } = require("pg");

/**
* Does the heavy lifting.
*
*
* Function is async so we can use await.
*/
async function run() {
Expand All @@ -17,36 +17,35 @@ async function run() {
try {
// Connect to DB
await client.connect();

// Create table
await client.query(`CREATE TABLE IF NOT EXISTS Earthquake
(Name VARCHAR(20), Magnitude REAL)`);

// Clear out the table if it already existed
// Sometimes you want to do this, sometimes you don't.
await client.query('DELETE FROM Earthquake');
await client.query("DELETE FROM Earthquake");

// Insert new data
let data = [
["Earthquake 1", 2.2],
["Earthquake 2", 7.0],
["Earthquake 3", 1.8],
["Earthquake 4", 5.2],
["Earthquake 5", 2.9],
["Earthquake 6", 0.6],
["Earthquake 7", 6.6]
["Earthquake 1", 2.2],
["Earthquake 2", 7.0],
["Earthquake 3", 1.8],
["Earthquake 4", 5.2],
["Earthquake 5", 2.9],
["Earthquake 6", 0.6],
["Earthquake 7", 6.6]
];

const query = 'INSERT INTO Earthquake(Name, Magnitude) VALUES($1, $2)';
const query = "INSERT INTO Earthquake(Name, Magnitude) VALUES($1, $2)";
for (let record of data) {
await client.query(query, record);
}

await client.end();

} catch (err) {
console.log(err);
}
}

run();
run();