Silence linter some more
[e-mobility-charging-stations-simulator.git] / src / performance / storage / MikroORMStorage.ts
index 4d39dc7403beb97b3b46d24bbae4cb5198502a85..6c9e63d8da96b87452bfdbc6330d79c725347514 100644 (file)
@@ -8,15 +8,16 @@ import { PerformanceData } from '../../types/orm/entities/PerformanceData';
 import { PerformanceRecord } from '../../types/orm/entities/PerformanceRecord';
 import Statistics from '../../types/Statistics';
 import { Storage } from './Storage';
+import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
 
 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);
     this.storageType = storageType;
-    this.dbName = this.storageURI.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ?? Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME;
+    this.dbName = this.getDBName();
   }
 
   public async storePerformanceStatistics(performanceStatistics: Statistics): Promise<void> {
@@ -24,25 +25,54 @@ export class MikroORMStorage extends Storage {
       const performanceRecord = new PerformanceRecord();
       await this.orm.em.persistAndFlush(performanceRecord);
     } catch (error) {
-      this.handleDBError(this.storageType, error, Constants.PERFORMANCE_RECORDS_TABLE);
+      this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE);
     }
   }
 
   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 as 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 as Error);
+    }
+  }
+
+  private getDBName(): string {
+    if (this.storageType === StorageType.SQLITE) {
+      return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`;
+    }
+    return this.storageURI.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ?? Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME;
   }
 
   private getOptions(): Configuration<IDatabaseDriver<Connection>> | Options<IDatabaseDriver<Connection>> {
     return {
+      metadataProvider: TsMorphMetadataProvider,
       entities: [PerformanceRecord, PerformanceData],
-      dbName: this.dbName,
       type: this.storageType as MikroORMDBType,
-      clientUrl: this.storageURI.toString()
+      clientUrl: this.getClientUrl()
     };
   }
+
+  private getClientUrl(): string {
+    switch (this.storageType) {
+      case StorageType.SQLITE:
+      case StorageType.MARIA_DB:
+      case StorageType.MYSQL:
+        return this.storageURI.toString();
+    }
+  }
 }