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