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:
Restaurant
Menu
Now that we have the starting point, let’s get to work coding it out! Use the following as a guide.
- Once the forked repository has been cloned, make sure to run the command below. This will install all dependencies.
npm install
- Additionally, you’ll need to create the following file in the root of the project: db.sqlite. This database file is considered sensitive in nature, so it will not be pushed up to a repository when the project is pushed.
- To run the tests, run
npm test
- 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!
Code in all the spots where it says TODO in the project (you can search across files to find the spots to edit!).
- In
db.js
, create a new sequelize connection. - In
/models/Restaurant.js
, define aRestaurant
model. TheRestaurant
model should have the following properties:
name
: a stringlocation
: a stringcuisine
: a string
- In
/models/Menu.js
, define aMenu
model. TheMenu
model should have the following properties:
title
: a string
- Now that we have our models defined, it is time to create some tests in
index.test.js
to verify that our models function as intended. Create tests that do the following:
- Can create a new
Restaurant
- Can create a new
Menu
- Can update a
Restaurant
instance - Can update a
Menu
instance - Can delete a
Restaurant
instance - Can delete a
Menu
instance
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"
We are now going to add associations and eager loading to the database we created in part 1.
- In
./models/index.js
, after therequire
s, but before themodule.exports
, associate the 2 models:
- Multiple
Menu
instances can be added to aRestaurant
.
- In
index.test.js
, create a test to account for the association
- Define an
Item
model! Here are the details:
- Create a new
Item
file in the models directory - The
Item
model should have the following properties:name
: a stringimage
: a stringprice
: a numbervegetarian
: a boolean
- Make sure to export the model and import it anywhere you need it.
- Create tests to verify you can perform CRUD operations using the
Item
model.
- In
./models/index.js
, associate theMenu
andItem
models:
- Multiple
Item
instances can be added to aMenu
. - Many
Menu
instances can have anItem
.
- Add another test to account for the association
- Add a test or two that eager loads the data.
- For example, find all Menu instance, and include their
Item
model