Skip to content

Instantly share code, notes, and snippets.

@arielivandiaz
Created December 1, 2018 14:50
Show Gist options
  • Save arielivandiaz/83781b8557ff40b780c4d66aa18c4776 to your computer and use it in GitHub Desktop.
Save arielivandiaz/83781b8557ff40b780c4d66aa18c4776 to your computer and use it in GitHub Desktop.
NodeJS MySQL connection and query with Promise
var mysql = require('mysql');
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
});
let run_mysql = (mysql_query) => {
return new Promise(function (resolve, reject) {
connection.query(mysql_query, function (err, rows, fields) {
if (err) {
return reject(err);
}
resolve(rows);
});
});
}
var createDB = "CREATE DATABASE mydb";
run_mysql(createDB).then(function (rows) {
console.log(rows);
console.log("Database Created")
}).catch((err) => setImmediate(() => {
throw err;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment