2018-07-08 21:48:14 +00:00
|
|
|
import * as plugins from './smartdata.plugins';
|
2020-06-11 23:05:32 +00:00
|
|
|
import { ObjectMap } from '@pushrocks/lik';
|
2016-09-13 20:53:21 +00:00
|
|
|
|
2018-07-09 22:02:04 +00:00
|
|
|
import { SmartdataCollection } from './smartdata.classes.collection';
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2020-06-11 23:05:32 +00:00
|
|
|
import { logger } from './smartdata.logging';
|
2020-08-18 12:05:07 +00:00
|
|
|
import { IMongoDescriptor } from './interfaces';
|
2018-01-07 16:26:34 +00:00
|
|
|
|
2016-11-17 11:20:52 +00:00
|
|
|
/**
|
|
|
|
* interface - indicates the connection status of the db
|
|
|
|
*/
|
2018-07-08 21:48:14 +00:00
|
|
|
export type TConnectionStatus = 'initial' | 'disconnected' | 'connected' | 'failed';
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2018-07-08 21:48:14 +00:00
|
|
|
export class SmartdataDb {
|
2020-08-18 12:05:07 +00:00
|
|
|
smartdataOptions: IMongoDescriptor;
|
2018-07-08 21:48:14 +00:00
|
|
|
mongoDbClient: plugins.mongodb.MongoClient;
|
|
|
|
mongoDb: plugins.mongodb.Db;
|
|
|
|
status: TConnectionStatus;
|
2020-06-11 23:05:32 +00:00
|
|
|
smartdataCollectionMap = new ObjectMap<SmartdataCollection<any>>();
|
2018-07-08 21:48:14 +00:00
|
|
|
|
2020-08-18 12:05:07 +00:00
|
|
|
constructor(smartdataOptions: IMongoDescriptor) {
|
2018-07-08 21:48:14 +00:00
|
|
|
this.smartdataOptions = smartdataOptions;
|
|
|
|
this.status = 'initial';
|
2018-01-12 00:22:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-25 10:37:05 +00:00
|
|
|
// basic connection stuff ----------------------------------------------
|
2016-09-13 20:53:21 +00:00
|
|
|
|
2017-02-25 10:37:05 +00:00
|
|
|
/**
|
|
|
|
* connects to the database that was specified during instance creation
|
|
|
|
*/
|
2019-09-02 14:42:29 +00:00
|
|
|
public async init(): Promise<any> {
|
2020-08-18 12:01:46 +00:00
|
|
|
const finalConnectionUrl = this.smartdataOptions.mongoDbUrl
|
|
|
|
.replace('<USERNAME>', this.smartdataOptions.mongoDbUser)
|
|
|
|
.replace('<username>', this.smartdataOptions.mongoDbUser)
|
|
|
|
.replace('<PASSWORD>', this.smartdataOptions.mongoDbPass)
|
|
|
|
.replace('<password>', this.smartdataOptions.mongoDbPass)
|
|
|
|
.replace('<DBNAME>', this.smartdataOptions.mongoDbName)
|
|
|
|
.replace('<dbname>', this.smartdataOptions.mongoDbName);
|
|
|
|
|
2019-02-13 21:08:58 +00:00
|
|
|
this.mongoDbClient = await plugins.mongodb.MongoClient.connect(finalConnectionUrl, {
|
2020-02-08 14:37:44 +00:00
|
|
|
useNewUrlParser: true,
|
2020-08-18 12:01:46 +00:00
|
|
|
useUnifiedTopology: true,
|
2019-02-13 21:08:58 +00:00
|
|
|
});
|
2018-07-08 21:48:14 +00:00
|
|
|
this.mongoDb = this.mongoDbClient.db(this.smartdataOptions.mongoDbName);
|
|
|
|
this.status = 'connected';
|
2018-07-09 22:02:04 +00:00
|
|
|
console.log(`Connected to database ${this.smartdataOptions.mongoDbName}`);
|
2017-02-25 10:37:05 +00:00
|
|
|
}
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2017-02-25 10:37:05 +00:00
|
|
|
/**
|
|
|
|
* closes the connection to the databse
|
|
|
|
*/
|
2019-09-02 14:42:29 +00:00
|
|
|
public async close(): Promise<any> {
|
2018-07-08 21:48:14 +00:00
|
|
|
await this.mongoDbClient.close();
|
|
|
|
this.status = 'disconnected';
|
2020-06-11 23:05:32 +00:00
|
|
|
logger.log('info', `disconnected from database ${this.smartdataOptions.mongoDbName}`);
|
2017-02-25 10:37:05 +00:00
|
|
|
}
|
2016-09-13 20:53:21 +00:00
|
|
|
|
2018-01-07 16:58:30 +00:00
|
|
|
// handle table to class distribution
|
|
|
|
|
2019-09-02 14:42:29 +00:00
|
|
|
public addTable(SmartdataCollectionArg: SmartdataCollection<any>) {
|
2018-07-08 21:48:14 +00:00
|
|
|
this.smartdataCollectionMap.add(SmartdataCollectionArg);
|
2018-01-14 16:32:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 16:58:30 +00:00
|
|
|
/**
|
2018-07-08 21:48:14 +00:00
|
|
|
* Gets a collection's name and returns a SmartdataCollection instance
|
2018-01-07 16:58:30 +00:00
|
|
|
* @param nameArg
|
|
|
|
* @returns DbTable
|
|
|
|
*/
|
2019-09-02 14:42:29 +00:00
|
|
|
public async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
|
2020-08-18 12:01:46 +00:00
|
|
|
const resultCollection = this.smartdataCollectionMap.find((dbTableArg) => {
|
2018-07-08 21:48:14 +00:00
|
|
|
return dbTableArg.collectionName === nameArg;
|
2018-01-14 16:32:04 +00:00
|
|
|
});
|
|
|
|
return resultCollection;
|
2018-01-07 16:58:30 +00:00
|
|
|
}
|
2016-09-11 16:01:46 +00:00
|
|
|
}
|