const mysql = require("mysql");

const config = require("./config");
const util = require("../shared/util");


let connection = mysql.createConnection(config.database);


connection.config.queryFormat = util.mysqlQueryFormat;

exports.closeConnection = () => connection.end();

exports.getPosts = () => {
    return new Promise((resolve, reject) => {
        let query = `
        SELECT
            blog.entry_id AS post_id,
            blog.date,
            blog_categories.category,
            blog.title,
            blog.body
        FROM blog
        LEFT JOIN blog_categories ON blog_categories.category_id = blog.category
        ORDER BY blog.date DESC;`;

        connection.query(query, (err, results) => {
            if (err) reject(err);
            resolve(results); });
    });
};