const mysql = require('mysql2/promise');
const config = require('./config.js');

module.exports = {
    init() {
        if (!this.pool) {
            this.pool = mysql.createPool(config.mysql);
            this.pool.on('connection', connection => {
                connection.config.namedPlaceholders = true;
            });
        }
    },
    /**
     * Makes a query from a raw sql
     * @param {String} query - The raw SQL query
     * @param {Object} params - Parameterized values
     *
     * @return {Object} - Result promise
     */
    rawSql(query, params) {
        return this.pool.query(query, params).then(([rows]) => {
            return rows;
        });
    },
    /**
     * Returns the first row from a given raw sql
     * @param {String} query - The raw SQL query
     * @param {Object} params - Parameterized values
     *
     * @return {Object} - Result promise
     */
    findOne(query, params) {
        return this.rawSql(query, params).then(([row]) => row);
    },
    /**
     * Returns the first property from a given raw sql
     * @param {String} query - The raw SQL query
     * @param {Object} params - Parameterized values
     *
     * @return {Object} - Result promise
     */
    findValue(query, params) {
        return this.findOne(query, params).then(row => {
            return Object.values(row)[0];
        });
    },
    findFromDef() {

    }
};