c4a0ce9c5669d2516554d161b11f8234a8e08c09
1 // Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import { DBName
, StorageType
} from
'../../types/Storage';
5 import { EmptyObject
} from
'../../types/EmptyObject';
6 import { HandleErrorParams
} from
'../../types/Error';
7 import Statistics from
'../../types/Statistics';
8 import { URL
} from
'url';
9 import Utils from
'../../utils/Utils';
10 import logger from
'../../utils/Logger';
12 export abstract class Storage
{
13 protected readonly storageUri
: URL
;
14 protected readonly logPrefix
: string;
15 protected dbName
: string;
17 constructor(storageUri
: string, logPrefix
: string) {
18 this.storageUri
= new URL(storageUri
);
19 this.logPrefix
= logPrefix
;
22 protected handleDBError(type: StorageType
, error
: Error, table
?: string, params
: HandleErrorParams
<EmptyObject
> = { throwError
: false }): void {
23 logger
.error(`${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${error.message}'${(!Utils.isNullOrUndefined(table) || !table) && ` in table or collection '${table}'`}: %j
`, error);
24 if (params?.throwError) {
29 protected getDBNameFromStorageType(type: StorageType): DBName {
31 case StorageType.MARIA_DB:
32 return DBName.MARIA_DB;
33 case StorageType.MONGO_DB:
34 return DBName.MONGO_DB;
35 case StorageType.MYSQL:
37 case StorageType.SQLITE:
42 public abstract open(): void | Promise<void>;
43 public abstract close(): void | Promise<void>;
44 public abstract storePerformanceStatistics(performanceStatistics: Statistics): void | Promise<void>;