Guest User

Untitled

a guest
Oct 7th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. const mysql = require('mysql');
  2.  
  3.  
  4. class Mysql {
  5.  
  6. constructor() {
  7.  
  8. if (this.instance) {
  9. return this.instance;
  10. }
  11.  
  12. this.conexion = mysql.createConnection({
  13. host: 'localhost',
  14. user: 'root',
  15. password: '',
  16. database: 'cerveza',
  17. port: '3306'
  18. });
  19.  
  20. this.conectarDB();
  21.  
  22. this.instance = this;
  23. }
  24.  
  25.  
  26. static conectarDB() {
  27. this.conexion.connect((err) => {
  28. if (err) {
  29. console.log(err);
  30. return;
  31. }
  32. this.conectado = true;
  33. console.log('Base de datos conectado');
  34. });
  35. }
  36.  
  37. static ejecutarQuery(query, callback) {
  38.  
  39. this.instance.conexion.query(query, (err, results, fields) => {
  40. if (err) {
  41. console.log("Error en el query");
  42. console.log(err);
  43. return callback(err);
  44. }
  45.  
  46. if (results.length === 0) {
  47. callback('El registro solicitado no existe');
  48. } else {
  49. callback(null, results);
  50. }
  51. });
  52. }
  53.  
  54. }
  55.  
  56.  
  57. module.exports = {
  58. Mysql
  59. }
Add Comment
Please, Sign In to add comment