Sequelize ORM – Build CRUD RestAPIs with NodeJs/Express, Sequelize, MySQL

Sequelize is a promise-based ORM for Node.js v4 and later. In the tutorial, we will show how to build CRUD RestAPIs with NodeJs/Express, Sequelize, MySQL.

– Related posts:
Sequelize One-To-One association – NodeJS/Express, MySQL

Sequelize ORM

Sequelize is a promise-based Node.js ORM for Postgres, MySQL, SQLite and Microsoft SQL Server. It has many solid features for transaction, relations, read replication and more.

Getting started with MySQL:

Installation

Set up a connection

Sequelize model

Sequelize queries

– Find all:

– Find By ID:

– Update a Customer:

– Delete a Customer:

Practice

We create a NodeJS/Express-Sequelize project as below structure:

Setting up NodeJs/Express project

Following the guide to create a NodeJS/Express project.

Install Sequelize with MySQL:

-> package.json file:

Setting up Sequelize MySQL connection

– Create ‘./app/config/env.js’ file:

– Setup Sequelize-MySQL connection in ‘./app/config/db.config.js’ file:

Create Sequelize model

Express RestAPIs
Route

Define Customer’s routes in ‘./app/controller/customer.route.js’ file:

Controller

Implement Customer’s controller in ‘./app/controller/customer.controller.js’ file:

Server.js

Run & Check results

Start NodeJs server:

Use Postman to check result ->

– POST customers:

nodejs-express-restapis-sequelize-crud-post

nodejs-express-restapis-sequelize-crud-record-in-database

– GET all customers:

nodejs-express-restapis-sequelize-crud-get-all-customers

– GET a customer by id:

nodejs-express-restapis-sequelize-crud-get-a-customer-by-id

– PUT a customer:

nodejs-express-restapis-sequelize-crud-put-a-customer

– DELETE a customer by Id:

nodejs-express-restapis-sequelize-crud-delete-a-customer-with-id

Log from Server:

– Check customer table:

nodejs-express-restapis-sequelize-crud-check-database-customer-table

Sourcecode

Nodejs-Express-Sequelizejs-Mysql

By JavaSampleApproach | April 18, 2018.

Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*