From 907ca0b1ceceb78ab54e9e392014106f0b50b8d2 Mon Sep 17 00:00:00 2001
From: tushar5526 <codingid6@gmail.com>
Date: Mon, 16 Jan 2023 21:12:43 +0530
Subject: [PATCH] feat: added env vars

---
 .gitignore          |  2 ++
 sample.env          |  5 +++++
 src/app.module.ts   |  4 ++--
 src/app.service.ts  | 28 +++++++++++++++++++---------
 src/db/db.module.ts | 29 +++++++++++++++++++++++++++--
 src/main.ts         |  5 +++--
 6 files changed, 58 insertions(+), 15 deletions(-)
 create mode 100644 sample.env

diff --git a/.gitignore b/.gitignore
index 22f55ad..aabbe8f 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 0000000..fb1d94a
--- /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 4c4e005..f06a4bb 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 02c7bbe..83f3450 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 1f282fa..6643f79 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 83c2900..f7a04a2 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();
-- 
GitLab