Updated all functions to return nodeObjects instead of SQL response
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const mysql = require('mysql');
|
const mysql = require('mysql');
|
||||||
const utils = require('./utils');
|
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({
|
const connection = mysql.createPool({
|
||||||
host: process.env.NODE_DB_HOST,
|
host: process.env.NODE_DB_HOST,
|
||||||
@@ -11,23 +16,76 @@ const connection = mysql.createPool({
|
|||||||
|
|
||||||
const nodesTable = `${process.env.NODE_DB_NAME}.nodes`;
|
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
|
/** Get all nodes the server knows about regardless of status
|
||||||
* @param {*} callback Callback function
|
* @param {*} callback Callback function
|
||||||
*/
|
*/
|
||||||
exports.getAllNodes = (callback) => {
|
exports.getAllNodes = (callback) => {
|
||||||
const sqlQuery = `SELECT * FROM ${nodesTable}`
|
const sqlQuery = `SELECT * FROM ${nodesTable}`
|
||||||
runSQL(sqlQuery, (rows) => {
|
runSQL(sqlQuery, (rows) => {
|
||||||
return callback(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)
|
/** Get all nodes that have the online status set true (are online)
|
||||||
* @param callback Callback function
|
* @param callback Callback function
|
||||||
*/
|
*/
|
||||||
exports.getOnlineNodes = (callback) => {
|
exports.getOnlineNodes = (callback) => {
|
||||||
const sqlQuery = `SELECT * FROM ${nodesTable} WHERE online = 1;`
|
const sqlQuery = `SELECT * FROM ${nodesTable} WHERE online = 1;`
|
||||||
runSQL(sqlQuery, (rows) => {
|
runSQL(sqlQuery, (rows) => {
|
||||||
return callback(rows);
|
return callback(returnNodeObjectFromRows(rows));
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +98,7 @@ exports.getNodeInfoFromId = (nodeId, callback) => {
|
|||||||
runSQL(sqlQuery, (rows) => {
|
runSQL(sqlQuery, (rows) => {
|
||||||
// Call back the first (and theoretically only) row
|
// Call back the first (and theoretically only) row
|
||||||
// Specify 0 so downstream functions don't have to worry about it
|
// 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})`;
|
const sqlQuery = `INSERT INTO ${nodesTable} (name, ip, port, location, nearbySystems, online) VALUES ('${name}', '${ip}', ${port}, '${location}', '${nearbySystems}', ${online})`;
|
||||||
|
|
||||||
runSQL(sqlQuery, (rows) => {
|
runSQL(sqlQuery, (rows) => {
|
||||||
return callback(rows);
|
return callback(returnNodeObjectFromRows(rows));
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +170,7 @@ exports.updateNodeInfo = (nodeObject, callback) => {
|
|||||||
|
|
||||||
runSQL(sqlQuery, (rows) => {
|
runSQL(sqlQuery, (rows) => {
|
||||||
if (rows.affectedRows === 1) return callback(true);
|
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);
|
console.log(err);
|
||||||
throw err;
|
throw err;
|
||||||
}) {
|
}) {
|
||||||
connection.query(sqlQuery, (err, rows) => {
|
return connection.query(sqlQuery, (err, rows) => {
|
||||||
if (err) return error(err);
|
if (err) return error(err);
|
||||||
//console.log('The rows are:', rows);
|
//console.log('The rows are:', rows);
|
||||||
return callback(rows);
|
return callback(rows);
|
||||||
|
|||||||
Reference in New Issue
Block a user