import * as plugins from './smartdata.plugins'; import { ObjectMap } from '@pushrocks/lik'; import { SmartdataCollection } from './smartdata.classes.collection'; import { logger } from './smartdata.logging'; import { IMongoDescriptor } from './interfaces'; /** * interface - indicates the connection status of the db */ export type TConnectionStatus = 'initial' | 'disconnected' | 'connected' | 'failed'; export class SmartdataDb { smartdataOptions: IMongoDescriptor; mongoDbClient: plugins.mongodb.MongoClient; mongoDb: plugins.mongodb.Db; status: TConnectionStatus; smartdataCollectionMap = new ObjectMap>(); constructor(smartdataOptions: IMongoDescriptor) { this.smartdataOptions = smartdataOptions; this.status = 'initial'; } // basic connection stuff ---------------------------------------------- /** * connects to the database that was specified during instance creation */ public async init(): Promise { const finalConnectionUrl = this.smartdataOptions.mongoDbUrl .replace('', this.smartdataOptions.mongoDbUser) .replace('', this.smartdataOptions.mongoDbUser) .replace('', this.smartdataOptions.mongoDbPass) .replace('', this.smartdataOptions.mongoDbPass) .replace('', this.smartdataOptions.mongoDbName) .replace('', this.smartdataOptions.mongoDbName); this.mongoDbClient = await plugins.mongodb.MongoClient.connect(finalConnectionUrl, { useNewUrlParser: true, useUnifiedTopology: true, maxPoolSize: 100, maxIdleTimeMS: 10 }); this.mongoDb = this.mongoDbClient.db(this.smartdataOptions.mongoDbName); this.status = 'connected'; console.log(`Connected to database ${this.smartdataOptions.mongoDbName}`); } /** * closes the connection to the databse */ public async close(): Promise { await this.mongoDbClient.close(); this.status = 'disconnected'; logger.log('info', `disconnected from database ${this.smartdataOptions.mongoDbName}`); } // handle table to class distribution public addTable(SmartdataCollectionArg: SmartdataCollection) { this.smartdataCollectionMap.add(SmartdataCollectionArg); } /** * Gets a collection's name and returns a SmartdataCollection instance * @param nameArg * @returns DbTable */ public async getSmartdataCollectionByName(nameArg: string): Promise> { const resultCollection = this.smartdataCollectionMap.find((dbTableArg) => { return dbTableArg.collectionName === nameArg; }); return resultCollection; } }