Commit | Line | Data |
---|---|---|
a19b897d | 1 | // Copyright Jerome Benoit. 2021-2024. All Rights Reserved. |
c27c3eee | 2 | |
66a7748d | 3 | import { URL } from 'node:url' |
c27c3eee | 4 | |
268a74bb JB |
5 | import { |
6 | DBName, | |
7 | type EmptyObject, | |
8 | type HandleErrorParams, | |
9 | type Statistics, | |
66a7748d JB |
10 | StorageType |
11 | } from '../../types/index.js' | |
64c14c99 | 12 | import { logger } from '../../utils/index.js' |
72f041bd JB |
13 | |
14 | export abstract class Storage { | |
66a7748d JB |
15 | protected readonly storageUri: URL |
16 | protected readonly logPrefix: string | |
17 | protected dbName!: string | |
a66bbcfe | 18 | private static readonly performanceStatistics = new Map<string, Statistics>() |
72f041bd | 19 | |
66a7748d JB |
20 | constructor (storageUri: string, logPrefix: string) { |
21 | this.storageUri = new URL(storageUri) | |
22 | this.logPrefix = logPrefix | |
72f041bd JB |
23 | } |
24 | ||
a03b18c4 | 25 | protected handleDBStorageError ( |
e7aeea18 JB |
26 | type: StorageType, |
27 | error: Error, | |
28 | table?: string, | |
48847bc0 JB |
29 | params: HandleErrorParams<EmptyObject> = { |
30 | throwError: false, | |
31 | consoleOut: false | |
32 | } | |
e7aeea18 | 33 | ): void { |
64c14c99 JB |
34 | params = { |
35 | ...{ | |
36 | throwError: false, | |
37 | consoleOut: false | |
38 | }, | |
39 | ...params | |
40 | } | |
66a7748d | 41 | const inTableOrCollectionStr = table != null && ` in table or collection '${table}'` |
e7aeea18 | 42 | logger.error( |
8f3d04b0 JB |
43 | `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${ |
44 | error.message | |
32de5a57 | 45 | }'${inTableOrCollectionStr}:`, |
66a7748d JB |
46 | error |
47 | ) | |
5199f9fd | 48 | if (params.throwError === true) { |
66a7748d | 49 | throw error |
e0a50bcd | 50 | } |
c27c3eee JB |
51 | } |
52 | ||
66a7748d | 53 | protected getDBNameFromStorageType (type: StorageType): DBName | undefined { |
c27c3eee | 54 | switch (type) { |
4ccf551d JB |
55 | case StorageType.SQLITE: |
56 | return DBName.SQLITE | |
c27c3eee | 57 | case StorageType.MARIA_DB: |
66a7748d | 58 | return DBName.MARIA_DB |
c27c3eee | 59 | case StorageType.MYSQL: |
66a7748d | 60 | return DBName.MYSQL |
4ccf551d JB |
61 | case StorageType.MONGO_DB: |
62 | return DBName.MONGO_DB | |
c27c3eee | 63 | } |
2a370053 JB |
64 | } |
65 | ||
a66bbcfe JB |
66 | public getPerformanceStatistics (): IterableIterator<Statistics> { |
67 | return Storage.performanceStatistics.values() | |
68 | } | |
69 | ||
70 | protected setPerformanceStatistics (performanceStatistics: Statistics): void { | |
71 | Storage.performanceStatistics.set(performanceStatistics.id, performanceStatistics) | |
72 | } | |
73 | ||
74 | protected clearPerformanceStatistics (): void { | |
75 | Storage.performanceStatistics.clear() | |
76 | } | |
77 | ||
66a7748d JB |
78 | public abstract open (): void | Promise<void> |
79 | public abstract close (): void | Promise<void> | |
80 | public abstract storePerformanceStatistics ( | |
81 | performanceStatistics: Statistics | |
82 | ): void | Promise<void> | |
72f041bd | 83 | } |