Adding test case and updating how prefix updates in filters work.

This commit is contained in:
mike12345567 2024-06-28 14:19:11 +01:00
parent 2d31e327e7
commit 510baf4f6e
3 changed files with 42 additions and 15 deletions

View File

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

View File

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

View File

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