Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.2 KiB

  1. const mysql = require('mysql');
  2. const con = mysql.createConnection({
  3. host: "localhost",
  4. user: "yourusername",
  5. password: "yourpassword"
  6. });
  7. con.connect(function(err) {
  8. if (err) throw err;
  9. console.log("Connected!");
  10. });
  11. module.exports=
  12. {
  13. /**
  14. * Function used to query the database for records
  15. *
  16. * @param sqlStatement
  17. * @returns {Array}
  18. */
  19. fetch : function(sqlStatement)
  20. {
  21. con.connect(function(err)
  22. {
  23. if (err) throw err;
  24. con.query(sqlStatement, function (err, result)
  25. {
  26. if (err) throw err;
  27. return result;
  28. });
  29. });
  30. return [];
  31. },
  32. /**
  33. * Function used to use insert statements into the database
  34. *
  35. * @param sqlStatement
  36. * @return the id of the new record - if there is one
  37. */
  38. insert : function(sqlStatement)
  39. {
  40. con.connect(function(err)
  41. {
  42. if (err) throw err;
  43. con.query(sqlStatement, function (err, result)
  44. {
  45. if (err) throw err;
  46. return result.insertId;
  47. });
  48. });
  49. return 0;
  50. }
  51. };