lucianopereira86 / CRUD-NodeJS-Sequelize-Swagger-MySQL

Simple CRUD example with NodeJS, Sequelize, Swagger and MySQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Alt Text

CRUD-NodeJS-Sequelize-Swagger-MySQL

Simple CRUD example with NodeJS, Sequelize, Swagger and MySQL

Technologies:

  • NodeJS Express v4.16.4
  • Sequelize v4.38.0
  • Swagger UI v3.20.1
  • MySQL

Objective

The objective of this guide is to make a NodeJS Express web API connecting with a MySQL database by using the Sequelize ORM (Object-relational mapping). The Swagger UI will be used to test the API.

To run this project

  • Run the script located at "/docs/crud.sql" to create the database on MySQL Workbench or on another program of your preference.

  • Install the node modules running the comands below at the root folder:

    npm install
  • Run the API:
    npm run dev
  • Acess the URL:
    http://localhost:3000

Alt Text

How it works

The structure of the project is this:

Alt Text

The "server.js" file creates a MySQL pool to allow multiple connections:

Alt Text

The "/config/config.json" contains database connections for different environments. They are used by the "/models/index.js" file during the Sequelize initialization:

Alt Text

The "/models" folder contains the Sequelize table models. For example: the "profiles" model.

Alt Text

The "users" model makes reference to the "profiles" model:

Alt Text

The "/plugins/transaction.js" file is responsible for making the transaction's commit and rollback:

Alt Text

The "/controllers/users.js" has the GET/POST/PUT/DELETE methods:

Alt Text

About

Simple CRUD example with NodeJS, Sequelize, Swagger and MySQL


Languages

Language:JavaScript 68.9%Language:HTML 31.1%