Storage manager and providers

This commit is contained in:
Erik 2020-04-11 22:16:37 +03:00
parent 4c2f0d2465
commit 976574a5a0
4 changed files with 30 additions and 10 deletions

View File

@ -1,14 +1,15 @@
class Provider { class Provider {
constructor(manager, config) { constructor(manager, config, name) {
if(!config) throw new Error('No config file provided!'); 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.manager = manager;
this.config = config;
this.db; this.db;
this.loaded = false; this.loaded = false;
this.name = name;
} }

View File

@ -1,15 +1,34 @@
const { Collection } = require('../util/'); const { Collection } = require('../util/');
const path = require('path');
const fs = require('fs');
class StorageManager { class StorageManager {
constructor(manager) { constructor(manager, options = {}) {
this.providers = new Collection(); this.providers = new Collection();
this.manager = manager;
this.options = options;
} }
async initialize() { 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);
}
} }
} }

View File

@ -1,11 +1,11 @@
const Provider = require('./Provider.js'); const Provider = require('../Provider.js');
const MySQL = require('mysql'); const MySQL = require('mysql');
class MariaDBProvider extends Provider { class MariaDBProvider extends Provider {
constructor(manager, config) { constructor(manager, config) {
super(manager, config); super(manager, config, 'mariadb');
} }
@ -49,7 +49,7 @@ class MariaDBProvider extends Provider {
* Query using SQL to MariaDB * Query using SQL to MariaDB
* *
* @param {string} query SQL query string. * @param {string} query SQL query string.
* @param {array} values Array of values to replace ? with in the query string * @param {array<Object>} values Array of values to replace ? with in the query string
* @returns {object} Returns an object containing the query result * @returns {object} Returns an object containing the query result
* @memberof MariaDBProvider * @memberof MariaDBProvider
*/ */

View File

@ -1,11 +1,11 @@
const Provider = require('./Provider.js'); const Provider = require('../Provider.js');
const { MongoClient } = require('mongodb'); const { MongoClient } = require('mongodb');
class MongoDBProvider extends Provider { class MongoDBProvider extends Provider {
constructor(manager, config) { constructor(manager, config) {
super(manager, config); super(manager, config, 'mongodb');
this.client; this.client;
@ -13,7 +13,7 @@ class MongoDBProvider extends Provider {
async init() { async init() {
this.manager.logger.log('Initializing mongodb.'); //this.manager.logger.log('Initializing mongodb.');
try { try {