Skip to content

Commit

Permalink
Fix query
Browse files Browse the repository at this point in the history
  • Loading branch information
adrinr committed May 13, 2024
1 parent d154dd1 commit 01036dc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/server/src/integrations/base/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
)
}

Expand Down
6 changes: 4 additions & 2 deletions packages/server/src/sdk/app/rows/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Check failure on line 15 in packages/server/src/sdk/app/rows/search.ts

View workflow job for this annotation

GitHub Actions / lint

'helpers' is defined but never used. Allowed unused vars must match /^_/u
import sdk from "../../index"
import { searchInputMapping } from "./search/utils"

Expand Down Expand Up @@ -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)
Expand Down
29 changes: 25 additions & 4 deletions packages/server/src/sdk/app/rows/search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand All @@ -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") {
Expand All @@ -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
}
Expand All @@ -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
}
}
Expand Down

0 comments on commit 01036dc

Please sign in to comment.