| 1 | // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
| 2 | |
| 3 | import { |
| 4 | Configuration, |
| 5 | Connection, |
| 6 | type IDatabaseDriver, |
| 7 | MikroORM, |
| 8 | type Options, |
| 9 | } from '@mikro-orm/core'; |
| 10 | import { TsMorphMetadataProvider } from '@mikro-orm/reflection'; |
| 11 | |
| 12 | import { Storage } from './Storage'; |
| 13 | import { |
| 14 | type MikroOrmDbType, |
| 15 | PerformanceData, |
| 16 | PerformanceRecord, |
| 17 | type Statistics, |
| 18 | StorageType, |
| 19 | } from '../../types'; |
| 20 | import { Constants } from '../../utils'; |
| 21 | |
| 22 | export class MikroOrmStorage extends Storage { |
| 23 | private storageType: StorageType; |
| 24 | private orm!: MikroORM | null; |
| 25 | |
| 26 | constructor(storageUri: string, logPrefix: string, storageType: StorageType) { |
| 27 | super(storageUri, logPrefix); |
| 28 | this.storageType = storageType; |
| 29 | this.dbName = this.getDBName(); |
| 30 | } |
| 31 | |
| 32 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 33 | public async storePerformanceStatistics(performanceStatistics: Statistics): Promise<void> { |
| 34 | try { |
| 35 | const performanceRecord = new PerformanceRecord(); |
| 36 | await this.orm?.em.persistAndFlush(performanceRecord); |
| 37 | } catch (error) { |
| 38 | this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public async open(): Promise<void> { |
| 43 | try { |
| 44 | if (!this?.orm) { |
| 45 | this.orm = await MikroORM.init(this.getOptions(), true); |
| 46 | } |
| 47 | } catch (error) { |
| 48 | this.handleDBError(this.storageType, error as Error); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public async close(): Promise<void> { |
| 53 | try { |
| 54 | if (this?.orm) { |
| 55 | await this.orm.close(); |
| 56 | this.orm = null; |
| 57 | } |
| 58 | } catch (error) { |
| 59 | this.handleDBError(this.storageType, error as Error); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private getDBName(): string { |
| 64 | if (this.storageType === StorageType.SQLITE) { |
| 65 | return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`; |
| 66 | } |
| 67 | return ( |
| 68 | this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ?? |
| 69 | Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | private getOptions(): |
| 74 | | Configuration<IDatabaseDriver<Connection>> |
| 75 | | Options<IDatabaseDriver<Connection>> { |
| 76 | return { |
| 77 | metadataProvider: TsMorphMetadataProvider, |
| 78 | entities: [PerformanceRecord, PerformanceData], |
| 79 | type: this.storageType as MikroOrmDbType, |
| 80 | clientUrl: this.getClientUrl(), |
| 81 | }; |
| 82 | } |
| 83 | |
| 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(); |
| 90 | } |
| 91 | } |
| 92 | } |