Commit | Line | Data |
---|---|---|
a19b897d | 1 | // Copyright Jerome Benoit. 2021-2024. All Rights Reserved. |
c27c3eee | 2 | |
66a7748d | 3 | import { MongoClient } from 'mongodb' |
8114d10e | 4 | |
66a7748d JB |
5 | import { BaseError } from '../../exception/index.js' |
6 | import { type Statistics, StorageType } from '../../types/index.js' | |
7 | import { Constants } from '../../utils/index.js' | |
4c3f6c20 | 8 | import { Storage } from './Storage.js' |
72f041bd JB |
9 | |
10 | export class MongoDBStorage extends Storage { | |
66a7748d JB |
11 | private readonly client?: MongoClient |
12 | private connected: boolean | |
2a370053 | 13 | |
66a7748d JB |
14 | constructor (storageUri: string, logPrefix: string) { |
15 | super(storageUri, logPrefix) | |
16 | this.client = new MongoClient(this.storageUri.toString()) | |
17 | this.connected = false | |
5199f9fd | 18 | this.dbName = this.storageUri.pathname.replace(/(?:^\/)|(?:\/$)/g, '') |
72f041bd JB |
19 | } |
20 | ||
66a7748d | 21 | public async storePerformanceStatistics (performanceStatistics: Statistics): Promise<void> { |
2a370053 | 22 | try { |
a66bbcfe | 23 | this.setPerformanceStatistics(performanceStatistics) |
66a7748d | 24 | this.checkDBConnection() |
e7aeea18 | 25 | await this.client |
72092cfc | 26 | ?.db(this.dbName) |
e7aeea18 | 27 | .collection<Statistics>(Constants.PERFORMANCE_RECORDS_TABLE) |
48847bc0 JB |
28 | .replaceOne({ id: performanceStatistics.id }, performanceStatistics, { |
29 | upsert: true | |
30 | }) | |
2a370053 | 31 | } catch (error) { |
66a7748d | 32 | this.handleDBError(StorageType.MONGO_DB, error as Error, Constants.PERFORMANCE_RECORDS_TABLE) |
2a370053 | 33 | } |
72f041bd JB |
34 | } |
35 | ||
66a7748d | 36 | public async open (): Promise<void> { |
2a370053 | 37 | try { |
5199f9fd | 38 | if (!this.connected && this.client != null) { |
66a7748d JB |
39 | await this.client.connect() |
40 | this.connected = true | |
2a370053 JB |
41 | } |
42 | } catch (error) { | |
66a7748d | 43 | this.handleDBError(StorageType.MONGO_DB, error as Error) |
2a370053 JB |
44 | } |
45 | } | |
72f041bd | 46 | |
66a7748d | 47 | public async close (): Promise<void> { |
a66bbcfe | 48 | this.clearPerformanceStatistics() |
2a370053 | 49 | try { |
5199f9fd | 50 | if (this.connected && this.client != null) { |
66a7748d JB |
51 | await this.client.close() |
52 | this.connected = false | |
2a370053 JB |
53 | } |
54 | } catch (error) { | |
66a7748d | 55 | this.handleDBError(StorageType.MONGO_DB, error as Error) |
2a370053 JB |
56 | } |
57 | } | |
58 | ||
66a7748d | 59 | private checkDBConnection (): void { |
5199f9fd | 60 | if (this.client == null) { |
7b5dbe91 | 61 | throw new BaseError( |
e7aeea18 | 62 | `${this.logPrefix} ${this.getDBNameFromStorageType( |
66a7748d JB |
63 | StorageType.MONGO_DB |
64 | )} client initialization failed while trying to issue a request` | |
65 | ) | |
a6ceb16a | 66 | } |
2a370053 | 67 | if (!this.connected) { |
7b5dbe91 | 68 | throw new BaseError( |
e7aeea18 | 69 | `${this.logPrefix} ${this.getDBNameFromStorageType( |
66a7748d JB |
70 | StorageType.MONGO_DB |
71 | )} connection not opened while trying to issue a request` | |
72 | ) | |
2a370053 JB |
73 | } |
74 | } | |
72f041bd | 75 | } |