From 3b5c08936d70996cf5d899969c83f34639be7063 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 20 Feb 2021 22:52:39 +0400 Subject: [PATCH] Moved queryFormat func that is used in several places to util.js. --- dwelling/guestbook.js | 14 ++------------ dwelling/mindflow.js | 14 ++++---------- util.js | 11 +++++++++++ 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/dwelling/guestbook.js b/dwelling/guestbook.js index 0d56317..9d7e5cf 100644 --- a/dwelling/guestbook.js +++ b/dwelling/guestbook.js @@ -1,27 +1,17 @@ const mysql = require("mysql"); const config = require("../config"); +const util = require("../util"); 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)); -}; - +connection.config.queryFormat = util.mysqlQueryFormat; exports.pageSize = config.dwelling.guestbook.pageSize !== undefined ? config.dwelling.guestbook.pageSize : 20; - exports.closeConnection = () => connection.end(); exports.getPosts = async (page = 1, page_size = exports.pageSize) => { diff --git a/dwelling/mindflow.js b/dwelling/mindflow.js index d19f77b..a96c61f 100644 --- a/dwelling/mindflow.js +++ b/dwelling/mindflow.js @@ -1,19 +1,13 @@ const mysql = require("mysql"); const config = require("../config"); +const util = require("../util"); + 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)); -}; + +connection.config.queryFormat = util.mysqlQueryFormat; exports.closeConnection = () => connection.end(); diff --git a/util.js b/util.js index 65e9272..f68a4aa 100644 --- a/util.js +++ b/util.js @@ -40,3 +40,14 @@ exports.datetime = datetime; exports.rssLink = (proto, host, date, category, timezone="UTC") => `${proto}://${host}/mindflow#${category}-${datetime(date, formats.id_date, timezone)}`; +// This code was taken from https://www.npmjs.com/package/mysql#custom-format +// Only fourth line was changed. +exports.mysqlQueryFormat = 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)); +}; +