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

sequelize-orm-build-crud-restapis-with-nodejs-express-sequelize-mysql-feature-image

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.

Last updated on September 28, 2018.



Related Posts


13 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

  2. COOL GREAT, I WISH I COULD DONATE SOMETHING.

    However, with this Api, will a user be authenticated with a token before they can Post or Create something on the website ?

    For instance, if you have a website where user are authenticated before doing something, will this api apply or they are separately done? I wish we could talk more.

  3. Thank you so so much! You have helped me a lot.
    One question sir, how can I associate multiple models with this kind of setup?
    I’ve only seen examples of models being associated when they are on the same file.
    Please help me, sir. I’m totally lost.

  4. could you explain me this part i didn’t get it from database config file

Got Something To Say:

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

*