dbconnection.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const mysql2 = require('mysql2');
  2. const db = require('../configs/config.json')
  3. const pool = mysql2.createPool(
  4. {
  5. connectionLimit: 20,
  6. host: db.database.host,
  7. user: db.database.user,
  8. password: db.database.password,
  9. database: db.database.database,
  10. port:db.database.port
  11. }
  12. );
  13. exports.executequery = function (query) {
  14. return new Promise((result, reject) => {
  15. // //console.log(pool);
  16. // get the connection for connection pool
  17. pool.getConnection((error, connection) => {
  18. // if error during getting connection
  19. if (error) {
  20. //console.log("Connection Error -->>", error);
  21. reject(error)
  22. } else {
  23. //console.log("db connected");
  24. connection.query(query, function (err, rows, fields) {
  25. connection.release();
  26. if (err) {
  27. result(err);
  28. };
  29. result(rows);
  30. });
  31. }
  32. });
  33. });
  34. }
  35. exports.executevaluesquery = function (query, values) {
  36. return new Promise((result, reject) => {
  37. // get the connection for connection pool
  38. pool.getConnection((error, connection) => {
  39. if (error) {
  40. //console.log("Connection Error -->>", error);
  41. reject(error)
  42. } else {
  43. connection.query(query, values, function (err, rows, fields) {
  44. //release Conncetion
  45. connection.release();
  46. if (err) {
  47. reject(err);
  48. };
  49. result(rows);
  50. });
  51. }
  52. });
  53. });
  54. }