import pg from 'pg';
import log from './utils/log.js'
import fs from 'fs';
import config from '../config.json' with {type: "json"};

const { Pool } = pg;

log.debug(
    `Connecting to PG database ${config.dbname}@${config.dbhost}:${config.dbport} with credentials:
username: ${config.dbuser}
password: <hidden>`
);

const pool = new Pool({
    user: config.dbuser,
    host: config.dbhost,
    port: config.dbport,
    database: config.dbname,
    password: config.dbpassword
});

log.debug("Database connection successfull. Creating tables");

pool.query(fs.readFileSync('./db_schema.psql').toString());

log.debug("Tables succesfully created");

export default pool;