diff --git a/lib/DboBuilder/QueryBuilder/getJoinQuery.ts b/lib/DboBuilder/QueryBuilder/getJoinQuery.ts index c07db4c6..9190fb54 100644 --- a/lib/DboBuilder/QueryBuilder/getJoinQuery.ts +++ b/lib/DboBuilder/QueryBuilder/getJoinQuery.ts @@ -44,6 +44,7 @@ type GetJoinQueryResult = { resultAlias: string; queryLines: string[]; firstJoinTableJoinFields: string[]; + isOrJoin: boolean; } /** @@ -79,6 +80,7 @@ export const getJoinQuery = (viewHandler: ViewHandler, { q1, q2, depth }: Args): const joinType = q2.isLeftJoin? "LEFT" : "INNER"; + const isOrJoin = firstJoinTablePath.on.length > 1; const joinCondition = getJoinOnCondition({ on: firstJoinTablePath.on, leftAlias: asName(q1.tableAlias || q1.table), @@ -94,33 +96,38 @@ export const getJoinQuery = (viewHandler: ViewHandler, { q1, q2, depth }: Args): const { innerQuery } = getInnerJoinQuery({ paths, q1, q2, rootSelectItems, targetTableAliasRaw }); - // const requiredJoinFields = joinFields.map(field => getJoinCol(field).alias); + const requiredJoinFields = joinFields.map(field => getJoinCol(field).alias); /** * Used to prevent duplicates in case of OR filters */ const rootTableIdField = `${ROOT_TABLE_ALIAS}.${ROOT_TABLE_ROW_NUM_ID}`; const wrappingQuery = [ `SELECT `, - // ...indentLines([...requiredJoinFields, jsonAgg, ...rootNestedSort.map(d => d.nested!.wrapperQuerySortItem)], { appendCommas: true }), - ...indentLines([rootTableIdField, jsonAgg, ...rootNestedSort.map(d => d.nested!.wrapperQuerySortItem)], { appendCommas: true }), + ...indentLines([ + ...(isOrJoin? [rootTableIdField]: requiredJoinFields), + jsonAgg, + ...rootNestedSort.map(d => d.nested!.wrapperQuerySortItem) + ], { appendCommas: true }), `FROM (`, ...indentLines(innerQuery), `) ${targetTableAlias}`, `WHERE ${joinCondition}`, - `GROUP BY ${rootTableIdField}` + `GROUP BY ${isOrJoin? rootTableIdField : requiredJoinFields}`, ]; const queryLines = [ `${joinType} JOIN LATERAL (`, ...indentLines(wrappingQuery), `) ${targetTableAlias}`, - // `ON ${joinCondition}` - `ON ${targetTableAlias}.${ROOT_TABLE_ROW_NUM_ID} = ${rootTableIdField}` + isOrJoin? + `ON ${targetTableAlias}.${ROOT_TABLE_ROW_NUM_ID} = ${rootTableIdField}` : + `ON ${joinCondition}` ]; return { resultAlias: JSON_AGG_FIELD_NAME, queryLines, + isOrJoin, firstJoinTableJoinFields: firstJoinTableJoinFields, } } diff --git a/lib/DboBuilder/QueryBuilder/getSelectQuery.ts b/lib/DboBuilder/QueryBuilder/getSelectQuery.ts index 8f2227cb..ee7c3416 100644 --- a/lib/DboBuilder/QueryBuilder/getSelectQuery.ts +++ b/lib/DboBuilder/QueryBuilder/getSelectQuery.ts @@ -50,7 +50,7 @@ export function getSelectQuery( `SELECT` ,...indentLines(selectItems, { appendCommas: true }) , `FROM ( ` - , ` SELECT * ${parsedJoins.length? `, ROW_NUMBER() OVER() as ${ROOT_TABLE_ROW_NUM_ID}` : ""}` + , ` SELECT * ${parsedJoins.some(j => j.isOrJoin)? `, ROW_NUMBER() OVER() as ${ROOT_TABLE_ROW_NUM_ID}` : ""}` , ` FROM ${q.table}` , ` ${q.where}` , `) ${ROOT_TABLE_ALIAS}` diff --git a/lib/Prostgles.ts b/lib/Prostgles.ts index 3cc39ee4..5f625a50 100644 --- a/lib/Prostgles.ts +++ b/lib/Prostgles.ts @@ -871,6 +871,8 @@ export class Prostgles { const isAllowed = await canRunSQL(this, localParams); if(isAllowed){ localParams.returnQuery = (param3 as LocalParams).returnQuery; + } else { + throw "Must be allowed to run sql to use returnQuery" } } const res = await this.dbo[tableName]![command]!(param1, param2, param3, valid_table_command_rules, localParams); diff --git a/package-lock.json b/package-lock.json index 520e5191..fd21bcee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prostgles-server", - "version": "4.1.63", + "version": "4.1.64", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "prostgles-server", - "version": "4.1.63", + "version": "4.1.64", "license": "MIT", "dependencies": { "@types/express": "^4.17.13", diff --git a/package.json b/package.json index 3fc10bb7..6cc1296c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prostgles-server", - "version": "4.1.63", + "version": "4.1.64", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/tests/client/PID.txt b/tests/client/PID.txt index eb31a72e..fcf258aa 100644 --- a/tests/client/PID.txt +++ b/tests/client/PID.txt @@ -1 +1 @@ -157499 +284153 diff --git a/tests/server/package-lock.json b/tests/server/package-lock.json index b0504eaf..bbcb09e2 100644 --- a/tests/server/package-lock.json +++ b/tests/server/package-lock.json @@ -21,7 +21,7 @@ }, "../..": { "name": "prostgles-server", - "version": "4.1.63", + "version": "4.1.64", "license": "MIT", "dependencies": { "@types/express": "^4.17.13",