Potential final fix for #30

This commit is contained in:
Logan Cusano
2023-06-23 17:48:28 -04:00
parent 47d18c494d
commit 7163df21e9
4 changed files with 48 additions and 34 deletions

View File

@@ -24,6 +24,8 @@ const nodeConnectionsTable = `${process.env.NODE_DB_NAME}.node_connections`;
* @returns {nodeObject} The converted node object to be used downstream
*/
function returnNodeObjectFromRow(row) {
if (!isNaN(row.online)) row.online = Boolean(row.online);
else if (row.online == "true" || row.online == "false") row.online = (row.online == "true");
return new nodeObject({
_id: row.id,
_name: row.name,
@@ -31,7 +33,7 @@ function returnNodeObjectFromRow(row) {
_port: row.port,
_location: row.location,
_nearbySystems: BufferToJson(row.nearbySystems),
_online: (row.online === 1) ? true : false,
_online: row.online,
});
}
@@ -122,6 +124,7 @@ exports.getOnlineNodes = (callback) => {
// Call back the first (and theoretically only) row
// Specify 0 so downstream functions don't have to worry about it
if (!sqlResponse.length > 0) return (callback) ? callback(false) : false;
return (callback) ? callback(await returnNodeObjectFromRow(sqlResponse[0])) : await returnNodeObjectFromRow(sqlResponse[0]);
}
exports.getNodeInfoFromId = getNodeInfoFromId
@@ -136,9 +139,15 @@ exports.addNewNode = async (nodeObject, callback) => {
ip = nodeObject.ip,
port = nodeObject.port,
location = nodeObject.location,
nearbySystems = utils.JsonToBuffer(nodeObject.nearbySystems ?? {}),
online = nodeObject.online
const sqlQuery = `INSERT INTO ${nodesTable} (name, ip, port, location, nearbySystems, online) VALUES ('${name}', '${ip}', ${port}, '${location}', '${nearbySystems}', ${online})`;
nearbySystems = utils.JsonToBuffer(nodeObject.nearbySystems ?? {})
var online = nodeObject.online;
if (typeof online === "boolean" || typeof online === "number") {
if (online) online = 1;
else online = 0;
}
const sqlQuery = `INSERT INTO ${nodesTable} (name, ip, port, location, nearbySystems, online) VALUES ('${name}', '${ip}', ${port}, '${location}', '${nearbySystems}', ${online})`;
const sqlResponse = await new Promise((recordResolve, recordReject) => {
runSQL(sqlQuery, (rows) => {

View File

@@ -109,7 +109,7 @@ class nodeObject {
* @param {*} param0._ip The IP that the master can contact the node at
* @param {*} param0._port The port that the client is listening on
* @param {*} param0._location The physical location of the node
* @param {*} param0._online True/False if the node is online or offline
* @param {*} param0._online True/False if the node is online or offline
* @param {*} param0._nearbySystems An object array of nearby systems
*/
constructor({ _id = undefined, _name = undefined, _ip = undefined, _port = undefined, _location = undefined, _nearbySystems = undefined, _online = undefined }) {
@@ -117,10 +117,9 @@ class nodeObject {
this.name = _name;
this.ip = _ip;
this.port = _port;
this.location = _location;
if (_nearbySystems && Array.isArray(_nearbySystems)) this.nearbySystems = _nearbySystems;
else if (_nearbySystems) this.nearbySystems = Object.keys(_nearbySystems);
else this.nearbySystems = _nearbySystems;
this.location = _location;
this.nearbySystems = _nearbySystems;
if (this.nearbySystems) this.presets = Object.keys(_nearbySystems);
this.online = _online;
}
}