Update .gitignore, add dist directory
This commit is contained in:
Vendored
+94
@@ -0,0 +1,94 @@
|
||||
import dbHandler from "../db-handler";
|
||||
import _ from "lodash";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
function quoteIdentifier(identifier) {
|
||||
return `\`${identifier.replace(/`/g, "``")}\``;
|
||||
}
|
||||
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),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
sqlObj = sqlGenerator({
|
||||
tableName: quoteIdentifier(table),
|
||||
genObject: finalQuery,
|
||||
});
|
||||
const res = await dbHandler({
|
||||
query: sqlObj.string,
|
||||
values: sqlObj.values,
|
||||
config,
|
||||
});
|
||||
if (!res.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Database select failed",
|
||||
debug: {
|
||||
sqlObj,
|
||||
sql: sqlObj.string,
|
||||
},
|
||||
};
|
||||
}
|
||||
const batchRes = (res.payload || []);
|
||||
let resp = {
|
||||
success: true,
|
||||
payload: batchRes,
|
||||
single_res: batchRes[0],
|
||||
debug: {
|
||||
sqlObj,
|
||||
sql: sqlObj.string,
|
||||
},
|
||||
};
|
||||
if (count) {
|
||||
const countSqlObject = sqlGenerator({
|
||||
tableName: quoteIdentifier(table),
|
||||
genObject: finalQuery,
|
||||
count,
|
||||
});
|
||||
const countSql = `SELECT COUNT(*) AS count FROM (${countSqlObject.string}) AS c`;
|
||||
const countRes = await dbHandler({
|
||||
query: countSql,
|
||||
values: countSqlObject.values,
|
||||
config,
|
||||
});
|
||||
if (!countRes.success) {
|
||||
return {
|
||||
...resp,
|
||||
success: false,
|
||||
msg: "Database count failed",
|
||||
debug: {
|
||||
...resp.debug,
|
||||
count_sql: countSql,
|
||||
},
|
||||
};
|
||||
}
|
||||
const countRows = countRes.payload || [];
|
||||
const countVal = countRows[0]?.count ?? countRows[0]?.["count"];
|
||||
resp = {
|
||||
...resp,
|
||||
count: Number(countVal),
|
||||
debug: {
|
||||
...resp.debug,
|
||||
count_sql: countSql,
|
||||
},
|
||||
};
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user