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.js'
19 } from
'../../types/index.js'
20 import { Constants
} from
'../../utils/index.js'
22 export class MikroOrmStorage
extends Storage
{
23 private readonly storageType
: StorageType
24 private orm
?: MikroORM
26 constructor (storageUri
: string, logPrefix
: string, storageType
: StorageType
) {
27 super(storageUri
, logPrefix
)
28 this.storageType
= storageType
29 this.dbName
= this.getDBName()
32 public async storePerformanceStatistics (performanceStatistics
: Statistics
): Promise
<void> {
34 const performanceRecord
= new PerformanceRecord()
35 await this.orm
?.em
.persistAndFlush(performanceRecord
)
37 this.handleDBError(this.storageType
, error
as Error, Constants
.PERFORMANCE_RECORDS_TABLE
)
41 public async open (): Promise
<void> {
43 if (this?.orm
== null) {
44 this.orm
= await MikroORM
.init(this.getOptions(), true)
47 this.handleDBError(this.storageType
, error
as Error)
51 public async close (): Promise
<void> {
53 if (this?.orm
!= null) {
54 await this.orm
.close()
58 this.handleDBError(this.storageType
, error
as Error)
62 private getDBName (): string {
63 if (this.storageType
=== StorageType
.SQLITE
) {
64 return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`
67 this.storageUri
.pathname
.replace(/(?:^\
/)|(?:\
/$
)/g
, '') ??
68 Constants
.DEFAULT_PERFORMANCE_RECORDS_DB_NAME
72 private getOptions ():
73 | Configuration
<IDatabaseDriver
<Connection
>>
74 | Options
<IDatabaseDriver
<Connection
>> {
76 metadataProvider
: TsMorphMetadataProvider
,
77 entities
: [PerformanceRecord
, PerformanceData
],
78 type: this.storageType
as MikroOrmDbType
,
79 clientUrl
: this.getClientUrl()
83 private getClientUrl (): string | undefined {
84 switch (this.storageType
) {
85 case StorageType
.SQLITE
:
86 case StorageType
.MARIA_DB
:
87 case StorageType
.MYSQL
:
88 return this.storageUri
.toString()