The back end for an Object Relational Mapping (ORM) eCommerce site by modifying starter code using Express.js API to use Sequelize to interact with a MySQL database. Use Insomnia to check all routes.
npm init
MySQL2 CLI to connect to the SQL database and perform queries.
npm install --save mysql2
npm install sequelize
To hide password using .env file
npm install dotenv --save
To Install Insomnia Insomnia is not installed as a CLI but it it a program used for opening API routes.
To activate SQL Terminal.
mysql -u root -p
Run schema file in SQL server.
source db/schema.sql
Exit SQL Terminal.
exit
Run the following commands in the Node Terminal.
npm run seed
npm start
✅A functional Express.js. API in which a database name, MySQL username, and MySQL password to an environment variable.
✅Can connect to the database using Sequelize and run schema and seed commands.
✅Development database is created and is seeded with test data.
✅Command prompt invokes the application so the server is started and the Sequelize models are synced to the MySQL database
✅Open API GET
routes in Insomnia for categories, products, or tags so the data for each of these routes is displayed in a formatted JSON
GET
Categories
localhost:3001/api/categories
GET
Products
localhost:3001/api/products
GET
tags
localhost:3001/api/tags
✅Open API GET
routes to return a single category, a single product, and a single tag being tested in Insomnia
GET
Category by ID
localhost:3001/api/categories/1
GET
Products Get One
localhost:3001/api/products/1
GET
tags by ID
localhost:3001/api/tags/1
✅Test API POST, PUT, and DELETE routes in Insomnia to successfully create, update, and delete data in my database. Repeat the below walkthrough with Products & tags.
DELETE
Categories: Delete Category by ID
localhost:3001/api/categories/6
POST
Categories: Create Category
localhost:3001/api/categories
PUT
Categories: Update Category
localhost:3001/api/categories/6
ORM.create.seeds.schema.database.webm
ORM.eCommerce.Insomnia.Demo.webm
-
Category
-
id
-
Integer.
-
Doesn't allow null values.
-
Set as primary key.
-
Uses auto increment.
-
-
category_name
-
String.
-
Doesn't allow null values.
-
-
-
Product
-
id
-
Integer.
-
Doesn't allow null values.
-
Set as primary key.
-
Uses auto increment.
-
-
product_name
-
String.
-
Doesn't allow null values.
-
-
price
-
Decimal.
-
Doesn't allow null values.
-
Validates that the value is a decimal.
-
-
stock
- Integer.
*** Does** allow null values for deletion.
-
Set a default value of
10
. -
Validates that the value is numeric.
-
category_id
-
Integer.
-
References the
Category
model'sid
.
-
-
-
Tag
-
id
-
Integer.
-
Doesn't allow null values.
-
Set as primary key.
-
Uses auto increment.
-
-
tag_name
- String.
-
-
ProductTag
-
id
-
Integer.
-
Doesn't allow null values.
-
Set as primary key.
-
Uses auto increment.
-
-
product_id
-
Integer.
-
References the
Product
model'sid
.
-
-
tag_id
-
Integer.
-
References the
Tag
model'sid
.
-
-
-
Product
belongs toCategory
, andCategory
has manyProduct
models, as a category can have multiple products but a product can only belong to one category. -
Product
belongs to manyTag
models, andTag
belongs to manyProduct
models. Allow products to have multiple tags and tags to have many products by using theProductTag
through model.
Fill out the unfinished routes in product-routes.js
, tag-routes.js
, and category-routes.js
to perform create, read, update, and delete operations using your Sequelize models.
Note that the functionality for creating the many-to-many relationship for products has already been completed for you.
Hint: Be sure to look at the mini-project code for syntax help and use your model's column definitions to figure out what
req.body
will be for POST and PUT routes!