diff --git a/.gitignore b/.gitignore index 22f55adc5647206db11558139164c3deb77f5c01..aabbe8f9bb361ff227ab70ef580b77c5781d70fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.env + # compiled output /dist /node_modules diff --git a/sample.env b/sample.env new file mode 100644 index 0000000000000000000000000000000000000000..fb1d94a4064461dda99648860e5ed93740b3de88 --- /dev/null +++ b/sample.env @@ -0,0 +1,5 @@ +MSSQL_USER=sa +MSSQL_SERVER=localhost +MSSQL_DATABASE=test +MSSQL_PASSWORD=Pass@word +MSSQL_PORT=1433 \ No newline at end of file diff --git a/src/app.module.ts b/src/app.module.ts index 4c4e005f037367f155a66b1e26c0acc813c2f63a..f06a4bb5797a58390783ebce7e703b2f90530e7a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,11 +1,11 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; -import { DbModule } from './db/db.module'; +import { dbConnector, DbModule } from './db/db.module'; @Module({ imports: [DbModule], controllers: [AppController], - providers: [AppService], + providers: [AppService, dbConnector], }) export class AppModule {} diff --git a/src/app.service.ts b/src/app.service.ts index 02c7bbe270c16f6e3f1a5bb0414ab9ce89ff78c6..83f3450de0c6e4085bd96416a72ca7a5c7df2e94 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -1,10 +1,20 @@ import { Injectable, Inject } from '@nestjs/common'; import { MSSQL_CONNECTION } from './constants'; import * as CryptoJS from 'crypto'; - +import { dbConnector } from './db/db.module'; +import { ConnectionPool } from 'mssql'; @Injectable() export class AppService { - constructor(@Inject(MSSQL_CONNECTION) private conn: any) {} + dbConnection: ConnectionPool = null; + constructor(private conn: dbConnector) { + this.dbConnection = conn.getConnectionPool( + process.env.MSSQL_USER, + process.env.MSSQL_SERVER, + process.env.MSSQL_DATABASE, + process.env.MSSQL_PASSWORD, + parseInt(process.env.MSSQL_PORT), + ); + } // async mysqlCallStoredProcedure(msg: string) { //returns a promise that resolves to a result set on success @@ -23,7 +33,7 @@ export class AppService { async mssqlCallStoredProcedure(username: string) { //returns a promise that resolves to a result set on success - const db = await this.conn.connect(); + const db = await this.dbConnection.connect(); const res = await db.query(`EXEC get_user @Username = N'${username}'`); console.log(res); return { pass: res['recordset'][0]['Password'] }; @@ -58,7 +68,7 @@ export class AppService { } async getUsers(search: string, first: number, max: number) { - const db = await this.conn.connect(); + const db = await this.dbConnection.connect(); const res = this.parseQueryResponse( await db.query(`SELECT * FROM Master_user`), ); @@ -66,7 +76,7 @@ export class AppService { } async getUsersCount() { - const db = await this.conn.connect(); + const db = await this.dbConnection.connect(); const res = await db.query( `SELECT COUNT(UserKey) as count FROM Master_user`, ); @@ -74,7 +84,7 @@ export class AppService { } async getUserById(username: string) { - const db = await this.conn.connect(); + const db = await this.dbConnection.connect(); const res = this.parseQueryResponse( await db.query(`EXEC get_user @Username = N'${username}'`), ); @@ -82,7 +92,7 @@ export class AppService { } async getUserCredentials(username: string) { - const db = await this.conn.connect(); + const db = await this.dbConnection.connect(); const res = await db.query(`EXEC get_user @Username = N'${username}'`); const saltRounds = 1000; const cred_res = []; @@ -109,7 +119,7 @@ export class AppService { } async getTutorById(id: string) { - const db = await this.conn.connect(); + const db = await this.dbConnection.connect(); const res = await db.query( `Select * from test.dbo.Master_Tutor_Basic_Info where TutorKey='${id}'`, ); @@ -117,7 +127,7 @@ export class AppService { } async getStudentById(id: string) { - const db = await this.conn.connect(); + const db = await this.dbConnection.connect(); const res = await db.query( `Select * from test.dbo.Master_StudentProfile where StudentProfileKey='${id}'`, ); diff --git a/src/db/db.module.ts b/src/db/db.module.ts index 1f282fa0c6abe2410a816689e793d71a50c5a64f..6643f79443f709e100a77a8126f05c9677263de2 100644 --- a/src/db/db.module.ts +++ b/src/db/db.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { Injectable, Module } from '@nestjs/common'; import { Pool } from 'pg'; import { createPool } from 'mysql'; import { ConnectionPool } from 'mssql'; @@ -48,8 +48,33 @@ const mssqlProvider = { }), }; +@Injectable() +export class dbConnector { + getConnectionPool( + user: string, + server: string, + database: string, + password: string, + port: number, + ) { + return new ConnectionPool({ + user: user, + server: server, + database: database, + password: password, + port: port, + options: { + trustedConnection: true, + encrypt: true, + enableArithAbort: true, + trustServerCertificate: true, + }, + }); + } +} + @Module({ - providers: [pgProvider, mysqlProvider, mssqlProvider], + providers: [pgProvider, mysqlProvider, mssqlProvider, dbConnector], exports: [pgProvider, mysqlProvider, mssqlProvider], }) export class DbModule {} diff --git a/src/main.ts b/src/main.ts index 83c29002f81582520f946fcf8f39feed2e0f267c..f7a04a20c2fd24b0c1c60e4a78abec1bfca9dc25 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,10 @@ - import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; +import { config } from 'dotenv'; +config(); async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); } -bootstrap(); \ No newline at end of file +bootstrap();