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.");
|
|
|
|
|
2020-04-11 21:16:37 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-14 17:05:27 +02:00
|
|
|
return this;
|
|
|
|
|
2020-04-09 16:30:52 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 10:26:59 +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-14 10:26:59 +02:00
|
|
|
|
2020-04-17 22:17:52 +02:00
|
|
|
/** 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-04-14 10:26:59 +02:00
|
|
|
|
2020-04-16 13:03:05 +02:00
|
|
|
const response = await this.providers.get(message.provider)._query(message.request);
|
2020-04-14 10:26:59 +02:00
|
|
|
|
|
|
|
if(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 10:26:59 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 17:05:27 +02:00
|
|
|
this._send(shard, { transactionID: message.transactionID, result: response });
|
|
|
|
|
2020-04-14 10:26:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_send(shard, message) {
|
|
|
|
shard.send({
|
|
|
|
_storage: true,
|
2020-04-14 17:05:27 +02:00
|
|
|
transactionID: message.transactionID,
|
2020-04-14 10:26:59 +02:00
|
|
|
id: message.id,
|
|
|
|
error: message.error,
|
2020-04-14 17:05:27 +02:00
|
|
|
message: message.message,
|
|
|
|
result: message.result
|
|
|
|
});
|
2020-04-14 10:26:59 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:27:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = StorageManager;
|