Getting started with MEAN (Mysql, Express, Angular 6, Node) Part-2

I am explaining about node and MySQL connections using sequelize.

You need to install Express, Sequelize, Body Parser, Express Handlebars, Cors and JWT using below command

npm i express sequelize mysql2 body-parser express-handlebars cors jsonwebtoken

Express – Node js framework.
Sequelize – ORM concept for Node.
Body Parser – It is used to handle the requests.
Cors – It is used to handle headers.
JWT – Json Web Token.

Install sequelize-cli using below command.

npm i sequelize-cli -g

Now you can initialize sequelize using below command.

sequelize init

Now you get below code structure.

sequelize stracture

Now you install the MySQL server. My recommendation is Install XAMPP (Download Here).

Change the database credentials in code structure config->config.json and add the below line in the development section.

“operatorsAliases” : false

My credientials are like below(only change in the development section).

mysql connections.PNG

Then Create Database using sequelize cli.

sequelize db:create

Then Create Models as you want. In this section am creating 2 models that are users and roles.

sequelize model:create –name roles –attributes role_name:string

sequelize model:generate –name user –attributes name:string,email:string,password:string,phone:string

Models and migration files created in your code base.

then open the migrations/xxxx.create-user.js file and add the below line in after updatedAt.

rolesId: {
       type:Sequelize.INTEGER
}

Then Write the relation between the two models.

First, open the models/user.js and write the below code.

user.associate = function(models) {
        user.belongsTo(models.roles);
};

Then Open models/roles.js and write the below code.

roles.associate = function(models) {
        role.hasMany(models.user);
};
Then Create the tables in Database using sequelize cli command below.
sequelize db:migrate
db tables.PNG
Thank you, in the next section, I will explain about node server creation and get data from database using sequelize models.

Git Links:

Front End: Click Here

BackendClick Here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
<span>%d</span> bloggers like this: