2022-05-16 22:33:44 +00:00
|
|
|
import * as plugins from './smartdata.plugins.js';
|
2016-09-13 20:53:21 +00:00
|
|
|
|
2022-05-16 22:33:44 +00:00
|
|
|
import { SmartdataCollection } from './smartdata.classes.collection.js';
|
|
|
|
import { EasyStore } from './smartdata.classes.easystore.js';
|
2016-09-11 16:01:46 +00:00
|
|
|
|
2022-05-16 22:33:44 +00:00
|
|
|
import { logger } from './smartdata.logging.js';
|
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 {
|
2022-06-05 15:18:13 +00:00
|
|
|
smartdataOptions: plugins.tsclass.database.IMongoDescriptor;
|
2018-07-08 21:48:14 +00:00
|
|
|
mongoDbClient: plugins.mongodb.MongoClient;
|
|
|
|
mongoDb: plugins.mongodb.Db;
|
|
|
|
status: TConnectionStatus;
|
2023-02-06 10:43:11 +00:00
|
|
|
statusConnectedDeferred = plugins.smartpromise.defer();
|
2023-07-21 18:08:18 +00:00
|
|
|
smartdataCollectionMap = new plugins.lik.ObjectMap<SmartdataCollection<any>>();
|
2018-07-08 21:48:14 +00:00
|
|
|
|
2022-06-05 15:18:13 +00:00
|
|
|
constructor(smartdataOptions: plugins.tsclass.database.IMongoDescriptor) {
|
2018-07-08 21:48:14 +00:00
|
|
|
this.smartdataOptions = smartdataOptions;
|
|
|
|
this.status = 'initial';
|
2018-01-12 00:22:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 17:54:05 +00:00
|
|
|
// easystore
|
|
|
|
public async createEasyStore(nameIdArg: string) {
|
|
|
|
const easyStore = new EasyStore(nameIdArg, this);
|
|
|
|
return easyStore;
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
});
|
2018-07-08 21:48:14 +00:00
|
|
|
this.mongoDb = this.mongoDbClient.db(this.smartdataOptions.mongoDbName);
|
|
|
|
this.status = 'connected';
|
2023-02-06 10:43:11 +00:00
|
|
|
this.statusConnectedDeferred.resolve();
|
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
|
|
|
|
|
2020-09-10 10:12:17 +00:00
|
|
|
public addCollection(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>> {
|
2021-11-12 16:22:31 +00:00
|
|
|
const resultCollection = await this.smartdataCollectionMap.find(async (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
|
|
|
}
|