Use the Sequelize ORM to access a PostgreSQL databases Module#61 Flashcards

1
Q

How can we install the sequelize package via npm

A

First run npm init -y

npm install sequelize

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

What is the first step in using sequelize?

A

First make a connection to the DB as follows, update the default fields;

const user = ''
const host = 'localhost'
const database = ''
const password = ''
const port = ''
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When you specify “dialect” what are you telling Sequelize?

A

On this line we’re telling Sequelize what kind of database to expect.

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

What is the syntax for creating a new Sequelize object instance?

A
const sequelize = new Sequelize(database, user, password, {
  host,
  port,
  dialect: 'postgres',
  logging: false
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What do you need to do in order to manipulate a table

A

You need to create a model

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

How can we get data out of the table?

A

Use the .findAll( ) method

Dog.findAll()

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

When using the findAll( ) method it will return a list of rows. How can we access them?

A
Assign it to a variable called results:
const results = await Dog.findAll( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can we limit the columns we retrieve?

A

By passing an object with the attributes array:
Dog.findAll({
attributes: [‘age’]
})

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

What if you want to narrow the results even more, for example all dogs aged *

A

Add a WHERE clause, with your age parameter:

Dog.findAll({
  where: {
    age: 8,
  }
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

To get all dogs equal or higher to 5?

A

Use Op.gte

Dog.findAll({
  where: {
    age: {
      [Op.gte]: 5,
    }
  }
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is new data entered into our DB?

A

Using the .create( ) method

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

How do we update the data we already have?

A

Surprise! Using the .update( ) method. But it needs to get attached to the proper object you want to update.

Example:

Dog.update({
  age: 9
}, {
  where: {
    name: 'Roger'
  }
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens when we remove the where property?

A

It will update all rows instead of just a single row.

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