{"size": 1101, "ext": "js", "max_stars_repo_path": "restaurantFinder/models/db.js", "max_stars_repo_name": "jancx03/RestaurantInfo", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "restaurantFinder/models/db.js", "max_issues_repo_name": "jancx03/RestaurantInfo", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "restaurantFinder/models/db.js", "max_forks_repo_name": "jancx03/RestaurantInfo", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 18.6610169492, "max_line_length": 71, "alphanum_fraction": 0.5386012716}
import mysql from 'mysql';
import dotenv from 'dotenv';

const { createPool } = mysql;
if (process.env.NODE_ENV !== 'production') {
  dotenv.config();
}

class Db {
  constructor() {
    this.connection_details = {
      host: process.env.host,
      user: process.env.user,
      password: process.env.secret,
      database: process.env.database,
      connectionLimit: 5,
    };

    this.pool = this.createConnPool();
  }

  createConnPool() {
    return createPool({
      ...this.connection_details,
    });
  }

  connect(callback) {
    return this.pool.getConnection((err, conn) => callback(err, conn));
  }

  query(string) {
    return new Promise((resolve, reject) => {
      this.connect((err, conn) => {
        if (err) {
          conn.release();
          return reject(err);
        }

        conn.query(string, (_err, data) => {
          if (_err) {
            conn.release();
            return reject(_err);
          }

          conn.release();
          return resolve(data);
        });

        return 0;
      });

      return 0;
    });
  }
}

export default new Db();
