-// Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
+// Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
import { MongoClient } from 'mongodb'
-import { Storage } from './Storage.js'
import { BaseError } from '../../exception/index.js'
import { type Statistics, StorageType } from '../../types/index.js'
import { Constants } from '../../utils/index.js'
+import { Storage } from './Storage.js'
export class MongoDBStorage extends Storage {
private readonly client?: MongoClient
super(storageUri, logPrefix)
this.client = new MongoClient(this.storageUri.toString())
this.connected = false
- this.dbName =
- this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '') ??
- Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME
+ this.dbName = this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '')
}
public async storePerformanceStatistics (performanceStatistics: Statistics): Promise<void> {
try {
+ this.setPerformanceStatistics(performanceStatistics)
this.checkDBConnection()
await this.client
?.db(this.dbName)
public async open (): Promise<void> {
try {
- if (!this.connected && this?.client != null) {
+ if (!this.connected && this.client != null) {
await this.client.connect()
this.connected = true
}
}
public async close (): Promise<void> {
+ this.clearPerformanceStatistics()
try {
- if (this.connected && this?.client != null) {
+ if (this.connected && this.client != null) {
await this.client.close()
this.connected = false
}
}
private checkDBConnection (): void {
- if (this?.client == null) {
+ if (this.client == null) {
throw new BaseError(
`${this.logPrefix} ${this.getDBNameFromStorageType(
StorageType.MONGO_DB