refactor: prepare for MikroORM storage support
[e-mobility-charging-stations-simulator.git] / src / performance / storage / MikroOrmStorage.ts
CommitLineData
a19b897d 1// Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
a6b3c6c3 2
4ccf551d
JB
3import { MikroORM as MariaDbORM, type Options as MariaDbOptions } from '@mikro-orm/mariadb'
4import { MikroORM as SqliteORM, type Options as SqliteOptions } from '@mikro-orm/sqlite'
a6b3c6c3 5
66a7748d 6import { Storage } from './Storage.js'
789007bd 7import { type PerformanceRecord, type Statistics, StorageType } from '../../types/index.js'
66a7748d 8import { Constants } from '../../utils/index.js'
a6b3c6c3 9
100a5301 10export class MikroOrmStorage extends Storage {
66a7748d 11 private readonly storageType: StorageType
4ccf551d 12 private orm?: SqliteORM | MariaDbORM
a6b3c6c3 13
66a7748d
JB
14 constructor (storageUri: string, logPrefix: string, storageType: StorageType) {
15 super(storageUri, logPrefix)
16 this.storageType = storageType
17 this.dbName = this.getDBName()
a6b3c6c3
JB
18 }
19
66a7748d 20 public async storePerformanceStatistics (performanceStatistics: Statistics): Promise<void> {
a6b3c6c3 21 try {
789007bd
JB
22 await this.orm?.em.persistAndFlush({
23 ...performanceStatistics,
24 statisticsData: Array.from(performanceStatistics.statisticsData, ([name, value]) => ({
25 name,
26 ...value
27 }))
28 } satisfies PerformanceRecord)
a6b3c6c3 29 } catch (error) {
66a7748d 30 this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE)
a6b3c6c3
JB
31 }
32 }
33
66a7748d 34 public async open (): Promise<void> {
a6ceb16a 35 try {
5199f9fd 36 if (this.orm == null) {
4ccf551d
JB
37 switch (this.storageType) {
38 case StorageType.SQLITE:
39 this.orm = await SqliteORM.init(this.getOptions() as SqliteOptions)
40 break
41 case StorageType.MARIA_DB:
42 case StorageType.MYSQL:
43 this.orm = await MariaDbORM.init(this.getOptions() as MariaDbOptions)
44 break
45 }
a6ceb16a
JB
46 }
47 } catch (error) {
66a7748d 48 this.handleDBError(this.storageType, error as Error)
a6ceb16a 49 }
a6b3c6c3
JB
50 }
51
66a7748d 52 public async close (): Promise<void> {
a6ceb16a 53 try {
5199f9fd 54 if (this.orm != null) {
66a7748d 55 await this.orm.close()
5199f9fd 56 delete this.orm
a6ceb16a
JB
57 }
58 } catch (error) {
66a7748d 59 this.handleDBError(this.storageType, error as Error)
a6ceb16a 60 }
a6b3c6c3
JB
61 }
62
66a7748d 63 private getDBName (): string {
d5603918 64 if (this.storageType === StorageType.SQLITE) {
789007bd 65 return `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`
d5603918 66 }
5199f9fd 67 return this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '')
d5603918
JB
68 }
69
4ccf551d 70 private getOptions (): SqliteOptions | MariaDbOptions {
a6b3c6c3 71 return {
4ccf551d
JB
72 dbName: this.dbName,
73 entities: ['./dist/types/orm/entities/*.js'],
74 entitiesTs: ['./src/types/orm/entities/*.ts'],
66a7748d
JB
75 clientUrl: this.getClientUrl()
76 }
a6b3c6c3 77 }
d5603918 78
66a7748d 79 private getClientUrl (): string | undefined {
d5603918
JB
80 switch (this.storageType) {
81 case StorageType.SQLITE:
d5603918
JB
82 case StorageType.MARIA_DB:
83 case StorageType.MYSQL:
66a7748d 84 return this.storageUri.toString()
d5603918
JB
85 }
86 }
a6b3c6c3 87}