feat: improve get composite schedule
[e-mobility-charging-stations-simulator.git] / src / performance / storage / MikroOrmStorage.ts
index 82bdd23419fde02c82419952c17b2597a5c7c75f..3c4e7fd3cb036140753f010959e1c0fc131db234 100644 (file)
@@ -1,18 +1,27 @@
-// Copyright Jerome Benoit. 2021. All Rights Reserved.
+// Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import { Configuration, Connection, IDatabaseDriver, MikroORM, Options } from '@mikro-orm/core';
-import { MikroORMDBType, StorageType } from '../../types/Storage';
+import {
+  Configuration,
+  Connection,
+  type IDatabaseDriver,
+  MikroORM,
+  type Options,
+} from '@mikro-orm/core';
+import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
 
-import Constants from '../../utils/Constants';
-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';
+import {
+  type MikroOrmDbType,
+  PerformanceData,
+  PerformanceRecord,
+  type Statistics,
+  StorageType,
+} from '../../types';
+import { Constants } from '../../utils';
 
 export class MikroOrmStorage extends Storage {
   private storageType: StorageType;
-  private orm: MikroORM | null;
+  private orm!: MikroORM | null;
 
   constructor(storageUri: string, logPrefix: string, storageType: StorageType) {
     super(storageUri, logPrefix);
@@ -20,10 +29,11 @@ export class MikroOrmStorage extends Storage {
     this.dbName = this.getDBName();
   }
 
+  // eslint-disable-next-line @typescript-eslint/no-unused-vars
   public async storePerformanceStatistics(performanceStatistics: Statistics): Promise<void> {
     try {
       const performanceRecord = new PerformanceRecord();
-      await this.orm.em.persistAndFlush(performanceRecord);
+      await this.orm?.em.persistAndFlush(performanceRecord);
     } catch (error) {
       this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE);
     }
@@ -54,19 +64,24 @@ export class MikroOrmStorage extends Storage {
     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;
+    return (
+      this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ??
+      Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME
+    );
   }
 
-  private getOptions(): Configuration<IDatabaseDriver<Connection>> | Options<IDatabaseDriver<Connection>> {
+  private getOptions():
+    | Configuration<IDatabaseDriver<Connection>>
+    | Options<IDatabaseDriver<Connection>> {
     return {
       metadataProvider: TsMorphMetadataProvider,
       entities: [PerformanceRecord, PerformanceData],
-      type: this.storageType as MikroORMDBType,
-      clientUrl: this.getClientUrl()
+      type: this.storageType as MikroOrmDbType,
+      clientUrl: this.getClientUrl(),
     };
   }
 
-  private getClientUrl(): string {
+  private getClientUrl(): string | undefined {
     switch (this.storageType) {
       case StorageType.SQLITE:
       case StorageType.MARIA_DB:
@@ -75,4 +90,3 @@ export class MikroOrmStorage extends Storage {
     }
   }
 }
-