X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fperformance%2Fstorage%2FMongoDBStorage.ts;h=5a3783818cba77bcb16bf25e622ae81015f77fe8;hb=65334d061f549a3136373b27c35cb0e508c75a9f;hp=851df0a1ba8248a582153056ce1bad688fd37d92;hpb=a6b3c6c313f1c0314a1445ed630cac85edf55b2c;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/storage/MongoDBStorage.ts b/src/performance/storage/MongoDBStorage.ts index 851df0a1..5a378381 100644 --- a/src/performance/storage/MongoDBStorage.ts +++ b/src/performance/storage/MongoDBStorage.ts @@ -7,7 +7,7 @@ import { Storage } from './Storage'; 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) { @@ -28,7 +28,7 @@ export class MongoDBStorage extends Storage { public async open(): Promise { try { - if (!this.connected) { + if (!this.connected && this?.client) { await this.client.connect(); this.connected = true; } @@ -39,7 +39,7 @@ export class MongoDBStorage extends Storage { public async close(): Promise { try { - if (this.connected) { + if (this.connected && this?.client) { await this.client.close(); this.connected = false; } @@ -49,6 +49,9 @@ export class MongoDBStorage extends Storage { } 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`); }