The node-postgres module Module #60 Flashcards

1
Q

What is the process for installing the PostgreSQL node module?

A

As always npm init -y to create a blank package.json file and then ‘node install pg’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Sitting down to write the JS to connect to the DB, what is the first step?

A

You need to get the Pool object from pg using the require ( ) syntax. For example:

const { Pool } = require( ‘pg’ ) // this is for node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you create a pool instance?

A
const user = ''
const host = 'localhost'
const database = ''
const password = ''
const port = ''
const pool = new Pool({
  user,
  host,
  database,
  password,
  port
}) // don't forget to update the generic values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is a basic query performed?

A
(async ( ) => {
  const res = await pool.query('SELECT name FROM dogs')
  for (const row of res.rows) {
    console.log(row.name)
  }
})()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Does the ‘pg’ library support promises/callbacks

A

Yes it does, but async syntax is better

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are inserts performed?

A
const name = 'Roger'
const age = 8
try {
  await pool.query('INSERT INTO dogs VALUES $1, $2', name, age)
} catch(err) {
  console.error(err)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly