diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index 7a0e78eb1c..87d0aa72c7 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -262,11 +262,19 @@ describe.each([ { name: "serverDate", appointment: serverTime.toISOString() }, { name: "single user, session user", - single_user: JSON.stringify([currentUser]), + single_user: JSON.stringify(currentUser), }, { name: "single user", - single_user: JSON.stringify([globalUsers[0]]), + single_user: JSON.stringify(globalUsers[0]), + }, + { + name: "deprecated single user, session user", + deprecated_single_user: JSON.stringify([currentUser]), + }, + { + name: "deprecated single user", + deprecated_single_user: JSON.stringify([globalUsers[0]]), }, { name: "multi user", @@ -276,6 +284,14 @@ describe.each([ name: "multi user with session user", multi_user: JSON.stringify([...globalUsers, currentUser]), }, + { + name: "deprecated multi user", + deprecated_multi_user: JSON.stringify(globalUsers), + }, + { + name: "deprecated multi user with session user", + deprecated_multi_user: JSON.stringify([...globalUsers, currentUser]), + }, ] } @@ -301,13 +317,29 @@ describe.each([ appointment: { name: "appointment", type: FieldType.DATETIME }, single_user: { name: "single_user", + type: FieldType.BB_REFERENCE_SINGLE, + subtype: BBReferenceFieldSubType.USER, + }, + deprecated_single_user: { + name: "deprecated_single_user", type: FieldType.BB_REFERENCE, subtype: BBReferenceFieldSubType.USER, }, multi_user: { name: "multi_user", type: FieldType.BB_REFERENCE, + subtype: BBReferenceFieldSubType.USER, + constraints: { + type: "array", + }, + }, + deprecated_multi_user: { + name: "deprecated_multi_user", + type: FieldType.BB_REFERENCE, subtype: BBReferenceFieldSubType.USERS, + constraints: { + type: "array", + }, }, }) await createRows(rows(config.getUser())) @@ -398,7 +430,18 @@ describe.each([ }).toContainExactly([ { name: "single user, session user", - single_user: [{ _id: config.getUser()._id }], + single_user: { _id: config.getUser()._id }, + }, + ]) + }) + + it("should match a deprecated single user row by the session user id", async () => { + await expectQuery({ + equal: { deprecated_single_user: "{{ [user]._id }}" }, + }).toContainExactly([ + { + name: "deprecated single user, session user", + deprecated_single_user: [{ _id: config.getUser()._id }], }, ]) }) @@ -420,6 +463,23 @@ describe.each([ ]) }) + // TODO(samwho): fix for SQS + !isSqs && + it("should match the session user id in a deprecated multi user field", async () => { + const allUsers = [...globalUsers, config.getUser()].map((user: any) => { + return { _id: user._id } + }) + + await expectQuery({ + contains: { deprecated_multi_user: ["{{ [user]._id }}"] }, + }).toContainExactly([ + { + name: "deprecated multi user with session user", + deprecated_multi_user: allUsers, + }, + ]) + }) + // TODO(samwho): fix for SQS !isSqs && it("should not match the session user id in a multi user field", async () => { @@ -436,6 +496,22 @@ describe.each([ ]) }) + // TODO(samwho): fix for SQS + !isSqs && + it("should not match the session user id in a deprecated multi user field", async () => { + await expectQuery({ + notContains: { deprecated_multi_user: ["{{ [user]._id }}"] }, + notEmpty: { deprecated_multi_user: true }, + }).toContainExactly([ + { + name: "deprecated multi user", + deprecated_multi_user: globalUsers.map((user: any) => { + return { _id: user._id } + }), + }, + ]) + }) + it("should match the session user id and a user table row id using helpers, user binding and a static user id.", async () => { await expectQuery({ oneOf: { @@ -447,11 +523,31 @@ describe.each([ }).toContainExactly([ { name: "single user, session user", - single_user: [{ _id: config.getUser()._id }], + single_user: { _id: config.getUser()._id }, }, { name: "single user", - single_user: [{ _id: globalUsers[0]._id }], + single_user: { _id: globalUsers[0]._id }, + }, + ]) + }) + + it("should match the session user id and a user table row id using helpers, user binding and a static user id. (deprecated single user)", async () => { + await expectQuery({ + oneOf: { + deprecated_single_user: [ + "{{ default [user]._id '_empty_' }}", + globalUsers[0]._id, + ], + }, + }).toContainExactly([ + { + name: "deprecated single user, session user", + deprecated_single_user: [{ _id: config.getUser()._id }], + }, + { + name: "deprecated single user", + deprecated_single_user: [{ _id: globalUsers[0]._id }], }, ]) }) @@ -467,7 +563,23 @@ describe.each([ }).toContainExactly([ { name: "single user", - single_user: [{ _id: globalUsers[0]._id }], + single_user: { _id: globalUsers[0]._id }, + }, + ]) + }) + + it("should resolve 'default' helper to '_empty_' when binding resolves to nothing (deprecated single user)", async () => { + await expectQuery({ + oneOf: { + deprecated_single_user: [ + "{{ default [user]._idx '_empty_' }}", + globalUsers[0]._id, + ], + }, + }).toContainExactly([ + { + name: "deprecated single user", + deprecated_single_user: [{ _id: globalUsers[0]._id }], }, ]) }) 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/integrations/base/sqlTable.ts b/packages/server/src/integrations/base/sqlTable.ts index 2af3fb096e..a82a9fcea8 100644 --- a/packages/server/src/integrations/base/sqlTable.ts +++ b/packages/server/src/integrations/base/sqlTable.ts @@ -11,7 +11,7 @@ import { TableSourceType, } from "@budibase/types" import { breakExternalTableId, getNativeSql, SqlClient } from "../utils" -import { utils } from "@budibase/shared-core" +import { helpers, utils } from "@budibase/shared-core" import SchemaBuilder = Knex.SchemaBuilder import CreateTableBuilder = Knex.CreateTableBuilder @@ -85,7 +85,12 @@ function generateSchema( break case FieldType.ARRAY: case FieldType.BB_REFERENCE: - schema.json(key) + if (helpers.schema.isDeprecatedSingleUserColumn(column)) { + // This is still required for unit testing, in order to create "deprecated" schemas + schema.text(key) + } else { + schema.json(key) + } break case FieldType.LINK: // this side of the relationship doesn't need any SQL work diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index e347a8657d..2d194ab196 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -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..62f5af2b70 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,23 +70,34 @@ function userColumnMapping(column: string, options: RowSearchParams) { return input } } + + let wrapper = (s: string) => s + if (isDeprecatedSingleUserColumn && filterValue && isSql) { + // Decreated single users are stored as stringified arrays of a single value + wrapper = (s: string) => JSON.stringify([s]) + } + if (isArray) { return filterValue.map(el => { if (typeof el === "string") { - return processString(el) + return wrapper(processString(el)) } else { return el } }) } else { - return processString(filterValue) + return wrapper(processString(filterValue)) } }) } // 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 +116,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 } } diff --git a/packages/server/src/utilities/schema.ts b/packages/server/src/utilities/schema.ts index 2dff1acb5c..4f0feb3c93 100644 --- a/packages/server/src/utilities/schema.ts +++ b/packages/server/src/utilities/schema.ts @@ -139,8 +139,10 @@ export function parse(rows: Rows, schema: TableSchema): Rows { ? new Date(columnData).toISOString() : columnData } else if (columnType === FieldType.BB_REFERENCE) { - const parsedValues = - (!!columnData && parseCsvExport<{ _id: string }[]>(columnData)) || [] + let parsedValues: { _id: string }[] = columnData || [] + if (columnData) { + parsedValues = parseCsvExport<{ _id: string }[]>(columnData) + } parsedRow[columnName] = parsedValues?.map(u => u._id) } else if (columnType === FieldType.BB_REFERENCE_SINGLE) { diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 2438e7f840..1401b75e93 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -9,10 +9,11 @@ import { SearchFilterOperator, SortDirection, SortType, + FieldConstraints, } from "@budibase/types" import dayjs from "dayjs" import { OperatorOptions, SqlNumberTypeRangeMap } from "./constants" -import { deepGet } from "./helpers" +import { deepGet, schema } from "./helpers" const HBS_REGEX = /{{([^{].*?)}}/g @@ -24,6 +25,7 @@ export const getValidOperatorsForType = ( type: FieldType subtype?: BBReferenceFieldSubType formulaType?: FormulaType + constraints?: FieldConstraints }, field?: string, datasource?: Datasource & { tableId: any } @@ -68,7 +70,10 @@ export const getValidOperatorsForType = ( ops = numOps } else if (type === FieldType.FORMULA && formulaType === FormulaType.STATIC) { ops = stringOps.concat([Op.MoreThan, Op.LessThan]) - } else if (type === FieldType.BB_REFERENCE_SINGLE) { + } else if ( + type === FieldType.BB_REFERENCE_SINGLE || + schema.isDeprecatedSingleUserColumn(fieldType) + ) { ops = [Op.Equals, Op.NotEquals, Op.Empty, Op.NotEmpty, Op.In] } else if (type === FieldType.BB_REFERENCE) { ops = [Op.Contains, Op.NotContains, Op.ContainsAny, Op.Empty, Op.NotEmpty] diff --git a/packages/shared-core/src/helpers/schema.ts b/packages/shared-core/src/helpers/schema.ts index 548a5c9766..985e068be0 100644 --- a/packages/shared-core/src/helpers/schema.ts +++ b/packages/shared-core/src/helpers/schema.ts @@ -4,7 +4,9 @@ import { FieldType, } from "@budibase/types" -export function isDeprecatedSingleUserColumn(schema: FieldSchema) { +export function isDeprecatedSingleUserColumn( + schema: Pick +) { const result = schema.type === FieldType.BB_REFERENCE && schema.subtype === BBReferenceFieldSubType.USER &&