Binato/server/DatabaseHelper.js
2020-09-07 19:23:06 +01:00

31 lines
No EOL
1.1 KiB
JavaScript

const tcpx = require('tcp-netx'),
aes256 = require('aes256');
module.exports = class {
constructor(databaseAddress, databasePort, databaseKey) {
this.databaseKey = databaseKey;
// Create a new instance of tcp-netx and connect to the database
this.databaseServer = new tcpx.server(databaseAddress, databasePort);
this.databaseServer.connect();
// First response will always be broken
this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `p|`)});
this.databaseServer.read();
}
executeInDB(query) {
const result = this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `r|${query}`)});
if (result.ok == 1) return this.databaseServer.read();
else throw "Database error"
}
getFromDB(query) {
const result = this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `g|${query}`)});
if (result.ok == 1) return JSON.parse(aes256.decrypt(this.databaseKey, this.databaseServer.read()["data"]));
else throw "Database error";
}
}