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 | |
c1565026 | 12 | import { Storage } from './Storage'; |
268a74bb | 13 | import { |
1abe401e | 14 | type MikroOrmDbType, |
268a74bb JB |
15 | PerformanceData, |
16 | PerformanceRecord, | |
17 | type Statistics, | |
18 | StorageType, | |
19 | } from '../../types'; | |
60a74391 | 20 | import { Constants } from '../../utils'; |
a6b3c6c3 | 21 | |
100a5301 | 22 | export class MikroOrmStorage extends Storage { |
a6b3c6c3 | 23 | private storageType: StorageType; |
f1c729e0 | 24 | private orm?: MikroORM; |
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 | ||
e1d9a0f4 | 32 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
a6b3c6c3 JB |
33 | public async storePerformanceStatistics(performanceStatistics: Statistics): Promise<void> { |
34 | try { | |
35 | const performanceRecord = new PerformanceRecord(); | |
1895299d | 36 | await this.orm?.em.persistAndFlush(performanceRecord); |
a6b3c6c3 | 37 | } catch (error) { |
dc100e97 | 38 | this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE); |
a6b3c6c3 JB |
39 | } |
40 | } | |
41 | ||
42 | public async open(): Promise<void> { | |
a6ceb16a JB |
43 | try { |
44 | if (!this?.orm) { | |
45 | this.orm = await MikroORM.init(this.getOptions(), true); | |
46 | } | |
47 | } catch (error) { | |
dc100e97 | 48 | this.handleDBError(this.storageType, error as Error); |
a6ceb16a | 49 | } |
a6b3c6c3 JB |
50 | } |
51 | ||
52 | public async close(): Promise<void> { | |
a6ceb16a JB |
53 | try { |
54 | if (this?.orm) { | |
55 | await this.orm.close(); | |
f1c729e0 | 56 | delete this?.orm; |
a6ceb16a JB |
57 | } |
58 | } catch (error) { | |
dc100e97 | 59 | this.handleDBError(this.storageType, error as Error); |
a6ceb16a | 60 | } |
a6b3c6c3 JB |
61 | } |
62 | ||
d5603918 JB |
63 | private getDBName(): string { |
64 | if (this.storageType === StorageType.SQLITE) { | |
ae8ee665 | 65 | return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db`; |
d5603918 | 66 | } |
e7aeea18 JB |
67 | return ( |
68 | this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ?? | |
69 | Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME | |
70 | ); | |
d5603918 JB |
71 | } |
72 | ||
e7aeea18 JB |
73 | private getOptions(): |
74 | | Configuration<IDatabaseDriver<Connection>> | |
75 | | Options<IDatabaseDriver<Connection>> { | |
a6b3c6c3 | 76 | return { |
ae8ee665 | 77 | metadataProvider: TsMorphMetadataProvider, |
a6b3c6c3 | 78 | entities: [PerformanceRecord, PerformanceData], |
1abe401e | 79 | type: this.storageType as MikroOrmDbType, |
e7aeea18 | 80 | clientUrl: this.getClientUrl(), |
a6b3c6c3 JB |
81 | }; |
82 | } | |
d5603918 | 83 | |
1895299d | 84 | private getClientUrl(): string | undefined { |
d5603918 JB |
85 | switch (this.storageType) { |
86 | case StorageType.SQLITE: | |
d5603918 JB |
87 | case StorageType.MARIA_DB: |
88 | case StorageType.MYSQL: | |
1f5df42a | 89 | return this.storageUri.toString(); |
d5603918 JB |
90 | } |
91 | } | |
a6b3c6c3 | 92 | } |