| 1 | // Copyright Jerome Benoit. 2021-2024. All Rights Reserved. |
| 2 | |
| 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' |
| 5 | |
| 6 | import { Storage } from './Storage.js' |
| 7 | import { type PerformanceRecord, type Statistics, StorageType } from '../../types/index.js' |
| 8 | import { Constants } from '../../utils/index.js' |
| 9 | |
| 10 | export class MikroOrmStorage extends Storage { |
| 11 | private readonly storageType: StorageType |
| 12 | private orm?: SqliteORM | MariaDbORM |
| 13 | |
| 14 | constructor (storageUri: string, logPrefix: string, storageType: StorageType) { |
| 15 | super(storageUri, logPrefix) |
| 16 | this.storageType = storageType |
| 17 | this.dbName = this.getDBName() |
| 18 | } |
| 19 | |
| 20 | public async storePerformanceStatistics (performanceStatistics: Statistics): Promise<void> { |
| 21 | try { |
| 22 | this.setPerformanceStatistics(performanceStatistics) |
| 23 | await this.orm?.em.upsert({ |
| 24 | ...performanceStatistics, |
| 25 | statisticsData: Array.from(performanceStatistics.statisticsData, ([name, value]) => ({ |
| 26 | name, |
| 27 | ...value |
| 28 | })) |
| 29 | } satisfies PerformanceRecord) |
| 30 | } catch (error) { |
| 31 | this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public async open (): Promise<void> { |
| 36 | try { |
| 37 | if (this.orm == null) { |
| 38 | switch (this.storageType) { |
| 39 | case StorageType.SQLITE: |
| 40 | this.orm = await SqliteORM.init(this.getOptions() as SqliteOptions) |
| 41 | break |
| 42 | case StorageType.MARIA_DB: |
| 43 | case StorageType.MYSQL: |
| 44 | this.orm = await MariaDbORM.init(this.getOptions() as MariaDbOptions) |
| 45 | break |
| 46 | } |
| 47 | } |
| 48 | } catch (error) { |
| 49 | this.handleDBError(this.storageType, error as Error) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public async close (): Promise<void> { |
| 54 | this.clearPerformanceStatistics() |
| 55 | try { |
| 56 | if (this.orm != null) { |
| 57 | await this.orm.close() |
| 58 | delete this.orm |
| 59 | } |
| 60 | } catch (error) { |
| 61 | this.handleDBError(this.storageType, error as Error) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private getDBName (): string { |
| 66 | if (this.storageType === StorageType.SQLITE) { |
| 67 | return `${Constants.DEFAULT_PERFORMANCE_DIRECTORY}/${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db` |
| 68 | } |
| 69 | return this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '') |
| 70 | } |
| 71 | |
| 72 | private getOptions (): SqliteOptions | MariaDbOptions { |
| 73 | return { |
| 74 | dbName: this.dbName, |
| 75 | entities: ['./dist/types/orm/entities/*.js'], |
| 76 | entitiesTs: ['./src/types/orm/entities/*.ts'], |
| 77 | clientUrl: this.getClientUrl() |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private getClientUrl (): string | undefined { |
| 82 | switch (this.storageType) { |
| 83 | case StorageType.SQLITE: |
| 84 | case StorageType.MARIA_DB: |
| 85 | case StorageType.MYSQL: |
| 86 | return this.storageUri.toString() |
| 87 | } |
| 88 | } |
| 89 | } |