Node JS with mySQL using Sequelize and Express

ref – https://www.esparkinfo.com/node-js-with-mysql-using-sequelize-express.html

source download

setup MySQL

Make sure your mySQL is installed on a mac like this:
http://chineseruleof8.com/code/index.php/2016/05/17/install-mysql-and-use-terminal-to-use-it/’

When you set up your mySQL, create a database called testdb

Node JS

$ mkdir nodejs-express-sequelize-mysql
$ cd nodejs-express-sequelize-mysql

name: (nodejs-express-sequelize-mysql)
version: (1.0.0)
description: Node.js Rest Apis with Express, Sequelize & MySQL.
entry point: (index.js) server.js
test command:
git repository:
keywords: nodejs, express, sequelize, mysql, rest, api
author: esparkinfo
license: (ISC)

Setting Up Express JS Web Server

in the root directory, create server.js

Configuring MySQL Database & Sequelize

In the root directory, create a config folder.
In that folder, create db.config.js

Note – The first five parameters are intended for MySQL. The pool parameter is optional and is deployed in the Sequelize connection pool config. The parameters are explained below.

max -maximum number of connections permissible in a pool
min – minimum number of connections permissible in a pool
idle – maximum time, in terms of milliseconds, that a connection can be held idly before being released
acquire – maximum time, in terms of milliseconds, that the pool seeks to make the connection before an error message pops up on screen

Sequelize

In the root directory, create a models folder

In the models folder, create index.js by entering the following code :

models/index.js

In MySQL databases, this model represents tutorial tables. The columns are automatically generated, a few of which are id, description, createdAt, and published.

tutorial.model.js

Then in server.js, we require this model, and use

What happens is that in in model/index.js, we allocated a new Sequelize object and initialized it with a mysql database connection using our db configuration. We import this functionality into our server.js. We call the sync function with the force option set to true in order to DROP TABLE IF EXISTS before trying to create the table – if you force, existing tables will be overwritten.

You’ll be signed in as db user root with your password before this takes place, so make sure its all correct.

Controller

Routes import Controllers and Controller’s exported functions create, find, update, delete, etc.
Controller’s create will first error check all the incoming parameters, then uses model. Thus controllers are the middleman.

Routes

If the client sends a request for an endpoint via an HTTP request such as POST, DELETE, PUT, or GET, the user must determine how the server responds.

Such a response from the server is possible by setting up the below routes:

/api/tutorials: GET, POST, DELETE
/api/tutorials/:id: GET, PUT, DELETE
/api/tutorials/published: GET
To create a tutorial.routes.js inside the app/routes folder, the user performs the following steps:

In order to use routes, remember to put

in server.js, right above code for port.

Hence, our server.js has code for routes, which routes to middleman controller, which then uses models to make changes to the database.

In the end, your server should look like this:

server.js

Using Postman to test

In Postman, new tab:

POST on http://localhost:8080/api/tutorials

Body, raw, then select JSON.

In the body:

In your database terminal, to see what you’ve inserted, get all results:

select * from tutorials;

Get all tutorials

Get specific tutorial

GET on http://localhost:8080/api/tutorials/2

Finding All Tutorials where Title=”node”

GET on http://localhost:8080/api/tutorials?title=tut

Find all published posts

GET on http://localhost:8080/api/tutorials/published

Delete depending on request param id

DELETE on http://localhost:8080/api/tutorials/2