From c08db13f3dac357fba9e062983857d607ea1731b Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Wed, 19 Feb 2025 08:42:23 +0100 Subject: [PATCH] Bugfix --- .../functions/dsql/sql/sql-generator.d.ts | 2 +- .../functions/dsql/sql/sql-generator.js | 23 ++++++------- .../functions/dsql/sql/sql-generator.ts | 33 ++++++++----------- package.json | 2 +- 4 files changed, 26 insertions(+), 34 deletions(-) diff --git a/dist/package-shared/functions/dsql/sql/sql-generator.d.ts b/dist/package-shared/functions/dsql/sql/sql-generator.d.ts index 5bc2791..a107975 100644 --- a/dist/package-shared/functions/dsql/sql/sql-generator.d.ts +++ b/dist/package-shared/functions/dsql/sql/sql-generator.d.ts @@ -11,7 +11,7 @@ type Param { 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, diff --git a/package-shared/functions/dsql/sql/sql-generator.ts b/package-shared/functions/dsql/sql/sql-generator.ts index 87c418a..499f62d 100644 --- a/package-shared/functions/dsql/sql/sql-generator.ts +++ b/package-shared/functions/dsql/sql/sql-generator.ts @@ -10,12 +10,10 @@ type Param = { 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): 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, diff --git a/package.json b/package.json index 1e198bc..1918c11 100644 --- a/package.json +++ b/package.json @@ -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": {