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