1 // Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
9 } from
'@mikro-orm/core';
10 import { TsMorphMetadataProvider
} from
'@mikro-orm/reflection';
12 import { Storage
} from
'./Storage';
20 import { Constants
} from
'../../utils';
22 export class MikroOrmStorage
extends Storage
{
23 private storageType
: StorageType
;
24 private orm
!: MikroORM
| null;
26 constructor(storageUri
: string, logPrefix
: string, storageType
: StorageType
) {
27 super(storageUri
, logPrefix
);
28 this.storageType
= storageType
;
29 this.dbName
= this.getDBName();
32 // eslint-disable-next-line @typescript-eslint/no-unused-vars
33 public async storePerformanceStatistics(performanceStatistics
: Statistics
): Promise
<void> {
35 const performanceRecord
= new PerformanceRecord();
36 await this.orm
?.em
.persistAndFlush(performanceRecord
);
38 this.handleDBError(this.storageType
, error
as Error, Constants
.PERFORMANCE_RECORDS_TABLE
);
42 public async open(): Promise
<void> {
45 this.orm
= await MikroORM
.init(this.getOptions(), true);
48 this.handleDBError(this.storageType
, error
as Error);
52 public async close(): Promise
<void> {
55 await this.orm
.close();
59 this.handleDBError(this.storageType
, error
as Error);
63 private getDBName(): string {
64 if (this.storageType
=== StorageType
.SQLITE
) {
65 return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`;
68 this.storageUri
.pathname
.replace(/(?:^\
/)|(?:\
/$
)/g
, '') ??
69 Constants
.DEFAULT_PERFORMANCE_RECORDS_DB_NAME
74 | Configuration
<IDatabaseDriver
<Connection
>>
75 | Options
<IDatabaseDriver
<Connection
>> {
77 metadataProvider
: TsMorphMetadataProvider
,
78 entities
: [PerformanceRecord
, PerformanceData
],
79 type: this.storageType
as MikroOrmDbType
,
80 clientUrl
: this.getClientUrl(),
84 private getClientUrl(): string | undefined {
85 switch (this.storageType
) {
86 case StorageType
.SQLITE
:
87 case StorageType
.MARIA_DB
:
88 case StorageType
.MYSQL
:
89 return this.storageUri
.toString();