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
18 private static readonly performanceStatistics
= new Map
<string, Statistics
>()
20 constructor (storageUri
: string, logPrefix
: string) {
21 this.storageUri
= new URL(storageUri
)
22 this.logPrefix
= logPrefix
25 protected handleDBError (
29 params
: HandleErrorParams
<EmptyObject
> = {
34 params
= setDefaultErrorParams(params
, { throwError
: false, consoleOut
: false })
35 const inTableOrCollectionStr
= table
!= null && ` in table or collection '${table}'`
37 `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${
39 }'${inTableOrCollectionStr}:`,
42 if (params
.throwError
=== true) {
47 protected getDBNameFromStorageType (type: StorageType
): DBName
| undefined {
49 case StorageType
.SQLITE
:
51 case StorageType
.MARIA_DB
:
52 return DBName
.MARIA_DB
53 case StorageType
.MYSQL
:
55 case StorageType
.MONGO_DB
:
56 return DBName
.MONGO_DB
60 public getPerformanceStatistics (): IterableIterator
<Statistics
> {
61 return Storage
.performanceStatistics
.values()
64 protected setPerformanceStatistics (performanceStatistics
: Statistics
): void {
65 Storage
.performanceStatistics
.set(performanceStatistics
.id
, performanceStatistics
)
68 protected clearPerformanceStatistics (): void {
69 Storage
.performanceStatistics
.clear()
72 public abstract open (): void | Promise
<void>
73 public abstract close (): void | Promise
<void>
74 public abstract storePerformanceStatistics (
75 performanceStatistics
: Statistics
76 ): void | Promise
<void>