smartdata/ts/smartdata.classes.db.ts

80 lines
2.7 KiB
TypeScript
Raw Normal View History

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
*/
export type TConnectionStatus = 'initial' | 'disconnected' | 'connected' | 'failed';
2016-09-11 16:01:46 +00:00
export class SmartdataDb {
2020-08-18 12:05:07 +00:00
smartdataOptions: IMongoDescriptor;
mongoDbClient: plugins.mongodb.MongoClient;
mongoDb: plugins.mongodb.Db;
status: TConnectionStatus;
2020-06-11 23:05:32 +00:00
smartdataCollectionMap = new ObjectMap<SmartdataCollection<any>>();
2020-08-18 12:05:07 +00:00
constructor(smartdataOptions: IMongoDescriptor) {
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-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)
2020-09-24 14:10:54 +00:00
.replace('<USER>', this.smartdataOptions.mongoDbUser)
.replace('<user>', this.smartdataOptions.mongoDbUser)
2020-08-18 12:01:46 +00:00
.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,
2020-09-09 02:21:15 +00:00
maxPoolSize: 100,
2020-10-19 16:44:28 +00:00
maxIdleTimeMS: 10,
2019-02-13 21:08:58 +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> {
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
2020-09-10 10:12:17 +00:00
public addCollection(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
*/
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) => {
return dbTableArg.collectionName === nameArg;
});
return resultCollection;
2018-01-07 16:58:30 +00:00
}
2016-09-11 16:01:46 +00:00
}