Commit | Line | Data |
---|---|---|
c27c3eee JB |
1 | // Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
a6b3c6c3 | 3 | import { DBName, StorageType } from '../../types/Storage'; |
c27c3eee | 4 | |
e0a50bcd | 5 | import { HandleErrorParams } from '../../types/Error'; |
72f041bd JB |
6 | import Statistics from '../../types/Statistics'; |
7 | import { URL } from 'url'; | |
a6b3c6c3 | 8 | import Utils from '../../utils/Utils'; |
9f2e3130 | 9 | import logger from '../../utils/Logger'; |
72f041bd JB |
10 | |
11 | export abstract class Storage { | |
1f5df42a | 12 | protected readonly storageUri: URL; |
2a370053 JB |
13 | protected readonly logPrefix: string; |
14 | protected dbName: string; | |
72f041bd | 15 | |
1f5df42a JB |
16 | constructor(storageUri: string, logPrefix: string) { |
17 | this.storageUri = new URL(storageUri); | |
72f041bd JB |
18 | this.logPrefix = logPrefix; |
19 | } | |
20 | ||
e0a50bcd | 21 | protected handleDBError(type: StorageType, error: Error, table?: string, params: HandleErrorParams = { throwError: false }): void { |
9f2e3130 | 22 | logger.error(`${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${error.message}'${(!Utils.isNullOrUndefined(table) || !table) && ` in table or collection '${table}'`}: %j`, error); |
e0a50bcd JB |
23 | if (params?.throwError) { |
24 | throw error; | |
25 | } | |
c27c3eee JB |
26 | } |
27 | ||
a6b3c6c3 | 28 | protected getDBNameFromStorageType(type: StorageType): DBName { |
c27c3eee JB |
29 | switch (type) { |
30 | case StorageType.MARIA_DB: | |
a6b3c6c3 | 31 | return DBName.MARIA_DB; |
c27c3eee | 32 | case StorageType.MONGO_DB: |
a6b3c6c3 | 33 | return DBName.MONGO_DB; |
c27c3eee | 34 | case StorageType.MYSQL: |
a6b3c6c3 | 35 | return DBName.MYSQL; |
c27c3eee | 36 | case StorageType.SQLITE: |
a6b3c6c3 | 37 | return DBName.SQLITE; |
c27c3eee | 38 | } |
2a370053 JB |
39 | } |
40 | ||
41 | public abstract open(): void | Promise<void>; | |
42 | public abstract close(): void | Promise<void>; | |
43 | public abstract storePerformanceStatistics(performanceStatistics: Statistics): void | Promise<void>; | |
72f041bd | 44 | } |