w10D3 HW for Thursday - Using Sequelize Flashcards

1
Q

what’s a model file?

A

a file that’ll be used by the application to query (search/question) the database. (basically like a file that does a fetch to the database)

  • it’s used by the express application
  • it’s part of the running software
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what’s a migration file?

A

a file used to construct the database. It’s like a blueprint for what a table will look like.

  • only used by sequelize cli tool to build the database
  • it changes your db as your app grows
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are migration files different from model files?

A

Model files can be updated w/o having to remigrate and reseed our database.

Model files are usually used to change one row within a table in some way (ie. creating, reading, updating, deleting), while migrations are meant to build the table in the first place and seed files are meant to just add a bunch of rows at once.

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

How do seeder files interact with migration and model files?

A

They don’t directly.

Seeder files: keep them in mind when editing migration files.
- when making changes to migration files, be sure to unseed first!

  • As long as you seed a table AFTER you migrate it (b/c you can’t add rows to a table that doesn’t exist) it’ll be fine.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are seeder files?

A

Files where you input data

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

Why do we need a sequelize.close()?

A

This closes the connection to the database when done with it. we put it at the end of our promise.

ex. 
async function main() {
   ...
   }
   await sequelize.close();
}

main()

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

How do we save a new record with sequelize?

A
  1. we call the static build method on the Cat class (for ex.) with the desired values.
  2. we call the save method on the cat instance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what does .findByPk(1) do?

A

The findByPk method obtains only a single entry from the table, using the provided primary key. (in parenthesies)

Pk is primary key. 1 is the id of 1.

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

How do we read a record by primary key?

A

by using findByPk. be sure to use .close

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

How do we update a record?

A

.findByPk
update what you want
.save
.close

Be sure to save and close for the update to take effect!

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

How do we destroy a record?

A

by using .destroy

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

How can you make the create a one step process?

A

by calling the create class method:

in this example it 1) creates a cat instance and 2) saves the instance method

ex.
const { sequelize, Cat } = require(“./models”);

async function main() {
  const cat = await Cat.create({
    firstName: "Curie",
    specialSkill: "jumping",
    age: 4,
  });

console.log(cat.toJSON());

await sequelize.close();
}

main();

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

What’s the one step method to delete?

A

use the destroy class method directly:

this example does 1) fetch the record and 2) destroy the instance method

const { sequelize, Cat } = require(“./models”);

async function main() {
  // Destroy the Cat record with id #3.
  await Cat.destroy({ where: { id: 3 } });

await sequelize.close();
}

main();

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

What does JSON.stringify do?

A

it turns JS into a JSON string

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

What does JSON.parse do?

A

it turns JSON into JS

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

How do we use sequelize to find just one thing that matches a criteria? Ex. one cat name that matches.

A

use the .findAll class method

17
Q

How do we use sequelize to find one or another thing that matches a criteria? Ex. one cat name or another

A

use .findAll class method and where option. the where option tells sequelize to use a WHERE clause.

18
Q

how do you use findAll to find objects that don’t match your criteria?

A

use the Op object from sequelize.
use [Op.ne]: “Markov” for example to just find names that aren’t equal to “Markov”.

the Op.ne means the “not equal” operator

19
Q

what does ne stand for?

A

not equal

20
Q

what does Op.or do?

A

it allows us to find two diff things. here in the ex. below we’re looking to find cats that are either named “Markov” or are age 4

const { Op } = require(“sequelize”);
const { sequelize, Cat } = require(“./models”);

async function main() {
  // fetch cats with name == Markov OR age = 4.
  const cats = await Cat.findAll({
    where: {
      [Op.or]: [
        { firstName: "Markov" },
        { age: 4 },
      ],
    },
  });
  console.log(JSON.stringify(cats, null, 2));

await sequelize.close();
}

main();

21
Q

What does Op.gt and Op.lt do?

A

it helps us find things that are greater than or less than.

22
Q

What’s Op?

A

and object is a sequelize operator. it’s used to create more complex comparisons.

https://sequelize.org/v5/manual/querying.html#operators

23
Q

How do we find limiting results with sequelize?

A

with the limit query option we can limit how many results we want

with findOne we can find one result

24
Q

sequelize validation: how to limit amount of something?

A

use a len validation with args

[lowNum, highNum]

25
Q

What’s a race condition (transactions)?

A

the possibility that someone else modifies previously fetched data before you are finished using/updating it.

26
Q

What of the following (whether reading or writing) can take a transaction: tx option?

  • findByPk
  • findAll
  • save
  • create
  • destory
A

all of them!

27
Q

What’s the difference between type: sequelize.string/varchar and squelize.text?

A

string will convert to varchar and they both have more restrictive length (limited amount of text)

text will stay text (unlimited text)

28
Q

what’s the difference between create and build?

A

create is a 2 in 1 that creates and saves

build requires you to use a save as well in the statement

29
Q

What does the query .allPosts return?

A

an array of items

30
Q

what does the query .findByPk return?

A

a single item

31
Q

what does the query .findAll return?

A

all posts that have a trait that you’re looking for

32
Q

what does the query .findOne return?

A

a single item

33
Q

For a .findOne query do we need a where clause?

A

yes!