From 48672244f4b4135566c0c063cb09afc6fe634619 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 15 Aug 2024 18:46:28 +0100 Subject: [PATCH] Fixing up column renaming everywhere that it is needed, making sure works for external as well. --- .../api/controllers/row/ExternalRequest.ts | 39 +++++++++-------- .../server/src/sdk/app/rows/search/filters.ts | 5 ++- .../src/sdk/app/rows/search/internal/sqs.ts | 43 +++++++++++-------- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index 5ee2d0fe2b..ac2d1e8c39 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -45,6 +45,7 @@ import { db as dbCore } from "@budibase/backend-core" import sdk from "../../../sdk" import env from "../../../environment" import { makeExternalQuery } from "../../../integrations/base/query" +import { dataFilters } from "@budibase/shared-core" export interface ManyRelationship { tableId?: string @@ -195,29 +196,33 @@ export class ExternalRequest { if (filters) { // need to map over the filters and make sure the _id field isn't present let prefix = 1 - for (const [operatorType, operator] of Object.entries(filters)) { - const isArrayOp = sdk.rows.utils.isArrayFilter(operatorType) - for (const field of Object.keys(operator || {})) { - if (dbCore.removeKeyNumbering(field) === "_id") { - if (primary) { - const parts = breakRowIdField(operator[field]) - if (primary.length > 1 && isArrayOp) { - operator[InternalSearchFilterOperator.COMPLEX_ID_OPERATOR] = { - id: primary, - values: parts[0], + const checkFilters = (innerFilters: SearchFilters): SearchFilters => { + for (const [operatorType, operator] of Object.entries(innerFilters)) { + const isArrayOp = sdk.rows.utils.isArrayFilter(operatorType) + for (const field of Object.keys(operator || {})) { + if (dbCore.removeKeyNumbering(field) === "_id") { + if (primary) { + const parts = breakRowIdField(operator[field]) + if (primary.length > 1 && isArrayOp) { + operator[InternalSearchFilterOperator.COMPLEX_ID_OPERATOR] = { + id: primary, + values: parts[0], + } + } else { + for (let field of primary) { + operator[`${prefix}:${field}`] = parts.shift() + } + prefix++ } - } else { - for (let field of primary) { - operator[`${prefix}:${field}`] = parts.shift() - } - prefix++ } + // make sure this field doesn't exist on any filter + delete operator[field] } - // make sure this field doesn't exist on any filter - delete operator[field] } } + return dataFilters.recurseLogicalOperators(innerFilters, checkFilters) } + checkFilters(filters) } // there is no id, just use the user provided filters if (!idCopy || !table) { diff --git a/packages/server/src/sdk/app/rows/search/filters.ts b/packages/server/src/sdk/app/rows/search/filters.ts index ccce0ab86a..4049fc5352 100644 --- a/packages/server/src/sdk/app/rows/search/filters.ts +++ b/packages/server/src/sdk/app/rows/search/filters.ts @@ -5,6 +5,7 @@ import { Table, } from "@budibase/types" import { isPlainObject } from "lodash" +import { dataFilters } from "@budibase/shared-core" export function getRelationshipColumns(table: Table): { name: string @@ -58,5 +59,7 @@ export function updateFilterKeys( } } } - return filters + return dataFilters.recurseLogicalOperators(filters, (f: SearchFilters) => { + return updateFilterKeys(f, updates) + }) } diff --git a/packages/server/src/sdk/app/rows/search/internal/sqs.ts b/packages/server/src/sdk/app/rows/search/internal/sqs.ts index 0c7946339e..4bfa8f8fa5 100644 --- a/packages/server/src/sdk/app/rows/search/internal/sqs.ts +++ b/packages/server/src/sdk/app/rows/search/internal/sqs.ts @@ -139,29 +139,34 @@ function cleanupFilters( allTables.some(table => table.schema[key]) const splitter = new dataFilters.ColumnSplitter(allTables) - for (const filterKey of Object.keys(filters) as (keyof SearchFilters)[]) { - if (!isLogicalSearchOperator(filterKey)) { - const filter = filters[filterKey]! - if (typeof filter !== "object") { - continue - } - for (const key of Object.keys(filter)) { - const { numberPrefix, relationshipPrefix, column } = splitter.run(key) - if (keyInAnyTable(column)) { - filter[ - `${numberPrefix || ""}${relationshipPrefix || ""}${mapToUserColumn( - column - )}` - ] = filter[key] - delete filter[key] + + const prefixFilters = (filters: SearchFilters) => { + for (const filterKey of Object.keys(filters) as (keyof SearchFilters)[]) { + if (isLogicalSearchOperator(filterKey)) { + for (const condition of filters[filterKey]!.conditions) { + prefixFilters(condition) + } + } else { + const filter = filters[filterKey]! + if (typeof filter !== "object") { + continue + } + for (const key of Object.keys(filter)) { + const { numberPrefix, relationshipPrefix, column } = splitter.run(key) + if (keyInAnyTable(column)) { + filter[ + `${numberPrefix || ""}${ + relationshipPrefix || "" + }${mapToUserColumn(column)}` + ] = filter[key] + delete filter[key] + } } } } } - - return dataFilters.recurseLogicalOperators(filters, (f: SearchFilters) => { - return cleanupFilters(f, table, allTables) - }) + prefixFilters(filters) + return filters } function buildTableMap(tables: Table[]) {