From: Jérôme Benoit Date: Sat, 13 Jan 2024 19:28:41 +0000 (+0100) Subject: refactor: make storage init compliant with MikroORM 6 X-Git-Tag: v1.2.32~17 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4ccf551d961ef001acef6fafe1165ad9b6dac4e3;p=e-mobility-charging-stations-simulator.git refactor: make storage init compliant with MikroORM 6 Signed-off-by: Jérôme Benoit --- diff --git a/mikro-orm.config-template.ts b/mikro-orm.config-template.ts index ddfcb4c3..a418c75e 100644 --- a/mikro-orm.config-template.ts +++ b/mikro-orm.config-template.ts @@ -1,10 +1,10 @@ import { defineConfig } from '@mikro-orm/sqlite' -import { PerformanceData, PerformanceRecord } from './src/types/index.js' import { Constants } from './src/utils/index.js' export default defineConfig({ dbName: `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`, - entities: [PerformanceRecord, PerformanceData], + 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 b88bee70..d5e25bb2 100644 --- a/src/performance/storage/MikroOrmStorage.ts +++ b/src/performance/storage/MikroOrmStorage.ts @@ -1,21 +1,15 @@ // Copyright Jerome Benoit. 2021-2024. All Rights Reserved. -import { type Configuration, 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 { Storage } from './Storage.js' -import { - type MikroOrmDbType, - PerformanceData, - PerformanceRecord, - type Statistics, - StorageType -} from '../../types/index.js' +import { type Statistics, StorageType } from '../../types/index.js' import { Constants } from '../../utils/index.js' export class MikroOrmStorage extends Storage { private readonly storageType: StorageType - private orm?: MikroORM + private orm?: SqliteORM | MariaDbORM constructor (storageUri: string, logPrefix: string, storageType: StorageType) { super(storageUri, logPrefix) @@ -25,8 +19,8 @@ export class MikroOrmStorage extends Storage { public async storePerformanceStatistics (performanceStatistics: Statistics): Promise { try { - const performanceRecord = new PerformanceRecord() - await this.orm?.em.persistAndFlush(performanceRecord) + // TODO: build PerformanceRecord entity + await this.orm?.em.persistAndFlush({}) } catch (error) { this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE) } @@ -35,7 +29,15 @@ export class MikroOrmStorage extends Storage { public async open (): Promise { try { if (this.orm == null) { - this.orm = await MikroORM.init(this.getOptions(), true) + 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) @@ -60,11 +62,11 @@ export class MikroOrmStorage extends Storage { 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, + dbName: this.dbName, + entities: ['./dist/types/orm/entities/*.js'], + entitiesTs: ['./src/types/orm/entities/*.ts'], clientUrl: this.getClientUrl() } } diff --git a/src/performance/storage/Storage.ts b/src/performance/storage/Storage.ts index c6cf2896..d9539190 100644 --- a/src/performance/storage/Storage.ts +++ b/src/performance/storage/Storage.ts @@ -42,14 +42,14 @@ export abstract class Storage { protected getDBNameFromStorageType (type: StorageType): DBName | undefined { switch (type) { + case StorageType.SQLITE: + return DBName.SQLITE case StorageType.MARIA_DB: return DBName.MARIA_DB - case StorageType.MONGO_DB: - return DBName.MONGO_DB case StorageType.MYSQL: return DBName.MYSQL - case StorageType.SQLITE: - return DBName.SQLITE + case StorageType.MONGO_DB: + return DBName.MONGO_DB } } diff --git a/src/performance/storage/StorageFactory.ts b/src/performance/storage/StorageFactory.ts index a2bfa82b..e1e42751 100644 --- a/src/performance/storage/StorageFactory.ts +++ b/src/performance/storage/StorageFactory.ts @@ -27,9 +27,9 @@ export class StorageFactory { case StorageType.MONGO_DB: storageInstance = new MongoDBStorage(connectionUri, logPrefix) break + // case StorageType.SQLITE: // case StorageType.MYSQL: // case StorageType.MARIA_DB: - // case StorageType.SQLITE: // storageInstance = new MikroOrmStorage(connectionUri, logPrefix, type) // break default: diff --git a/src/types/Storage.ts b/src/types/Storage.ts index 3301f5ca..4d4a4e4e 100644 --- a/src/types/Storage.ts +++ b/src/types/Storage.ts @@ -1,7 +1,3 @@ -import type { Configuration } from '@mikro-orm/core' - -export type MikroOrmDbType = keyof typeof Configuration.PLATFORMS - export enum StorageType { JSON_FILE = 'jsonfile', MONGO_DB = 'mongodb', diff --git a/src/types/index.ts b/src/types/index.ts index 3b1f799c..600a4c0e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -186,7 +186,7 @@ export { } from './ocpp/Configuration.js' export type { ConnectorStatus } from './ConnectorStatus.js' export { ConnectorStatusEnum, type ConnectorStatusTransition } from './ocpp/ConnectorStatusEnum.js' -export { DBName, type MikroOrmDbType, StorageType } from './Storage.js' +export { DBName, StorageType } from './Storage.js' export type { EmptyObject } from './EmptyObject.js' export { ErrorType } from './ocpp/ErrorType.js' export type { EvseTemplate, EvseStatus } from './Evse.js' diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 9d2b92fa..2472bf61 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -14,7 +14,11 @@ import { } from 'date-fns' import { Constants } from './Constants.js' -import { type TimestampedData, WebSocketCloseEventStatusString } from '../types/index.js' +import { + type EmptyObject, + type TimestampedData, + WebSocketCloseEventStatusString +} from '../types/index.js' export const logPrefix = (prefixString = ''): string => { return `${new Date().toLocaleString()}${prefixString}` @@ -261,7 +265,7 @@ export const isObject = (value: unknown): value is object => { return value != null && typeof value === 'object' && !Array.isArray(value) } -export const isEmptyObject = (object: object): object is Record => { +export const isEmptyObject = (object: object): object is EmptyObject => { if (object.constructor !== Object) { return false }