const mysql = require('mysql'); const con = mysql.createConnection({ host: "localhost", user: "realmsdivided", password: "realmsdivided", database: "realms_divided" }); function connect(){ con.connect(function(err) { if (err) { console.log(err); } else { console.log('DB Connected'); } }); } function disconnect(){ con.end(); } // Is calling a promise in a promise the best way to do this? // Keeping because this is how I've figured to call other funcs from database function getCardById(cardId){ const dPromise = new Promise((resolve, reject) => { getCards(' card.id = '+cardId).then(cards => { resolve(cards); }) .catch(err => { throw err; reject(new Error(err)); }); }); return dPromise; } function getCards(whereClause){ const cPromise = new Promise((resolve, reject) => { let cards = []; let sql = `SELECT card.id AS cardId -- TEMP UNTIL UID ,cardName ,cardCost ,typeName ,cardAttack ,rarityName FROM card LEFT JOIN type ON type.id = card.id LEFT JOIN rarity ON rarity.id = card.cardRarity `; // TODO: Rewrite this so it's not so insecure!!!! if(whereClause){ sql = sql + " WHERE "+whereClause; } con.query(sql, function (err, result, fields) { if (err) { throw err; reject(new Error(err)); } result.forEach((card) => { let tempCard = {}; tempCard.id = card.cardId; tempCard.name = card.cardName; tempCard.colour = 'temp'; tempCard.cost = card.cardCost; tempCard.type = card.typeName; tempCard.atk = card.cardAttack; tempCard.rarity = card.rarityName; tempCard.effect = 'temp effect'; // TODO: Will need more SQL statements, or some function/procedure // class // colour requirements // card effects cards.push(tempCard); }); //console.log(cards); resolve(cards); }); }); return cPromise; } module.exports = { connect, disconnect , getCards , getCardById };