Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require("fs");
- var mysql = require('mysql');
- var async = require('async');
- var path = require('path');
- function Db(){
- var filePath = path.join(__dirname, '../certificates/dbCredentials.txt');
- var str = fs.readFileSync(filePath, {encoding: 'utf-8'}).toString();
- str = str.split(',');
- var pool;
- this.init = function(){
- pool = mysql.createPool({
- connectionLimit : 10,
- host : 'mysql.stud.iie.ntnu.no',
- user : str[0],
- password : str[1],
- database : str[0]
- });
- };
- this.sql_query = function(sql, callback){
- function dbconnect(sql){
- return new Promise(resolve => {
- pool.getConnection(function(err, connection) {
- // Use the connection
- connection.query(sql, function (error, results, fields) {
- // And done with the connection.
- connection.release();
- // Handle error after the release.
- if (error) throw error;
- resolve(results)
- // Don't use the connection here, it has been returned to the pool.
- });
- });
- });
- }
- async function f1() {
- var result = await dbconnect(sql);
- callback(result)
- }
- f1();
- };
- this.init();
- }
- module.exports = Db;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement