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) { |
a03b18c4 JB |
32 | this.handleDBStorageError( |
33 | StorageType.MONGO_DB, | |
34 | error as Error, | |
35 | Constants.PERFORMANCE_RECORDS_TABLE | |
36 | ) | |
2a370053 | 37 | } |
72f041bd JB |
38 | } |
39 | ||
66a7748d | 40 | public async open (): Promise<void> { |
2a370053 | 41 | try { |
5199f9fd | 42 | if (!this.connected && this.client != null) { |
66a7748d JB |
43 | await this.client.connect() |
44 | this.connected = true | |
2a370053 JB |
45 | } |
46 | } catch (error) { | |
a03b18c4 | 47 | this.handleDBStorageError(StorageType.MONGO_DB, error as Error) |
2a370053 JB |
48 | } |
49 | } | |
72f041bd | 50 | |
66a7748d | 51 | public async close (): Promise<void> { |
a66bbcfe | 52 | this.clearPerformanceStatistics() |
2a370053 | 53 | try { |
5199f9fd | 54 | if (this.connected && this.client != null) { |
66a7748d JB |
55 | await this.client.close() |
56 | this.connected = false | |
2a370053 JB |
57 | } |
58 | } catch (error) { | |
a03b18c4 | 59 | this.handleDBStorageError(StorageType.MONGO_DB, error as Error) |
2a370053 JB |
60 | } |
61 | } | |
62 | ||
66a7748d | 63 | private checkDBConnection (): void { |
5199f9fd | 64 | if (this.client == null) { |
7b5dbe91 | 65 | throw new BaseError( |
e7aeea18 | 66 | `${this.logPrefix} ${this.getDBNameFromStorageType( |
66a7748d JB |
67 | StorageType.MONGO_DB |
68 | )} client initialization failed while trying to issue a request` | |
69 | ) | |
a6ceb16a | 70 | } |
2a370053 | 71 | if (!this.connected) { |
7b5dbe91 | 72 | throw new BaseError( |
e7aeea18 | 73 | `${this.logPrefix} ${this.getDBNameFromStorageType( |
66a7748d JB |
74 | StorageType.MONGO_DB |
75 | )} connection not opened while trying to issue a request` | |
76 | ) | |
2a370053 JB |
77 | } |
78 | } | |
72f041bd | 79 | } |