-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtransactions1.js
54 lines (49 loc) · 1.69 KB
/
transactions1.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
'use strict';
const pg = require('pg');
const async = require('async');
const _ = require('underscore');
let pool = new pg.Pool({
host: 'localhost',
database: 'booktown'
});
function runQuery(query, callback) {
pool.connect((err, client, done) => {
if (err) {
//likely a connection error that will print to console.
done();
throw err;
}
client.query(query, (err, results) => {
done(); //call done to release the client to the connection pool.
callback(err, results); //make it the callers responsiblity for checking for query errors.
});
});
}
//Let's run all of the queries listed in the 'let's explore' section.
async.eachSeries([
'select * from books',
'select * from authors',
'select title, first_name, last_name from books join authors on books.author_id = authors.id',
"select title from authors join books on authors.id = books.author_id where authors.first_name = 'Mark' and authors.last_name = 'Lutz'",
'select title, id, isbn from editions join books on editions.book_id = books.id',
'select sum(cost * stock) as cost, sum(retail * stock) as retail_value from stock'
], (query, callback) => {
console.log(`Running query: ${query}`);
runQuery(query, (err, results) => {
if (err) {
console.error(`Error running query: ${err}`);
} else {
console.log('Rows from query');
_.each(results.rows, (r) => {
console.log(r);
});
}
callback();
});
}, (err) => {
if (err) {
throw err;
}
console.log('Finished running lets explore queries.');
process.exit();
});