1 // Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
3 import { URL
} from
'node:url';
8 type HandleErrorParams
,
12 import { isNullOrUndefined
, logger
, setDefaultErrorParams
} 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, consoleOut
: false },
30 setDefaultErrorParams(params
, { throwError
: false, consoleOut
: false });
31 const inTableOrCollectionStr
=
32 (!isNullOrUndefined(table
) || !table
) && ` in table or collection '${table}'`;
34 `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${
36 }'${inTableOrCollectionStr}:`,
39 if (params
?.throwError
) {
44 protected getDBNameFromStorageType(type: StorageType
): DBName
| undefined {
46 case StorageType
.MARIA_DB
:
47 return DBName
.MARIA_DB
;
48 case StorageType
.MONGO_DB
:
49 return DBName
.MONGO_DB
;
50 case StorageType
.MYSQL
:
52 case StorageType
.SQLITE
:
57 public abstract open(): void | Promise
<void>;
58 public abstract close(): void | Promise
<void>;
59 public abstract storePerformanceStatistics(
60 performanceStatistics
: Statistics
,
61 ): void | Promise
<void>;