Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
c27c3eee | 2 | |
130783a7 | 3 | import { URL } from 'node:url'; |
c27c3eee | 4 | |
268a74bb JB |
5 | import { |
6 | DBName, | |
7 | type EmptyObject, | |
8 | type HandleErrorParams, | |
9 | type Statistics, | |
10 | StorageType, | |
11 | } from '../../types'; | |
60a74391 | 12 | import { Utils, logger } from '../../utils'; |
72f041bd JB |
13 | |
14 | export abstract class Storage { | |
1f5df42a | 15 | protected readonly storageUri: URL; |
2a370053 | 16 | protected readonly logPrefix: string; |
1895299d | 17 | protected dbName!: string; |
72f041bd | 18 | |
1f5df42a JB |
19 | constructor(storageUri: string, logPrefix: string) { |
20 | this.storageUri = new URL(storageUri); | |
72f041bd JB |
21 | this.logPrefix = logPrefix; |
22 | } | |
23 | ||
e7aeea18 JB |
24 | protected handleDBError( |
25 | type: StorageType, | |
26 | error: Error, | |
27 | table?: string, | |
28 | params: HandleErrorParams<EmptyObject> = { throwError: false } | |
29 | ): void { | |
8f3d04b0 JB |
30 | const inTableOrCollectionStr = |
31 | (!Utils.isNullOrUndefined(table) || !table) && ` in table or collection '${table}'`; | |
e7aeea18 | 32 | logger.error( |
8f3d04b0 JB |
33 | `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${ |
34 | error.message | |
32de5a57 | 35 | }'${inTableOrCollectionStr}:`, |
e7aeea18 JB |
36 | error |
37 | ); | |
e0a50bcd JB |
38 | if (params?.throwError) { |
39 | throw error; | |
40 | } | |
c27c3eee JB |
41 | } |
42 | ||
1895299d | 43 | protected getDBNameFromStorageType(type: StorageType): DBName | undefined { |
c27c3eee JB |
44 | switch (type) { |
45 | case StorageType.MARIA_DB: | |
a6b3c6c3 | 46 | return DBName.MARIA_DB; |
c27c3eee | 47 | case StorageType.MONGO_DB: |
a6b3c6c3 | 48 | return DBName.MONGO_DB; |
c27c3eee | 49 | case StorageType.MYSQL: |
a6b3c6c3 | 50 | return DBName.MYSQL; |
c27c3eee | 51 | case StorageType.SQLITE: |
a6b3c6c3 | 52 | return DBName.SQLITE; |
c27c3eee | 53 | } |
2a370053 JB |
54 | } |
55 | ||
56 | public abstract open(): void | Promise<void>; | |
57 | public abstract close(): void | Promise<void>; | |
e7aeea18 JB |
58 | public abstract storePerformanceStatistics( |
59 | performanceStatistics: Statistics | |
60 | ): void | Promise<void>; | |
72f041bd | 61 | } |