Skip to content

Instantly share code, notes, and snippets.

View jinalshah999's full-sized avatar

Jinal Shah jinalshah999

View GitHub Profile
const productMssql = require('./product.mssql');
class product {
async getAllProducts(req, res) {
try {
const output = await productMssql.getAllProducts();
res.sendResponse(output);
}
catch (error) {
console.log(error);
res.sendError(global.HELPER.getException({ error, message: 'Error in getting products list.' }));
bindResponse(req, res, next) {
this.request = req;
res.sendResponse = (response) => {
res.json({
status: 'success',
data: response
});
};
res.sendError = (error) => {
if(error.httpCode) {
class Helper {
static getException({error, message}) {
if(error instanceof EXCEPTION) {
return error;
} else {
console.error(error);
console.error(error.stack);
return new global.EXCEPTION('UnKnownError', message);
}
}
const util = require('util');
const EXCEPTION_MESSAGES = require('./exceptions.json');
class Exception {
constructor(errorName, params) {
Error.captureStackTrace(this, Exception);
this.errorName = errorName;
this.params = params;
this.code = EXCEPTION_MESSAGES[errorName]['code'];
this.httpCode = EXCEPTION_MESSAGES[errorName]['httpCode'];
this.errorMessage = EXCEPTION_MESSAGES[errorName]['message'];
{
"InvalidAuthRequest": {
"code": "1001",
"httpCode": "404",
"message": "",
"sendNotification": false,
"log": "log"
},
"InvalidPassword": {
"code": "1003",
class Response {
constructor(app) {
app.use((req,res,next) => this.bindResponse(req,res,next));
}
bindResponse(req,res,next) {
this.request = req;
res.sendResponse = (response) => {
res.json({
status: 'success',
data: response
class ProductMSSql {
async getAllProducts() {
const res = await global.MSSQLConnection.request()
.execute("getAllProducts");
return res.recordset;
}
async addProduct(prod) {
const res = await global.MSSQLConnection.request()
.input("product_name", prod.product_name)
.input("product_price", prod.product_price)
const express = require('express');
global.CONFIG = require('./config/environment');
const bodyparser = require('body-parser');
const app = express();
async function init() {
const mssqlconnection = require('./utilities/mssqlconnection');
global.MSSQLConnection = mssqlconnection.getConnection(global.CONFIG.mssql);
app.use(bodyparser.json({ limit: '50mb' }));
const sql = require('mssql');
class MSSqlconnection {
async getConnection(dbConfig) {
try {
return await sql.connect({
user: dbConfig.user,
password: dbConfig.password,
server: dbConfig.server,
database: dbConfig.database,
port: dbConfig.port,
{
"server": {
"host": "0.0.0.0",
"port": "4001"
},
"mssql": {
"user": "sa",
"password": "sa",
"server": "localhost",
"database": "Demo",