From 976574a5a0fa6c1477da89bd9dde05106a897969 Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 11 Apr 2020 22:16:37 +0300 Subject: [PATCH] Storage manager and providers --- storage/Provider.js | 7 ++++--- storage/StorageManager.js | 21 ++++++++++++++++++++- storage/providers/Mariadb.js | 6 +++--- storage/providers/Mongodb.js | 6 +++--- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/storage/Provider.js b/storage/Provider.js index 5b726bd..918e9bf 100644 --- a/storage/Provider.js +++ b/storage/Provider.js @@ -1,14 +1,15 @@ class Provider { - constructor(manager, config) { + constructor(manager, config, name) { if(!config) throw new Error('No config file provided!'); - if(config && (!config.database || !config.url)) throw new Error('Invalid config file provided!'); + this.config = config[name]; + if(config && (!this.config.database || !this.config.host)) throw new Error('Invalid config file provided!' + JSON.stringify(this.config)); this.manager = manager; - this.config = config; this.db; this.loaded = false; + this.name = name; } diff --git a/storage/StorageManager.js b/storage/StorageManager.js index 988fc1b..00c65e3 100644 --- a/storage/StorageManager.js +++ b/storage/StorageManager.js @@ -1,15 +1,34 @@ const { Collection } = require('../util/'); +const path = require('path'); +const fs = require('fs'); class StorageManager { - constructor(manager) { + constructor(manager, options = {}) { this.providers = new Collection(); + this.manager = manager; + this.options = options; } async initialize() { + console.log('Initiating storage providers'); + let _providers = path.join(process.cwd(), 'storage', 'providers'); + let providers = fs.readdirSync(_providers); + + for(let _provider of providers) { + + let provider = require(path.join(_providers, _provider)); + provider = new provider(this.manager, this.options); + + await provider.init(); + + this.providers.set(provider.name, provider); + + } + } } diff --git a/storage/providers/Mariadb.js b/storage/providers/Mariadb.js index 7871f5f..51cda4f 100644 --- a/storage/providers/Mariadb.js +++ b/storage/providers/Mariadb.js @@ -1,11 +1,11 @@ -const Provider = require('./Provider.js'); +const Provider = require('../Provider.js'); const MySQL = require('mysql'); class MariaDBProvider extends Provider { constructor(manager, config) { - super(manager, config); + super(manager, config, 'mariadb'); } @@ -49,7 +49,7 @@ class MariaDBProvider extends Provider { * Query using SQL to MariaDB * * @param {string} query SQL query string. - * @param {array} values Array of values to replace ? with in the query string + * @param {array} values Array of values to replace ? with in the query string * @returns {object} Returns an object containing the query result * @memberof MariaDBProvider */ diff --git a/storage/providers/Mongodb.js b/storage/providers/Mongodb.js index 34bf0a2..0ed9456 100644 --- a/storage/providers/Mongodb.js +++ b/storage/providers/Mongodb.js @@ -1,11 +1,11 @@ -const Provider = require('./Provider.js'); +const Provider = require('../Provider.js'); const { MongoClient } = require('mongodb'); class MongoDBProvider extends Provider { constructor(manager, config) { - super(manager, config); + super(manager, config, 'mongodb'); this.client; @@ -13,7 +13,7 @@ class MongoDBProvider extends Provider { async init() { - this.manager.logger.log('Initializing mongodb.'); + //this.manager.logger.log('Initializing mongodb.'); try {