Commit 907ca0b1 authored by tushar5526's avatar tushar5526
Browse files

feat: added env vars

No related merge requests found
Showing with 58 additions and 15 deletions
+58 -15
.env
# compiled output # compiled output
/dist /dist
/node_modules /node_modules
......
sample.env 0 → 100644
MSSQL_USER=sa
MSSQL_SERVER=localhost
MSSQL_DATABASE=test
MSSQL_PASSWORD=Pass@word
MSSQL_PORT=1433
\ No newline at end of file
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { DbModule } from './db/db.module'; import { dbConnector, DbModule } from './db/db.module';
@Module({ @Module({
imports: [DbModule], imports: [DbModule],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [AppService, dbConnector],
}) })
export class AppModule {} export class AppModule {}
import { Injectable, Inject } from '@nestjs/common'; import { Injectable, Inject } from '@nestjs/common';
import { MSSQL_CONNECTION } from './constants'; import { MSSQL_CONNECTION } from './constants';
import * as CryptoJS from 'crypto'; import * as CryptoJS from 'crypto';
import { dbConnector } from './db/db.module';
import { ConnectionPool } from 'mssql';
@Injectable() @Injectable()
export class AppService { 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) { // async mysqlCallStoredProcedure(msg: string) {
//returns a promise that resolves to a result set on success //returns a promise that resolves to a result set on success
...@@ -23,7 +33,7 @@ export class AppService { ...@@ -23,7 +33,7 @@ export class AppService {
async mssqlCallStoredProcedure(username: string) { async mssqlCallStoredProcedure(username: string) {
//returns a promise that resolves to a result set on success //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}'`); const res = await db.query(`EXEC get_user @Username = N'${username}'`);
console.log(res); console.log(res);
return { pass: res['recordset'][0]['Password'] }; return { pass: res['recordset'][0]['Password'] };
...@@ -58,7 +68,7 @@ export class AppService { ...@@ -58,7 +68,7 @@ export class AppService {
} }
async getUsers(search: string, first: number, max: number) { async getUsers(search: string, first: number, max: number) {
const db = await this.conn.connect(); const db = await this.dbConnection.connect();
const res = this.parseQueryResponse( const res = this.parseQueryResponse(
await db.query(`SELECT * FROM Master_user`), await db.query(`SELECT * FROM Master_user`),
); );
...@@ -66,7 +76,7 @@ export class AppService { ...@@ -66,7 +76,7 @@ export class AppService {
} }
async getUsersCount() { async getUsersCount() {
const db = await this.conn.connect(); const db = await this.dbConnection.connect();
const res = await db.query( const res = await db.query(
`SELECT COUNT(UserKey) as count FROM Master_user`, `SELECT COUNT(UserKey) as count FROM Master_user`,
); );
...@@ -74,7 +84,7 @@ export class AppService { ...@@ -74,7 +84,7 @@ export class AppService {
} }
async getUserById(username: string) { async getUserById(username: string) {
const db = await this.conn.connect(); const db = await this.dbConnection.connect();
const res = this.parseQueryResponse( const res = this.parseQueryResponse(
await db.query(`EXEC get_user @Username = N'${username}'`), await db.query(`EXEC get_user @Username = N'${username}'`),
); );
...@@ -82,7 +92,7 @@ export class AppService { ...@@ -82,7 +92,7 @@ export class AppService {
} }
async getUserCredentials(username: string) { 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 res = await db.query(`EXEC get_user @Username = N'${username}'`);
const saltRounds = 1000; const saltRounds = 1000;
const cred_res = []; const cred_res = [];
...@@ -109,7 +119,7 @@ export class AppService { ...@@ -109,7 +119,7 @@ export class AppService {
} }
async getTutorById(id: string) { async getTutorById(id: string) {
const db = await this.conn.connect(); const db = await this.dbConnection.connect();
const res = await db.query( const res = await db.query(
`Select * from test.dbo.Master_Tutor_Basic_Info where TutorKey='${id}'`, `Select * from test.dbo.Master_Tutor_Basic_Info where TutorKey='${id}'`,
); );
...@@ -117,7 +127,7 @@ export class AppService { ...@@ -117,7 +127,7 @@ export class AppService {
} }
async getStudentById(id: string) { async getStudentById(id: string) {
const db = await this.conn.connect(); const db = await this.dbConnection.connect();
const res = await db.query( const res = await db.query(
`Select * from test.dbo.Master_StudentProfile where StudentProfileKey='${id}'`, `Select * from test.dbo.Master_StudentProfile where StudentProfileKey='${id}'`,
); );
......
import { Module } from '@nestjs/common'; import { Injectable, Module } from '@nestjs/common';
import { Pool } from 'pg'; import { Pool } from 'pg';
import { createPool } from 'mysql'; import { createPool } from 'mysql';
import { ConnectionPool } from 'mssql'; import { ConnectionPool } from 'mssql';
...@@ -48,8 +48,33 @@ const mssqlProvider = { ...@@ -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({ @Module({
providers: [pgProvider, mysqlProvider, mssqlProvider], providers: [pgProvider, mysqlProvider, mssqlProvider, dbConnector],
exports: [pgProvider, mysqlProvider, mssqlProvider], exports: [pgProvider, mysqlProvider, mssqlProvider],
}) })
export class DbModule {} export class DbModule {}
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
import { config } from 'dotenv';
config();
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
await app.listen(3000); await app.listen(3000);
} }
bootstrap(); bootstrap();
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment