Moved queryFormat func that is used in several places to util.js.

This commit is contained in:
Alexander Andreev 2021-02-20 22:52:39 +04:00
parent 02f046c789
commit 3b5c08936d
Signed by: Arav
GPG Key ID: 610DF2574456329F
3 changed files with 17 additions and 22 deletions

View File

@ -1,27 +1,17 @@
const mysql = require("mysql"); const mysql = require("mysql");
const config = require("../config"); const config = require("../config");
const util = require("../util");
let connection = mysql.createConnection(config.dwelling.database); let connection = mysql.createConnection(config.dwelling.database);
// This code was taken from https://www.npmjs.com/package/mysql#custom-format connection.config.queryFormat = util.mysqlQueryFormat;
// 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.pageSize = config.dwelling.guestbook.pageSize !== undefined exports.pageSize = config.dwelling.guestbook.pageSize !== undefined
? config.dwelling.guestbook.pageSize : 20; ? config.dwelling.guestbook.pageSize : 20;
exports.closeConnection = () => connection.end(); exports.closeConnection = () => connection.end();
exports.getPosts = async (page = 1, page_size = exports.pageSize) => { exports.getPosts = async (page = 1, page_size = exports.pageSize) => {

View File

@ -1,19 +1,13 @@
const mysql = require("mysql"); const mysql = require("mysql");
const config = require("../config"); const config = require("../config");
const util = require("../util");
let connection = mysql.createConnection(config.dwelling.database); 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 = util.mysqlQueryFormat;
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.closeConnection = () => connection.end();

11
util.js
View File

@ -40,3 +40,14 @@ exports.datetime = datetime;
exports.rssLink = (proto, host, date, category, timezone="UTC") => exports.rssLink = (proto, host, date, category, timezone="UTC") =>
`${proto}://${host}/mindflow#${category}-${datetime(date, formats.id_date, timezone)}`; `${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));
};