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

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`

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,

View File

@ -10,12 +10,10 @@ type Param<T extends { [key: string]: any } = { [key: string]: any }> = {
dbFullName?: string;
};
type Return =
| {
string: string;
values: string[];
}
| undefined;
type Return = {
string: string;
values: string[];
};
/**
* # SQL Query Generator
@ -24,8 +22,6 @@ type Return =
export default function sqlGenerator<
T extends { [key: string]: any } = { [key: string]: any }
>({ tableName, genObject, dbFullName }: Param<T>): Return {
if (!genObject) return undefined;
const finalQuery = genObject?.query ? genObject.query : undefined;
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
@ -109,7 +105,7 @@ export default function sqlGenerator<
return genSqlSrchStr({
queryObj: newSubQueryObj,
field: _field,
join: genObject.join,
join: genObject?.join,
});
});
@ -120,7 +116,7 @@ export default function sqlGenerator<
);
}
return genSqlSrchStr({ queryObj, field, join: genObject.join });
return genSqlSrchStr({ queryObj, field, join: genObject?.join });
});
function generateJoinStr(
@ -162,7 +158,7 @@ export default function sqlGenerator<
let queryString = (() => {
let str = "SELECT";
if (genObject.selectFields?.[0]) {
if (genObject?.selectFields?.[0]) {
if (genObject.join) {
str += ` ${genObject.selectFields
?.map((fld) => `${finalDbName}${tableName}.${fld}`)
@ -171,15 +167,14 @@ export default function sqlGenerator<
str += ` ${genObject.selectFields?.join(",")}`;
}
} else {
if (genObject.join) {
if (genObject?.join) {
str += ` ${finalDbName}${tableName}.*`;
} else {
str += " *";
}
}
if (genObject.join) {
/** @type {string[]} */
if (genObject?.join) {
const existingJoinTableNames: string[] = [tableName];
str +=
@ -219,7 +214,7 @@ export default function sqlGenerator<
str += ` FROM ${finalDbName}${tableName}`;
if (genObject.join) {
if (genObject?.join) {
str +=
" " +
genObject.join
@ -262,18 +257,18 @@ export default function sqlGenerator<
if (sqlSearhString?.[0] && sqlSearhString.find((str) => str)) {
const stringOperator = genObject?.searchOperator || "AND";
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)} `;
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)}`;
}
if (genObject.order)
if (genObject?.order)
queryString += ` ORDER BY ${
genObject.join
? `${finalDbName}${tableName}.${String(genObject.order.field)}`
: String(genObject.order.field)
} ${genObject.order.strategy}`;
if (genObject.limit) queryString += ` LIMIT ${genObject.limit}`;
if (genObject.offset) queryString += ` OFFSET ${genObject.offset}`;
if (genObject?.limit) queryString += ` LIMIT ${genObject.limit}`;
if (genObject?.offset) queryString += ` OFFSET ${genObject.offset}`;
return {
string: queryString,

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/datasquirel",
"version": "4.1.4",
"version": "4.1.5",
"description": "Cloud-based SQL data management tool",
"main": "dist/index.js",
"bin": {