1 // Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import { DBType
, StorageType
} from
'../../types/Storage';
5 import Statistics from
'../../types/Statistics';
6 import { URL
} from
'url';
7 import Utils from
'../Utils';
8 import logger from
'../Logger';
10 export abstract class Storage
{
11 protected readonly storageURI
: URL
;
12 protected readonly logPrefix
: string;
13 protected dbName
: string;
15 constructor(storageURI
: string, logPrefix
: string) {
16 this.storageURI
= new URL(storageURI
);
17 this.logPrefix
= logPrefix
;
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);
24 protected getDBTypeFromStorageType(type: StorageType): DBType {
26 case StorageType.MARIA_DB:
27 return DBType.MARIA_DB;
28 case StorageType.MONGO_DB:
29 return DBType.MONGO_DB;
30 case StorageType.MYSQL:
32 case StorageType.SQLITE:
37 public abstract open(): void | Promise<void>;
38 public abstract close(): void | Promise<void>;
39 public abstract storePerformanceStatistics(performanceStatistics: Statistics): void | Promise<void>;