Last active
May 24, 2017 02:51
-
-
Save bkawk/7bc4d6a9ec837734c073a966b1ebe7e0 to your computer and use it in GitHub Desktop.
Send SQL statement to MySQL
This file contains hidden or 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
// This file does only one thing it can not call on other functions | |
const mysql = require('mysql'); | |
const connection = mysql.createConnection({ | |
host : process.env.MYSQL_HOST, | |
user : process.env.MYSQL_USER, | |
password : process.env.MYSQL_PASSWORD, | |
database : process.env.MYSQL_DATABASE | |
}); | |
exports.query = (SQLstatement) => { | |
return new Promise((resolve, reject) => { | |
if(SQLstatement){ | |
connection.connect((err) => { | |
if(err){ | |
reject(Error(err)) | |
} else { | |
connection.query(SQLstatement, (error, results, fields) => { | |
resolve(results) | |
connection.end(function(err) { | |
if (err) { | |
reject(Error(err)) | |
} | |
}); | |
}); | |
} | |
}) | |
} else ( | |
reject(Error('No SQL statement provided')) | |
) | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment