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(
26 params
: HandleErrorParams
<EmptyObject
> = { throwError
: false }
29 `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${error.message}'${
30 (!Utils.isNullOrUndefined(table) || !table) && ` in table or collection
'${table}'`
34 if (params
?.throwError
) {
39 protected getDBNameFromStorageType(type: StorageType
): DBName
{
41 case StorageType
.MARIA_DB
:
42 return DBName
.MARIA_DB
;
43 case StorageType
.MONGO_DB
:
44 return DBName
.MONGO_DB
;
45 case StorageType
.MYSQL
:
47 case StorageType
.SQLITE
:
52 public abstract open(): void | Promise
<void>;
53 public abstract close(): void | Promise
<void>;
54 public abstract storePerformanceStatistics(
55 performanceStatistics
: Statistics
56 ): void | Promise
<void>;