import { StorageType } from '../../types/Storage';
export class MongoDBStorage extends Storage {
- private client: MongoClient;
+ private client: MongoClient | null;
private connected: boolean;
constructor(storageURI: string, logPrefix: string) {
public async open(): Promise<void> {
try {
- if (!this.connected) {
+ if (!this.connected && this?.client) {
await this.client.connect();
this.connected = true;
}
public async close(): Promise<void> {
try {
- if (this.connected) {
+ if (this.connected && this?.client) {
await this.client.close();
this.connected = false;
}
}
private checkDBConnection() {
+ if (!this?.client) {
+ throw new Error(`${this.logPrefix} ${this.getDBNameFromStorageType(StorageType.MONGO_DB)} client initialization failed while trying to issue a request`);
+ }
if (!this.connected) {
throw new Error(`${this.logPrefix} ${this.getDBNameFromStorageType(StorageType.MONGO_DB)} connection not opened while trying to issue a request`);
}