1 // Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import { URL
} from
'node:url';
8 type HandleErrorParams
,
12 import { Utils
, logger
} from
'../../utils';
14 export abstract class Storage
{
15 protected readonly storageUri
: URL
;
16 protected readonly logPrefix
: string;
17 protected dbName
!: string;
19 constructor(storageUri
: string, logPrefix
: string) {
20 this.storageUri
= new URL(storageUri
);
21 this.logPrefix
= logPrefix
;
24 protected handleDBError(
28 params
: HandleErrorParams
<EmptyObject
> = { throwError
: false }
30 const inTableOrCollectionStr
=
31 (!Utils
.isNullOrUndefined(table
) || !table
) && ` in table or collection '${table}'`;
33 `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${
35 }'${inTableOrCollectionStr}:`,
38 if (params
?.throwError
) {
43 protected getDBNameFromStorageType(type: StorageType
): DBName
| undefined {
45 case StorageType
.MARIA_DB
:
46 return DBName
.MARIA_DB
;
47 case StorageType
.MONGO_DB
:
48 return DBName
.MONGO_DB
;
49 case StorageType
.MYSQL
:
51 case StorageType
.SQLITE
:
56 public abstract open(): void | Promise
<void>;
57 public abstract close(): void | Promise
<void>;
58 public abstract storePerformanceStatistics(
59 performanceStatistics
: Statistics
60 ): void | Promise
<void>;