Add support for more DB engines via TypeORM
[e-mobility-charging-stations-simulator.git] / src / utils / performance-storage / Storage.ts
CommitLineData
c27c3eee
JB
1// Copyright Jerome Benoit. 2021. All Rights Reserved.
2
3import { DBType, StorageType } from '../../types/Storage';
4
72f041bd
JB
5import Statistics from '../../types/Statistics';
6import { URL } from 'url';
c27c3eee 7import Utils from '../Utils';
2a370053 8import logger from '../Logger';
72f041bd
JB
9
10export 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}