Created
January 31, 2018 22:57
-
-
Save isabellachen/8547dbc75d06c6cc83dd5282762bcf83 to your computer and use it in GitHub Desktop.
quickly set up a connection to mysql db
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
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