From 5c8a7890474293764b43cdeedb14f0e9c109ad92 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 13 May 2024 10:50:59 +0200 Subject: [PATCH] Fix query --- packages/server/src/integrations/base/sql.ts | 4 +-- packages/server/src/sdk/app/rows/search.ts | 6 ++-- .../server/src/sdk/app/rows/search/utils.ts | 29 ++++++++++++++++--- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index 7a2b819007..85db642e47 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -26,6 +26,7 @@ import { INTERNAL_TABLE_SOURCE_ID, } from "@budibase/types" import environment from "../../environment" +import { helpers } from "@budibase/shared-core" type QueryFunction = (query: SqlQuery | SqlQuery[], operation: Operation) => any @@ -786,8 +787,7 @@ class SqlQueryBuilder extends SqlTableQueryBuilder { return ( field.type === FieldType.JSON || (field.type === FieldType.BB_REFERENCE && - // Handling old single user type - field.constraints?.type === "array") + !helpers.schema.isDeprecatedSingleUserColumn(field)) ) } diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index e347a8657d..7c61f48e31 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -12,7 +12,7 @@ import { NoEmptyFilterStrings } from "../../../constants" import * as sqs from "./search/sqs" import env from "../../../environment" import { ExportRowsParams, ExportRowsResult } from "./search/types" -import { dataFilters } from "@budibase/shared-core" +import { dataFilters, helpers } from "@budibase/shared-core" import sdk from "../../index" import { searchInputMapping } from "./search/utils" @@ -79,7 +79,9 @@ export async function search( } const table = await sdk.tables.getTable(options.tableId) - options = searchInputMapping(table, options) + options = searchInputMapping(table, options, { + isSql: !!table.sql || !!env.SQS_SEARCH_ENABLE, + }) if (isExternalTable) { return external.search(options, table) diff --git a/packages/server/src/sdk/app/rows/search/utils.ts b/packages/server/src/sdk/app/rows/search/utils.ts index 75f5d4e0ae..ca9ab1959c 100644 --- a/packages/server/src/sdk/app/rows/search/utils.ts +++ b/packages/server/src/sdk/app/rows/search/utils.ts @@ -11,7 +11,7 @@ import { RowSearchParams, } from "@budibase/types" import { db as dbCore, context } from "@budibase/backend-core" -import { utils } from "@budibase/shared-core" +import { helpers, utils } from "@budibase/shared-core" export async function paginatedSearch( query: SearchFilters, @@ -49,13 +49,19 @@ function findColumnInQueries( } } -function userColumnMapping(column: string, options: RowSearchParams) { +function userColumnMapping( + column: string, + options: RowSearchParams, + isDeprecatedSingleUserColumn: boolean = false, + isSql: boolean = false +) { findColumnInQueries(column, options, (filterValue: any): any => { const isArray = Array.isArray(filterValue), isString = typeof filterValue === "string" if (!isString && !isArray) { return filterValue } + const processString = (input: string) => { const rowPrefix = DocumentType.ROW + SEPARATOR if (input.startsWith(rowPrefix)) { @@ -64,6 +70,12 @@ function userColumnMapping(column: string, options: RowSearchParams) { return input } } + + if (isDeprecatedSingleUserColumn && filterValue && isString && isSql) { + // Decreated single users are stored as stringified arrays of a single value + return JSON.stringify([processString(filterValue)]) + } + if (isArray) { return filterValue.map(el => { if (typeof el === "string") { @@ -80,7 +92,11 @@ function userColumnMapping(column: string, options: RowSearchParams) { // maps through the search parameters to check if any of the inputs are invalid // based on the table schema, converts them to something that is valid. -export function searchInputMapping(table: Table, options: RowSearchParams) { +export function searchInputMapping( + table: Table, + options: RowSearchParams, + datasourceOptions: { isSql?: boolean } +) { if (!table?.schema) { return options } @@ -99,7 +115,12 @@ export function searchInputMapping(table: Table, options: RowSearchParams) { break } case FieldType.BB_REFERENCE: { - userColumnMapping(key, options) + userColumnMapping( + key, + options, + helpers.schema.isDeprecatedSingleUserColumn(column), + datasourceOptions.isSql + ) break } }