This commit is contained in:
Benjamin Toby
2025-02-19 08:42:23 +01:00
parent 2e0b6d0a70
commit c08db13f3d
4 changed files with 26 additions and 34 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ type Param<T extends {
type Return = {
string: string;
values: string[];
} | undefined;
};
/**
* # SQL Query Generator
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
+10 -13
View File
@@ -6,8 +6,6 @@ exports.default = sqlGenerator;
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
*/
function sqlGenerator({ tableName, genObject, dbFullName }) {
if (!genObject)
return undefined;
const finalQuery = (genObject === null || genObject === void 0 ? void 0 : genObject.query) ? genObject.query : undefined;
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
const sqlSearhValues = [];
@@ -72,14 +70,14 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
return genSqlSrchStr({
queryObj: newSubQueryObj,
field: _field,
join: genObject.join,
join: genObject === null || genObject === void 0 ? void 0 : genObject.join,
});
});
return ("(" +
subSearchString.join(` ${queryObj.operator || "AND"} `) +
")");
}
return genSqlSrchStr({ queryObj, field, join: genObject.join });
return genSqlSrchStr({ queryObj, field, join: genObject === null || genObject === void 0 ? void 0 : genObject.join });
});
function generateJoinStr(
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch,
@@ -105,7 +103,7 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
let queryString = (() => {
var _a, _b, _c;
let str = "SELECT";
if ((_a = genObject.selectFields) === null || _a === void 0 ? void 0 : _a[0]) {
if ((_a = genObject === null || genObject === void 0 ? void 0 : genObject.selectFields) === null || _a === void 0 ? void 0 : _a[0]) {
if (genObject.join) {
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${finalDbName}${tableName}.${fld}`).join(",")}`;
}
@@ -114,15 +112,14 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
}
}
else {
if (genObject.join) {
if (genObject === null || genObject === void 0 ? void 0 : genObject.join) {
str += ` ${finalDbName}${tableName}.*`;
}
else {
str += " *";
}
}
if (genObject.join) {
/** @type {string[]} */
if (genObject === null || genObject === void 0 ? void 0 : genObject.join) {
const existingJoinTableNames = [tableName];
str +=
"," +
@@ -159,7 +156,7 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
.join(",");
}
str += ` FROM ${finalDbName}${tableName}`;
if (genObject.join) {
if (genObject === null || genObject === void 0 ? void 0 : genObject.join) {
str +=
" " +
genObject.join
@@ -193,15 +190,15 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
})();
if ((sqlSearhString === null || sqlSearhString === void 0 ? void 0 : sqlSearhString[0]) && sqlSearhString.find((str) => str)) {
const stringOperator = (genObject === null || genObject === void 0 ? void 0 : genObject.searchOperator) || "AND";
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)} `;
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)}`;
}
if (genObject.order)
if (genObject === null || genObject === void 0 ? void 0 : genObject.order)
queryString += ` ORDER BY ${genObject.join
? `${finalDbName}${tableName}.${String(genObject.order.field)}`
: String(genObject.order.field)} ${genObject.order.strategy}`;
if (genObject.limit)
if (genObject === null || genObject === void 0 ? void 0 : genObject.limit)
queryString += ` LIMIT ${genObject.limit}`;
if (genObject.offset)
if (genObject === null || genObject === void 0 ? void 0 : genObject.offset)
queryString += ` OFFSET ${genObject.offset}`;
return {
string: queryString,