Fix query

This commit is contained in:
Adria Navarro 2024-05-13 10:50:59 +02:00
parent 68e2313e6c
commit 5c8a789047
3 changed files with 31 additions and 8 deletions

View File

@ -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))
)
}

View File

@ -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)

View File

@ -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
}
}