This commit is contained in:
Benjamin Toby 2024-12-19 21:37:34 +01:00
parent a2ba913340
commit d627655422
4 changed files with 17 additions and 7 deletions

View File

@ -74,6 +74,8 @@ function sqlGenerator({ tableName, genObject }) {
return `'${mtch.targetLiteral}'`; return `'${mtch.targetLiteral}'`;
} }
if (join.alias) return `${join.alias}.${mtch.target}`;
return `${ return `${
typeof mtch.target == "object" typeof mtch.target == "object"
? mtch.target.tableName ? mtch.target.tableName
@ -112,17 +114,21 @@ function sqlGenerator({ tableName, genObject }) {
"," + "," +
genObject.join genObject.join
.map((joinObj) => { .map((joinObj) => {
if (existingJoinTableNames.includes(joinObj.tableName)) const joinTableName = joinObj.alias
? joinObj.alias
: joinObj.tableName;
if (existingJoinTableNames.includes(joinTableName))
return null; return null;
existingJoinTableNames.push(joinObj.tableName); existingJoinTableNames.push(joinTableName);
if (joinObj.selectFields) { if (joinObj.selectFields) {
return joinObj.selectFields return joinObj.selectFields
.map((slFld) => { .map((slFld) => {
if (typeof slFld == "string") { if (typeof slFld == "string") {
return `${joinObj.tableName}.${slFld}`; return `${joinTableName}.${slFld}`;
} else if (typeof slFld == "object") { } else if (typeof slFld == "object") {
let aliasSlctFld = `${joinObj.tableName}.${slFld.field}`; let aliasSlctFld = `${joinTableName}.${slFld.field}`;
if (slFld.alias) if (slFld.alias)
aliasSlctFld += ` as ${slFld.alias}`; aliasSlctFld += ` as ${slFld.alias}`;
return aliasSlctFld; return aliasSlctFld;
@ -130,7 +136,7 @@ function sqlGenerator({ tableName, genObject }) {
}) })
.join(","); .join(",");
} else { } else {
return `${joinObj.tableName}.*`; return `${joinTableName}.*`;
} }
}) })
.filter((_) => Boolean(_)) .filter((_) => Boolean(_))
@ -147,7 +153,9 @@ function sqlGenerator({ tableName, genObject }) {
return ( return (
join.joinType + join.joinType +
" " + " " +
join.tableName + (join.alias
? join.tableName + " AS " + join.alias
: join.tableName) +
" ON " + " ON " +
(() => { (() => {
if (Array.isArray(join.match)) { if (Array.isArray(join.match)) {

View File

@ -949,6 +949,7 @@ export type ServerQueryParamsJoin<Table extends string = string, Field extends o
[key: string]: any; [key: string]: any;
}> = { }> = {
joinType: "INNER JOIN" | "JOIN" | "LEFT JOIN" | "RIGHT JOIN"; joinType: "INNER JOIN" | "JOIN" | "LEFT JOIN" | "RIGHT JOIN";
alias?: string;
tableName: Table; tableName: Table;
match?: ServerQueryParamsJoinMatchObject<Field> | ServerQueryParamsJoinMatchObject<Field>[]; match?: ServerQueryParamsJoinMatchObject<Field> | ServerQueryParamsJoinMatchObject<Field>[];
selectFields?: (keyof Field | { selectFields?: (keyof Field | {

View File

@ -1131,6 +1131,7 @@ export type ServerQueryParamsJoin<
Field extends object = { [key: string]: any } Field extends object = { [key: string]: any }
> = { > = {
joinType: "INNER JOIN" | "JOIN" | "LEFT JOIN" | "RIGHT JOIN"; joinType: "INNER JOIN" | "JOIN" | "LEFT JOIN" | "RIGHT JOIN";
alias?: string;
tableName: Table; tableName: Table;
match?: match?:
| ServerQueryParamsJoinMatchObject<Field> | ServerQueryParamsJoinMatchObject<Field>

View File

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