From d627655422f01315894f5fa1a90c65391f1a0f2a Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Thu, 19 Dec 2024 21:37:34 +0100 Subject: [PATCH] Updates --- .../functions/dsql/sql/sql-generator.js | 20 +++++++++++++------ package-shared/types/index.d.ts | 1 + package-shared/types/index.ts | 1 + package.json | 2 +- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/package-shared/functions/dsql/sql/sql-generator.js b/package-shared/functions/dsql/sql/sql-generator.js index 5eea1e7..2459145 100644 --- a/package-shared/functions/dsql/sql/sql-generator.js +++ b/package-shared/functions/dsql/sql/sql-generator.js @@ -74,6 +74,8 @@ function sqlGenerator({ tableName, genObject }) { return `'${mtch.targetLiteral}'`; } + if (join.alias) return `${join.alias}.${mtch.target}`; + return `${ typeof mtch.target == "object" ? mtch.target.tableName @@ -112,17 +114,21 @@ function sqlGenerator({ tableName, genObject }) { "," + genObject.join .map((joinObj) => { - if (existingJoinTableNames.includes(joinObj.tableName)) + const joinTableName = joinObj.alias + ? joinObj.alias + : joinObj.tableName; + + if (existingJoinTableNames.includes(joinTableName)) return null; - existingJoinTableNames.push(joinObj.tableName); + existingJoinTableNames.push(joinTableName); if (joinObj.selectFields) { return joinObj.selectFields .map((slFld) => { if (typeof slFld == "string") { - return `${joinObj.tableName}.${slFld}`; + return `${joinTableName}.${slFld}`; } else if (typeof slFld == "object") { - let aliasSlctFld = `${joinObj.tableName}.${slFld.field}`; + let aliasSlctFld = `${joinTableName}.${slFld.field}`; if (slFld.alias) aliasSlctFld += ` as ${slFld.alias}`; return aliasSlctFld; @@ -130,7 +136,7 @@ function sqlGenerator({ tableName, genObject }) { }) .join(","); } else { - return `${joinObj.tableName}.*`; + return `${joinTableName}.*`; } }) .filter((_) => Boolean(_)) @@ -147,7 +153,9 @@ function sqlGenerator({ tableName, genObject }) { return ( join.joinType + " " + - join.tableName + + (join.alias + ? join.tableName + " AS " + join.alias + : join.tableName) + " ON " + (() => { if (Array.isArray(join.match)) { diff --git a/package-shared/types/index.d.ts b/package-shared/types/index.d.ts index 26a9571..ece093a 100644 --- a/package-shared/types/index.d.ts +++ b/package-shared/types/index.d.ts @@ -949,6 +949,7 @@ export type ServerQueryParamsJoin = { joinType: "INNER JOIN" | "JOIN" | "LEFT JOIN" | "RIGHT JOIN"; + alias?: string; tableName: Table; match?: ServerQueryParamsJoinMatchObject | ServerQueryParamsJoinMatchObject[]; selectFields?: (keyof Field | { diff --git a/package-shared/types/index.ts b/package-shared/types/index.ts index facd5f6..449f570 100644 --- a/package-shared/types/index.ts +++ b/package-shared/types/index.ts @@ -1131,6 +1131,7 @@ export type ServerQueryParamsJoin< Field extends object = { [key: string]: any } > = { joinType: "INNER JOIN" | "JOIN" | "LEFT JOIN" | "RIGHT JOIN"; + alias?: string; tableName: Table; match?: | ServerQueryParamsJoinMatchObject diff --git a/package.json b/package.json index 9b97942..a438fc2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "3.1.7", + "version": "3.1.8", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": {