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