Skip to content

Instantly share code, notes, and snippets.

@isabellachen
Created January 31, 2018 22:57
Show Gist options
  • Save isabellachen/8547dbc75d06c6cc83dd5282762bcf83 to your computer and use it in GitHub Desktop.
Save isabellachen/8547dbc75d06c6cc83dd5282762bcf83 to your computer and use it in GitHub Desktop.
quickly set up a connection to mysql db
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'myLibrary',
});
connection.connect(function (err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
const getAll = () => {
return new Promise ((resolve, reject) => {
connection.query('SELECT * from authors', (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
};
getAll().then(data => console.log(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment