1 // Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
3 import { URL
} from
'node:url'
8 type HandleErrorParams
,
11 } from
'../../types/index.js'
12 import { logger
, setDefaultErrorParams
} from
'../../utils/index.js'
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
= table
!= null && ` in table or collection '${table}'`
33 `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${
35 }'${inTableOrCollectionStr}:`,
38 if (params
.throwError
=== true) {
43 protected getDBNameFromStorageType (type: StorageType
): DBName
| undefined {
45 case StorageType
.SQLITE
:
47 case StorageType
.MARIA_DB
:
48 return DBName
.MARIA_DB
49 case StorageType
.MYSQL
:
51 case StorageType
.MONGO_DB
:
52 return DBName
.MONGO_DB
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>