1 import Constants from
'../Constants';
2 import { DBType
} from
'../../types/Storage';
3 import { MongoClient
} from
'mongodb';
4 import Statistics from
'../../types/Statistics';
5 import { Storage
} from
'./Storage';
7 export class MongoDBStorage
extends Storage
{
8 private client
: MongoClient
;
9 private connected
: boolean;
11 constructor(storageURI
: string, logPrefix
: string) {
12 super(storageURI
, logPrefix
);
13 this.client
= new MongoClient(this.storageURI
.toString());
14 this.connected
= false;
15 this.dbName
= this.storageURI
.pathname
.replace(/(?:^\
/)|(?:\
/$
)/g
, '') ?? Constants
.DEFAULT_PERFORMANCE_RECORDS_DB_NAME
;
18 public async storePerformanceStatistics(performanceStatistics
: Statistics
): Promise
<void> {
20 this.checkDBConnection();
21 await this.client
.db(this.dbName
).collection
<Statistics
>(Constants
.PERFORMANCE_RECORDS_TABLE
).insertOne(performanceStatistics
);
23 this.handleDBError(DBType
.MONGO_DB
, error
, Constants
.PERFORMANCE_RECORDS_TABLE
);
27 public async open(): Promise
<void> {
29 if (!this.connected
) {
30 await this.client
.connect();
31 this.connected
= true;
34 this.handleDBError(DBType
.MONGO_DB
, error
);
38 public async close(): Promise
<void> {
41 await this.client
.close();
42 this.connected
= false;
45 this.handleDBError(DBType
.MONGO_DB
, error
);
49 private checkDBConnection() {
50 if (!this.connected
) {
51 throw new Error(`${this.logPrefix} ${DBType.MONGO_DB} connection not opened while trying to issue a request`);