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
|
|
|
|
2018-07-08 21:48:14 +00:00
|
|
|
import * as mongoHelpers from './smartdata.mongohelpers';
|
2020-06-11 23:05:32 +00:00
|
|
|
import { logger } from './smartdata.logging';
|
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 interface ISmartdataOptions {
|
|
|
|
/**
|
|
|
|
* the URL to connect to
|
|
|
|
*/
|
|
|
|
mongoDbUrl: string;
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2018-07-08 21:48:14 +00:00
|
|
|
/**
|
|
|
|
* the db to use for the project
|
|
|
|
*/
|
|
|
|
mongoDbName: string;
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2018-01-12 00:22:58 +00:00
|
|
|
/**
|
2018-07-08 21:48:14 +00:00
|
|
|
* an optional password that will be replace <PASSWORD> in the connection string
|
2018-01-12 00:22:58 +00:00
|
|
|
*/
|
2018-07-08 21:48:14 +00:00
|
|
|
mongoDbPass?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class SmartdataDb {
|
|
|
|
smartdataOptions: ISmartdataOptions;
|
|
|
|
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
|
|
|
|
|
|
|
constructor(smartdataOptions: ISmartdataOptions) {
|
|
|
|
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> {
|
2018-07-08 21:48:14 +00:00
|
|
|
let finalConnectionUrl = this.smartdataOptions.mongoDbUrl;
|
|
|
|
if (this.smartdataOptions.mongoDbPass) {
|
|
|
|
finalConnectionUrl = mongoHelpers.addPassword(
|
2018-07-09 22:02:04 +00:00
|
|
|
this.smartdataOptions.mongoDbUrl,
|
2018-07-08 21:48:14 +00:00
|
|
|
this.smartdataOptions.mongoDbPass
|
|
|
|
);
|
|
|
|
}
|
2019-09-02 14:42:29 +00:00
|
|
|
console.log(`connection Url: ${finalConnectionUrl}`);
|
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,
|
|
|
|
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>> {
|
|
|
|
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
|
|
|
}
|