Skip to content

A skeleton express app that allows you to use ES6 syntax and super_controller and super_mysql lib. Generated by express-generator cli.

Notifications You must be signed in to change notification settings

imavinashsingh/superexpress.js

Repository files navigation

Super-express.js

An skeleton express app that allows you to use ES6 syntax with super_controller and super_mysql lib. Generated by express-generator cli.

How to run

Before anything else, you must have node installed on your machine.

Running Dev Server

Run on your terminal npm run watch:dev, the server will restart every time you make a change in your code.

Running Production Server

For stuff like Heroku deployment, AWS Elastic beanstalk, run npm run start

Other scripts

  • transpile - convert es6 and beyond code to es5 to a folder named dist-server
  • clean - delete transpiled folder
  • build - clean and transpile

Example Controller

import { validateBodyMiddleware } from "../middleware/userMiddleware";
import UserModel from "../models/UserExample";

const { Router, Get, sendSuccess, sendError } = require("../lib/super_router");

@Router('/user')
export default class UserController {
    constructor() {
        this.users = new UserModel();
    } 

    @Get('/',[
        validateBodyMiddleware
    ])
    async getUser(req, res, next) {
        try {
            const data = await this.users.getALLUsers().run(['1']);
            sendSuccess(res, data, null, 200);
        } catch (error) {
            sendError(res, error)
        }
    }
}

Example Model

import { MySQLModel, rawQuery ,sp} from "../lib/super_mysql";

@MySQLModel()
export default class UserModel{
    @rawQuery()
    getALLUsers(){
        return {
            query:"select * from user where id=?"
        }
    }

    @sp()
    getUser() {
        return {
            args: ``,
            query: `
      SELECT * FROM taptwointeract.user;
      `
        }
    }

    @sp()
    getUserDetails(){
        const data={
            args:`
            IN f_email text,
            IN f_user_id int(11)`,
            query:`select *
            from user u
            where 
            (case 
                when f_user_id is not null then u.id = f_user_id
                else u.email = f_email
            end);`
        };
        return data
    }

}

super_controller lib

This lib register each controller and its methods to express router with the help of decorators. So make code clean and easy to maintain


Available decorators are:

// This for annonate class
@Router(path,[middleware])

// This for annonate router class methods
@Get(path,[middleware])   

@Put(path,[middleware])

@Post(path,[middleware])

@Delete(path,[middleware])

It has two helpers function

sendSuccess(res, data, message?, status?);
sendError(res, message?,status?);

super_mysql lib

This lib register class and its methods with the help of decorators to run MySQL raw query and Store Procedure. Now we can manage SP same as raw query from model. This lib load all Store Procedure at time of starting of server


Available decorators are:

// This for annonate class
@MySQLModel()

// This for annonate router class methods
@sp()
//this method must return object as 
{
    args:'',
    query:'',
}

@rawQuery()
//this method must return object as 
{
    query:'',
}

About

A skeleton express app that allows you to use ES6 syntax and super_controller and super_mysql lib. Generated by express-generator cli.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages