import * as mysql from "mysql";

const env = process.env
const { host, port, user, password, database } = env
const mysqlConfig :mysql.PoolConfig = {
    host,
    user,
    password,
    database,
    port: Number(port)
}

const pool = mysql.createPool(mysqlConfig)

const query = (sql: string) => {
    return new Promise<any>((resolve, reject) => {
      pool.getConnection((error, connection) => {
        if (error) {
          reject(error)
        } else {
          connection.query(sql, (error, results) => {
            if (error) {
              reject(error)
            } else {
              resolve(results)
            }
            connection.release()
          })
        }
      })
    })
  }

export const mysqlConnect = query
{'avg_line_length': 21.970588235294116, 'max_line_length': 53, 'alphanum_fraction': 0.5301204819277109, 'licenses': ['MIT'], 'repository_name': 'KyrieZheng/node-examination', 'path': 'app/common/database/mysql.ts', 'size': 747, 'lang': 'TypeScript'}