Major version update. Major bugfixes. Security vulnerabilites fixed
This commit is contained in:
Vendored
+32
-23
@@ -1,27 +1,24 @@
|
||||
import mysql from "mysql";
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import dbHandler from "../db-handler";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
export default async function DbSelect({ table, query, count, targetId, }) {
|
||||
export default async function DbSelect({ table, query, count, targetId, config, }) {
|
||||
let sqlObj = null;
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
if (targetId) {
|
||||
finalQuery = _.merge(finalQuery, {
|
||||
query: {
|
||||
id: {
|
||||
value: String(targetId),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
const finalQuery = query || {};
|
||||
sqlObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
targetId,
|
||||
});
|
||||
let sql = mysql.format(sqlObj.string, sqlObj.values);
|
||||
const res = DbClient.query(sql);
|
||||
const batchRes = res.all();
|
||||
const sql = sqlObj.string;
|
||||
const res = dbHandler({
|
||||
query: sql,
|
||||
values: sqlObj.values,
|
||||
config,
|
||||
});
|
||||
if (!res.success) {
|
||||
return { ...res, debug: { sqlObj, sql } };
|
||||
}
|
||||
const batchRes = res.payload || [];
|
||||
let resp = {
|
||||
success: Boolean(batchRes[0]),
|
||||
payload: batchRes,
|
||||
@@ -32,16 +29,28 @@ export default async function DbSelect({ table, query, count, targetId, }) {
|
||||
},
|
||||
};
|
||||
if (count) {
|
||||
let count_sql_object = sqlGenerator({
|
||||
const count_sql_object = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
targetId,
|
||||
count,
|
||||
});
|
||||
let count_sql = mysql.format(count_sql_object.string, count_sql_object.values);
|
||||
count_sql = `SELECT COUNT(*) FROM (${count_sql}) as c`;
|
||||
const count_res = DbClient.query(count_sql).all();
|
||||
const count_val = count_res[0]?.["COUNT(*)"];
|
||||
resp["count"] = Number(count_val);
|
||||
const count_sql = `SELECT COUNT(*) AS count FROM (${count_sql_object.string}) AS c`;
|
||||
const count_res = dbHandler({
|
||||
query: count_sql,
|
||||
values: count_sql_object.values,
|
||||
config,
|
||||
});
|
||||
if (!count_res.success) {
|
||||
return {
|
||||
...resp,
|
||||
success: false,
|
||||
error: count_res.error,
|
||||
msg: "Database count failed",
|
||||
debug: { ...resp.debug, count_sql },
|
||||
};
|
||||
}
|
||||
resp["count"] = Number(count_res.singleRes?.count);
|
||||
resp["debug"]["count_sql"] = count_sql;
|
||||
}
|
||||
return resp;
|
||||
|
||||
Reference in New Issue
Block a user