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 = { type Return = {
string: string; string: string;
values: string[]; values: string[];
} | undefined; };
/** /**
* # SQL Query Generator * # SQL Query Generator
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql` * @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` * @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
*/ */
function sqlGenerator({ tableName, genObject, dbFullName }) { function sqlGenerator({ tableName, genObject, dbFullName }) {
if (!genObject)
return undefined;
const finalQuery = (genObject === null || genObject === void 0 ? void 0 : genObject.query) ? genObject.query : undefined; const finalQuery = (genObject === null || genObject === void 0 ? void 0 : genObject.query) ? genObject.query : undefined;
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined; const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
const sqlSearhValues = []; const sqlSearhValues = [];
@ -72,14 +70,14 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
return genSqlSrchStr({ return genSqlSrchStr({
queryObj: newSubQueryObj, queryObj: newSubQueryObj,
field: _field, field: _field,
join: genObject.join, join: genObject === null || genObject === void 0 ? void 0 : genObject.join,
}); });
}); });
return ("(" + return ("(" +
subSearchString.join(` ${queryObj.operator || "AND"} `) + 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( function generateJoinStr(
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch, /** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch,
@ -105,7 +103,7 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
let queryString = (() => { let queryString = (() => {
var _a, _b, _c; var _a, _b, _c;
let str = "SELECT"; 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) { if (genObject.join) {
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${finalDbName}${tableName}.${fld}`).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 { else {
if (genObject.join) { if (genObject === null || genObject === void 0 ? void 0 : genObject.join) {
str += ` ${finalDbName}${tableName}.*`; str += ` ${finalDbName}${tableName}.*`;
} }
else { else {
str += " *"; str += " *";
} }
} }
if (genObject.join) { if (genObject === null || genObject === void 0 ? void 0 : genObject.join) {
/** @type {string[]} */
const existingJoinTableNames = [tableName]; const existingJoinTableNames = [tableName];
str += str +=
"," + "," +
@ -159,7 +156,7 @@ function sqlGenerator({ tableName, genObject, dbFullName }) {
.join(","); .join(",");
} }
str += ` FROM ${finalDbName}${tableName}`; str += ` FROM ${finalDbName}${tableName}`;
if (genObject.join) { if (genObject === null || genObject === void 0 ? void 0 : genObject.join) {
str += str +=
" " + " " +
genObject.join 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)) { 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"; 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 queryString += ` ORDER BY ${genObject.join
? `${finalDbName}${tableName}.${String(genObject.order.field)}` ? `${finalDbName}${tableName}.${String(genObject.order.field)}`
: String(genObject.order.field)} ${genObject.order.strategy}`; : String(genObject.order.field)} ${genObject.order.strategy}`;
if (genObject.limit) if (genObject === null || genObject === void 0 ? void 0 : genObject.limit)
queryString += ` LIMIT ${genObject.limit}`; queryString += ` LIMIT ${genObject.limit}`;
if (genObject.offset) if (genObject === null || genObject === void 0 ? void 0 : genObject.offset)
queryString += ` OFFSET ${genObject.offset}`; queryString += ` OFFSET ${genObject.offset}`;
return { return {
string: queryString, string: queryString,

View File

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

View File

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