diff --git a/libStorage.js b/libStorage.js index 960bb3c..de6a6b0 100644 --- a/libStorage.js +++ b/libStorage.js @@ -16,6 +16,14 @@ const accountsTable = process.env.DB_ACCOUNTS_TABLE; const transactionsTable = process.env.DB_TRANSACTIONS_TABLE; const pricingTable = process.env.DB_PRICING_TABLE; +const connection = mysql.createPool({ + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_NAME, + connectionLimit: 10 +}); + // Helper Functions // Function to run and handle SQL errors function runSQL(sqlQuery, connection, callback = (err, rows) => { @@ -43,20 +51,13 @@ function returnMysqlTime(){ } class Storage { - constructor(_dbTable) { - this.connection = mysql.createPool({ - host: process.env.DB_HOST, - user: process.env.DB_USER, - password: process.env.DB_PASS, - database: process.env.DB_NAME - }); - + constructor(_dbTable) { this.dbTable = _dbTable; this.validKeys = []; var sqlQuery = `SHOW COLUMNS FROM ${this.dbTable};`; - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (err, rows) => { if (err) return log.ERROR("Error getting column names: ", err); if (rows){ for (const validKey of rows){ @@ -95,7 +96,7 @@ class Storage { const sqlQuery = `SELECT * FROM ${this.dbTable} WHERE ${key} = "${keyValue}"`; - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (err, rows) => { if (err) return callback(err, undefined); if (rows[0]?.[key]) return callback(undefined, rows[0]); else return callback(undefined, false); @@ -112,7 +113,7 @@ class Storage { let records = []; - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (err, rows) => { if (err) return callback(err, undefined); for (const row of rows) { if (this.dbTable == rssFeedsTable){ @@ -138,7 +139,7 @@ class Storage { let records = []; - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (err, rows) => { if (err) return callback(err, undefined); for (const row of rows) { if (this.dbTable == rssFeedsTable){ @@ -170,7 +171,7 @@ exports.UserStorage = class UserStorage extends Storage { log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`) - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (err, rows) => { if (err) return callback(err, undefined); if (rows?.affectedRows > 0) return callback(undefined, rows); return callback(undefined, undefined); @@ -230,7 +231,7 @@ exports.UserStorage = class UserStorage extends Storage { log.DEBUG("Updating Balance with SQL Query: ", sqlQuery); - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (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); @@ -248,7 +249,7 @@ exports.TransactionStorage = class TransactionStorage extends Storage { log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`) - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (err, rows) => { if (err) return callback(err, undefined); if (rows?.affectedRows > 0) return callback(undefined, rows); return callback(undefined, undefined); @@ -315,7 +316,7 @@ exports.FeedStorage = class FeedStorage extends Storage { log.DEBUG(`Adding new entry with SQL query: '${sqlQuery}'`) - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (err, rows) => { if (err) return callback(err, undefined); return callback(undefined, rows); }) @@ -354,7 +355,7 @@ exports.FeedStorage = class FeedStorage extends Storage { log.DEBUG(`Updating entry with SQL query: '${sqlQuery}'`) - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (err, rows) => { if (err) return callback(err, undefined); return callback(undefined, rows); }) @@ -372,7 +373,7 @@ exports.FeedStorage = class FeedStorage extends Storage { const sqlQuery = `DELETE FROM ${this.dbTable} WHERE id = "${id}";`; - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (err, rows) => { if (err) return callback(err, undefined); return callback(undefined, rows[0]); }) @@ -455,7 +456,7 @@ exports.PostStorage = class PostStorage extends Storage { log.DEBUG(`Adding new post with SQL query: '${sqlQuery}'`) - runSQL(sqlQuery, this.connection, (err, rows) => { + runSQL(sqlQuery, (err, rows) => { if (err) return callback(err, undefined); return callback(undefined, rows); })