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
} 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 handleDBStorageError (
29 params
: HandleErrorParams
<EmptyObject
> = {
41 const inTableOrCollectionStr
= table
!= null && ` in table or collection '${table}'`
43 `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${
45 }'${inTableOrCollectionStr}:`,
48 if (params
.throwError
=== true) {
53 protected getDBNameFromStorageType (type: StorageType
): DBName
| undefined {
55 case StorageType
.SQLITE
:
57 case StorageType
.MARIA_DB
:
58 return DBName
.MARIA_DB
59 case StorageType
.MYSQL
:
61 case StorageType
.MONGO_DB
:
62 return DBName
.MONGO_DB
66 public getPerformanceStatistics (): IterableIterator
<Statistics
> {
67 return Storage
.performanceStatistics
.values()
70 protected setPerformanceStatistics (performanceStatistics
: Statistics
): void {
71 Storage
.performanceStatistics
.set(performanceStatistics
.id
, performanceStatistics
)
74 protected clearPerformanceStatistics (): void {
75 Storage
.performanceStatistics
.clear()
78 public abstract open (): void | Promise
<void>
79 public abstract close (): void | Promise
<void>
80 public abstract storePerformanceStatistics (
81 performanceStatistics
: Statistics
82 ): void | Promise
<void>