Skip to content

Instantly share code, notes, and snippets.

import { StudentDA } from "../DA";
import { IStudent } from "../types/types";
import shortid from "shortid";
export class StudentService {
constructor(private studentDA: StudentDA) { }
public async GetStudents() {
try {
const data = await this.studentDA.GetStudents();
import { StudentService } from '../service';
import { Router, Response, Request } from 'express';
export const StudentRouter = (router: Router, service: StudentService): void => {
router.get('/', async (req: Request, res: Response) => {
try {
const data = await service.GetStudents();
res.status(200).send(data)
import express from 'express';
import dotenv from 'dotenv';
import { StudentDA } from './DA/index';
import { StudentService } from './service/index';
import { StudentRouter } from "./routes/index";
dotenv.config();
import { DBManager } from "./DBManager";
import { IStudent } from "../types/types";
export class StudentDA extends DBManager {
public async GetStudents() {
const query = "select * from student"
try {
const data = await this.ReadData(query);
return data;
Create table student(id varchar(10), name varchar(50), email varchar(100), age int(5));
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"sourceMap": true,
"outDir": "./build",
"rootDir": "./src",
/* Strict Type-Checking Options */
"strict": true,
/* Module Resolution Options */
PORT=3000
HOST='localhost'
USER='root'
PASSWORD='root'
DATABASE='onion'
import { MysqlError } from "mysql";
export interface IDBManager {
ReadData(query: string, paramCollection: (number | string | boolean)[]): Promise<any | MysqlError>
InsertOrUpdateData(query: string, paramCollection: (number | string | boolean)[]): Promise<any | MysqlError>
DeleteData(query: string, paramCollection: (number | string | boolean)[]): Promise<any | MysqlError>
}
import mysql from 'mysql';
import dotenv from 'dotenv'
dotenv.config();
const connection = mysql.createPool({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
import { IDBManager, MySqlType } from "../types/types";
import { connection } from "./DBConnection";
export class DBManager implements IDBManager {
public ReadData(query: string, paramCollection: (string | number | boolean)[] = []): Promise<MySqlType> {
return new Promise((resolve, reject) => {
connection.query(query, paramCollection, (err, result) => {
if (err) {
return reject(err)