1 // Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import { DBName
, StorageType
} from
'../../types/Storage';
5 import Statistics from
'../../types/Statistics';
6 import { URL
} from
'url';
7 import Utils from
'../../utils/Utils';
8 import logger from
'../../utils/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.getDBNameFromStorageType(type)} error '${error.message}'${(!Utils.isNullOrUndefined(table) || !table) && ` in table or collection '${table}'`}: %j
`, error);
24 protected getDBNameFromStorageType(type: StorageType): DBName {
26 case StorageType.MARIA_DB:
27 return DBName.MARIA_DB;
28 case StorageType.MONGO_DB:
29 return DBName.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>;