d85d8c415efdd1b254694976597bfd722af4c42d
1 // Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import { URL
} from
'url';
5 import type { EmptyObject
} from
'../../types/EmptyObject';
6 import type { HandleErrorParams
} from
'../../types/Error';
7 import type Statistics from
'../../types/Statistics';
8 import { DBName
, StorageType
} from
'../../types/Storage';
9 import logger from
'../../utils/Logger';
10 import Utils from
'../../utils/Utils';
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 }
28 const inTableOrCollectionStr
=
29 (!Utils
.isNullOrUndefined(table
) || !table
) && ` in table or collection '${table}'`;
31 `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${
33 }'${inTableOrCollectionStr}:`,
36 if (params
?.throwError
) {
41 protected getDBNameFromStorageType(type: StorageType
): DBName
{
43 case StorageType
.MARIA_DB
:
44 return DBName
.MARIA_DB
;
45 case StorageType
.MONGO_DB
:
46 return DBName
.MONGO_DB
;
47 case StorageType
.MYSQL
:
49 case StorageType
.SQLITE
:
54 public abstract open(): void | Promise
<void>;
55 public abstract close(): void | Promise
<void>;
56 public abstract storePerformanceStatistics(
57 performanceStatistics
: Statistics
58 ): void | Promise
<void>;