refactor: rename Storage.handleDBError -> Storage.handleDBStorageError
[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
789007bd 6import { type PerformanceRecord, type Statistics, StorageType } from '../../types/index.js'
66a7748d 7import { Constants } from '../../utils/index.js'
4c3f6c20 8import { Storage } from './Storage.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 {
a66bbcfe 22 this.setPerformanceStatistics(performanceStatistics)
70b73ed6 23 await this.orm?.em.upsert({
789007bd
JB
24 ...performanceStatistics,
25 statisticsData: Array.from(performanceStatistics.statisticsData, ([name, value]) => ({
26 name,
27 ...value
28 }))
29 } satisfies PerformanceRecord)
a6b3c6c3 30 } catch (error) {
a03b18c4
JB
31 this.handleDBStorageError(
32 this.storageType,
33 error as Error,
34 Constants.PERFORMANCE_RECORDS_TABLE
35 )
a6b3c6c3
JB
36 }
37 }
38
66a7748d 39 public async open (): Promise<void> {
a6ceb16a 40 try {
5199f9fd 41 if (this.orm == null) {
4ccf551d
JB
42 switch (this.storageType) {
43 case StorageType.SQLITE:
44 this.orm = await SqliteORM.init(this.getOptions() as SqliteOptions)
45 break
46 case StorageType.MARIA_DB:
47 case StorageType.MYSQL:
48 this.orm = await MariaDbORM.init(this.getOptions() as MariaDbOptions)
49 break
50 }
a6ceb16a
JB
51 }
52 } catch (error) {
a03b18c4 53 this.handleDBStorageError(this.storageType, error as Error)
a6ceb16a 54 }
a6b3c6c3
JB
55 }
56
66a7748d 57 public async close (): Promise<void> {
a66bbcfe 58 this.clearPerformanceStatistics()
a6ceb16a 59 try {
5199f9fd 60 if (this.orm != null) {
66a7748d 61 await this.orm.close()
5199f9fd 62 delete this.orm
a6ceb16a
JB
63 }
64 } catch (error) {
a03b18c4 65 this.handleDBStorageError(this.storageType, error as Error)
a6ceb16a 66 }
a6b3c6c3
JB
67 }
68
66a7748d 69 private getDBName (): string {
d5603918 70 if (this.storageType === StorageType.SQLITE) {
789007bd 71 return `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`
d5603918 72 }
5199f9fd 73 return this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '')
d5603918
JB
74 }
75
4ccf551d 76 private getOptions (): SqliteOptions | MariaDbOptions {
a6b3c6c3 77 return {
4ccf551d
JB
78 dbName: this.dbName,
79 entities: ['./dist/types/orm/entities/*.js'],
80 entitiesTs: ['./src/types/orm/entities/*.ts'],
66a7748d
JB
81 clientUrl: this.getClientUrl()
82 }
a6b3c6c3 83 }
d5603918 84
66a7748d 85 private getClientUrl (): string | undefined {
d5603918
JB
86 switch (this.storageType) {
87 case StorageType.SQLITE:
d5603918
JB
88 case StorageType.MARIA_DB:
89 case StorageType.MYSQL:
66a7748d 90 return this.storageUri.toString()
d5603918
JB
91 }
92 }
a6b3c6c3 93}