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 grokonez | April 18, 2018.



Related Posts


5 thoughts on “Sequelize ORM – Build CRUD RestAPIs with NodeJs/Express, Sequelize, MySQL”

  1. Great article. I do not understand the part of the routes because in Express we usually import the Route () and use it like this:

    I also found the structure of server.js a bit different from an Express application, but it works very well.
    Would this be a more modern form of construction?

    1. Hello Julio,

      Please take a look at the overall design structure of Node.js project,

      -> With about organized structure, we create an easy way to develop big Node.js projects, more easy way to extends functions with seperated controller & router files. It also a design to reduce many codes for server.js.

      With mini project, you can develop restAPIs right in server.js -> BUT not good for real development.

      Here is an option for your development.

      Thanks & Regards,
      JSA

Got Something To Say:

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

*