Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. var fs = require("fs");
  2. var mysql = require('mysql');
  3. var async = require('async');
  4. var path = require('path');
  5.  
  6. function Db(){
  7. var filePath = path.join(__dirname, '../certificates/dbCredentials.txt');
  8. var str = fs.readFileSync(filePath, {encoding: 'utf-8'}).toString();
  9. str = str.split(',');
  10. var pool;
  11.  
  12. this.init = function(){
  13. pool = mysql.createPool({
  14. connectionLimit : 10,
  15. host : 'mysql.stud.iie.ntnu.no',
  16. user : str[0],
  17. password : str[1],
  18. database : str[0]
  19. });
  20. };
  21.  
  22. this.sql_query = function(sql, callback){
  23. function dbconnect(sql){
  24. return new Promise(resolve => {
  25. pool.getConnection(function(err, connection) {
  26. // Use the connection
  27. connection.query(sql, function (error, results, fields) {
  28. // And done with the connection.
  29. connection.release();
  30. // Handle error after the release.
  31. if (error) throw error;
  32. resolve(results)
  33. // Don't use the connection here, it has been returned to the pool.
  34. });
  35. });
  36. });
  37. }
  38.  
  39. async function f1() {
  40. var result = await dbconnect(sql);
  41. callback(result)
  42. }
  43. f1();
  44. };
  45. this.init();
  46. }
  47.  
  48.  
  49.  
  50. module.exports = Db;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement