const mysql = require("mysql"); const config = require("../config"); let connection = mysql.createConnection(config.dwelling.database); // This code was taken from https://www.npmjs.com/package/mysql#custom-format // Only fourth line was changed. connection.config.queryFormat = function (query, values) { if (!values) return query; return query.replace(/\:(\w+)/g, function (txt, key) { if (values[key] !== undefined) return this.escape(values[key]); return txt; }.bind(this)); }; exports.closeConnection = () => connection.end(); exports.getPosts = category => { return new Promise((resolve, reject) => { let query = "\ SELECT\ `blog`.`entry_id` AS `post_id`,\ `blog`.`date`,\ `blog`.`title`,\ `blog`.`body`\ FROM `blog`\ LEFT JOIN `blog_categories` ON `blog_categories`.`category_id` = `blog`.`category`\ WHERE `blog_categories`.`category` = :category\ ORDER BY `blog`.`date` DESC;"; connection.query(query, { category: category }, (err, results) => { if (err) reject(err); resolve(results); }); }); }; exports.getPostsForRSS = () => { 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); }); }); };