refactor: switch eslint configuration to strict type checking
[e-mobility-charging-stations-simulator.git] / src / performance / storage / Storage.ts
CommitLineData
edd13439 1// Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
c27c3eee 2
66a7748d 3import { URL } from 'node:url'
c27c3eee 4
268a74bb
JB
5import {
6 DBName,
7 type EmptyObject,
8 type HandleErrorParams,
9 type Statistics,
66a7748d
JB
10 StorageType
11} from '../../types/index.js'
12import { logger, setDefaultErrorParams } from '../../utils/index.js'
72f041bd
JB
13
14export abstract class Storage {
66a7748d
JB
15 protected readonly storageUri: URL
16 protected readonly logPrefix: string
17 protected dbName!: string
72f041bd 18
66a7748d
JB
19 constructor (storageUri: string, logPrefix: string) {
20 this.storageUri = new URL(storageUri)
21 this.logPrefix = logPrefix
72f041bd
JB
22 }
23
66a7748d 24 protected handleDBError (
e7aeea18
JB
25 type: StorageType,
26 error: Error,
27 table?: string,
66a7748d 28 params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false }
e7aeea18 29 ): void {
66a7748d
JB
30 setDefaultErrorParams(params, { throwError: false, consoleOut: false })
31 const inTableOrCollectionStr = table != null && ` in table or collection '${table}'`
e7aeea18 32 logger.error(
8f3d04b0
JB
33 `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${
34 error.message
32de5a57 35 }'${inTableOrCollectionStr}:`,
66a7748d
JB
36 error
37 )
5199f9fd 38 if (params.throwError === true) {
66a7748d 39 throw error
e0a50bcd 40 }
c27c3eee
JB
41 }
42
66a7748d 43 protected getDBNameFromStorageType (type: StorageType): DBName | undefined {
c27c3eee
JB
44 switch (type) {
45 case StorageType.MARIA_DB:
66a7748d 46 return DBName.MARIA_DB
c27c3eee 47 case StorageType.MONGO_DB:
66a7748d 48 return DBName.MONGO_DB
c27c3eee 49 case StorageType.MYSQL:
66a7748d 50 return DBName.MYSQL
c27c3eee 51 case StorageType.SQLITE:
66a7748d 52 return DBName.SQLITE
c27c3eee 53 }
2a370053
JB
54 }
55
66a7748d
JB
56 public abstract open (): void | Promise<void>
57 public abstract close (): void | Promise<void>
58 public abstract storePerformanceStatistics (
59 performanceStatistics: Statistics
60 ): void | Promise<void>
72f041bd 61}