Working on #2
This commit is contained in:
@@ -16,7 +16,7 @@ const accountsTable = process.env.DB_ACCOUNTS_TABLE;
|
||||
const transactionsTable = process.env.DB_TRANSACTIONS_TABLE;
|
||||
const pricingTable = process.env.DB_PRICING_TABLE;
|
||||
|
||||
var connection = mysql.createPool({
|
||||
var Connection = mysql.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
@@ -26,14 +26,15 @@ var connection = mysql.createPool({
|
||||
|
||||
// Helper Functions
|
||||
// Function to run and handle SQL errors
|
||||
function runSQL(sqlQuery, callback = (err, rows) => {
|
||||
function runSQL(sqlQuery, connection, callback = (err, rows) => {
|
||||
log.ERROR(err);
|
||||
throw err;
|
||||
}) {
|
||||
// Start the MySQL Connection
|
||||
// Start the MySQL Connection
|
||||
if (!connection) connection = Connection;
|
||||
connection.query(sqlQuery, (err, rows) => {
|
||||
if (err) {
|
||||
log.ERROR("SQL Error:", err)
|
||||
log.ERROR("SQL Error on query:", sqlQuery, err);
|
||||
return callback(err, undefined);
|
||||
}
|
||||
log.VERBOSE(`SQL result for query '${sqlQuery}':`, rows);
|
||||
@@ -51,13 +52,14 @@ function returnMysqlTime(){
|
||||
}
|
||||
|
||||
class Storage {
|
||||
constructor(_dbTable) {
|
||||
constructor(_dbTable, connection = undefined) {
|
||||
this.dbTable = _dbTable;
|
||||
this.connection = connection
|
||||
this.validKeys = [];
|
||||
|
||||
var sqlQuery = `SHOW COLUMNS FROM ${this.dbTable};`;
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return log.ERROR("Error getting column names: ", err);
|
||||
if (rows){
|
||||
for (const validKey of rows){
|
||||
@@ -96,7 +98,7 @@ class Storage {
|
||||
|
||||
const sqlQuery = `SELECT * FROM ${this.dbTable} WHERE ${key} = "${keyValue}"`;
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
if (rows[0]?.[key]) return callback(undefined, rows[0]);
|
||||
else return callback(undefined, false);
|
||||
@@ -113,7 +115,7 @@ class Storage {
|
||||
|
||||
let records = [];
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
for (const row of rows) {
|
||||
if (this.dbTable == rssFeedsTable){
|
||||
@@ -139,7 +141,7 @@ class Storage {
|
||||
|
||||
let records = [];
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
for (const row of rows) {
|
||||
if (this.dbTable == rssFeedsTable){
|
||||
@@ -153,11 +155,21 @@ class Storage {
|
||||
return callback(undefined, records);
|
||||
});
|
||||
}
|
||||
|
||||
closeConnection() {
|
||||
try {
|
||||
this.connection.end();
|
||||
}
|
||||
catch (err) {
|
||||
log.ERROR("Error closing connection :", this.connection, err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.UserStorage = class UserStorage extends Storage {
|
||||
constructor() {
|
||||
super(accountsTable);
|
||||
constructor(connection = undefined) {
|
||||
super(accountsTable, connection);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,7 +183,7 @@ exports.UserStorage = class UserStorage extends Storage {
|
||||
|
||||
log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`)
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
if (rows?.affectedRows > 0) return callback(undefined, rows);
|
||||
return callback(undefined, undefined);
|
||||
@@ -231,7 +243,7 @@ exports.UserStorage = class UserStorage extends Storage {
|
||||
|
||||
log.DEBUG("Updating Balance with SQL Query: ", sqlQuery);
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
if (!rows?.affectedRows > 0) return callback(new Error("Error updating Balance", rows), undefined);
|
||||
return callback(undefined, rows);
|
||||
@@ -240,8 +252,8 @@ exports.UserStorage = class UserStorage extends Storage {
|
||||
}
|
||||
|
||||
exports.TransactionStorage = class TransactionStorage extends Storage {
|
||||
constructor() {
|
||||
super(transactionsTable);
|
||||
constructor(connection = undefined) {
|
||||
super(transactionsTable, connection);
|
||||
}
|
||||
|
||||
createTransaction(transaction, callback){
|
||||
@@ -249,7 +261,7 @@ exports.TransactionStorage = class TransactionStorage extends Storage {
|
||||
|
||||
log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`)
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
if (rows?.affectedRows > 0) return callback(undefined, rows);
|
||||
return callback(undefined, undefined);
|
||||
@@ -258,8 +270,8 @@ exports.TransactionStorage = class TransactionStorage extends Storage {
|
||||
}
|
||||
|
||||
exports.FeedStorage = class FeedStorage extends Storage {
|
||||
constructor() {
|
||||
super(rssFeedsTable);
|
||||
constructor(connection = undefined) {
|
||||
super(rssFeedsTable, connection);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -316,7 +328,7 @@ exports.FeedStorage = class FeedStorage extends Storage {
|
||||
|
||||
log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`)
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
return callback(undefined, rows);
|
||||
})
|
||||
@@ -355,7 +367,7 @@ exports.FeedStorage = class FeedStorage extends Storage {
|
||||
|
||||
log.DEBUG(`Updating entry with SQL query: '${sqlQuery}'`)
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
return callback(undefined, rows);
|
||||
})
|
||||
@@ -373,7 +385,7 @@ exports.FeedStorage = class FeedStorage extends Storage {
|
||||
|
||||
const sqlQuery = `DELETE FROM ${this.dbTable} WHERE id = "${id}";`;
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
return callback(undefined, rows[0]);
|
||||
})
|
||||
@@ -439,8 +451,8 @@ exports.FeedStorage = class FeedStorage extends Storage {
|
||||
}
|
||||
|
||||
exports.PostStorage = class PostStorage extends Storage {
|
||||
constructor() {
|
||||
super(rssPostsTable);
|
||||
constructor(connection = undefined) {
|
||||
super(rssPostsTable, connection);
|
||||
}
|
||||
|
||||
savePost(_postObject, callback){
|
||||
@@ -456,7 +468,7 @@ exports.PostStorage = class PostStorage extends Storage {
|
||||
|
||||
log.DEBUG(`Adding new post with SQL query: '${sqlQuery}'`)
|
||||
|
||||
runSQL(sqlQuery, (err, rows) => {
|
||||
runSQL(sqlQuery, this.connection, (err, rows) => {
|
||||
if (err) return callback(err, undefined);
|
||||
return callback(undefined, rows);
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user