Here we are building a backend using express and creating a database with mysql for an ecommerce app.
We use the schema.sql
file in the db
folder to create your database with MySQL shell commands. We use environment variables to store sensitive data like your MySQL username, password, and database name.
our database should contain the following four models, including the requirements listed for each model:
-
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.
-
Doesn't allow null values.
-
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
.
-
-
We need to execute association methods on your Sequelize models to create the following relationships between them:
-
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.
In the video below shows how to run th app from the terminal.
*navigate to the root folder of the app then run npmi to istall dependencies *enter mysql -u root -p to start up mysql *enter source schema.sql to create the database *enter quit to exit mysql *run npm run seed to seed the database *run npm start to run the app
We can then check our api routes using Postman
First walkthrough video
ecommerce.backend.vid.1.mp4
second walkthrough video
Checking the api routes