65 lines
1.8 KiB
JavaScript
65 lines
1.8 KiB
JavaScript
const { Collection } = require('../util/');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
class StorageManager {
|
|
|
|
constructor(manager, options = {}) {
|
|
|
|
this.manager = manager;
|
|
|
|
this.providers = new Collection();
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
async initialize() {
|
|
|
|
this.manager.logger.write('debug', "Initializing 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async _handleStorageRequest(shard, message) {
|
|
|
|
// vv TO BE GENERATED ON CLIENT vv
|
|
//`${shard.id}-${new Date().getTime().toString(36)
|
|
|
|
if(!message.provider) return this._send(shard, { error: true, message: "Provider not provided" });
|
|
if(!this.providers.has(message.provider)) return this._send(shard, { error: true, message: `Invalid provider, expected 'mongodb' or 'mariadb', got '${message.provider}'` });
|
|
if(!message.query) return this._send(shard, { error: true, message: `Missing query.` });
|
|
|
|
const response = await this.providers.get(message.provider)._query(message.query);
|
|
|
|
if(response.error) {
|
|
this.manager.logger.write('error', `Provider ${message.provider} errored: ${response.error.message}`, shard);
|
|
return this._send(shard, error);
|
|
}
|
|
|
|
}
|
|
|
|
_send(shard, message) {
|
|
shard.send({
|
|
_storage: true,
|
|
id: message.id,
|
|
error: message.error,
|
|
message: message.message
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = StorageManager; |