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
> = { throwError
: false, consoleOut
: false }
31 setDefaultErrorParams(params
, { throwError
: false, consoleOut
: false })
32 const inTableOrCollectionStr
= table
!= null && ` in table or collection '${table}'`
34 `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${
36 }'${inTableOrCollectionStr}:`,
39 if (params
.throwError
=== true) {
44 protected getDBNameFromStorageType (type: StorageType
): DBName
| undefined {
46 case StorageType
.SQLITE
:
48 case StorageType
.MARIA_DB
:
49 return DBName
.MARIA_DB
50 case StorageType
.MYSQL
:
52 case StorageType
.MONGO_DB
:
53 return DBName
.MONGO_DB
57 public getPerformanceStatistics (): IterableIterator
<Statistics
> {
58 return Storage
.performanceStatistics
.values()
61 protected setPerformanceStatistics (performanceStatistics
: Statistics
): void {
62 Storage
.performanceStatistics
.set(performanceStatistics
.id
, performanceStatistics
)
65 protected clearPerformanceStatistics (): void {
66 Storage
.performanceStatistics
.clear()
69 public abstract open (): void | Promise
<void>
70 public abstract close (): void | Promise
<void>
71 public abstract storePerformanceStatistics (
72 performanceStatistics
: Statistics
73 ): void | Promise
<void>