From: Jérôme Benoit Date: Sun, 14 Jan 2024 11:23:34 +0000 (+0100) Subject: refactor: prepare for MikroORM storage support X-Git-Tag: v1.2.32~12 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=789007bd09242370a9d3502da967deb414886504;p=e-mobility-charging-stations-simulator.git refactor: prepare for MikroORM storage support Signed-off-by: Jérôme Benoit --- diff --git a/mikro-orm.config-template.ts b/mikro-orm.config-template.ts index a418c75e..1291cbd2 100644 --- a/mikro-orm.config-template.ts +++ b/mikro-orm.config-template.ts @@ -3,7 +3,7 @@ import { defineConfig } from '@mikro-orm/sqlite' import { Constants } from './src/utils/index.js' export default defineConfig({ - dbName: `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`, + dbName: `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`, entities: ['./dist/types/orm/entities/*.js'], entitiesTs: ['./src/types/orm/entities/*.ts'], debug: true diff --git a/src/performance/storage/MikroOrmStorage.ts b/src/performance/storage/MikroOrmStorage.ts index d5e25bb2..9746c6bd 100644 --- a/src/performance/storage/MikroOrmStorage.ts +++ b/src/performance/storage/MikroOrmStorage.ts @@ -4,7 +4,7 @@ import { MikroORM as MariaDbORM, type Options as MariaDbOptions } from '@mikro-o import { MikroORM as SqliteORM, type Options as SqliteOptions } from '@mikro-orm/sqlite' import { Storage } from './Storage.js' -import { type Statistics, StorageType } from '../../types/index.js' +import { type PerformanceRecord, type Statistics, StorageType } from '../../types/index.js' import { Constants } from '../../utils/index.js' export class MikroOrmStorage extends Storage { @@ -19,8 +19,13 @@ export class MikroOrmStorage extends Storage { public async storePerformanceStatistics (performanceStatistics: Statistics): Promise { try { - // TODO: build PerformanceRecord entity - await this.orm?.em.persistAndFlush({}) + await this.orm?.em.persistAndFlush({ + ...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) } @@ -57,7 +62,7 @@ export class MikroOrmStorage extends Storage { 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, '') } diff --git a/src/performance/storage/StorageFactory.ts b/src/performance/storage/StorageFactory.ts index e1e42751..8753bea1 100644 --- a/src/performance/storage/StorageFactory.ts +++ b/src/performance/storage/StorageFactory.ts @@ -1,7 +1,6 @@ // Copyright Jerome Benoit. 2021-2024. All Rights Reserved. import { JsonFileStorage } from './JsonFileStorage.js' -// eslint-disable-next-line @typescript-eslint/no-unused-vars import { MikroOrmStorage } from './MikroOrmStorage.js' import { MongoDBStorage } from './MongoDBStorage.js' import type { Storage } from './Storage.js' @@ -27,12 +26,13 @@ export class StorageFactory { case StorageType.MONGO_DB: storageInstance = new MongoDBStorage(connectionUri, logPrefix) break - // case StorageType.SQLITE: - // case StorageType.MYSQL: - // case StorageType.MARIA_DB: - // storageInstance = new MikroOrmStorage(connectionUri, logPrefix, type) - // break + case StorageType.SQLITE: + case StorageType.MARIA_DB: + case StorageType.MYSQL: + storageInstance = new MikroOrmStorage(connectionUri, logPrefix, type) + break default: + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions throw new BaseError(`${logPrefix} Unknown storage type: ${type}`) } return storageInstance diff --git a/src/types/orm/entities/PerformanceRecord.ts b/src/types/orm/entities/PerformanceRecord.ts index dbe9ceec..bdd88aba 100644 --- a/src/types/orm/entities/PerformanceRecord.ts +++ b/src/types/orm/entities/PerformanceRecord.ts @@ -1,6 +1,6 @@ import { Entity, PrimaryKey, Property } from '@mikro-orm/core' -interface PerformanceData { +interface StatisticsData { name: string requestCount: number responseCount: number @@ -38,5 +38,5 @@ export class PerformanceRecord { updatedAt?: Date @Property() - performanceData!: PerformanceData[] + statisticsData!: Array> } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index dc3ca595..8f6e6c84 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -177,17 +177,31 @@ export class Configuration { } private static buildPerformanceStorageSection (): StorageConfiguration { - let storageConfiguration: StorageConfiguration = { - enabled: false, - type: StorageType.JSON_FILE, - uri: getDefaultPerformanceStorageUri(StorageType.JSON_FILE) + let storageConfiguration: StorageConfiguration + switch (Configuration.getConfigurationData()?.performanceStorage?.type) { + case StorageType.SQLITE: + storageConfiguration = { + enabled: false, + type: StorageType.SQLITE, + uri: getDefaultPerformanceStorageUri(StorageType.SQLITE) + } + break + case StorageType.JSON_FILE: + default: + storageConfiguration = { + enabled: false, + type: StorageType.JSON_FILE, + uri: getDefaultPerformanceStorageUri(StorageType.JSON_FILE) + } + break } if (hasOwnProp(Configuration.getConfigurationData(), ConfigurationSection.performanceStorage)) { storageConfiguration = { ...storageConfiguration, ...Configuration.getConfigurationData()?.performanceStorage, - ...(Configuration.getConfigurationData()?.performanceStorage?.type === - StorageType.JSON_FILE && + ...((Configuration.getConfigurationData()?.performanceStorage?.type === + StorageType.JSON_FILE || + Configuration.getConfigurationData()?.performanceStorage?.type === StorageType.SQLITE) && Configuration.getConfigurationData()?.performanceStorage?.uri != null && { uri: buildPerformanceUriFilePath( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion diff --git a/tsconfig-mikro-orm.json b/tsconfig-mikro-orm.json index 1d40e6e2..5d7590cd 100644 --- a/tsconfig-mikro-orm.json +++ b/tsconfig-mikro-orm.json @@ -3,8 +3,12 @@ "compilerOptions": { "rootDir": "./src/types/orm/entities", "outDir": "./dist/types/orm/entities", - "sourceMap": true, - "esModuleInterop": true + "declaration": true, + "sourceMap": true }, - "include": ["src/types/orm/entities/*.ts"] + "include": ["src/types/orm/entities/*.ts"], + "ts-node": { + "esm": true, + "transpileOnly": true + } }