1 // Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import { Configuration
, Connection
, IDatabaseDriver
, MikroORM
, Options
} from
'@mikro-orm/core';
4 import { TsMorphMetadataProvider
} from
'@mikro-orm/reflection';
6 import { Storage
} from
'./Storage';
14 import { Constants
} from
'../../utils/Constants';
16 export class MikroOrmStorage
extends Storage
{
17 private storageType
: StorageType
;
18 private orm
!: MikroORM
| null;
20 constructor(storageUri
: string, logPrefix
: string, storageType
: StorageType
) {
21 super(storageUri
, logPrefix
);
22 this.storageType
= storageType
;
23 this.dbName
= this.getDBName();
26 public async storePerformanceStatistics(performanceStatistics
: Statistics
): Promise
<void> {
28 const performanceRecord
= new PerformanceRecord();
29 await this.orm
?.em
.persistAndFlush(performanceRecord
);
31 this.handleDBError(this.storageType
, error
as Error, Constants
.PERFORMANCE_RECORDS_TABLE
);
35 public async open(): Promise
<void> {
38 this.orm
= await MikroORM
.init(this.getOptions(), true);
41 this.handleDBError(this.storageType
, error
as Error);
45 public async close(): Promise
<void> {
48 await this.orm
.close();
52 this.handleDBError(this.storageType
, error
as Error);
56 private getDBName(): string {
57 if (this.storageType
=== StorageType
.SQLITE
) {
58 return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`;
61 this.storageUri
.pathname
.replace(/(?:^\
/)|(?:\
/$
)/g
, '') ??
62 Constants
.DEFAULT_PERFORMANCE_RECORDS_DB_NAME
67 | Configuration
<IDatabaseDriver
<Connection
>>
68 | Options
<IDatabaseDriver
<Connection
>> {
70 metadataProvider
: TsMorphMetadataProvider
,
71 entities
: [PerformanceRecord
, PerformanceData
],
72 type: this.storageType
as MikroORMDBType
,
73 clientUrl
: this.getClientUrl(),
77 private getClientUrl(): string | undefined {
78 switch (this.storageType
) {
79 case StorageType
.SQLITE
:
80 case StorageType
.MARIA_DB
:
81 case StorageType
.MYSQL
:
82 return this.storageUri
.toString();