Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
c27c3eee | 2 | |
66a7748d | 3 | import { MongoClient } from 'mongodb' |
8114d10e | 4 | |
66a7748d JB |
5 | import { Storage } from './Storage.js' |
6 | import { BaseError } from '../../exception/index.js' | |
7 | import { type Statistics, StorageType } from '../../types/index.js' | |
8 | import { Constants } from '../../utils/index.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 { |
66a7748d | 23 | this.checkDBConnection() |
e7aeea18 | 24 | await this.client |
72092cfc | 25 | ?.db(this.dbName) |
e7aeea18 | 26 | .collection<Statistics>(Constants.PERFORMANCE_RECORDS_TABLE) |
66a7748d | 27 | .replaceOne({ id: performanceStatistics.id }, performanceStatistics, { upsert: true }) |
2a370053 | 28 | } catch (error) { |
66a7748d | 29 | this.handleDBError(StorageType.MONGO_DB, error as Error, Constants.PERFORMANCE_RECORDS_TABLE) |
2a370053 | 30 | } |
72f041bd JB |
31 | } |
32 | ||
66a7748d | 33 | public async open (): Promise<void> { |
2a370053 | 34 | try { |
5199f9fd | 35 | if (!this.connected && this.client != null) { |
66a7748d JB |
36 | await this.client.connect() |
37 | this.connected = true | |
2a370053 JB |
38 | } |
39 | } catch (error) { | |
66a7748d | 40 | this.handleDBError(StorageType.MONGO_DB, error as Error) |
2a370053 JB |
41 | } |
42 | } | |
72f041bd | 43 | |
66a7748d | 44 | public async close (): Promise<void> { |
2a370053 | 45 | try { |
5199f9fd | 46 | if (this.connected && this.client != null) { |
66a7748d JB |
47 | await this.client.close() |
48 | this.connected = false | |
2a370053 JB |
49 | } |
50 | } catch (error) { | |
66a7748d | 51 | this.handleDBError(StorageType.MONGO_DB, error as Error) |
2a370053 JB |
52 | } |
53 | } | |
54 | ||
66a7748d | 55 | private checkDBConnection (): void { |
5199f9fd | 56 | if (this.client == null) { |
7b5dbe91 | 57 | throw new BaseError( |
e7aeea18 | 58 | `${this.logPrefix} ${this.getDBNameFromStorageType( |
66a7748d JB |
59 | StorageType.MONGO_DB |
60 | )} client initialization failed while trying to issue a request` | |
61 | ) | |
a6ceb16a | 62 | } |
2a370053 | 63 | if (!this.connected) { |
7b5dbe91 | 64 | throw new BaseError( |
e7aeea18 | 65 | `${this.logPrefix} ${this.getDBNameFromStorageType( |
66a7748d JB |
66 | StorageType.MONGO_DB |
67 | )} connection not opened while trying to issue a request` | |
68 | ) | |
2a370053 JB |
69 | } |
70 | } | |
72f041bd | 71 | } |