import dotenv from 'dotenv';
import mysql from 'mysql2/promise';
import fs from 'fs';

dotenv.config();

const connection = mysql.createPool({
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    port: process.env.DB_PORT,
    multipleStatements: true,
});

const initialize = async () => {
    try{
        const sql = await fs.readFileSync('sql/sql.sql', 'utf8');
        await connection.query(sql);
        console.log('Database initialized successfully!');
    } catch(err) {
        console.log(err);
    }
}

export { connection, initialize };