Skip to content

Commit

Permalink
Added: Feature For load OpenAPIValidator Cache on app boot
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyvishal committed Jun 18, 2024
1 parent 6d1f612 commit 049acd4
Showing 1 changed file with 43 additions and 32 deletions.
75 changes: 43 additions & 32 deletions src/utils/mongo.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,52 @@ import { Db, MongoClient } from "mongodb";
import { Exception, ExceptionType } from "../models/exception.model";
import logger from "./logger.utils";

export class DBClient{
private db: Db;
private client: MongoClient;
public isConnected: boolean = false;

constructor(dbURL: string){
this.client = new MongoClient(dbURL, {
minPoolSize: 10,
maxPoolSize: 15,
});

this.db = this.client.db();
export class DBClient {
private db: Db;
private client: MongoClient;
public isConnected: boolean = false;

constructor(dbURL: string) {
this.client = new MongoClient(dbURL, {
minPoolSize: 10,
maxPoolSize: 15
});

this.db = this.client.db();
}

public async connect(): Promise<void> {
try {
this.client = await this.client.connect();
this.db = this.client.db();
this.isConnected = true;
logger.info(`Mongo Client Connected For DB: ${this.db.databaseName}`);
} catch (error: any) {
console.log(error);
}

public async connect(): Promise<void> {
this.client = await this.client.connect();
this.db = this.client.db();
this.isConnected = true;
logger.info(`Mongo Client Connected For DB: ${this.db.databaseName}`);
}

public getDB(): Db{
if(!this.isConnected){
throw new Exception(ExceptionType.Mongo_ClientNotInitialized, "Mongo client is not connected.", 500);
}

return this.db;
}

public getDB(): Db {
if (!this.isConnected) {
throw new Exception(
ExceptionType.Mongo_ClientNotInitialized,
"Mongo client is not connected.",
500
);
}

public getClient(): MongoClient{
if(!this.isConnected){
throw new Exception(ExceptionType.Mongo_ClientNotInitialized, "Mongo client is not connected.", 500);
}
return this.db;
}

return this.client;
public getClient(): MongoClient {
if (!this.isConnected) {
throw new Exception(
ExceptionType.Mongo_ClientNotInitialized,
"Mongo client is not connected.",
500
);
}

}
return this.client;
}
}

0 comments on commit 049acd4

Please sign in to comment.