Updated all functions to return nodeObjects instead of SQL response

This commit is contained in:
Logan Cusano
2023-06-03 15:41:29 -04:00
parent 5757c51fa3
commit 9450b78bd4

View File

@@ -1,6 +1,11 @@
require('dotenv').config();
const mysql = require('mysql');
const utils = require('./utils');
const { nodeObject } = require("./recordHelper");
const { DebugBuilder } = require("../utilities/debugBuilder");
const { BufferToJson } = require("../utilities/utils");
const log = new DebugBuilder("server", "mysSQLHandler");
const connection = mysql.createPool({
host: process.env.NODE_DB_HOST,
@@ -11,23 +16,76 @@ const connection = mysql.createPool({
const nodesTable = `${process.env.NODE_DB_NAME}.nodes`;
/**
* Return a node object from a single SQL row
*
* @param {object} row The row to convert to a node object
* @returns {nodeObject} The converted node object to be used downstream
*/
function returnNodeObjectFromRow(row) {
return new nodeObject({
_id: row.id,
_name: row.name,
_ip: row.ip,
_port: row.port,
_location: row.location,
_nearbySystems: BufferToJson(row.nearbySystems),
_online: row.online
});
}
/**
* Wrapper to convert an array of rows to an array of nodeObjects
*
* @param {array} rows The array of SQL results to be converted into node objects
* @returns {array} An array of node objects
*/
function returnNodeObjectFromRows(rows) {
var i = 0;
for (var row of rows){
log.DEBUG("Row: ", row);
rows[i] = returnNodeObjectFromRow(row);
i += 1;
}
log.DEBUG("Converted Objects from Rows: ", rows);
return rows;
}
/** Get all nodes the server knows about regardless of status
* @param {*} callback Callback function
*/
exports.getAllNodes = (callback) => {
const sqlQuery = `SELECT * FROM ${nodesTable}`
runSQL(sqlQuery, (rows) => {
return callback(rows);
runSQL(sqlQuery, (rows) => {
if(!rows || rows.length == 0) callback(undefined);
return callback(returnNodeObjectFromRows(rows));
})
}
/**
* Get all Nodes synchronously **May not be working**
*
* @returns
*/
exports.getAllNodesSync = async () => {
const sqlQuery = `SELECT * FROM ${nodesTable}`
var returnObjects = [];
const rows = await runSQL(sqlQuery);
console.log("Rows: ", rows);
return returnNodeObjectFromRows(rows);
}
/** Get all nodes that have the online status set true (are online)
* @param callback Callback function
*/
exports.getOnlineNodes = (callback) => {
const sqlQuery = `SELECT * FROM ${nodesTable} WHERE online = 1;`
runSQL(sqlQuery, (rows) => {
return callback(rows);
return callback(returnNodeObjectFromRows(rows));
})
}
@@ -40,7 +98,7 @@ exports.getNodeInfoFromId = (nodeId, callback) => {
runSQL(sqlQuery, (rows) => {
// Call back the first (and theoretically only) row
// Specify 0 so downstream functions don't have to worry about it
return callback(rows[0]);
return callback(returnNodeObjectFromRow(rows[0]));
})
}
@@ -59,7 +117,7 @@ exports.addNewNode = (nodeObject, callback) => {
const sqlQuery = `INSERT INTO ${nodesTable} (name, ip, port, location, nearbySystems, online) VALUES ('${name}', '${ip}', ${port}, '${location}', '${nearbySystems}', ${online})`;
runSQL(sqlQuery, (rows) => {
return callback(rows);
return callback(returnNodeObjectFromRows(rows));
})
}
@@ -112,7 +170,7 @@ exports.updateNodeInfo = (nodeObject, callback) => {
runSQL(sqlQuery, (rows) => {
if (rows.affectedRows === 1) return callback(true);
else return callback(rows);
else return callback(returnNodeObjectFromRows(rows));
})
}
@@ -121,7 +179,7 @@ function runSQL(sqlQuery, callback, error = (err) => {
console.log(err);
throw err;
}) {
connection.query(sqlQuery, (err, rows) => {
return connection.query(sqlQuery, (err, rows) => {
if (err) return error(err);
//console.log('The rows are:', rows);
return callback(rows);