Commit | Line | Data |
---|---|---|
a6b3c6c3 JB |
1 | // Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
3 | import { Configuration, Connection, IDatabaseDriver, MikroORM, Options } from '@mikro-orm/core'; | |
4 | import { MikroORMDBType, StorageType } from '../../types/Storage'; | |
5 | ||
6 | import Constants from '../../utils/Constants'; | |
7 | import { PerformanceData } from '../../types/orm/entities/PerformanceData'; | |
8 | import { PerformanceRecord } from '../../types/orm/entities/PerformanceRecord'; | |
9 | import Statistics from '../../types/Statistics'; | |
10 | import { Storage } from './Storage'; | |
ae8ee665 | 11 | import { TsMorphMetadataProvider } from '@mikro-orm/reflection'; |
a6b3c6c3 JB |
12 | |
13 | export class MikroORMStorage extends Storage { | |
14 | private storageType: StorageType; | |
a6ceb16a | 15 | private orm: MikroORM | null; |
a6b3c6c3 JB |
16 | |
17 | constructor(storageURI: string, logPrefix: string, storageType: StorageType) { | |
18 | super(storageURI, logPrefix); | |
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 JB |
56 | } |
57 | return this.storageURI.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ?? Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME; | |
58 | } | |
59 | ||
a6b3c6c3 JB |
60 | private getOptions(): Configuration<IDatabaseDriver<Connection>> | Options<IDatabaseDriver<Connection>> { |
61 | return { | |
ae8ee665 | 62 | metadataProvider: TsMorphMetadataProvider, |
a6b3c6c3 | 63 | entities: [PerformanceRecord, PerformanceData], |
a6b3c6c3 | 64 | type: this.storageType as MikroORMDBType, |
d5603918 | 65 | clientUrl: this.getClientUrl() |
a6b3c6c3 JB |
66 | }; |
67 | } | |
d5603918 JB |
68 | |
69 | private getClientUrl(): string { | |
70 | switch (this.storageType) { | |
71 | case StorageType.SQLITE: | |
d5603918 JB |
72 | case StorageType.MARIA_DB: |
73 | case StorageType.MYSQL: | |
74 | return this.storageURI.toString(); | |
75 | } | |
76 | } | |
a6b3c6c3 JB |
77 | } |
78 |