Skip to content

Instantly share code, notes, and snippets.

@tochman
Last active April 7, 2020 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tochman/04b31e1f9206ae21ad5b4c4bad481f07 to your computer and use it in GitHub Desktop.
Save tochman/04b31e1f9206ae21ad5b4c4bad481f07 to your computer and use it in GitHub Desktop.

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.

In order to be able to create an instance of a book in our code, we want to experimeent with some SQL in our node console.

Once again, open your terminal and open up the console:

$ node
Welcome to Node.js v13.1.0.
Type ".help" for more information.
> const { pool } = require('./config.js')
undefined

Now, that we have the pool available, we can try to use the INSERT command and write a new book in out table. Execute this JavaScript code in you console.

> pool.query('INSERT INTO books (author, title) VALUES ($1, $2)', ['T. Ochman', 'Getting Started With NodeJS'], error => {})

If we now query the database for all books, we should see a new row.

> 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' │
│    2    │ 3  │  'T. Ochman'   │     'Getting Started With NodeJS'      │
└─────────┴────┴────────────────┴────────────────────────────────────────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment