galactic-bot/structure/storage/StorageManager.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const { Provider } = require('./interfaces/');
class StorageManager {
constructor(client, options = {}) {
this.client = client;
this.providers = {};
this.options = options;
}
async initialize() {
// this.manager.logger.write('debug', "Initializing storage providers.");
const _providers = path.join(process.cwd(), "structure/storage/providers");
const providers = fs.readdirSync(_providers);
for(const _provider of providers) {
let provider = require(path.join(_providers, _provider));
provider = new provider(this.client, this.options);
this.providers[provider.name] = provider;
await provider.initialize();
this._log(`Provider ${chalk.bold(provider.name)} was ${chalk.bold('loaded')}.`, provider);
await provider.loadTables();
}
return this;
}
_getName(instance) {
if(instance instanceof Provider) return instance.name.substring(0, 5);
return `${instance.provider.name.substring(0, 5)}:${instance.name}`;
}
_error(info, instance = null) {
this.client.logger.error(`${chalk.bold(`[STORA]`)} ${instance ? `(${this._getName(instance)}) ` : ''}${info}`);
}
_log(info, instance = null) {
this.client.logger.info(`${chalk.bold(`[STORA]`)} ${instance ? `(${this._getName(instance)}) ` : ''}${info}`);
}
get mongodb() {
return this.providers.mongodb;
}
get mariadb() {
return this.providers.mariadb;
}
}
module.exports = StorageManager;