refactor(simulator): switch utils to internal module export/import
[e-mobility-charging-stations-simulator.git] / src / performance / storage / MikroOrmStorage.ts
CommitLineData
edd13439 1// Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
a6b3c6c3
JB
2
3import { Configuration, Connection, IDatabaseDriver, MikroORM, Options } from '@mikro-orm/core';
8114d10e 4import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
a6b3c6c3 5
268a74bb
JB
6import {
7 type MikroORMDBType,
8 PerformanceData,
9 PerformanceRecord,
10 type Statistics,
11 StorageType,
12} from '../../types';
60a74391 13import { Constants } from '../../utils';
2896e06d 14import { Storage } from '../internal';
a6b3c6c3 15
100a5301 16export class MikroOrmStorage extends Storage {
a6b3c6c3 17 private storageType: StorageType;
1895299d 18 private orm!: MikroORM | null;
a6b3c6c3 19
1f5df42a
JB
20 constructor(storageUri: string, logPrefix: string, storageType: StorageType) {
21 super(storageUri, logPrefix);
a6b3c6c3 22 this.storageType = storageType;
d5603918 23 this.dbName = this.getDBName();
a6b3c6c3
JB
24 }
25
26 public async storePerformanceStatistics(performanceStatistics: Statistics): Promise<void> {
27 try {
28 const performanceRecord = new PerformanceRecord();
1895299d 29 await this.orm?.em.persistAndFlush(performanceRecord);
a6b3c6c3 30 } catch (error) {
dc100e97 31 this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE);
a6b3c6c3
JB
32 }
33 }
34
35 public async open(): Promise<void> {
a6ceb16a
JB
36 try {
37 if (!this?.orm) {
38 this.orm = await MikroORM.init(this.getOptions(), true);
39 }
40 } catch (error) {
dc100e97 41 this.handleDBError(this.storageType, error as Error);
a6ceb16a 42 }
a6b3c6c3
JB
43 }
44
45 public async close(): Promise<void> {
a6ceb16a
JB
46 try {
47 if (this?.orm) {
48 await this.orm.close();
49 this.orm = null;
50 }
51 } catch (error) {
dc100e97 52 this.handleDBError(this.storageType, error as Error);
a6ceb16a 53 }
a6b3c6c3
JB
54 }
55
d5603918
JB
56 private getDBName(): string {
57 if (this.storageType === StorageType.SQLITE) {
ae8ee665 58 return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`;
d5603918 59 }
e7aeea18
JB
60 return (
61 this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ??
62 Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME
63 );
d5603918
JB
64 }
65
e7aeea18
JB
66 private getOptions():
67 | Configuration<IDatabaseDriver<Connection>>
68 | Options<IDatabaseDriver<Connection>> {
a6b3c6c3 69 return {
ae8ee665 70 metadataProvider: TsMorphMetadataProvider,
a6b3c6c3 71 entities: [PerformanceRecord, PerformanceData],
a6b3c6c3 72 type: this.storageType as MikroORMDBType,
e7aeea18 73 clientUrl: this.getClientUrl(),
a6b3c6c3
JB
74 };
75 }
d5603918 76
1895299d 77 private getClientUrl(): string | undefined {
d5603918
JB
78 switch (this.storageType) {
79 case StorageType.SQLITE:
d5603918
JB
80 case StorageType.MARIA_DB:
81 case StorageType.MYSQL:
1f5df42a 82 return this.storageUri.toString();
d5603918
JB
83 }
84 }
a6b3c6c3 85}