smartdata/ts/smartdata.classes.db.ts

94 lines
2.6 KiB
TypeScript
Raw Permalink Normal View History

import * as plugins from './smartdata.plugins';
2019-01-07 01:38:30 +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
import * as mongoHelpers from './smartdata.mongohelpers';
2018-01-07 16:26:34 +00:00
2016-11-17 11:20:52 +00:00
/**
* interface - indicates the connection status of the db
*/
export type TConnectionStatus = 'initial' | 'disconnected' | 'connected' | 'failed';
2016-09-11 16:01:46 +00:00
export interface ISmartdataOptions {
/**
* the URL to connect to
*/
mongoDbUrl: string;
2016-09-11 16:01:46 +00:00
/**
* the db to use for the project
*/
mongoDbName: string;
2016-09-11 16:01:46 +00:00
/**
* an optional password that will be replace <PASSWORD> in the connection string
*/
mongoDbPass?: string;
}
export class SmartdataDb {
smartdataOptions: ISmartdataOptions;
mongoDbClient: plugins.mongodb.MongoClient;
mongoDb: plugins.mongodb.Db;
status: TConnectionStatus;
smartdataCollectionMap = new Objectmap<SmartdataCollection<any>>();
constructor(smartdataOptions: ISmartdataOptions) {
this.smartdataOptions = smartdataOptions;
this.status = 'initial';
}
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-01-08 18:55:13 +00:00
async init(): Promise<any> {
let finalConnectionUrl = this.smartdataOptions.mongoDbUrl;
if (this.smartdataOptions.mongoDbPass) {
finalConnectionUrl = mongoHelpers.addPassword(
2018-07-09 22:02:04 +00:00
this.smartdataOptions.mongoDbUrl,
this.smartdataOptions.mongoDbPass
);
}
2018-07-09 22:02:04 +00:00
console.log(finalConnectionUrl);
2019-02-13 21:08:58 +00:00
this.mongoDbClient = await plugins.mongodb.MongoClient.connect(finalConnectionUrl, {
useNewUrlParser: true
});
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
*/
async close(): Promise<any> {
await this.mongoDbClient.close();
this.status = 'disconnected';
2019-01-07 01:41:38 +00:00
plugins.smartlog.defaultLogger.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
addTable(SmartdataCollectionArg: SmartdataCollection<any>) {
this.smartdataCollectionMap.add(SmartdataCollectionArg);
}
2018-01-07 16:58:30 +00:00
/**
* Gets a collection's name and returns a SmartdataCollection instance
2018-01-07 16:58:30 +00:00
* @param nameArg
* @returns DbTable
*/
async getSmartdataCollectionByName<T>(nameArg: string): Promise<SmartdataCollection<T>> {
let resultCollection = this.smartdataCollectionMap.find(dbTableArg => {
return dbTableArg.collectionName === nameArg;
});
return resultCollection;
2018-01-07 16:58:30 +00:00
}
2016-09-11 16:01:46 +00:00
}