galactic-bot/storage/StorageManager.js

91 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-04-08 16:27:34 +02:00
const { Collection } = require('../util/');
2020-04-11 21:16:37 +02:00
const path = require('path');
const fs = require('fs');
2020-04-08 16:27:34 +02:00
class StorageManager {
2020-04-11 21:16:37 +02:00
constructor(manager, options = {}) {
2020-04-08 16:27:34 +02:00
2020-04-11 21:16:37 +02:00
this.manager = manager;
2020-04-14 08:41:42 +02:00
this.providers = new Collection();
2020-04-11 21:16:37 +02:00
this.options = options;
2020-04-08 16:27:34 +02:00
}
2020-04-09 16:30:52 +02:00
async initialize() {
2020-04-14 08:41:42 +02:00
this.manager.logger.write('debug', "Initializing storage providers.");
const _providers = path.join(process.cwd(), 'storage', 'providers');
const providers = fs.readdirSync(_providers);
2020-04-11 21:16:37 +02:00
for(const _provider of providers) {
2020-04-11 21:16:37 +02:00
let provider = require(path.join(_providers, _provider));
provider = new provider(this.manager, this.options);
await provider.init();
this.providers.set(provider.name, provider);
}
2020-04-14 17:05:27 +02:00
return this;
2020-04-09 16:30:52 +02:00
}
async _handleStorageRequest(shard, message) {
// vv TO BE GENERATED ON CLIENT vv
2020-04-14 17:05:27 +02:00
// Transaction ID concept
//`${shard.id}-${new Date().getTime().toString(36)}`
2020-04-19 21:53:02 +02:00
//NULL RESPONSE IF COLLECTION DOES NOT EXIST!!
//EMPTY OBJECT IF NOTHING MATCHED!!
/** Message structure
* {
* provider: string, -- mongodb or mariadb
* transactionID: string,
* request: {
* type: string, -- The function to use from this class, ex. findOne
* collection: string, -- Which collection to query from
* query: { }, -- Actual search query
* data: { } -- If the operation is to update or insert something, this is what to insert
* }
* }
*/
2020-04-14 17:05:27 +02:00
if(!message.provider) return this._send(shard, { error: true, message: "Provider not provided", transactionID: message.transactionID });
if(!this.providers.has(message.provider)) return this._send(shard, { error: true, message: `Invalid provider, expected 'mongodb' or 'mariadb', got '${message.provider}'`, transactionID: message.transactionID });
2020-04-16 13:51:59 +02:00
if(!message.request) return this._send(shard, { error: true, message: `Missing request.`, transactionID: message.transactionID });
2020-07-28 20:49:34 +02:00
const response = await this.providers.get(message.provider)._query(message.request)
.catch((error) => {
this.manager.logger.write('error', `Provider ${message.provider} errored: ${error}`, shard);
});
2020-04-19 21:53:02 +02:00
if(response && response.error) {
2020-04-14 17:05:27 +02:00
this.manager.logger.write('error', `Provider ${message.provider} errored: ${response.message}`, shard);
return this._send(shard, response.error);
}
2020-04-14 17:05:27 +02:00
this._send(shard, { transactionID: message.transactionID, result: response });
}
_send(shard, message) {
shard.send({
_storage: true,
2020-04-14 17:05:27 +02:00
transactionID: message.transactionID,
id: message.id,
error: message.error,
2020-04-14 17:05:27 +02:00
message: message.message,
result: message.result
});
}
2020-04-08 16:27:34 +02:00
}
module.exports = StorageManager;