Once the db is created and populated with the 2 books, we want to test the connection in node console.
I your terminal, open the node console like this:
$ node
Welcome to Node.js v13.1.0.
Type ".help" for more information.
>
Then we want to load the db configuration:
> const { pool } = require('./config.js')
undefined
Then, we want to take a closer look at what pool
is:
> pool
BoundPool {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
options: {
connectionString: 'postgresql://api_user:password@localhost:5432/books_api',
ssl: false,
max: 10,
idleTimeoutMillis: 10000
},
log: [Function (anonymous)],
Client: [Function: Client] { Query: [Function: Query] },
Promise: [Function: Promise],
_clients: [],
_idle: [],
_pendingQueue: [],
_endCallback: undefined,
ending: false,
ended: false
}
We now want to query the db for all rows in the books
table:
> pool.query('SELECT * FROM books', (error, results) => { error ? console.log(error) : console.table(results.rows) })
undefined
> ┌─────────┬────┬────────────────┬────────────────────────────────────────┐
│ (index) │ id │ author │ title │
├─────────┼────┼────────────────┼────────────────────────────────────────┤
│ 0 │ 1 │ 'J.K. Rowling' │ 'Harry Potter' │
│ 1 │ 2 │ 'A. Lindgren' │ 'The Adventures of Pippi Longstocking' │
└─────────┴────┴────────────────┴────────────────────────────────────────┘
Now, IF there is an error, you will NOT see the table, but rather the error message.