X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fperformance%2Fstorage%2FMikroOrmStorage.ts;h=3c4e7fd3cb036140753f010959e1c0fc131db234;hb=b3d7d65476a4ab586b3ccd188f0bfbe8452aba0e;hp=82bdd23419fde02c82419952c17b2597a5c7c75f;hpb=95b343745d755d2209f028f676c9d25527e7eb4b;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/storage/MikroOrmStorage.ts b/src/performance/storage/MikroOrmStorage.ts index 82bdd234..3c4e7fd3 100644 --- a/src/performance/storage/MikroOrmStorage.ts +++ b/src/performance/storage/MikroOrmStorage.ts @@ -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 { 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> | Options> { + private getOptions(): + | Configuration> + | Options> { 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 { } } } -