diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 589f129f31..872759ac13 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -2108,4 +2108,26 @@ describe.each([ }).toNotHaveProperty(["totalRows"]) }) }) + + describe("special data_ case", () => { + beforeAll(async () => { + table = await createTable({ + name_data_test: { + name: "name_data_test", + type: FieldType.STRING, + }, + }) + await createRows([{ name_data_test: "a" }, { name_data_test: "b" }]) + }) + + it("should be able to query a column with data_ in it", async () => { + await expectSearch({ + query: { + equal: { + ["1:name_data_test"]: "a", + }, + }, + }).toContainExactly([{ name_data_test: "a" }]) + }) + }) }) diff --git a/packages/server/src/sdk/app/rows/search/sqs.ts b/packages/server/src/sdk/app/rows/search/sqs.ts index dbd4231664..644864efb0 100644 --- a/packages/server/src/sdk/app/rows/search/sqs.ts +++ b/packages/server/src/sdk/app/rows/search/sqs.ts @@ -36,6 +36,7 @@ import { getRelationshipColumns, getTableIDList, } from "./filters" +import { dataFilters } from "@budibase/shared-core" const builder = new sql.Sql(SqlClient.SQL_LITE) const NO_SUCH_COLUMN_REGEX = new RegExp(`no such colum.+${USER_COLUMN_PREFIX}`) @@ -101,19 +102,14 @@ function cleanupFilters( ) ) - // sort longest first, don't find substrings - const userColumnList = Object.keys(userColumnMap).sort( - (a, b) => b.length - a.length - ) // update the keys of filters to manage user columns - for (let filter of Object.values(filters)) { - for (let key of Object.keys(filter)) { - const found = userColumnList.find(possibleColumn => - key.endsWith(possibleColumn) - ) - if (found) { - const newKey = key.replace(found, userColumnMap[found]) - filter[newKey] = filter[key] + const keyInAnyTable = (key: string): boolean => + allTables.some(table => table.schema[key]) + for (const filter of Object.values(filters)) { + for (const key of Object.keys(filter)) { + const { prefix, key: rawKey } = dataFilters.getKeyNumbering(key) + if (keyInAnyTable(rawKey)) { + filter[`${prefix || ""}${mapToUserColumn(rawKey)}`] = filter[key] delete filter[key] } } diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 443dbbce8d..07c21f0481 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -131,13 +131,22 @@ const cleanupQuery = (query: SearchFilters) => { * Removes a numeric prefix on field names designed to give fields uniqueness */ export const removeKeyNumbering = (key: string): string => { + return getKeyNumbering(key).key +} + +/** + * Gets the part of the keys, returning the numeric prefix and the field name + */ +export const getKeyNumbering = ( + key: string +): { prefix?: string; key: string } => { if (typeof key === "string" && key.match(/\d[0-9]*:/g) != null) { const parts = key.split(":") // remove the number - parts.shift() - return parts.join(":") + const number = parts.shift() + return { prefix: `${number}:`, key: parts.join(":") } } else { - return key + return { key } } }