const { Client } = require ("pg");
const {postgresSQL} = require("./keys")

const client = new Client(postgresSQL);

async function connect() {
    try{
    await client.connect();
    console.log("Database connected!");
    }
    catch(error)
    {
        console.error(error)
    }
}
connect();

async function query(queryString,param)
{
    try {
    let result = await client.query(queryString,param);
    let resultarray = [];
    for(let row of result.rows)
    {
        resultarray.push(row.obj);
    }
    return resultarray;
    }
    catch(error)
    {
        console.log(error)
    }
}

async function nonQuery(queryString,param) {
    try {
        await client.query(queryString,param);
    } catch (error) {
        console.error(error)
    }
}

module.exports = {
    query, nonQuery
}