Node.js/Express RestAPIs CRUD – Sequelize ORM – PostgreSQL

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

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.

Now getting started with PostgreSQL ->

Installation

Set up 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:

nodejs-express-restapis-sequelize-postgresql + project structure

Setting up NodeJs/Express project

Following the guide to create a NodeJS/Express project.

Install Sequelize with PostgreSQL:

-> package.json file:

Setting up Sequelize PostgreSQL connection

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

– Setup Sequelize-PostgreSQL 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 Node.js server, Logs ->

Use Postman to check results ->

– Post request:

nodejs-express-restapis-sequelize-postgresql + post-request

-> Sequelize’s Insert Logs:

-> PostgreSQL records:

nodejs-express-restapis-sequelize-postgresql + postgresql-records

– Get requests:

nodejs-express-restapis-sequelize-postgresql + get-all-customer

nodejs-express-restapis-sequelize-postgresql + get-a-customer

– Put request:

nodejs-express-restapis-sequelize-postgresql + put-a-customer

– Delete request:

nodejs-express-restapis-sequelize-postgresql + delete-a-customer

-> Sequelize’s select, update, delete Logs:

-> final PostgreSQL’s records:

nodejs-express-restapis-sequelize-postgresql + final-postgresql-records

Sourcecode

Nodejs-Express-Sequelizejs-PostgreSQL

By grokonez | July 10, 2018.



Related Posts


Got Something To Say:

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

*