An skeleton express app that allows you to use ES6 syntax with super_controller and super_mysql
lib. Generated by express-generator cli.
Before anything else, you must have node installed on your machine.
Run on your terminal npm run watch:dev
, the server will restart every time you make a change in your code.
For stuff like Heroku deployment, AWS Elastic beanstalk, run npm run start
transpile
- convert es6 and beyond code to es5 to a folder nameddist-server
clean
- delete transpiled folderbuild
- clean and transpile
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)
}
}
}
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
}
}
This lib register each controller and its methods to express router with the help of decorators. So make code clean and easy to maintain
// 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?);
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
// 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:'',
}