-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbook.js
47 lines (39 loc) · 1.1 KB
/
book.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
'use strict';
const _ = require('underscore');
//Database functions
const pg = require('pg');
let pool = new pg.Pool({
host: 'localhost',
database: 'booktown'
});
function runQuery(query, argsArray, callback) {
pool.connect((err, client, done) => {
if (err) {
//likely a connection error that will print to console.
done();
throw err;
}
client.query(query, argsArray, (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.
});
});
}
const yargs = require('yargs');
const args = yargs
.alias('a', 'action')
.demand('a')
.describe('a', 'action to take [list]')
.argv;
if (args.action === 'list') {
runQuery('select id, title from books', [], (err, results) => {
if (err) {
throw err;
}
console.log('id', 'title');
_.each(results.rows, (r) => {
console.log(r.id, r.title);
});
process.exit();
});
}