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