Apply prettier formating
[e-mobility-charging-stations-simulator.git] / src / performance / storage / Storage.ts
CommitLineData
c27c3eee
JB
1// Copyright Jerome Benoit. 2021. All Rights Reserved.
2
a6b3c6c3 3import { DBName, StorageType } from '../../types/Storage';
c27c3eee 4
717c1e56 5import { EmptyObject } from '../../types/EmptyObject';
e0a50bcd 6import { HandleErrorParams } from '../../types/Error';
72f041bd
JB
7import Statistics from '../../types/Statistics';
8import { URL } from 'url';
a6b3c6c3 9import Utils from '../../utils/Utils';
9f2e3130 10import logger from '../../utils/Logger';
72f041bd
JB
11
12export abstract class Storage {
1f5df42a 13 protected readonly storageUri: URL;
2a370053
JB
14 protected readonly logPrefix: string;
15 protected dbName: string;
72f041bd 16
1f5df42a
JB
17 constructor(storageUri: string, logPrefix: string) {
18 this.storageUri = new URL(storageUri);
72f041bd
JB
19 this.logPrefix = logPrefix;
20 }
21
e7aeea18
JB
22 protected handleDBError(
23 type: StorageType,
24 error: Error,
25 table?: string,
26 params: HandleErrorParams<EmptyObject> = { throwError: false }
27 ): void {
28 logger.error(
29 `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${error.message}'${
30 (!Utils.isNullOrUndefined(table) || !table) && ` in table or collection '${table}'`
31 }: %j`,
32 error
33 );
e0a50bcd
JB
34 if (params?.throwError) {
35 throw error;
36 }
c27c3eee
JB
37 }
38
a6b3c6c3 39 protected getDBNameFromStorageType(type: StorageType): DBName {
c27c3eee
JB
40 switch (type) {
41 case StorageType.MARIA_DB:
a6b3c6c3 42 return DBName.MARIA_DB;
c27c3eee 43 case StorageType.MONGO_DB:
a6b3c6c3 44 return DBName.MONGO_DB;
c27c3eee 45 case StorageType.MYSQL:
a6b3c6c3 46 return DBName.MYSQL;
c27c3eee 47 case StorageType.SQLITE:
a6b3c6c3 48 return DBName.SQLITE;
c27c3eee 49 }
2a370053
JB
50 }
51
52 public abstract open(): void | Promise<void>;
53 public abstract close(): void | Promise<void>;
e7aeea18
JB
54 public abstract storePerformanceStatistics(
55 performanceStatistics: Statistics
56 ): void | Promise<void>;
72f041bd 57}