Created
December 1, 2018 14:50
-
-
Save arielivandiaz/83781b8557ff40b780c4d66aa18c4776 to your computer and use it in GitHub Desktop.
NodeJS MySQL connection and query with Promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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