Commit | Line | Data |
---|---|---|
a19b897d | 1 | // Copyright Jerome Benoit. 2021-2024. All Rights Reserved. |
a6b3c6c3 | 2 | |
4ccf551d JB |
3 | import { MikroORM as MariaDbORM, type Options as MariaDbOptions } from '@mikro-orm/mariadb' |
4 | import { MikroORM as SqliteORM, type Options as SqliteOptions } from '@mikro-orm/sqlite' | |
a6b3c6c3 | 5 | |
66a7748d | 6 | import { Storage } from './Storage.js' |
4ccf551d | 7 | import { type Statistics, StorageType } from '../../types/index.js' |
66a7748d | 8 | import { Constants } from '../../utils/index.js' |
a6b3c6c3 | 9 | |
100a5301 | 10 | export class MikroOrmStorage extends Storage { |
66a7748d | 11 | private readonly storageType: StorageType |
4ccf551d | 12 | private orm?: SqliteORM | MariaDbORM |
a6b3c6c3 | 13 | |
66a7748d JB |
14 | constructor (storageUri: string, logPrefix: string, storageType: StorageType) { |
15 | super(storageUri, logPrefix) | |
16 | this.storageType = storageType | |
17 | this.dbName = this.getDBName() | |
a6b3c6c3 JB |
18 | } |
19 | ||
66a7748d | 20 | public async storePerformanceStatistics (performanceStatistics: Statistics): Promise<void> { |
a6b3c6c3 | 21 | try { |
4ccf551d JB |
22 | // TODO: build PerformanceRecord entity |
23 | await this.orm?.em.persistAndFlush({}) | |
a6b3c6c3 | 24 | } catch (error) { |
66a7748d | 25 | this.handleDBError(this.storageType, error as Error, Constants.PERFORMANCE_RECORDS_TABLE) |
a6b3c6c3 JB |
26 | } |
27 | } | |
28 | ||
66a7748d | 29 | public async open (): Promise<void> { |
a6ceb16a | 30 | try { |
5199f9fd | 31 | if (this.orm == null) { |
4ccf551d JB |
32 | switch (this.storageType) { |
33 | case StorageType.SQLITE: | |
34 | this.orm = await SqliteORM.init(this.getOptions() as SqliteOptions) | |
35 | break | |
36 | case StorageType.MARIA_DB: | |
37 | case StorageType.MYSQL: | |
38 | this.orm = await MariaDbORM.init(this.getOptions() as MariaDbOptions) | |
39 | break | |
40 | } | |
a6ceb16a JB |
41 | } |
42 | } catch (error) { | |
66a7748d | 43 | this.handleDBError(this.storageType, error as Error) |
a6ceb16a | 44 | } |
a6b3c6c3 JB |
45 | } |
46 | ||
66a7748d | 47 | public async close (): Promise<void> { |
a6ceb16a | 48 | try { |
5199f9fd | 49 | if (this.orm != null) { |
66a7748d | 50 | await this.orm.close() |
5199f9fd | 51 | delete this.orm |
a6ceb16a JB |
52 | } |
53 | } catch (error) { | |
66a7748d | 54 | this.handleDBError(this.storageType, error as Error) |
a6ceb16a | 55 | } |
a6b3c6c3 JB |
56 | } |
57 | ||
66a7748d | 58 | private getDBName (): string { |
d5603918 | 59 | if (this.storageType === StorageType.SQLITE) { |
66a7748d | 60 | return `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db` |
d5603918 | 61 | } |
5199f9fd | 62 | return this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '') |
d5603918 JB |
63 | } |
64 | ||
4ccf551d | 65 | private getOptions (): SqliteOptions | MariaDbOptions { |
a6b3c6c3 | 66 | return { |
4ccf551d JB |
67 | dbName: this.dbName, |
68 | entities: ['./dist/types/orm/entities/*.js'], | |
69 | entitiesTs: ['./src/types/orm/entities/*.ts'], | |
66a7748d JB |
70 | clientUrl: this.getClientUrl() |
71 | } | |
a6b3c6c3 | 72 | } |
d5603918 | 73 | |
66a7748d | 74 | private getClientUrl (): string | undefined { |
d5603918 JB |
75 | switch (this.storageType) { |
76 | case StorageType.SQLITE: | |
d5603918 JB |
77 | case StorageType.MARIA_DB: |
78 | case StorageType.MYSQL: | |
66a7748d | 79 | return this.storageUri.toString() |
d5603918 JB |
80 | } |
81 | } | |
a6b3c6c3 | 82 | } |