Today, we’re building a Sequelize DB for restaurants to organize their menus. We will be using this across the next few lessons, so make sure to save this in a useful location!
We’ll have 2 database models:
- Restaurants
- Menus
Now that we have the starting point, let’s get to work coding it out! Use the following as a guide.
- Download all depndencies by running
npm install
- Run
npm test
to run all the tests. - The test file is created, but each test is currently empty. Initially, you’ll get many errors and messages like
Test suite failed to run since there are missing parts
. Let’s get to that!
- Follow the TODOs in the project (you can search across files to find the spots to edit!)
- The connection will have to be instantiated using
new Sequelize()
- Define the models! Here are the details:
- The
Restaurant
model should have name location and cuisine properties, all of which are strings. - The
Menu
model should have title which is also a string.
- The
- At this point, we can start writing the tests! Fill out each of the tests in the suite, following the describe statement provided.
- Make sure to read the following tip regarding how to use the seed data.
You’ll notice that a file called seedData.js
has been imported on lines 3 to 6 at the top of index.test.js
. If you look at this data you will see that it is a list of objects that can be utilized when testing your project. For example, if you run the following you will get a value from the seedData.js
that can be utilized in your tests.
console.log(seedRestaurant[0]); // Returns "AppleBees"
git add .
,git commit -m “somemessage”
, andgit push
so we can see your work!
We are now going to add associations and eager loading to the database we created in part 1.
- Let’s start up the tests again with
npm test
. You should have a few passing from last time. - Let’s add a few tests using the guidelines in the next few sections.
- In
./models/index.js
, after the requires, but before themodule.exports
, associate the 2 models:- Multiple menus can be added to a Restaurant.
- Add a third test to account for the association
- Define the model! Here are the details:
- Create a new
Item
file in the models directory - The
Item
model should have name and image properties, both of which are strings - The
Item
model should also haveprice
(number) andvegetarian
(boolean) properties
- Create a new
- Make sure to export the model and import it anywhere you need it!
- Back in
./models/index.js
Associate theMenu
andItem
models- Multiple items can be added to a menu.
- Items can be added to many menus
- Add another test to account for the association
- Add a test or two that eager loads the data.
- For example, find all
Menus
and include theirItem
model
- For example, find all
git add .
,git commit -m “somemessage”
, andgit push
so we can see your work!