1 // Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
3 import { MikroORM
as MariaDbORM
, type Options
as MariaDbOptions
} from
'@mikro-orm/mariadb'
4 import { MikroORM
as SqliteORM
, type Options
as SqliteOptions
} from
'@mikro-orm/sqlite'
6 import { Storage
} from
'./Storage.js'
7 import { type PerformanceRecord
, type Statistics
, StorageType
} from
'../../types/index.js'
8 import { Constants
} from
'../../utils/index.js'
10 export class MikroOrmStorage
extends Storage
{
11 private readonly storageType
: StorageType
12 private orm
?: SqliteORM
| MariaDbORM
14 constructor (storageUri
: string, logPrefix
: string, storageType
: StorageType
) {
15 super(storageUri
, logPrefix
)
16 this.storageType
= storageType
17 this.dbName
= this.getDBName()
20 public async storePerformanceStatistics (performanceStatistics
: Statistics
): Promise
<void> {
22 await this.orm
?.em
.upsert({
23 ...performanceStatistics
,
24 statisticsData
: Array.from(performanceStatistics
.statisticsData
, ([name
, value
]) => ({
28 } satisfies PerformanceRecord
)
30 this.handleDBError(this.storageType
, error
as Error, Constants
.PERFORMANCE_RECORDS_TABLE
)
34 public async open (): Promise
<void> {
36 if (this.orm
== null) {
37 switch (this.storageType
) {
38 case StorageType
.SQLITE
:
39 this.orm
= await SqliteORM
.init(this.getOptions() as SqliteOptions
)
41 case StorageType
.MARIA_DB
:
42 case StorageType
.MYSQL
:
43 this.orm
= await MariaDbORM
.init(this.getOptions() as MariaDbOptions
)
48 this.handleDBError(this.storageType
, error
as Error)
52 public async close (): Promise
<void> {
54 if (this.orm
!= null) {
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_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`
67 return this.storageUri
.pathname
.replace(/(?:^\
/)|(?:\
/$
)/g
, '')
70 private getOptions (): SqliteOptions
| MariaDbOptions
{
73 entities
: ['./dist/types/orm/entities/*.js'],
74 entitiesTs
: ['./src/types/orm/entities/*.ts'],
75 clientUrl
: this.getClientUrl()
79 private getClientUrl (): string | undefined {
80 switch (this.storageType
) {
81 case StorageType
.SQLITE
:
82 case StorageType
.MARIA_DB
:
83 case StorageType
.MYSQL
:
84 return this.storageUri
.toString()