All Projects → tariqulislam → express-mysql-rest

tariqulislam / express-mysql-rest

Licence: other
Building the simple api with sequelize, mysql and express js. this repository contains the code about how to use sequelize with mysql at express js. for example i have provide the crud operation to this repository. You can also testing the api with chai and mocha with chai-http by this repository

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to express-mysql-rest

BotBlock.org
BotBlock - The List of Discord Bot Lists and Services
Stars: ✭ 29 (+16%)
Mutual labels:  mocha, expressjs, chai, chai-http
Typescript Restful Starter
Node.js + ExpressJS + Joi + Typeorm + Typescript + JWT + ES2015 + Clustering + Tslint + Mocha + Chai
Stars: ✭ 97 (+288%)
Mutual labels:  mocha, expressjs, sequelize, chai
TvrboReact
Dream starter project: React, Redux, React Router, Webpack
Stars: ✭ 13 (-48%)
Mutual labels:  mocha, expressjs, chai
nodejs-application-architecture
👨‍🔧 A discussion on how Node.js projects can be organized.
Stars: ✭ 81 (+224%)
Mutual labels:  mocha, sequelize, chai
Bombanauts
Bombanauts, inspired by the original Bomberman game, is a 3D multiplayer online battle arena (MOBA) game where players can throw bombs at each other, make boxes explode, and even other players!
Stars: ✭ 54 (+116%)
Mutual labels:  mocha, expressjs, chai
template-server-nodejs
No description or website provided.
Stars: ✭ 20 (-20%)
Mutual labels:  mocha, sequelize, chai
Tvrboreact
Dream starter project: React, Redux, React Router, Webpack
Stars: ✭ 13 (-48%)
Mutual labels:  mocha, expressjs, chai
minimal-hapi-react-webpack
Minimal Hapi + React + Webpack + HMR (hot module reloading) Sandbox
Stars: ✭ 55 (+120%)
Mutual labels:  mocha, nodemon, chai
api-skel
Webpack + Typescript + Express + Jest + Chai + Gulp
Stars: ✭ 18 (-28%)
Mutual labels:  mocha, expressjs, chai
nodejs-integration-testing
Integration testing of a Node.js / Express.js / Sequelize app
Stars: ✭ 23 (-8%)
Mutual labels:  mocha, sequelize, chai
wily
Build Node.js APIs from the command line (Dead Project 😵)
Stars: ✭ 14 (-44%)
Mutual labels:  mocha, sequelize
eslint-config-adjunct
A reasonable collection of plugins to use alongside your main esLint configuration
Stars: ✭ 39 (+56%)
Mutual labels:  mocha, chai
qa-automation-base
There are basic projects for automation frameworks based on Kotlin/Java and TypeScript for the backend, frontend, and mobile.
Stars: ✭ 45 (+80%)
Mutual labels:  mocha, chai
earl
☕ Ergonomic, modern and type-safe assertion library for TypeScript
Stars: ✭ 217 (+768%)
Mutual labels:  mocha, chai
dhiwise-nodejs
DhiWise Node.js API generator allows you to instantly generate secure REST APIs. Just supply your database schema to DhiWise, and a fully documented CRUD APIs will be ready for consumption in a few simple clicks. The generated code is clean, scalable, and customizable.
Stars: ✭ 224 (+796%)
Mutual labels:  expressjs, sequelize
KodersHub
CodeEditor Mern WebApp specifically designed for Kids and Teens🤩
Stars: ✭ 25 (+0%)
Mutual labels:  expressjs, chai
rest.api.test
Simple REST API Test Framework
Stars: ✭ 47 (+88%)
Mutual labels:  mocha, chai
todo-list
A practical web application built with Node.js, Express, and MySQL for you to readily record, view, and manage your tasks with an account: Create, view, edit, delete, filter, and sort expenses are as easy as pie 🥧
Stars: ✭ 18 (-28%)
Mutual labels:  expressjs, sequelize
opentelemetry-ext-js
js extensions for the open-telemetry project
Stars: ✭ 122 (+388%)
Mutual labels:  expressjs, sequelize
react-redux-immutable-webpack-ssr-starter
React + React-Router 4 + Redux + ImmutableJS + Bootstrap + webpack 3 with with Server side rendering, Hot Reload and redux-devtools STARTER
Stars: ✭ 21 (-16%)
Mutual labels:  mocha, chai

express-mysql-rest

Boilerplate for building the rest api with sequelize and mysql using express js. the repository will contains crud operation with mysql database using sequelize ORM.

For front end development purpose we need to example api server to design site or for practice of angular and react js or vue.js or other front end framework. we need to primary backend server. the repository is created for who are learning the server side development with node js and using as example api server for front and development. we can also use this for our production development purpose also.

Prerequisite

  1. Express JS
  2. mysql2
  3. sequelize-cli
  4. sequelize
  5. nodemon
  6. doenv

Installation

  1. clone the repository git clone https://github.com/tariqulislam/express-mysql-rest.git

  2. install the sequelize cli for support ORM command For npm package: npm install -g sequelize-cli For yarn package: yarn add global sequelize-cli

  3. run command for npm npm install and for yarn yarn install

  4. create database to mysql, if you use command line, command will be >mysql -u <username> -p <password> mysql> create DATABASE test_dev mysql> exit

  5. then use command for migrate the database 1. for Yarn command : yarn db:migrate 2. for npm command : npm run db:migrate

  6. For development purpose user command yarn start:dev

