public open(): void {
try {
- this.fd = fs.openSync(this.dbName, 'a+');
+ if (!this?.fd) {
+ this.fd = fs.openSync(this.dbName, 'a+');
+ }
} catch (error) {
FileUtils.handleFileException(this.logPrefix, Constants.PERFORMANCE_RECORDS_FILETYPE, this.dbName, error);
}
public close(): void {
try {
- if (this.fd) {
+ if (this?.fd) {
fs.closeSync(this.fd);
this.fd = null;
}
export class MikroORMStorage extends Storage {
private storageType: StorageType;
- private orm: MikroORM;
+ private orm: MikroORM | null;
constructor(storageURI: string, logPrefix: string, storageType: StorageType) {
super(storageURI, logPrefix);
}
public async open(): Promise<void> {
- this.orm = await MikroORM.init(this.getOptions(), true);
+ try {
+ if (!this?.orm) {
+ this.orm = await MikroORM.init(this.getOptions(), true);
+ }
+ } catch (error) {
+ this.handleDBError(this.storageType, error);
+ }
}
public async close(): Promise<void> {
- await this.orm.close();
+ try {
+ if (this?.orm) {
+ await this.orm.close();
+ this.orm = null;
+ }
+ } catch (error) {
+ this.handleDBError(this.storageType, error);
+ }
}
private getDBName(): string {
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`);
}