X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fperformance%2Fstorage%2FMikroOrmStorage.ts;h=7b4545bdff36ba824597c22549889641c6f60538;hb=8d2a9e1c5a2b21932c77846bdf10cbe726797685;hp=d77b95a0cbee9bc3a4a993f21a34e241c7ac1e14;hpb=1abe401efb31da0c1ae37aee93a1379272091987;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/storage/MikroOrmStorage.ts b/src/performance/storage/MikroOrmStorage.ts index d77b95a0..7b4545bd 100644 --- a/src/performance/storage/MikroOrmStorage.ts +++ b/src/performance/storage/MikroOrmStorage.ts @@ -1,91 +1,89 @@ -// Copyright Jerome Benoit. 2021-2023. All Rights Reserved. +// Copyright Jerome Benoit. 2021-2024. All Rights Reserved. -import { - Configuration, - Connection, - type IDatabaseDriver, - MikroORM, - type Options, -} from '@mikro-orm/core'; -import { TsMorphMetadataProvider } from '@mikro-orm/reflection'; +import { MikroORM as MariaDbORM, type Options as MariaDbOptions } from '@mikro-orm/mariadb' +import { MikroORM as SqliteORM, type Options as SqliteOptions } from '@mikro-orm/sqlite' -import { - type MikroOrmDbType, - PerformanceData, - PerformanceRecord, - type Statistics, - StorageType, -} from '../../types'; -import { Constants } from '../../utils'; -import { Storage } from '../internal'; +import { type PerformanceRecord, type Statistics, StorageType } from '../../types/index.js' +import { Constants } from '../../utils/index.js' +import { Storage } from './Storage.js' export class MikroOrmStorage extends Storage { - private storageType: StorageType; - private orm!: MikroORM | null; + private readonly storageType: StorageType + private orm?: SqliteORM | MariaDbORM - constructor(storageUri: string, logPrefix: string, storageType: StorageType) { - super(storageUri, logPrefix); - this.storageType = storageType; - this.dbName = this.getDBName(); + constructor (storageUri: string, logPrefix: string, storageType: StorageType) { + super(storageUri, logPrefix) + this.storageType = storageType + this.dbName = this.getDBName() } - public async storePerformanceStatistics(performanceStatistics: Statistics): Promise { + public async storePerformanceStatistics (performanceStatistics: Statistics): Promise { try { - const performanceRecord = new PerformanceRecord(); - await this.orm?.em.persistAndFlush(performanceRecord); + this.setPerformanceStatistics(performanceStatistics) + await this.orm?.em.upsert({ + ...performanceStatistics, + statisticsData: Array.from(performanceStatistics.statisticsData, ([name, value]) => ({ + name, + ...value + })) + } satisfies PerformanceRecord) } catch (error) { - this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE); + this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE) } } - public async open(): Promise { + public async open (): Promise { try { - if (!this?.orm) { - this.orm = await MikroORM.init(this.getOptions(), true); + if (this.orm == null) { + switch (this.storageType) { + case StorageType.SQLITE: + this.orm = await SqliteORM.init(this.getOptions() as SqliteOptions) + break + case StorageType.MARIA_DB: + case StorageType.MYSQL: + this.orm = await MariaDbORM.init(this.getOptions() as MariaDbOptions) + break + } } } catch (error) { - this.handleDBError(this.storageType, error as Error); + this.handleDBError(this.storageType, error as Error) } } - public async close(): Promise { + public async close (): Promise { + this.clearPerformanceStatistics() try { - if (this?.orm) { - await this.orm.close(); - this.orm = null; + if (this.orm != null) { + await this.orm.close() + delete this.orm } } catch (error) { - this.handleDBError(this.storageType, error as Error); + this.handleDBError(this.storageType, error as Error) } } - private getDBName(): string { + private getDBName (): string { if (this.storageType === StorageType.SQLITE) { - return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`; + return `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${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, '') } - private getOptions(): - | Configuration> - | Options> { + private getOptions (): SqliteOptions | MariaDbOptions { return { - metadataProvider: TsMorphMetadataProvider, - entities: [PerformanceRecord, PerformanceData], - type: this.storageType as MikroOrmDbType, - clientUrl: this.getClientUrl(), - }; + dbName: this.dbName, + entities: ['./dist/types/orm/entities/*.js'], + entitiesTs: ['./src/types/orm/entities/*.ts'], + clientUrl: this.getClientUrl() + } } - private getClientUrl(): string | undefined { + private getClientUrl (): string | undefined { switch (this.storageType) { case StorageType.SQLITE: case StorageType.MARIA_DB: case StorageType.MYSQL: - return this.storageUri.toString(); + return this.storageUri.toString() } } }