Predefiend api endpoint

  1. For consuming the get api or list for user [GET]http://localhost:3000/api/users
  2. For posting the data to api [POST]http://localhost:3000/api/users Request Body: { firstName: 'example name', lastName: 'example last name, email: 'example email' }

Create the New Model for Application

$ sequelize model:create --name User --attributes firstName:string, lastName:string, email:string

this command will create the model file with migration file at db folder.those are file name are based on model name 1. db/model/.js file 2. db/migration/ -create-user.js

Example of model file is

'use strict';
module.exports = (sequelize, DataTypes) => {
  var User = sequelize.define('User', {
    firstName: DataTypes.STRING,
    lastName: DataTypes.STRING,
    email: DataTypes.STRING
  }, {});
  User.associate = function(models) {
    // associations can be defined here
    User.hasMany(models.Comment,{
      foreignKey: 'userId',
      as: 'comments'
    });

  };
  return User;
};

Example of migration file is

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      firstName: {
        type: Sequelize.STRING
      },
      lastName: {
        type: Sequelize.STRING
      },
      email: {
        type: Sequelize.STRING
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Users');
  }
};

at migration two field are automatically added. those are createdAt and updatedAt field. then we need to migrate model to database just run the command yarn db:migrate or npm run db:migrate to create table at database and ORM mapping purpose.

Create the controller for Rest api

For controller we have to create the controller folder to root of the application. for example purpose users.js file is created. that controller control all the business logic of the application. We will export the different function from controller file.

For GET Api example code will be

  list(req, res) {
        return user
        .all()
        .then(users => res.status(200).json(ResponseFormat.build(
            users,
            "User Information Reterive successfully",
            200,
            "success"
        )))
        .catch(error => res.status(400).send(ResponseFormat.build(
            error,
            "Somthing went wrong when Reterieve Information",
            400,
            "error"
        )));
    }

For POST Api example code will be

 create(req, res) {
        return user
        .create({
            firstName: req.body.firstName,
            lastName: req.body.lastName,
            email: req.body.email
        })
        .then(user => res.status(201).json(ResponseFormat.build(
            user,
            "User Create Successfully",
            201,
            "success"
        )))
        .catch(error => res.status(400).json(ResponseFormat.error(
            error,
            "Something went wrong when create Users",
            "error"
        )))
    },

Routing the Api

For Api routing purpose i have added the index.js file to ```routes`` folder, and to routing the api we should use this code

const userController = require('../controllers').users
const commentController = require('../controllers').comments

module.exports = (app) => {
  app.post('/api/users', userController.create);
  app.get('/api/users', userController.list);
}

If you want to add the other controller to routes. just add the controller by Import the controller module:

const [ControllerName] = require(/* Import the file */).[Export Module Name]

then call the funtions inside the module.exports:

app.<REST API COSUME METHOD>(<API END POINT>, <CONTROLLER NAME>.<METHOD>)

It's so easy and minimal code and configuration to create the rest api with mysql and express. you can use this to your project for starter.

###sample test script with Mocha and Chai

  1. Create the folder test at root directory
  2. Create the file named user.js at test folder

For testing with Mocha and Chai we have just add the Mocha globally

> npm install -g mocha

At the user.js file i have provide you the sample testing script for api testing.

For testing purpose we have change Node js environment variable at user.js file

process.env.NODE_ENV = "test"

Then we have to add the model which will be use for testing

const User = require('../db/models').User;

Then add the chai and chai-http package for testing the api server. for testing with api server we need to add main js file app.js as server

const chai = require('chai');
const chaiHttp = require('chai-http');
const app = require('../app');
const should = chai.should();
chai.use(chaiHttp)

[GET] /users testing:

describe('/GET user', () => {
    it('it should Get all users', (done) => {
        chai.request(app)
        .get('/api/users')
        .end((err, res) => {
            res.should.have.status(200);
            res.body.should.be.a('object');
            done();
        });
    });
});

[POST] /users testing:

describe('/POST user', () => {
    it('it sould post the user info', (done) => {
        const user = {
            firstName: " Husne Ara",
            lastName: "Asma",
            email: "[email protected]"
        };

        chai.request(app)
        .post('/api/users')
        .send(user)
        .end((err, res) => {
            res.should.have.status(201);
            res.body.should.be.a('object');
            res.body.should.have.property('data');
            res.body.should.have.property('message');
            res.body.should.have.property('statusType').eq('success');
            done();
        });
    });
});

[PUT] /user Testing:

describe('/PUT/:id user', () => {
    it("should not update the user info", (done) => {
        const user = {
            firstName: "Mr.",
            lastName: "Himu",
        }
        const userId = 2;
         chai.request(app)
         .put('/api/users/'+ userId)
         .send(user)
         .end((err, res) => {
             res.should.have.status(404);
             res.body.should.be.a('object');
             res.body.should.have.property('message');
             res.body.should.have.property('statusType').eq('error');
             done();
         });
    });
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].