Server-Side Scripting/SQL Databases/Node.js (Express MySQL)

routes/lesson9.js
"       return result; }

async function countryExists(country) { let database = DATABASE; let sql = ` SELECT EXISTS(       SELECT * FROM Countries        WHERE Country = ?) AS Count; `   let parameters = [country]; let data = await mysqlQuery(database, sql, parameters); let result = !!data[0].Count; return result; }

async function insertCountry(country, temperature) { let database = DATABASE; let sql = ` INSERT INTO Countries (Country, Temperature) VALUES(?, ?); `   let parameters = [country, temperature]; await mysqlQuery(database, sql, parameters); }

async function updateCountry(country, temperature) { let database = DATABASE; let sql = ` UPDATE Countries SET Temperature = ? WHERE Country = ?; `   let parameters = [temperature, country]; await mysqlQuery(database, sql, parameters); }

async function deleteCountry(country) { let database = DATABASE; let sql = ` DELETE FROM Countries WHERE Country = ?; `   let parameters = [country]; await mysqlQuery(database, sql, parameters); }

async function mysqlConnection(database) { return new Promise((resolve, reject) => {       let connection = mysql.createConnection({ host: HOST, user: USER, password: PASSWORD, database: database, });

connection.connect( (error) => {           if (error) {                reject(error);            } else {                resolve(connection);            }        }); }); }

async function mysqlQuery(database, sql, parameters) { let connection = await mysqlConnection(database);

return new Promise((resolve, reject) => {       connection.query(sql, parameters, function(error, rows) { if (error) { reject(error); } else { resolve(rows); }       });        connection.end;    }); }

module.exports = router;

Try It
See ../../Routes and Templates/Node.js (Express) to create a test environment.