From 31c0ed69f1ce23e8dbf9cc796bc95f1124124278 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 16 Oct 2024 18:28:40 +0100 Subject: [PATCH 01/20] wip --- .../src/api/routes/tests/viewV2.spec.ts | 3 +- packages/server/src/sdk/app/views/internal.ts | 15 +++ packages/shared-core/src/filters.ts | 33 +++--- packages/shared-core/src/utils.ts | 109 ++++++++---------- 4 files changed, 86 insertions(+), 74 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 7979fac5551..7c866e19fe3 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -144,7 +144,7 @@ describe.each([ }) }) - it("can persist views with all fields", async () => { + it.only("can persist views with all fields", async () => { const newView: Required> = { name: generator.name(), tableId: table._id!, @@ -178,6 +178,7 @@ describe.each([ visible: true, }, }, + queryUI: {}, id: expect.any(String), version: 2, }) diff --git a/packages/server/src/sdk/app/views/internal.ts b/packages/server/src/sdk/app/views/internal.ts index 96b41bffe8d..30e0c18d057 100644 --- a/packages/server/src/sdk/app/views/internal.ts +++ b/packages/server/src/sdk/app/views/internal.ts @@ -4,6 +4,19 @@ import { context, HTTPError } from "@budibase/backend-core" import sdk from "../../../sdk" import * as utils from "../../../db/utils" import { enrichSchema, isV2 } from "." +import { utils as sharedUtils, dataFilters } from "@budibase/shared-core" + +function ensureQueryUISet(view: ViewV2) { + if (!view.queryUI && view.query) { + view.queryUI = sharedUtils.processSearchFilters(view.query) + } +} + +function ensureQuerySet(view: ViewV2) { + if (!view.query && view.queryUI) { + view.query = dataFilters.buildQuery(view.queryUI) + } +} export async function get(viewId: string): Promise { const { tableId } = utils.extractViewInfoFromID(viewId) @@ -13,6 +26,7 @@ export async function get(viewId: string): Promise { if (!found) { throw new Error("No view found") } + ensureQueryUISet(found) return found } @@ -24,6 +38,7 @@ export async function getEnriched(viewId: string): Promise { if (!found) { throw new Error("No view found") } + ensureQueryUISet(found) return await enrichSchema(found, table.schema) } diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 40c70a8a231..64958dd4f7d 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -572,29 +572,34 @@ export const buildQueryLegacy = ( * * @returns {SearchFilters} */ - -export const buildQuery = ( +export function buildQuery(filter: undefined): undefined +export function buildQuery( + filter: SearchFilterGroup | LegacyFilter[] +): SearchFilters +export function buildQuery( filter?: SearchFilterGroup | LegacyFilter[] -): SearchFilters | undefined => { - const parsedFilter: SearchFilterGroup | undefined = - processSearchFilters(filter) - - if (!parsedFilter) { +): SearchFilters | undefined { + if (!filter) { return } - const operatorMap: { [key in FilterGroupLogicalOperator]: LogicalOperator } = - { - [FilterGroupLogicalOperator.ALL]: LogicalOperator.AND, - [FilterGroupLogicalOperator.ANY]: LogicalOperator.OR, - } + let parsedFilter: SearchFilterGroup + if (Array.isArray(filter)) { + parsedFilter = processSearchFilters(filter) + } else { + parsedFilter = filter + } + + const operatorMap = { + [FilterGroupLogicalOperator.ALL]: LogicalOperator.AND, + [FilterGroupLogicalOperator.ANY]: LogicalOperator.OR, + } const globalOnEmpty = parsedFilter.onEmptyFilter ? parsedFilter.onEmptyFilter : null - const globalOperator: LogicalOperator = - operatorMap[parsedFilter.logicalOperator as FilterGroupLogicalOperator] + const globalOperator = operatorMap[parsedFilter.logicalOperator] return { ...(globalOnEmpty ? { onEmptyFilter: globalOnEmpty } : {}), diff --git a/packages/shared-core/src/utils.ts b/packages/shared-core/src/utils.ts index bead5961f0e..7f19371d794 100644 --- a/packages/shared-core/src/utils.ts +++ b/packages/shared-core/src/utils.ts @@ -137,12 +137,8 @@ export function isSupportedUserSearch(query: SearchFilters) { * @param {LegacyFilter[] | SearchFilterGroup} filters */ export const processSearchFilters = ( - filters: LegacyFilter[] | SearchFilterGroup | undefined -): SearchFilterGroup | undefined => { - if (!filters) { - return - } - + filters: LegacyFilter[] +): SearchFilterGroup => { // Base search config. const defaultCfg: SearchFilterGroup = { logicalOperator: FilterGroupLogicalOperator.ALL, @@ -160,65 +156,60 @@ export const processSearchFilters = ( "formulaType", ] - if (Array.isArray(filters)) { - let baseGroup: SearchFilterGroup = { - filters: [], - logicalOperator: FilterGroupLogicalOperator.ALL, - } - - return filters.reduce((acc: SearchFilterGroup, filter: LegacyFilter) => { - // Sort the properties for easier debugging - const filterPropertyKeys = (Object.keys(filter) as (keyof LegacyFilter)[]) - .sort((a, b) => { - return a.localeCompare(b) - }) - .filter(key => filter[key]) - - if (filterPropertyKeys.length == 1) { - const key = filterPropertyKeys[0], - value = filter[key] - // Global - if (key === "onEmptyFilter") { - // unset otherwise - acc.onEmptyFilter = value - } else if (key === "operator" && value === "allOr") { - // Group 1 logical operator - baseGroup.logicalOperator = FilterGroupLogicalOperator.ANY - } + let baseGroup: SearchFilterGroup = { + filters: [], + logicalOperator: FilterGroupLogicalOperator.ALL, + } - return acc + return filters.reduce((acc: SearchFilterGroup, filter: LegacyFilter) => { + // Sort the properties for easier debugging + const filterPropertyKeys = (Object.keys(filter) as (keyof LegacyFilter)[]) + .sort((a, b) => { + return a.localeCompare(b) + }) + .filter(key => filter[key]) + + if (filterPropertyKeys.length == 1) { + const key = filterPropertyKeys[0], + value = filter[key] + // Global + if (key === "onEmptyFilter") { + // unset otherwise + acc.onEmptyFilter = value + } else if (key === "operator" && value === "allOr") { + // Group 1 logical operator + baseGroup.logicalOperator = FilterGroupLogicalOperator.ANY } - const allowedFilterSettings: AllowedFilters = filterPropertyKeys.reduce( - (acc: AllowedFilters, key) => { - const value = filter[key] - if (filterAllowedKeys.includes(key)) { - if (key === "field") { - acc.push([key, removeKeyNumbering(value)]) - } else { - acc.push([key, value]) - } + return acc + } + + const allowedFilterSettings: AllowedFilters = filterPropertyKeys.reduce( + (acc: AllowedFilters, key) => { + const value = filter[key] + if (filterAllowedKeys.includes(key)) { + if (key === "field") { + acc.push([key, removeKeyNumbering(value)]) + } else { + acc.push([key, value]) } - return acc - }, - [] - ) + } + return acc + }, + [] + ) - const migratedFilter: LegacyFilter = Object.fromEntries( - allowedFilterSettings - ) as LegacyFilter + const migratedFilter: LegacyFilter = Object.fromEntries( + allowedFilterSettings + ) as LegacyFilter - baseGroup.filters!.push(migratedFilter) + baseGroup.filters!.push(migratedFilter) - if (!acc.groups || !acc.groups.length) { - // init the base group - acc.groups = [baseGroup] - } + if (!acc.groups || !acc.groups.length) { + // init the base group + acc.groups = [baseGroup] + } - return acc - }, defaultCfg) - } else if (!filters?.groups) { - return - } - return filters + return acc + }, defaultCfg) } From cb41861d13342d6ffcda69d2d4058dcf08857084 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 17 Oct 2024 11:54:34 +0100 Subject: [PATCH 02/20] Making progress toward testing buildCondition and friends. --- .../src/api/routes/tests/viewV2.spec.ts | 52 +++++++-- packages/server/src/sdk/app/views/external.ts | 7 ++ packages/server/src/sdk/app/views/internal.ts | 18 +--- packages/server/src/sdk/app/views/utils.ts | 30 ++++++ packages/shared-core/src/filters.ts | 101 +++++++++--------- packages/shared-core/src/utils.ts | 6 +- packages/types/src/api/web/searchFilter.ts | 11 +- packages/types/src/sdk/search.ts | 14 ++- 8 files changed, 156 insertions(+), 83 deletions(-) create mode 100644 packages/server/src/sdk/app/views/utils.ts diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 7c866e19fe3..b81c7314859 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -27,6 +27,8 @@ import { ViewV2Schema, ViewV2Type, JsonTypes, + FilterGroupLogicalOperator, + EmptyFilterOption, } from "@budibase/types" import { generator, mocks } from "@budibase/backend-core/tests" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" @@ -145,17 +147,26 @@ describe.each([ }) it.only("can persist views with all fields", async () => { - const newView: Required> = { + const newView: Required> = { name: generator.name(), tableId: table._id!, primaryDisplay: "id", - query: [ - { - operator: BasicOperator.EQUAL, - field: "field", - value: "value", - }, - ], + queryUI: { + logicalOperator: FilterGroupLogicalOperator.ALL, + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + groups: [ + { + logicalOperator: FilterGroupLogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "field", + value: "value", + }, + ], + }, + ], + }, sort: { field: "fieldToSort", order: SortOrder.DESCENDING, @@ -170,7 +181,7 @@ describe.each([ } const res = await config.api.viewV2.create(newView) - expect(res).toEqual({ + const expected: ViewV2 = { ...newView, schema: { id: { visible: true }, @@ -178,10 +189,29 @@ describe.each([ visible: true, }, }, - queryUI: {}, + query: { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + $and: { + conditions: [ + { + $and: { + conditions: [ + { + equal: { + field: "value", + }, + }, + ], + }, + }, + ], + }, + }, id: expect.any(String), version: 2, - }) + } + + expect(res).toEqual(expected) }) it("persist only UI schema overrides", async () => { diff --git a/packages/server/src/sdk/app/views/external.ts b/packages/server/src/sdk/app/views/external.ts index b69ac0b9eb4..d5251122c9c 100644 --- a/packages/server/src/sdk/app/views/external.ts +++ b/packages/server/src/sdk/app/views/external.ts @@ -5,6 +5,7 @@ import sdk from "../../../sdk" import * as utils from "../../../db/utils" import { enrichSchema, isV2 } from "." import { breakExternalTableId } from "../../../integrations/utils" +import { ensureQuerySet, ensureQueryUISet } from "./utils" export async function get(viewId: string): Promise { const { tableId } = utils.extractViewInfoFromID(viewId) @@ -18,6 +19,7 @@ export async function get(viewId: string): Promise { if (!found) { throw new Error("No view found") } + ensureQueryUISet(found) return found } @@ -33,6 +35,7 @@ export async function getEnriched(viewId: string): Promise { if (!found) { throw new Error("No view found") } + ensureQueryUISet(found) return await enrichSchema(found, table.schema) } @@ -46,6 +49,8 @@ export async function create( version: 2, } + ensureQuerySet(view) + const db = context.getAppDB() const { datasourceId, tableName } = breakExternalTableId(tableId) @@ -74,6 +79,8 @@ export async function update(tableId: string, view: ViewV2): Promise { throw new HTTPError(`Cannot update view type after creation`, 400) } + ensureQuerySet(view) + delete views[existingView.name] views[view.name] = view await db.put(ds) diff --git a/packages/server/src/sdk/app/views/internal.ts b/packages/server/src/sdk/app/views/internal.ts index 30e0c18d057..33b68759d72 100644 --- a/packages/server/src/sdk/app/views/internal.ts +++ b/packages/server/src/sdk/app/views/internal.ts @@ -4,19 +4,7 @@ import { context, HTTPError } from "@budibase/backend-core" import sdk from "../../../sdk" import * as utils from "../../../db/utils" import { enrichSchema, isV2 } from "." -import { utils as sharedUtils, dataFilters } from "@budibase/shared-core" - -function ensureQueryUISet(view: ViewV2) { - if (!view.queryUI && view.query) { - view.queryUI = sharedUtils.processSearchFilters(view.query) - } -} - -function ensureQuerySet(view: ViewV2) { - if (!view.query && view.queryUI) { - view.query = dataFilters.buildQuery(view.queryUI) - } -} +import { ensureQuerySet, ensureQueryUISet } from "./utils" export async function get(viewId: string): Promise { const { tableId } = utils.extractViewInfoFromID(viewId) @@ -52,6 +40,8 @@ export async function create( version: 2, } + ensureQuerySet(view) + const db = context.getAppDB() const table = await sdk.tables.getTable(tableId) @@ -78,6 +68,8 @@ export async function update(tableId: string, view: ViewV2): Promise { throw new HTTPError(`Cannot update view type after creation`, 400) } + ensureQuerySet(view) + delete table.views[existingView.name] table.views[view.name] = view await db.put(table) diff --git a/packages/server/src/sdk/app/views/utils.ts b/packages/server/src/sdk/app/views/utils.ts new file mode 100644 index 00000000000..45b091e0463 --- /dev/null +++ b/packages/server/src/sdk/app/views/utils.ts @@ -0,0 +1,30 @@ +import { ViewV2 } from "@budibase/types" +import { utils, dataFilters } from "@budibase/shared-core" + +export function ensureQueryUISet(view: ViewV2) { + if (!view.queryUI && view.query) { + if (!Array.isArray(view.query)) { + // In practice this should not happen. `view.query`, at the time this code + // goes into the codebase, only contains LegacyFilter[] in production. + // We're changing it in the change that this comment is part of to also + // include SearchFilters objects. These are created when we receive an + // update to a ViewV2 that contains a queryUI and not a query field. We + // can convert SearchFilterGroup (the type of queryUI) to SearchFilters, + // but not LegacyFilter[], they are incompatible due to SearchFilterGroup + // and SearchFilters being recursive types. + // + // So despite the type saying that `view.query` is a LegacyFilter[] | + // SearchFilters, it will never be a SearchFilters when a `view.queryUI` + // is specified, making it "safe" to throw an error here. + throw new Error("view is missing queryUI field") + } + + view.queryUI = utils.processSearchFilters(view.query) + } +} + +export function ensureQuerySet(view: ViewV2) { + if (!view.query && view.queryUI) { + view.query = dataFilters.buildQuery(view.queryUI) + } +} diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 64958dd4f7d..3b9253af6e6 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -21,6 +21,9 @@ import { isLogicalSearchOperator, SearchFilterGroup, FilterGroupLogicalOperator, + isBasicSearchOperator, + isArraySearchOperator, + isRangeSearchOperator, } from "@budibase/types" import dayjs from "dayjs" import { OperatorOptions, SqlNumberTypeRangeMap } from "./constants" @@ -307,36 +310,23 @@ export class ColumnSplitter { * Builds a JSON query from the filter a SearchFilter definition * @param filter the builder filter structure */ - const buildCondition = (expression: LegacyFilter) => { - // Filter body - let query: SearchFilters = { - string: {}, - fuzzy: {}, - range: {}, - equal: {}, - notEqual: {}, - empty: {}, - notEmpty: {}, - contains: {}, - notContains: {}, - oneOf: {}, - containsAny: {}, - } - let { operator, field, type, value, externalType, onEmptyFilter } = expression + const query: SearchFilters = {} + const { operator, field, type, externalType, onEmptyFilter } = expression + let { value } = expression if (!operator || !field) { return } - const queryOperator = operator as SearchFilterOperator const isHbs = typeof value === "string" && (value.match(HBS_REGEX) || []).length > 0 - // Parse all values into correct types + if (operator === "allOr") { query.allOr = true return } + if (onEmptyFilter) { query.onEmptyFilter = onEmptyFilter return @@ -344,15 +334,15 @@ const buildCondition = (expression: LegacyFilter) => { // Default the value for noValue fields to ensure they are correctly added // to the final query - if (queryOperator === "empty" || queryOperator === "notEmpty") { + if (operator === "empty" || operator === "notEmpty") { value = null } if ( type === "datetime" && !isHbs && - queryOperator !== "empty" && - queryOperator !== "notEmpty" + operator !== "empty" && + operator !== "notEmpty" ) { // Ensure date value is a valid date and parse into correct format if (!value) { @@ -365,7 +355,7 @@ const buildCondition = (expression: LegacyFilter) => { } } if (type === "number" && typeof value === "string" && !isHbs) { - if (queryOperator === "oneOf") { + if (operator === "oneOf") { value = value.split(",").map(item => parseFloat(item)) } else { value = parseFloat(value) @@ -383,50 +373,55 @@ const buildCondition = (expression: LegacyFilter) => { ) { value = value.split(",") } - if (operator.toLocaleString().startsWith("range") && query.range) { - const minint = - SqlNumberTypeRangeMap[externalType as keyof typeof SqlNumberTypeRangeMap] - ?.min || Number.MIN_SAFE_INTEGER - const maxint = - SqlNumberTypeRangeMap[externalType as keyof typeof SqlNumberTypeRangeMap] - ?.max || Number.MAX_SAFE_INTEGER - if (!query.range[field]) { - query.range[field] = { - low: type === "number" ? minint : "0000-00-00T00:00:00.000Z", - high: type === "number" ? maxint : "9999-00-00T00:00:00.000Z", - } + + if (isRangeSearchOperator(operator)) { + const key = externalType as keyof typeof SqlNumberTypeRangeMap + const limits = SqlNumberTypeRangeMap[key] || { + min: Number.MIN_SAFE_INTEGER, + max: Number.MAX_SAFE_INTEGER, } - if (operator === "rangeLow" && value != null && value !== "") { - query.range[field] = { - ...query.range[field], - low: value, - } - } else if (operator === "rangeHigh" && value != null && value !== "") { - query.range[field] = { - ...query.range[field], - high: value, - } + + query[operator] ??= {} + query[operator][field] = { + low: type === "number" ? limits.min : "0000-00-00T00:00:00.000Z", + high: type === "number" ? limits.max : "9999-00-00T00:00:00.000Z", } - } else if (isLogicalSearchOperator(queryOperator)) { + } else if (operator === "rangeHigh" && value != null && value !== "") { + query.range ??= {} + query.range[field] = { + ...query.range[field], + high: value, + } + } else if (operator === "rangeLow" && value != null && value !== "") { + query.range ??= {} + query.range[field] = { + ...query.range[field], + low: value, + } + } else if (isLogicalSearchOperator(operator)) { // TODO - } else if (query[queryOperator] && operator !== "onEmptyFilter") { + } else if ( + isBasicSearchOperator(operator) || + isArraySearchOperator(operator) || + isRangeSearchOperator(operator) + ) { if (type === "boolean") { // Transform boolean filters to cope with null. // "equals false" needs to be "not equals true" // "not equals false" needs to be "equals true" - if (queryOperator === "equal" && value === false) { + if (operator === "equal" && value === false) { query.notEqual = query.notEqual || {} query.notEqual[field] = true - } else if (queryOperator === "notEqual" && value === false) { + } else if (operator === "notEqual" && value === false) { query.equal = query.equal || {} query.equal[field] = true } else { - query[queryOperator] ??= {} - query[queryOperator]![field] = value + query[operator] ??= {} + query[operator][field] = value } } else { - query[queryOperator] ??= {} - query[queryOperator]![field] = value + query[operator] ??= {} + query[operator][field] = value } } @@ -604,7 +599,7 @@ export function buildQuery( return { ...(globalOnEmpty ? { onEmptyFilter: globalOnEmpty } : {}), [globalOperator]: { - conditions: parsedFilter.groups?.map((group: SearchFilterGroup) => { + conditions: parsedFilter.groups?.map(group => { return { [operatorMap[group.logicalOperator]]: { conditions: group.filters diff --git a/packages/shared-core/src/utils.ts b/packages/shared-core/src/utils.ts index 7f19371d794..98f7c9b2154 100644 --- a/packages/shared-core/src/utils.ts +++ b/packages/shared-core/src/utils.ts @@ -6,6 +6,8 @@ import { BasicOperator, ArrayOperator, isLogicalSearchOperator, + EmptyFilterOption, + SearchFilterChild, } from "@budibase/types" import * as Constants from "./constants" import { removeKeyNumbering } from "./filters" @@ -142,6 +144,7 @@ export const processSearchFilters = ( // Base search config. const defaultCfg: SearchFilterGroup = { logicalOperator: FilterGroupLogicalOperator.ALL, + onEmptyFilter: EmptyFilterOption.RETURN_ALL, groups: [], } @@ -156,8 +159,7 @@ export const processSearchFilters = ( "formulaType", ] - let baseGroup: SearchFilterGroup = { - filters: [], + let baseGroup: SearchFilterChild = { logicalOperator: FilterGroupLogicalOperator.ALL, } diff --git a/packages/types/src/api/web/searchFilter.ts b/packages/types/src/api/web/searchFilter.ts index 23c599027e5..305f2bab007 100644 --- a/packages/types/src/api/web/searchFilter.ts +++ b/packages/types/src/api/web/searchFilter.ts @@ -14,10 +14,15 @@ export type LegacyFilter = { externalType?: string } +export type SearchFilterChild = { + logicalOperator: FilterGroupLogicalOperator + groups?: SearchFilterChild[] + filters?: LegacyFilter[] +} + // this is a type purely used by the UI export type SearchFilterGroup = { logicalOperator: FilterGroupLogicalOperator - onEmptyFilter?: EmptyFilterOption - groups?: SearchFilterGroup[] - filters?: LegacyFilter[] + onEmptyFilter: EmptyFilterOption + groups: SearchFilterChild[] } diff --git a/packages/types/src/sdk/search.ts b/packages/types/src/sdk/search.ts index d41bb0fb99a..d64e87d4347 100644 --- a/packages/types/src/sdk/search.ts +++ b/packages/types/src/sdk/search.ts @@ -32,7 +32,19 @@ export enum LogicalOperator { export function isLogicalSearchOperator( value: string ): value is LogicalOperator { - return value === LogicalOperator.AND || value === LogicalOperator.OR + return Object.values(LogicalOperator).includes(value as LogicalOperator) +} + +export function isBasicSearchOperator(value: string): value is BasicOperator { + return Object.values(BasicOperator).includes(value as BasicOperator) +} + +export function isArraySearchOperator(value: string): value is ArrayOperator { + return Object.values(ArrayOperator).includes(value as ArrayOperator) +} + +export function isRangeSearchOperator(value: string): value is RangeOperator { + return Object.values(RangeOperator).includes(value as RangeOperator) } export type SearchFilterOperator = From 17e946845e49d9823ad5903e904085bb92b6d60a Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 17 Oct 2024 17:16:54 +0100 Subject: [PATCH 03/20] Get existing tests passing. --- .../src/api/routes/tests/viewV2.spec.ts | 2150 +++++++++-------- packages/server/src/sdk/app/views/utils.ts | 9 +- .../server/src/tests/utilities/api/viewV2.ts | 4 +- packages/shared-core/src/filters.ts | 88 +- 4 files changed, 1149 insertions(+), 1102 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index b81c7314859..71506da0fa9 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -146,7 +146,7 @@ describe.each([ }) }) - it.only("can persist views with all fields", async () => { + it("can persist views with all fields", async () => { const newView: Required> = { name: generator.name(), tableId: table._id!, @@ -2332,548 +2332,391 @@ describe.each([ }) }) - describe("search", () => { - it("returns empty rows from view when no schema is passed", async () => { - const rows = await Promise.all( - Array.from({ length: 10 }, () => config.api.row.save(table._id!, {})) - ) - const response = await config.api.viewV2.search(view.id) - expect(response.rows).toHaveLength(10) - expect(response).toEqual({ - rows: expect.arrayContaining( - rows.map(r => ({ - _viewId: view.id, - tableId: table._id, - id: r.id, - _id: r._id, - _rev: r._rev, - ...(isInternal - ? { - type: "row", - updatedAt: expect.any(String), - createdAt: expect.any(String), - } - : {}), - })) - ), - ...(isInternal - ? {} - : { - hasNextPage: false, - }), + !isLucene && + describe("search", () => { + it("returns empty rows from view when no schema is passed", async () => { + const rows = await Promise.all( + Array.from({ length: 10 }, () => + config.api.row.save(table._id!, {}) + ) + ) + const response = await config.api.viewV2.search(view.id) + expect(response.rows).toHaveLength(10) + expect(response).toEqual({ + rows: expect.arrayContaining( + rows.map(r => ({ + _viewId: view.id, + tableId: table._id, + id: r.id, + _id: r._id, + _rev: r._rev, + ...(isInternal + ? { + type: "row", + updatedAt: expect.any(String), + createdAt: expect.any(String), + } + : {}), + })) + ), + ...(isInternal + ? {} + : { + hasNextPage: false, + }), + }) }) - }) - it("searching respects the view filters", async () => { - await config.api.row.save(table._id!, { - one: "foo", - two: "bar", - }) - const two = await config.api.row.save(table._id!, { - one: "foo2", - two: "bar2", - }) + it("searching respects the view filters", async () => { + await config.api.row.save(table._id!, { + one: "foo", + two: "bar", + }) + const two = await config.api.row.save(table._id!, { + one: "foo2", + two: "bar2", + }) - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - query: [ - { - operator: BasicOperator.EQUAL, - field: "two", - value: "bar2", + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + queryUI: { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + logicalOperator: FilterGroupLogicalOperator.ALL, + groups: [ + { + logicalOperator: FilterGroupLogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "two", + value: "bar2", + }, + ], + }, + ], }, - ], - schema: { - id: { visible: true }, - one: { visible: false }, - two: { visible: true }, - }, - }) - - const response = await config.api.viewV2.search(view.id) - expect(response.rows).toHaveLength(1) - expect(response).toEqual({ - rows: expect.arrayContaining([ - { - _viewId: view.id, - tableId: table._id, - id: two.id, - two: two.two, - _id: two._id, - _rev: two._rev, - ...(isInternal - ? { - type: "row", - createdAt: expect.any(String), - updatedAt: expect.any(String), - } - : {}), + schema: { + id: { visible: true }, + one: { visible: false }, + two: { visible: true }, }, - ]), - ...(isInternal - ? {} - : { - hasNextPage: false, - }), - }) - }) - - it("views filters are respected even if the column is hidden", async () => { - await config.api.row.save(table._id!, { - one: "foo", - two: "bar", - }) - const two = await config.api.row.save(table._id!, { - one: "foo2", - two: "bar2", - }) + }) - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - query: [ - { - operator: BasicOperator.EQUAL, - field: "two", - value: "bar2", - }, - ], - schema: { - id: { visible: true }, - one: { visible: false }, - two: { visible: false }, - }, + const response = await config.api.viewV2.search(view.id) + expect(response.rows).toHaveLength(1) + expect(response).toEqual({ + rows: expect.arrayContaining([ + { + _viewId: view.id, + tableId: table._id, + id: two.id, + two: two.two, + _id: two._id, + _rev: two._rev, + ...(isInternal + ? { + type: "row", + createdAt: expect.any(String), + updatedAt: expect.any(String), + } + : {}), + }, + ]), + ...(isInternal + ? {} + : { + hasNextPage: false, + }), + }) }) - const response = await config.api.viewV2.search(view.id) - expect(response.rows).toHaveLength(1) - expect(response.rows).toEqual([ - expect.objectContaining({ _id: two._id }), - ]) - }) + it("views filters are respected even if the column is hidden", async () => { + await config.api.row.save(table._id!, { + one: "foo", + two: "bar", + }) + const two = await config.api.row.save(table._id!, { + one: "foo2", + two: "bar2", + }) - it("views without data can be returned", async () => { - const response = await config.api.viewV2.search(view.id) - expect(response.rows).toHaveLength(0) - }) + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + queryUI: { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + logicalOperator: FilterGroupLogicalOperator.ALL, + groups: [ + { + logicalOperator: FilterGroupLogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "two", + value: "bar2", + }, + ], + }, + ], + }, + schema: { + id: { visible: true }, + one: { visible: false }, + two: { visible: false }, + }, + }) - it("respects the limit parameter", async () => { - await Promise.all( - Array.from({ length: 10 }, () => config.api.row.save(table._id!, {})) - ) - const limit = generator.integer({ min: 1, max: 8 }) - const response = await config.api.viewV2.search(view.id, { - limit, - query: {}, + const response = await config.api.viewV2.search(view.id) + expect(response.rows).toHaveLength(1) + expect(response.rows).toEqual([ + expect.objectContaining({ _id: two._id }), + ]) }) - expect(response.rows).toHaveLength(limit) - }) - it("can handle pagination", async () => { - await Promise.all( - Array.from({ length: 10 }, () => config.api.row.save(table._id!, {})) - ) - const rows = (await config.api.viewV2.search(view.id)).rows - - const page1 = await config.api.viewV2.search(view.id, { - paginate: true, - limit: 4, - query: {}, - countRows: true, - }) - expect(page1).toEqual({ - rows: expect.arrayContaining(rows.slice(0, 4)), - hasNextPage: true, - bookmark: expect.anything(), - totalRows: 10, - }) - - const page2 = await config.api.viewV2.search(view.id, { - paginate: true, - limit: 4, - bookmark: page1.bookmark, - query: {}, - countRows: true, - }) - expect(page2).toEqual({ - rows: expect.arrayContaining(rows.slice(4, 8)), - hasNextPage: true, - bookmark: expect.anything(), - totalRows: 10, - }) - - const page3 = await config.api.viewV2.search(view.id, { - paginate: true, - limit: 4, - bookmark: page2.bookmark, - query: {}, - countRows: true, - }) - const expectation: SearchResponse = { - rows: expect.arrayContaining(rows.slice(8)), - hasNextPage: false, - totalRows: 10, - } - if (isLucene) { - expectation.bookmark = expect.anything() - } - expect(page3).toEqual(expectation) - }) - - const sortTestOptions: [ - { - field: string - order?: SortOrder - type?: SortType - }, - string[] - ][] = [ - [ - { - field: "name", - order: SortOrder.ASCENDING, - type: SortType.STRING, - }, - ["Alice", "Bob", "Charly", "Danny"], - ], - [ - { - field: "name", - }, - ["Alice", "Bob", "Charly", "Danny"], - ], - [ - { - field: "name", - order: SortOrder.DESCENDING, - }, - ["Danny", "Charly", "Bob", "Alice"], - ], - [ - { - field: "name", - order: SortOrder.DESCENDING, - type: SortType.STRING, - }, - ["Danny", "Charly", "Bob", "Alice"], - ], - [ - { - field: "age", - order: SortOrder.ASCENDING, - type: SortType.NUMBER, - }, - ["Danny", "Alice", "Charly", "Bob"], - ], - [ - { - field: "age", - order: SortOrder.ASCENDING, - }, - ["Danny", "Alice", "Charly", "Bob"], - ], - [ - { - field: "age", - order: SortOrder.DESCENDING, - }, - ["Bob", "Charly", "Alice", "Danny"], - ], - [ - { - field: "age", - order: SortOrder.DESCENDING, - type: SortType.NUMBER, - }, - ["Bob", "Charly", "Alice", "Danny"], - ], - ] - - describe("sorting", () => { - let table: Table - const viewSchema = { - id: { visible: true }, - age: { visible: true }, - name: { visible: true }, - } - - beforeAll(async () => { - table = await config.api.table.save( - saveTableRequest({ - type: "table", - schema: { - name: { - type: FieldType.STRING, - name: "name", - }, - surname: { - type: FieldType.STRING, - name: "surname", - }, - age: { - type: FieldType.NUMBER, - name: "age", - }, - address: { - type: FieldType.STRING, - name: "address", - }, - jobTitle: { - type: FieldType.STRING, - name: "jobTitle", - }, - }, - }) - ) + it("views without data can be returned", async () => { + const response = await config.api.viewV2.search(view.id) + expect(response.rows).toHaveLength(0) + }) - const users = [ - { name: "Alice", age: 25 }, - { name: "Bob", age: 30 }, - { name: "Charly", age: 27 }, - { name: "Danny", age: 15 }, - ] + it("respects the limit parameter", async () => { await Promise.all( - users.map(u => - config.api.row.save(table._id!, { - tableId: table._id, - ...u, - }) + Array.from({ length: 10 }, () => + config.api.row.save(table._id!, {}) ) ) + const limit = generator.integer({ min: 1, max: 8 }) + const response = await config.api.viewV2.search(view.id, { + limit, + query: {}, + }) + expect(response.rows).toHaveLength(limit) }) - it.each(sortTestOptions)( - "allow sorting (%s)", - async (sortParams, expected) => { - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - sort: sortParams, - schema: viewSchema, - }) - - const response = await config.api.viewV2.search(view.id) - - expect(response.rows).toHaveLength(4) - expect(response.rows).toEqual( - expected.map(name => expect.objectContaining({ name })) + it("can handle pagination", async () => { + await Promise.all( + Array.from({ length: 10 }, () => + config.api.row.save(table._id!, {}) ) - } - ) + ) + const rows = (await config.api.viewV2.search(view.id)).rows - it.each(sortTestOptions)( - "allow override the default view sorting (%s)", - async (sortParams, expected) => { - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - sort: { - field: "name", - order: SortOrder.ASCENDING, - type: SortType.STRING, - }, - schema: viewSchema, - }) + const page1 = await config.api.viewV2.search(view.id, { + paginate: true, + limit: 4, + query: {}, + countRows: true, + }) + expect(page1).toEqual({ + rows: expect.arrayContaining(rows.slice(0, 4)), + hasNextPage: true, + bookmark: expect.anything(), + totalRows: 10, + }) - const response = await config.api.viewV2.search(view.id, { - sort: sortParams.field, - sortOrder: sortParams.order, - sortType: sortParams.type, - query: {}, - }) + const page2 = await config.api.viewV2.search(view.id, { + paginate: true, + limit: 4, + bookmark: page1.bookmark, + query: {}, + countRows: true, + }) + expect(page2).toEqual({ + rows: expect.arrayContaining(rows.slice(4, 8)), + hasNextPage: true, + bookmark: expect.anything(), + totalRows: 10, + }) - expect(response.rows).toHaveLength(4) - expect(response.rows).toEqual( - expected.map(name => expect.objectContaining({ name })) - ) + const page3 = await config.api.viewV2.search(view.id, { + paginate: true, + limit: 4, + bookmark: page2.bookmark, + query: {}, + countRows: true, + }) + const expectation: SearchResponse = { + rows: expect.arrayContaining(rows.slice(8)), + hasNextPage: false, + totalRows: 10, } - ) - }) - - it("can query on top of the view filters", async () => { - await config.api.row.save(table._id!, { - one: "foo", - two: "bar", - }) - await config.api.row.save(table._id!, { - one: "foo2", - two: "bar2", - }) - const three = await config.api.row.save(table._id!, { - one: "foo3", - two: "bar3", + if (isLucene) { + expectation.bookmark = expect.anything() + } + expect(page3).toEqual(expectation) }) - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - query: [ + const sortTestOptions: [ + { + field: string + order?: SortOrder + type?: SortType + }, + string[] + ][] = [ + [ { - operator: BasicOperator.NOT_EQUAL, - field: "one", - value: "foo2", + field: "name", + order: SortOrder.ASCENDING, + type: SortType.STRING, }, + ["Alice", "Bob", "Charly", "Danny"], ], - schema: { - id: { visible: true }, - one: { visible: true }, - two: { visible: true }, - }, - }) - - const response = await config.api.viewV2.search(view.id, { - query: { - [BasicOperator.EQUAL]: { - two: "bar3", + [ + { + field: "name", }, - [BasicOperator.NOT_EMPTY]: { - two: null, + ["Alice", "Bob", "Charly", "Danny"], + ], + [ + { + field: "name", + order: SortOrder.DESCENDING, }, - }, - }) - expect(response.rows).toHaveLength(1) - expect(response.rows).toEqual( - expect.arrayContaining([expect.objectContaining({ _id: three._id })]) - ) - }) - - it("can query on top of the view filters (using or filters)", async () => { - const one = await config.api.row.save(table._id!, { - one: "foo", - two: "bar", - }) - await config.api.row.save(table._id!, { - one: "foo2", - two: "bar2", - }) - const three = await config.api.row.save(table._id!, { - one: "foo3", - two: "bar3", - }) - - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - query: [ + ["Danny", "Charly", "Bob", "Alice"], + ], + [ { - operator: BasicOperator.NOT_EQUAL, - field: "two", - value: "bar2", + field: "name", + order: SortOrder.DESCENDING, + type: SortType.STRING, }, + ["Danny", "Charly", "Bob", "Alice"], ], - schema: { - id: { visible: true }, - one: { visible: false }, - two: { visible: true }, - }, - }) - - const response = await config.api.viewV2.search(view.id, { - query: { - allOr: true, - [BasicOperator.NOT_EQUAL]: { - two: "bar", + [ + { + field: "age", + order: SortOrder.ASCENDING, + type: SortType.NUMBER, }, - [BasicOperator.NOT_EMPTY]: { - two: null, + ["Danny", "Alice", "Charly", "Bob"], + ], + [ + { + field: "age", + order: SortOrder.ASCENDING, }, - }, - }) - expect(response.rows).toHaveLength(2) - expect(response.rows).toEqual( - expect.arrayContaining([ - expect.objectContaining({ _id: one._id }), - expect.objectContaining({ _id: three._id }), - ]) - ) - }) + ["Danny", "Alice", "Charly", "Bob"], + ], + [ + { + field: "age", + order: SortOrder.DESCENDING, + }, + ["Bob", "Charly", "Alice", "Danny"], + ], + [ + { + field: "age", + order: SortOrder.DESCENDING, + type: SortType.NUMBER, + }, + ["Bob", "Charly", "Alice", "Danny"], + ], + ] - isLucene && - it.each([true, false])( - "in lucene, cannot override a view filter", - async allOr => { - await config.api.row.save(table._id!, { - one: "foo", - two: "bar", - }) - const two = await config.api.row.save(table._id!, { - one: "foo2", - two: "bar2", - }) + describe("sorting", () => { + let table: Table + const viewSchema = { + id: { visible: true }, + age: { visible: true }, + name: { visible: true }, + } - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - query: [ - { - operator: BasicOperator.EQUAL, - field: "two", - value: "bar2", + beforeAll(async () => { + table = await config.api.table.save( + saveTableRequest({ + type: "table", + schema: { + name: { + type: FieldType.STRING, + name: "name", + }, + surname: { + type: FieldType.STRING, + name: "surname", + }, + age: { + type: FieldType.NUMBER, + name: "age", + }, + address: { + type: FieldType.STRING, + name: "address", + }, + jobTitle: { + type: FieldType.STRING, + name: "jobTitle", + }, }, - ], - schema: { - id: { visible: true }, - one: { visible: false }, - two: { visible: true }, - }, - }) + }) + ) - const response = await config.api.viewV2.search(view.id, { - query: { - allOr, - equal: { - two: "bar", - }, - }, - }) - expect(response.rows).toHaveLength(1) - expect(response.rows).toEqual([ - expect.objectContaining({ _id: two._id }), - ]) - } - ) + const users = [ + { name: "Alice", age: 25 }, + { name: "Bob", age: 30 }, + { name: "Charly", age: 27 }, + { name: "Danny", age: 15 }, + ] + await Promise.all( + users.map(u => + config.api.row.save(table._id!, { + tableId: table._id, + ...u, + }) + ) + ) + }) - !isLucene && - it.each([true, false])( - "can filter a view without a view filter", - async allOr => { - const one = await config.api.row.save(table._id!, { - one: "foo", - two: "bar", - }) - await config.api.row.save(table._id!, { - one: "foo2", - two: "bar2", - }) + it.each(sortTestOptions)( + "allow sorting (%s)", + async (sortParams, expected) => { + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + sort: sortParams, + schema: viewSchema, + }) - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - schema: { - id: { visible: true }, - one: { visible: false }, - two: { visible: true }, - }, - }) + const response = await config.api.viewV2.search(view.id) - const response = await config.api.viewV2.search(view.id, { - query: { - allOr, - equal: { - two: "bar", + expect(response.rows).toHaveLength(4) + expect(response.rows).toEqual( + expected.map(name => expect.objectContaining({ name })) + ) + } + ) + + it.each(sortTestOptions)( + "allow override the default view sorting (%s)", + async (sortParams, expected) => { + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + sort: { + field: "name", + order: SortOrder.ASCENDING, + type: SortType.STRING, }, - }, - }) - expect(response.rows).toHaveLength(1) - expect(response.rows).toEqual([ - expect.objectContaining({ _id: one._id }), - ]) - } - ) + schema: viewSchema, + }) - !isLucene && - it.each([true, false])("cannot bypass a view filter", async allOr => { + const response = await config.api.viewV2.search(view.id, { + sort: sortParams.field, + sortOrder: sortParams.order, + sortType: sortParams.type, + query: {}, + }) + + expect(response.rows).toHaveLength(4) + expect(response.rows).toEqual( + expected.map(name => expect.objectContaining({ name })) + ) + } + ) + }) + + it("can query on top of the view filters", async () => { await config.api.row.save(table._id!, { one: "foo", two: "bar", @@ -2882,746 +2725,939 @@ describe.each([ one: "foo2", two: "bar2", }) + const three = await config.api.row.save(table._id!, { + one: "foo3", + two: "bar3", + }) const view = await config.api.viewV2.create({ tableId: table._id!, name: generator.guid(), - query: [ - { - operator: BasicOperator.EQUAL, - field: "two", - value: "bar2", - }, - ], + queryUI: { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + logicalOperator: FilterGroupLogicalOperator.ALL, + groups: [ + { + logicalOperator: FilterGroupLogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.NOT_EQUAL, + field: "one", + value: "foo2", + }, + ], + }, + ], + }, schema: { id: { visible: true }, - one: { visible: false }, + one: { visible: true }, two: { visible: true }, }, }) const response = await config.api.viewV2.search(view.id, { query: { - allOr, - equal: { - two: "bar", + [BasicOperator.EQUAL]: { + two: "bar3", + }, + [BasicOperator.NOT_EMPTY]: { + two: null, }, }, }) - expect(response.rows).toHaveLength(0) - }) - - describe("foreign relationship columns", () => { - let envCleanup: () => void - beforeAll(() => { - envCleanup = features.testutils.setFeatureFlags("*", { - ENRICHED_RELATIONSHIPS: true, - }) - }) - - afterAll(() => { - envCleanup?.() - }) - - const createMainTable = async ( - links: { - name: string - tableId: string - fk: string - }[] - ) => { - const table = await config.api.table.save( - saveTableRequest({ - schema: { title: { name: "title", type: FieldType.STRING } }, - }) - ) - await config.api.table.save({ - ...table, - schema: { - ...table.schema, - ...links.reduce((acc, c) => { - acc[c.name] = { - name: c.name, - relationshipType: RelationshipType.ONE_TO_MANY, - type: FieldType.LINK, - tableId: c.tableId, - fieldName: c.fk, - constraints: { type: "array" }, - } - return acc - }, {}), - }, - }) - return table - } - const createAuxTable = (schema: TableSchema) => - config.api.table.save( - saveTableRequest({ - primaryDisplay: "name", - schema: { - ...schema, - name: { name: "name", type: FieldType.STRING }, - }, - }) + expect(response.rows).toHaveLength(1) + expect(response.rows).toEqual( + expect.arrayContaining([ + expect.objectContaining({ _id: three._id }), + ]) ) + }) - it("returns squashed fields respecting the view config", async () => { - const auxTable = await createAuxTable({ - age: { name: "age", type: FieldType.NUMBER }, - }) - const auxRow = await config.api.row.save(auxTable._id!, { - name: generator.name(), - age: generator.age(), + it("can query on top of the view filters (using or filters)", async () => { + const one = await config.api.row.save(table._id!, { + one: "foo", + two: "bar", }) - - const table = await createMainTable([ - { name: "aux", tableId: auxTable._id!, fk: "fk_aux" }, - ]) await config.api.row.save(table._id!, { - title: generator.word(), - aux: [auxRow], + one: "foo2", + two: "bar2", + }) + const three = await config.api.row.save(table._id!, { + one: "foo3", + two: "bar3", }) const view = await config.api.viewV2.create({ tableId: table._id!, name: generator.guid(), - schema: { - title: { visible: true }, - aux: { - visible: true, - columns: { - name: { visible: false, readonly: false }, - age: { visible: true, readonly: true }, + queryUI: { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + logicalOperator: FilterGroupLogicalOperator.ALL, + groups: [ + { + logicalOperator: FilterGroupLogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.NOT_EQUAL, + field: "one", + value: "foo2", + }, + ], }, + ], + }, + schema: { + id: { visible: true }, + one: { visible: false }, + two: { visible: true }, + }, + }) + + const response = await config.api.viewV2.search(view.id, { + query: { + allOr: true, + [BasicOperator.NOT_EQUAL]: { + two: "bar", + }, + [BasicOperator.NOT_EMPTY]: { + two: null, }, }, }) + expect(response.rows).toHaveLength(2) + expect(response.rows).toEqual( + expect.arrayContaining([ + expect.objectContaining({ _id: one._id }), + expect.objectContaining({ _id: three._id }), + ]) + ) + }) - const response = await config.api.viewV2.search(view.id) - expect(response.rows).toEqual([ - expect.objectContaining({ - aux: [ - { - _id: auxRow._id, - primaryDisplay: auxRow.name, - age: auxRow.age, + !isLucene && + it.each([true, false])( + "can filter a view without a view filter", + async allOr => { + const one = await config.api.row.save(table._id!, { + one: "foo", + two: "bar", + }) + await config.api.row.save(table._id!, { + one: "foo2", + two: "bar2", + }) + + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + schema: { + id: { visible: true }, + one: { visible: false }, + two: { visible: true }, }, - ], - }), - ]) - }) + }) - it("enriches squashed fields", async () => { - const auxTable = await createAuxTable({ - user: { - name: "user", - type: FieldType.BB_REFERENCE_SINGLE, - subtype: BBReferenceFieldSubType.USER, - constraints: { presence: true }, - }, - }) - const table = await createMainTable([ - { name: "aux", tableId: auxTable._id!, fk: "fk_aux" }, - ]) + const response = await config.api.viewV2.search(view.id, { + query: { + allOr, + equal: { + two: "bar", + }, + }, + }) + expect(response.rows).toHaveLength(1) + expect(response.rows).toEqual([ + expect.objectContaining({ _id: one._id }), + ]) + } + ) - const user = config.getUser() - const auxRow = await config.api.row.save(auxTable._id!, { - name: generator.name(), - user: user._id, - }) - await config.api.row.save(table._id!, { - title: generator.word(), - aux: [auxRow], - }) + !isLucene && + it.each([true, false])("cannot bypass a view filter", async allOr => { + await config.api.row.save(table._id!, { + one: "foo", + two: "bar", + }) + await config.api.row.save(table._id!, { + one: "foo2", + two: "bar2", + }) + + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + queryUI: { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + logicalOperator: FilterGroupLogicalOperator.ALL, + groups: [ + { + logicalOperator: FilterGroupLogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "two", + value: "bar2", + }, + ], + }, + ], + }, + schema: { + id: { visible: true }, + one: { visible: false }, + two: { visible: true }, + }, + }) - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - schema: { - title: { visible: true }, - aux: { - visible: true, - columns: { - name: { visible: true, readonly: true }, - user: { visible: true, readonly: true }, + const response = await config.api.viewV2.search(view.id, { + query: { + allOr, + equal: { + two: "bar", }, }, - }, + }) + expect(response.rows).toHaveLength(0) }) - const response = await config.api.viewV2.search(view.id) - - expect(response.rows).toEqual([ - expect.objectContaining({ - aux: [ - { - _id: auxRow._id, - primaryDisplay: auxRow.name, - name: auxRow.name, - user: { - _id: user._id, - email: user.email, - firstName: user.firstName, - lastName: user.lastName, - primaryDisplay: user.email, - }, - }, - ], - }), - ]) - }) - }) + describe("foreign relationship columns", () => { + let envCleanup: () => void + beforeAll(() => { + envCleanup = features.testutils.setFeatureFlags("*", { + ENRICHED_RELATIONSHIPS: true, + }) + }) - !isLucene && - describe("calculations", () => { - let table: Table - let rows: Row[] + afterAll(() => { + envCleanup?.() + }) - beforeAll(async () => { - table = await config.api.table.save( + const createMainTable = async ( + links: { + name: string + tableId: string + fk: string + }[] + ) => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { title: { name: "title", type: FieldType.STRING } }, + }) + ) + await config.api.table.save({ + ...table, + schema: { + ...table.schema, + ...links.reduce((acc, c) => { + acc[c.name] = { + name: c.name, + relationshipType: RelationshipType.ONE_TO_MANY, + type: FieldType.LINK, + tableId: c.tableId, + fieldName: c.fk, + constraints: { type: "array" }, + } + return acc + }, {}), + }, + }) + return table + } + const createAuxTable = (schema: TableSchema) => + config.api.table.save( saveTableRequest({ + primaryDisplay: "name", schema: { - quantity: { - type: FieldType.NUMBER, - name: "quantity", - }, - price: { - type: FieldType.NUMBER, - name: "price", - }, + ...schema, + name: { name: "name", type: FieldType.STRING }, }, }) ) - rows = await Promise.all( - Array.from({ length: 10 }, () => - config.api.row.save(table._id!, { - quantity: generator.natural({ min: 1, max: 10 }), - price: generator.natural({ min: 1, max: 10 }), - }) - ) - ) - }) + it("returns squashed fields respecting the view config", async () => { + const auxTable = await createAuxTable({ + age: { name: "age", type: FieldType.NUMBER }, + }) + const auxRow = await config.api.row.save(auxTable._id!, { + name: generator.name(), + age: generator.age(), + }) + + const table = await createMainTable([ + { name: "aux", tableId: auxTable._id!, fk: "fk_aux" }, + ]) + await config.api.row.save(table._id!, { + title: generator.word(), + aux: [auxRow], + }) - it("should be able to search by calculations", async () => { const view = await config.api.viewV2.create({ tableId: table._id!, - type: ViewV2Type.CALCULATION, name: generator.guid(), schema: { - "Quantity Sum": { + title: { visible: true }, + aux: { visible: true, - calculationType: CalculationType.SUM, - field: "quantity", + columns: { + name: { visible: false, readonly: false }, + age: { visible: true, readonly: true }, + }, }, }, }) - const response = await config.api.viewV2.search(view.id, { - query: {}, - }) + const response = await config.api.viewV2.search(view.id) + expect(response.rows).toEqual([ + expect.objectContaining({ + aux: [ + { + _id: auxRow._id, + primaryDisplay: auxRow.name, + age: auxRow.age, + }, + ], + }), + ]) + }) - expect(response.rows).toHaveLength(1) - expect(response.rows).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - "Quantity Sum": rows.reduce((acc, r) => acc + r.quantity, 0), - }), - ]) - ) + it("enriches squashed fields", async () => { + const auxTable = await createAuxTable({ + user: { + name: "user", + type: FieldType.BB_REFERENCE_SINGLE, + subtype: BBReferenceFieldSubType.USER, + constraints: { presence: true }, + }, + }) + const table = await createMainTable([ + { name: "aux", tableId: auxTable._id!, fk: "fk_aux" }, + ]) - // Calculation views do not return rows that can be linked back to - // the source table, and so should not have an _id field. - for (const row of response.rows) { - expect("_id" in row).toBe(false) - } - }) + const user = config.getUser() + const auxRow = await config.api.row.save(auxTable._id!, { + name: generator.name(), + user: user._id, + }) + await config.api.row.save(table._id!, { + title: generator.word(), + aux: [auxRow], + }) - it("should be able to group by a basic field", async () => { const view = await config.api.viewV2.create({ tableId: table._id!, name: generator.guid(), - type: ViewV2Type.CALCULATION, schema: { - quantity: { - visible: true, - field: "quantity", - }, - "Total Price": { + title: { visible: true }, + aux: { visible: true, - calculationType: CalculationType.SUM, - field: "price", + columns: { + name: { visible: true, readonly: true }, + user: { visible: true, readonly: true }, + }, }, }, }) - const response = await config.api.viewV2.search(view.id, { - query: {}, - }) - - const priceByQuantity: Record = {} - for (const row of rows) { - priceByQuantity[row.quantity] ??= 0 - priceByQuantity[row.quantity] += row.price - } + const response = await config.api.viewV2.search(view.id) - for (const row of response.rows) { - expect(row["Total Price"]).toEqual(priceByQuantity[row.quantity]) - } + expect(response.rows).toEqual([ + expect.objectContaining({ + aux: [ + { + _id: auxRow._id, + primaryDisplay: auxRow.name, + name: auxRow.name, + user: { + _id: user._id, + email: user.email, + firstName: user.firstName, + lastName: user.lastName, + primaryDisplay: user.email, + }, + }, + ], + }), + ]) }) + }) - it.each([ - CalculationType.COUNT, - CalculationType.SUM, - CalculationType.AVG, - CalculationType.MIN, - CalculationType.MAX, - ])("should be able to calculate $type", async type => { - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - type: ViewV2Type.CALCULATION, - schema: { - aggregate: { - visible: true, - calculationType: type, - field: "price", - }, - }, + !isLucene && + describe("calculations", () => { + let table: Table + let rows: Row[] + + beforeAll(async () => { + table = await config.api.table.save( + saveTableRequest({ + schema: { + quantity: { + type: FieldType.NUMBER, + name: "quantity", + }, + price: { + type: FieldType.NUMBER, + name: "price", + }, + }, + }) + ) + + rows = await Promise.all( + Array.from({ length: 10 }, () => + config.api.row.save(table._id!, { + quantity: generator.natural({ min: 1, max: 10 }), + price: generator.natural({ min: 1, max: 10 }), + }) + ) + ) }) - const response = await config.api.viewV2.search(view.id, { - query: {}, + it("should be able to search by calculations", async () => { + const view = await config.api.viewV2.create({ + tableId: table._id!, + type: ViewV2Type.CALCULATION, + name: generator.guid(), + schema: { + "Quantity Sum": { + visible: true, + calculationType: CalculationType.SUM, + field: "quantity", + }, + }, + }) + + const response = await config.api.viewV2.search(view.id, { + query: {}, + }) + + expect(response.rows).toHaveLength(1) + expect(response.rows).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + "Quantity Sum": rows.reduce( + (acc, r) => acc + r.quantity, + 0 + ), + }), + ]) + ) + + // Calculation views do not return rows that can be linked back to + // the source table, and so should not have an _id field. + for (const row of response.rows) { + expect("_id" in row).toBe(false) + } }) - function calculate( - type: CalculationType, - numbers: number[] - ): number { - switch (type) { - case CalculationType.COUNT: - return numbers.length - case CalculationType.SUM: - return numbers.reduce((a, b) => a + b, 0) - case CalculationType.AVG: - return numbers.reduce((a, b) => a + b, 0) / numbers.length - case CalculationType.MIN: - return Math.min(...numbers) - case CalculationType.MAX: - return Math.max(...numbers) + it("should be able to group by a basic field", async () => { + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + type: ViewV2Type.CALCULATION, + schema: { + quantity: { + visible: true, + field: "quantity", + }, + "Total Price": { + visible: true, + calculationType: CalculationType.SUM, + field: "price", + }, + }, + }) + + const response = await config.api.viewV2.search(view.id, { + query: {}, + }) + + const priceByQuantity: Record = {} + for (const row of rows) { + priceByQuantity[row.quantity] ??= 0 + priceByQuantity[row.quantity] += row.price } - } - const prices = rows.map(row => row.price) - const expected = calculate(type, prices) - const actual = response.rows[0].aggregate - - if (type === CalculationType.AVG) { - // The average calculation can introduce floating point rounding - // errors, so we need to compare to within a small margin of - // error. - expect(actual).toBeCloseTo(expected) - } else { - expect(actual).toEqual(expected) - } - }) + for (const row of response.rows) { + expect(row["Total Price"]).toEqual( + priceByQuantity[row.quantity] + ) + } + }) - it("should be able to do a COUNT(DISTINCT)", async () => { - const table = await config.api.table.save( - saveTableRequest({ + it.each([ + CalculationType.COUNT, + CalculationType.SUM, + CalculationType.AVG, + CalculationType.MIN, + CalculationType.MAX, + ])("should be able to calculate $type", async type => { + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + type: ViewV2Type.CALCULATION, schema: { - name: { - name: "name", - type: FieldType.STRING, + aggregate: { + visible: true, + calculationType: type, + field: "price", }, }, }) - ) - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - type: ViewV2Type.CALCULATION, - schema: { - count: { - visible: true, - calculationType: CalculationType.COUNT, - distinct: true, - field: "name", + const response = await config.api.viewV2.search(view.id, { + query: {}, + }) + + function calculate( + type: CalculationType, + numbers: number[] + ): number { + switch (type) { + case CalculationType.COUNT: + return numbers.length + case CalculationType.SUM: + return numbers.reduce((a, b) => a + b, 0) + case CalculationType.AVG: + return numbers.reduce((a, b) => a + b, 0) / numbers.length + case CalculationType.MIN: + return Math.min(...numbers) + case CalculationType.MAX: + return Math.max(...numbers) + } + } + + const prices = rows.map(row => row.price) + const expected = calculate(type, prices) + const actual = response.rows[0].aggregate + + if (type === CalculationType.AVG) { + // The average calculation can introduce floating point rounding + // errors, so we need to compare to within a small margin of + // error. + expect(actual).toBeCloseTo(expected) + } else { + expect(actual).toEqual(expected) + } + }) + + it("should be able to do a COUNT(DISTINCT)", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + }, + }) + ) + + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + type: ViewV2Type.CALCULATION, + schema: { + count: { + visible: true, + calculationType: CalculationType.COUNT, + distinct: true, + field: "name", + }, }, - }, + }) + + await config.api.row.bulkImport(table._id!, { + rows: [ + { + name: "John", + }, + { + name: "John", + }, + { + name: "Sue", + }, + ], + }) + + const { rows } = await config.api.row.search(view.id) + expect(rows).toHaveLength(1) + expect(rows[0].count).toEqual(2) }) - await config.api.row.bulkImport(table._id!, { - rows: [ + it("should not be able to COUNT(DISTINCT ...) against a non-existent field", async () => { + await config.api.viewV2.create( { - name: "John", + tableId: table._id!, + name: generator.guid(), + type: ViewV2Type.CALCULATION, + schema: { + count: { + visible: true, + calculationType: CalculationType.COUNT, + distinct: true, + field: "does not exist oh no", + }, + }, }, { - name: "John", + status: 400, + body: { + message: + 'Calculation field "count" references field "does not exist oh no" which does not exist in the table schema', + }, + } + ) + }) + + it("should be able to filter rows on the view itself", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + quantity: { + type: FieldType.NUMBER, + name: "quantity", + }, + price: { + type: FieldType.NUMBER, + name: "price", + }, + }, + }) + ) + + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + type: ViewV2Type.CALCULATION, + queryUI: { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + logicalOperator: FilterGroupLogicalOperator.ALL, + groups: [ + { + logicalOperator: FilterGroupLogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "quantity", + value: 1, + }, + ], + }, + ], }, - { - name: "Sue", + schema: { + sum: { + visible: true, + calculationType: CalculationType.SUM, + field: "price", + }, }, - ], + }) + + await config.api.row.bulkImport(table._id!, { + rows: [ + { + quantity: 1, + price: 1, + }, + { + quantity: 1, + price: 2, + }, + { + quantity: 2, + price: 10, + }, + ], + }) + + const { rows } = await config.api.viewV2.search(view.id, { + query: {}, + }) + expect(rows).toHaveLength(1) + expect(rows[0].sum).toEqual(3) }) - const { rows } = await config.api.row.search(view.id) - expect(rows).toHaveLength(1) - expect(rows[0].count).toEqual(2) - }) + it("should be able to filter on group by fields", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + quantity: { + type: FieldType.NUMBER, + name: "quantity", + }, + price: { + type: FieldType.NUMBER, + name: "price", + }, + }, + }) + ) - it("should not be able to COUNT(DISTINCT ...) against a non-existent field", async () => { - await config.api.viewV2.create( - { + const view = await config.api.viewV2.create({ tableId: table._id!, name: generator.guid(), type: ViewV2Type.CALCULATION, schema: { - count: { + quantity: { visible: true }, + sum: { visible: true, - calculationType: CalculationType.COUNT, - distinct: true, - field: "does not exist oh no", + calculationType: CalculationType.SUM, + field: "price", }, }, - }, - { - status: 400, - body: { - message: - 'Calculation field "count" references field "does not exist oh no" which does not exist in the table schema', - }, - } - ) - }) + }) - it("should be able to filter rows on the view itself", async () => { - const table = await config.api.table.save( - saveTableRequest({ - schema: { - quantity: { - type: FieldType.NUMBER, - name: "quantity", + await config.api.row.bulkImport(table._id!, { + rows: [ + { + quantity: 1, + price: 1, }, - price: { - type: FieldType.NUMBER, - name: "price", + { + quantity: 1, + price: 2, }, - }, + { + quantity: 2, + price: 10, + }, + ], }) - ) - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - type: ViewV2Type.CALCULATION, - query: { - equal: { - quantity: 1, - }, - }, - schema: { - sum: { - visible: true, - calculationType: CalculationType.SUM, - field: "price", + const { rows } = await config.api.viewV2.search(view.id, { + query: { + equal: { + quantity: 1, + }, }, - }, - }) + }) - await config.api.row.bulkImport(table._id!, { - rows: [ - { - quantity: 1, - price: 1, - }, - { - quantity: 1, - price: 2, - }, - { - quantity: 2, - price: 10, - }, - ], + expect(rows).toHaveLength(1) + expect(rows[0].sum).toEqual(3) }) - const { rows } = await config.api.viewV2.search(view.id, { - query: {}, - }) - expect(rows).toHaveLength(1) - expect(rows[0].sum).toEqual(3) - }) + it("should be able to sort by group by field", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + quantity: { + type: FieldType.NUMBER, + name: "quantity", + }, + price: { + type: FieldType.NUMBER, + name: "price", + }, + }, + }) + ) - it("should be able to filter on group by fields", async () => { - const table = await config.api.table.save( - saveTableRequest({ + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + type: ViewV2Type.CALCULATION, schema: { - quantity: { - type: FieldType.NUMBER, - name: "quantity", - }, - price: { - type: FieldType.NUMBER, - name: "price", + quantity: { visible: true }, + sum: { + visible: true, + calculationType: CalculationType.SUM, + field: "price", }, }, }) - ) - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - type: ViewV2Type.CALCULATION, - schema: { - quantity: { visible: true }, - sum: { - visible: true, - calculationType: CalculationType.SUM, - field: "price", - }, - }, - }) + await config.api.row.bulkImport(table._id!, { + rows: [ + { + quantity: 1, + price: 1, + }, + { + quantity: 1, + price: 2, + }, + { + quantity: 2, + price: 10, + }, + ], + }) - await config.api.row.bulkImport(table._id!, { - rows: [ - { - quantity: 1, - price: 1, - }, - { - quantity: 1, - price: 2, - }, - { - quantity: 2, - price: 10, - }, - ], - }) + const { rows } = await config.api.viewV2.search(view.id, { + query: {}, + sort: "quantity", + sortOrder: SortOrder.DESCENDING, + }) - const { rows } = await config.api.viewV2.search(view.id, { - query: { - equal: { - quantity: 1, - }, - }, + expect(rows).toEqual([ + expect.objectContaining({ quantity: 2, sum: 10 }), + expect.objectContaining({ quantity: 1, sum: 3 }), + ]) }) - expect(rows).toHaveLength(1) - expect(rows[0].sum).toEqual(3) - }) + it("should be able to sort by a calculation", async () => { + const table = await config.api.table.save( + saveTableRequest({ + schema: { + quantity: { + type: FieldType.NUMBER, + name: "quantity", + }, + price: { + type: FieldType.NUMBER, + name: "price", + }, + }, + }) + ) - it("should be able to sort by group by field", async () => { - const table = await config.api.table.save( - saveTableRequest({ - schema: { - quantity: { - type: FieldType.NUMBER, - name: "quantity", + await config.api.row.bulkImport(table._id!, { + rows: [ + { + quantity: 1, + price: 1, }, - price: { - type: FieldType.NUMBER, - name: "price", + { + quantity: 1, + price: 2, }, - }, + { + quantity: 2, + price: 10, + }, + ], }) - ) - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - type: ViewV2Type.CALCULATION, - schema: { - quantity: { visible: true }, - sum: { - visible: true, - calculationType: CalculationType.SUM, - field: "price", + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + type: ViewV2Type.CALCULATION, + schema: { + quantity: { visible: true }, + sum: { + visible: true, + calculationType: CalculationType.SUM, + field: "price", + }, }, - }, - }) + }) - await config.api.row.bulkImport(table._id!, { - rows: [ - { - quantity: 1, - price: 1, - }, - { - quantity: 1, - price: 2, - }, - { - quantity: 2, - price: 10, - }, - ], - }) + const { rows } = await config.api.viewV2.search(view.id, { + query: {}, + sort: "sum", + sortOrder: SortOrder.DESCENDING, + }) - const { rows } = await config.api.viewV2.search(view.id, { - query: {}, - sort: "quantity", - sortOrder: SortOrder.DESCENDING, + expect(rows).toEqual([ + expect.objectContaining({ quantity: 2, sum: 10 }), + expect.objectContaining({ quantity: 1, sum: 3 }), + ]) }) - - expect(rows).toEqual([ - expect.objectContaining({ quantity: 2, sum: 10 }), - expect.objectContaining({ quantity: 1, sum: 3 }), - ]) }) - it("should be able to sort by a calculation", async () => { + !isLucene && + it("should not need required fields to be present", async () => { const table = await config.api.table.save( saveTableRequest({ schema: { - quantity: { - type: FieldType.NUMBER, - name: "quantity", + name: { + name: "name", + type: FieldType.STRING, + constraints: { + presence: true, + }, }, - price: { + age: { + name: "age", type: FieldType.NUMBER, - name: "price", }, }, }) ) - await config.api.row.bulkImport(table._id!, { - rows: [ - { - quantity: 1, - price: 1, - }, - { - quantity: 1, - price: 2, - }, - { - quantity: 2, - price: 10, - }, - ], - }) + await Promise.all([ + config.api.row.save(table._id!, { name: "Steve", age: 30 }), + config.api.row.save(table._id!, { name: "Jane", age: 31 }), + ]) const view = await config.api.viewV2.create({ tableId: table._id!, name: generator.guid(), type: ViewV2Type.CALCULATION, schema: { - quantity: { visible: true }, sum: { visible: true, calculationType: CalculationType.SUM, - field: "price", + field: "age", }, }, }) - const { rows } = await config.api.viewV2.search(view.id, { + const response = await config.api.viewV2.search(view.id, { query: {}, - sort: "sum", - sortOrder: SortOrder.DESCENDING, }) - expect(rows).toEqual([ - expect.objectContaining({ quantity: 2, sum: 10 }), - expect.objectContaining({ quantity: 1, sum: 3 }), - ]) + expect(response.rows).toHaveLength(1) + expect(response.rows[0].sum).toEqual(61) }) - }) - !isLucene && - it("should not need required fields to be present", async () => { + it("should be able to filter on a single user field in both the view query and search query", async () => { const table = await config.api.table.save( saveTableRequest({ schema: { - name: { - name: "name", - type: FieldType.STRING, - constraints: { - presence: true, - }, - }, - age: { - name: "age", - type: FieldType.NUMBER, + user: { + name: "user", + type: FieldType.BB_REFERENCE_SINGLE, + subtype: BBReferenceFieldSubType.USER, }, }, }) ) - await Promise.all([ - config.api.row.save(table._id!, { name: "Steve", age: 30 }), - config.api.row.save(table._id!, { name: "Jane", age: 31 }), - ]) + await config.api.row.save(table._id!, { + user: config.getUser()._id, + }) const view = await config.api.viewV2.create({ tableId: table._id!, name: generator.guid(), - type: ViewV2Type.CALCULATION, + queryUI: { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + logicalOperator: FilterGroupLogicalOperator.ALL, + groups: [ + { + logicalOperator: FilterGroupLogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "user", + value: "{{ [user].[_id] }}", + }, + ], + }, + ], + }, schema: { - sum: { + user: { visible: true, - calculationType: CalculationType.SUM, - field: "age", }, }, }) - const response = await config.api.viewV2.search(view.id, { - query: {}, - }) - - expect(response.rows).toHaveLength(1) - expect(response.rows[0].sum).toEqual(61) - }) - - it("should be able to filter on a single user field in both the view query and search query", async () => { - const table = await config.api.table.save( - saveTableRequest({ - schema: { - user: { - name: "user", - type: FieldType.BB_REFERENCE_SINGLE, - subtype: BBReferenceFieldSubType.USER, + const { rows } = await config.api.viewV2.search(view.id, { + query: { + equal: { + user: "{{ [user].[_id] }}", }, }, }) - ) - - await config.api.row.save(table._id!, { - user: config.getUser()._id, - }) - - const view = await config.api.viewV2.create({ - tableId: table._id!, - name: generator.guid(), - query: { - equal: { - user: "{{ [user].[_id] }}", - }, - }, - schema: { - user: { - visible: true, - }, - }, - }) - const { rows } = await config.api.viewV2.search(view.id, { - query: { - equal: { - user: "{{ [user].[_id] }}", - }, - }, + expect(rows).toHaveLength(1) + expect(rows[0].user._id).toEqual(config.getUser()._id) }) - - expect(rows).toHaveLength(1) - expect(rows[0].user._id).toEqual(config.getUser()._id) }) - }) describe("permissions", () => { beforeEach(async () => { diff --git a/packages/server/src/sdk/app/views/utils.ts b/packages/server/src/sdk/app/views/utils.ts index 45b091e0463..1468328921e 100644 --- a/packages/server/src/sdk/app/views/utils.ts +++ b/packages/server/src/sdk/app/views/utils.ts @@ -1,8 +1,13 @@ import { ViewV2 } from "@budibase/types" import { utils, dataFilters } from "@budibase/shared-core" +import { isPlainObject } from "lodash" + +function isEmptyObject(obj: any) { + return obj && isPlainObject(obj) && Object.keys(obj).length === 0 +} export function ensureQueryUISet(view: ViewV2) { - if (!view.queryUI && view.query) { + if (!view.queryUI && view.query && !isEmptyObject(view.query)) { if (!Array.isArray(view.query)) { // In practice this should not happen. `view.query`, at the time this code // goes into the codebase, only contains LegacyFilter[] in production. @@ -24,7 +29,7 @@ export function ensureQueryUISet(view: ViewV2) { } export function ensureQuerySet(view: ViewV2) { - if (!view.query && view.queryUI) { + if (!view.query && view.queryUI && !isEmptyObject(view.queryUI)) { view.query = dataFilters.buildQuery(view.queryUI) } } diff --git a/packages/server/src/tests/utilities/api/viewV2.ts b/packages/server/src/tests/utilities/api/viewV2.ts index 9741240f27b..ba07dbe5f04 100644 --- a/packages/server/src/tests/utilities/api/viewV2.ts +++ b/packages/server/src/tests/utilities/api/viewV2.ts @@ -10,7 +10,9 @@ import { Expectations, TestAPI } from "./base" export class ViewV2API extends TestAPI { create = async ( - view: CreateViewRequest, + // The frontend changed in v3 from sending query to sending only queryUI, + // making sure tests reflect that. + view: Omit, expectations?: Expectations ): Promise => { const exp: Expectations = { diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 3b9253af6e6..721cc7565c5 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -319,9 +319,6 @@ const buildCondition = (expression: LegacyFilter) => { return } - const isHbs = - typeof value === "string" && (value.match(HBS_REGEX) || []).length > 0 - if (operator === "allOr") { query.allOr = true return @@ -338,40 +335,45 @@ const buildCondition = (expression: LegacyFilter) => { value = null } - if ( - type === "datetime" && - !isHbs && - operator !== "empty" && - operator !== "notEmpty" - ) { - // Ensure date value is a valid date and parse into correct format - if (!value) { - return - } - try { - value = new Date(value).toISOString() - } catch (error) { - return - } - } - if (type === "number" && typeof value === "string" && !isHbs) { - if (operator === "oneOf") { - value = value.split(",").map(item => parseFloat(item)) - } else { - value = parseFloat(value) - } - } - if (type === "boolean") { - value = `${value}`?.toLowerCase() === "true" - } - if ( - ["contains", "notContains", "containsAny"].includes( - operator.toLocaleString() - ) && - type === "array" && - typeof value === "string" - ) { - value = value.split(",") + const isHbs = + typeof value === "string" && (value.match(HBS_REGEX) || []).length > 0 + + // Parsing value depending on what the type is. + switch (type) { + case FieldType.DATETIME: + if (!isHbs && operator !== "empty" && operator !== "notEmpty") { + if (!value) { + return + } + try { + value = new Date(value).toISOString() + } catch (error) { + return + } + } + break + case FieldType.NUMBER: + if (typeof value === "string" && !isHbs) { + if (operator === "oneOf") { + value = value.split(",").map(parseFloat) + } else { + value = parseFloat(value) + } + } + break + case FieldType.BOOLEAN: + value = `${value}`.toLowerCase() === "true" + break + case FieldType.ARRAY: + if ( + ["contains", "notContains", "containsAny"].includes( + operator.toLocaleString() + ) && + typeof value === "string" + ) { + value = value.split(",") + } + break } if (isRangeSearchOperator(operator)) { @@ -398,17 +400,17 @@ const buildCondition = (expression: LegacyFilter) => { ...query.range[field], low: value, } - } else if (isLogicalSearchOperator(operator)) { - // TODO } else if ( isBasicSearchOperator(operator) || isArraySearchOperator(operator) || isRangeSearchOperator(operator) ) { if (type === "boolean") { - // Transform boolean filters to cope with null. - // "equals false" needs to be "not equals true" - // "not equals false" needs to be "equals true" + // TODO(samwho): I suspect this boolean transformation isn't needed anymore, + // write some tests to confirm. + + // Transform boolean filters to cope with null. "equals false" needs to + // be "not equals true" "not equals false" needs to be "equals true" if (operator === "equal" && value === false) { query.notEqual = query.notEqual || {} query.notEqual[field] = true @@ -423,6 +425,8 @@ const buildCondition = (expression: LegacyFilter) => { query[operator] ??= {} query[operator][field] = value } + } else { + throw new Error(`Unsupported operator: ${operator}`) } return query From 714029b9a036b16f02d3c4c4b411bc8c6f814fd2 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 17 Oct 2024 18:06:45 +0100 Subject: [PATCH 04/20] Making more progress on testing view searching. --- .../src/api/routes/tests/viewV2.spec.ts | 148 ++++++++++++++++++ packages/server/src/sdk/app/rows/search.ts | 6 - packages/shared-core/src/filters.ts | 4 +- 3 files changed, 151 insertions(+), 7 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 71506da0fa9..c2626155c92 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -29,12 +29,18 @@ import { JsonTypes, FilterGroupLogicalOperator, EmptyFilterOption, + JsonFieldSubType, + SearchFilterGroup, + LegacyFilter, + SearchViewRowRequest, + SearchFilterChild, } from "@budibase/types" import { generator, mocks } from "@budibase/backend-core/tests" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" import merge from "lodash/merge" import { quotas } from "@budibase/pro" import { db, roles, features } from "@budibase/backend-core" +import { single } from "validate.js" describe.each([ ["lucene", undefined], @@ -3657,6 +3663,148 @@ describe.each([ expect(rows).toHaveLength(1) expect(rows[0].user._id).toEqual(config.getUser()._id) }) + + describe("search operators", () => { + let table: Table + beforeEach(async () => { + table = await config.api.table.save( + saveTableRequest({ + schema: { + string: { name: "string", type: FieldType.STRING }, + longform: { name: "longform", type: FieldType.LONGFORM }, + options: { + name: "options", + type: FieldType.OPTIONS, + constraints: { inclusion: ["a", "b", "c"] }, + }, + array: { + name: "array", + type: FieldType.ARRAY, + constraints: { + type: JsonFieldSubType.ARRAY, + inclusion: ["a", "b", "c"], + }, + }, + number: { name: "number", type: FieldType.NUMBER }, + bigint: { name: "bigint", type: FieldType.BIGINT }, + datetime: { name: "datetime", type: FieldType.DATETIME }, + timeOnly: { + name: "timeOnly", + type: FieldType.DATETIME, + timeOnly: true, + }, + boolean: { name: "boolean", type: FieldType.BOOLEAN }, + user: { + name: "user", + type: FieldType.BB_REFERENCE_SINGLE, + subtype: BBReferenceFieldSubType.USER, + }, + users: { + name: "users", + type: FieldType.BB_REFERENCE, + subtype: BBReferenceFieldSubType.USER, + }, + }, + }) + ) + }) + + interface TestCase { + name: string + query: SearchFilterGroup + insert: Row[] + expected: Row[] + searchOpts?: SearchViewRowRequest + } + + function defaultQuery( + query: Partial + ): SearchFilterGroup { + return { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + logicalOperator: FilterGroupLogicalOperator.ALL, + groups: [], + ...query, + } + } + + function defaultGroup( + group: Partial + ): SearchFilterChild { + return { + logicalOperator: FilterGroupLogicalOperator.ALL, + filters: [], + ...group, + } + } + + function simpleQuery(...filters: LegacyFilter[]): SearchFilterGroup { + return defaultQuery({ groups: [defaultGroup({ filters })] }) + } + + const testCases: TestCase[] = [ + { + name: "empty query return all", + insert: [{ string: "foo" }], + query: defaultQuery({ + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + }), + expected: [{ string: "foo" }], + }, + { + name: "empty query return none", + insert: [{ string: "foo" }], + query: defaultQuery({ + onEmptyFilter: EmptyFilterOption.RETURN_NONE, + }), + expected: [], + }, + { + name: "simple string search", + insert: [{ string: "foo" }], + query: simpleQuery({ + operator: BasicOperator.EQUAL, + field: "string", + value: "foo", + }), + expected: [{ string: "foo" }], + }, + { + name: "non matching string search", + insert: [{ string: "foo" }], + query: simpleQuery({ + operator: BasicOperator.EQUAL, + field: "string", + value: "bar", + }), + expected: [], + }, + ] + + it.only.each(testCases)( + "$name", + async ({ query, insert, expected, searchOpts }) => { + await config.api.row.bulkImport(table._id!, { rows: insert }) + + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: generator.guid(), + queryUI: query, + schema: { + string: { visible: true }, + }, + }) + + const { rows } = await config.api.viewV2.search( + view.id, + searchOpts + ) + expect(rows).toEqual( + expected.map(r => expect.objectContaining(r)) + ) + } + ) + }) }) describe("permissions", () => { diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index 7ac3bb8ead0..6abfe0c6818 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -89,9 +89,6 @@ export async function search( options = searchInputMapping(table, options) if (options.viewId) { - // Delete extraneous search params that cannot be overridden - delete options.query.onEmptyFilter - const view = source as ViewV2 // Enrich saved query with ephemeral query params. // We prevent searching on any fields that are saved as part of the query, as @@ -99,7 +96,6 @@ export async function search( let viewQuery = await enrichSearchContext(view.query || {}, context) viewQuery = dataFilters.buildQueryLegacy(viewQuery) || {} viewQuery = checkFilters(table, viewQuery) - delete viewQuery?.onEmptyFilter const sqsEnabled = await features.flags.isEnabled("SQS") const supportsLogicalOperators = @@ -112,8 +108,6 @@ export async function search( ? view.query : [] - delete options.query.onEmptyFilter - // Extract existing fields const existingFields = queryFilters diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 721cc7565c5..756a8147101 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -600,7 +600,7 @@ export function buildQuery( const globalOperator = operatorMap[parsedFilter.logicalOperator] - return { + const ret = { ...(globalOnEmpty ? { onEmptyFilter: globalOnEmpty } : {}), [globalOperator]: { conditions: parsedFilter.groups?.map(group => { @@ -614,6 +614,8 @@ export function buildQuery( }), }, } + + return ret } // The frontend can send single values for array fields sometimes, so to handle From b7979f4719ad029a66258caed81fbccf0b5c6422 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 18 Oct 2024 10:32:28 +0100 Subject: [PATCH 05/20] Refactor SearchFilterGroup type to be less verbose and more clearly named. --- .../src/api/routes/tests/viewV2.spec.ts | 73 +++++++------------ packages/server/src/sdk/app/views/utils.ts | 4 +- packages/shared-core/src/filters.ts | 52 ++++++------- packages/shared-core/src/utils.ts | 24 +++--- packages/types/src/api/web/searchFilter.ts | 20 ++--- packages/types/src/documents/app/view.ts | 4 +- packages/types/src/sdk/search.ts | 2 +- 7 files changed, 72 insertions(+), 107 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index c2626155c92..fa1f1690f66 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -27,20 +27,18 @@ import { ViewV2Schema, ViewV2Type, JsonTypes, - FilterGroupLogicalOperator, + UILogicalOperator, EmptyFilterOption, JsonFieldSubType, - SearchFilterGroup, + UISearchFilter, LegacyFilter, SearchViewRowRequest, - SearchFilterChild, } from "@budibase/types" import { generator, mocks } from "@budibase/backend-core/tests" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" import merge from "lodash/merge" import { quotas } from "@budibase/pro" import { db, roles, features } from "@budibase/backend-core" -import { single } from "validate.js" describe.each([ ["lucene", undefined], @@ -158,11 +156,11 @@ describe.each([ tableId: table._id!, primaryDisplay: "id", queryUI: { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, onEmptyFilter: EmptyFilterOption.RETURN_ALL, groups: [ { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -2388,10 +2386,10 @@ describe.each([ name: generator.guid(), queryUI: { onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -2452,10 +2450,10 @@ describe.each([ name: generator.guid(), queryUI: { onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -2741,10 +2739,10 @@ describe.each([ name: generator.guid(), queryUI: { onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.NOT_EQUAL, @@ -2799,10 +2797,10 @@ describe.each([ name: generator.guid(), queryUI: { onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.NOT_EQUAL, @@ -2894,10 +2892,10 @@ describe.each([ name: generator.guid(), queryUI: { onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -3338,10 +3336,10 @@ describe.each([ type: ViewV2Type.CALCULATION, queryUI: { onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -3631,10 +3629,10 @@ describe.each([ name: generator.guid(), queryUI: { onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -3711,52 +3709,31 @@ describe.each([ interface TestCase { name: string - query: SearchFilterGroup + query: UISearchFilter insert: Row[] expected: Row[] searchOpts?: SearchViewRowRequest } - function defaultQuery( - query: Partial - ): SearchFilterGroup { - return { - onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: FilterGroupLogicalOperator.ALL, - groups: [], - ...query, - } - } - - function defaultGroup( - group: Partial - ): SearchFilterChild { - return { - logicalOperator: FilterGroupLogicalOperator.ALL, - filters: [], - ...group, - } - } - - function simpleQuery(...filters: LegacyFilter[]): SearchFilterGroup { - return defaultQuery({ groups: [defaultGroup({ filters })] }) + function simpleQuery(...filters: LegacyFilter[]): UISearchFilter { + return { groups: [{ filters }] } } const testCases: TestCase[] = [ { name: "empty query return all", insert: [{ string: "foo" }], - query: defaultQuery({ + query: { onEmptyFilter: EmptyFilterOption.RETURN_ALL, - }), + }, expected: [{ string: "foo" }], }, { name: "empty query return none", insert: [{ string: "foo" }], - query: defaultQuery({ + query: { onEmptyFilter: EmptyFilterOption.RETURN_NONE, - }), + }, expected: [], }, { diff --git a/packages/server/src/sdk/app/views/utils.ts b/packages/server/src/sdk/app/views/utils.ts index 1468328921e..a110acf0722 100644 --- a/packages/server/src/sdk/app/views/utils.ts +++ b/packages/server/src/sdk/app/views/utils.ts @@ -14,8 +14,8 @@ export function ensureQueryUISet(view: ViewV2) { // We're changing it in the change that this comment is part of to also // include SearchFilters objects. These are created when we receive an // update to a ViewV2 that contains a queryUI and not a query field. We - // can convert SearchFilterGroup (the type of queryUI) to SearchFilters, - // but not LegacyFilter[], they are incompatible due to SearchFilterGroup + // can convert UISearchFilter (the type of queryUI) to SearchFilters, + // but not LegacyFilter[], they are incompatible due to UISearchFilter // and SearchFilters being recursive types. // // So despite the type saying that `view.query` is a LegacyFilter[] | diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 756a8147101..932d5a8ca02 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -19,8 +19,8 @@ import { RangeOperator, LogicalOperator, isLogicalSearchOperator, - SearchFilterGroup, - FilterGroupLogicalOperator, + UISearchFilter, + UILogicalOperator, isBasicSearchOperator, isArraySearchOperator, isRangeSearchOperator, @@ -561,7 +561,7 @@ export const buildQueryLegacy = ( } /** - * Converts a **SearchFilterGroup** filter definition into a grouped + * Converts a **UISearchFilter** filter definition into a grouped * search query of type **SearchFilters** * * Legacy support remains for the old **SearchFilter[]** format. @@ -573,49 +573,41 @@ export const buildQueryLegacy = ( */ export function buildQuery(filter: undefined): undefined export function buildQuery( - filter: SearchFilterGroup | LegacyFilter[] + filter: UISearchFilter | LegacyFilter[] ): SearchFilters export function buildQuery( - filter?: SearchFilterGroup | LegacyFilter[] + filter?: UISearchFilter | LegacyFilter[] ): SearchFilters | undefined { if (!filter) { return } - let parsedFilter: SearchFilterGroup + let parsedFilter: UISearchFilter if (Array.isArray(filter)) { parsedFilter = processSearchFilters(filter) } else { parsedFilter = filter } - const operatorMap = { - [FilterGroupLogicalOperator.ALL]: LogicalOperator.AND, - [FilterGroupLogicalOperator.ANY]: LogicalOperator.OR, - } + const operator = logicalOperatorFromUI( + parsedFilter.logicalOperator || UILogicalOperator.ALL + ) + const groups = parsedFilter.groups || [] + const conditions: SearchFilters[] = groups.map(group => { + const filters = group.filters || [] + return { [operator]: { conditions: filters.map(x => buildCondition(x)) } } + }) - const globalOnEmpty = parsedFilter.onEmptyFilter - ? parsedFilter.onEmptyFilter - : null - - const globalOperator = operatorMap[parsedFilter.logicalOperator] - - const ret = { - ...(globalOnEmpty ? { onEmptyFilter: globalOnEmpty } : {}), - [globalOperator]: { - conditions: parsedFilter.groups?.map(group => { - return { - [operatorMap[group.logicalOperator]]: { - conditions: group.filters - ?.map(x => buildCondition(x)) - .filter(filter => filter), - }, - } - }), - }, + return { + onEmptyFilter: parsedFilter.onEmptyFilter, + [operator]: { conditions }, } +} - return ret +function logicalOperatorFromUI(operator: UILogicalOperator): LogicalOperator { + return operator === UILogicalOperator.ALL + ? LogicalOperator.AND + : LogicalOperator.OR } // The frontend can send single values for array fields sometimes, so to handle diff --git a/packages/shared-core/src/utils.ts b/packages/shared-core/src/utils.ts index 98f7c9b2154..4f3c4f3deb5 100644 --- a/packages/shared-core/src/utils.ts +++ b/packages/shared-core/src/utils.ts @@ -1,13 +1,13 @@ import { LegacyFilter, - SearchFilterGroup, - FilterGroupLogicalOperator, + UISearchFilter, + UILogicalOperator, SearchFilters, BasicOperator, ArrayOperator, isLogicalSearchOperator, EmptyFilterOption, - SearchFilterChild, + SearchFilterGroup, } from "@budibase/types" import * as Constants from "./constants" import { removeKeyNumbering } from "./filters" @@ -132,18 +132,18 @@ export function isSupportedUserSearch(query: SearchFilters) { /** * Processes the filter config. Filters are migrated from - * SearchFilter[] to SearchFilterGroup + * SearchFilter[] to UISearchFilter * * If filters is not an array, the migration is skipped * - * @param {LegacyFilter[] | SearchFilterGroup} filters + * @param {LegacyFilter[] | UISearchFilter} filters */ export const processSearchFilters = ( filters: LegacyFilter[] -): SearchFilterGroup => { +): UISearchFilter => { // Base search config. - const defaultCfg: SearchFilterGroup = { - logicalOperator: FilterGroupLogicalOperator.ALL, + const defaultCfg: UISearchFilter = { + logicalOperator: UILogicalOperator.ALL, onEmptyFilter: EmptyFilterOption.RETURN_ALL, groups: [], } @@ -159,11 +159,11 @@ export const processSearchFilters = ( "formulaType", ] - let baseGroup: SearchFilterChild = { - logicalOperator: FilterGroupLogicalOperator.ALL, + let baseGroup: SearchFilterGroup = { + logicalOperator: UILogicalOperator.ALL, } - return filters.reduce((acc: SearchFilterGroup, filter: LegacyFilter) => { + return filters.reduce((acc: UISearchFilter, filter: LegacyFilter) => { // Sort the properties for easier debugging const filterPropertyKeys = (Object.keys(filter) as (keyof LegacyFilter)[]) .sort((a, b) => { @@ -180,7 +180,7 @@ export const processSearchFilters = ( acc.onEmptyFilter = value } else if (key === "operator" && value === "allOr") { // Group 1 logical operator - baseGroup.logicalOperator = FilterGroupLogicalOperator.ANY + baseGroup.logicalOperator = UILogicalOperator.ANY } return acc diff --git a/packages/types/src/api/web/searchFilter.ts b/packages/types/src/api/web/searchFilter.ts index 305f2bab007..ef35217f9d1 100644 --- a/packages/types/src/api/web/searchFilter.ts +++ b/packages/types/src/api/web/searchFilter.ts @@ -1,9 +1,5 @@ import { FieldType } from "../../documents" -import { - EmptyFilterOption, - FilterGroupLogicalOperator, - SearchFilters, -} from "../../sdk" +import { EmptyFilterOption, UILogicalOperator, SearchFilters } from "../../sdk" export type LegacyFilter = { operator: keyof SearchFilters | "rangeLow" | "rangeHigh" @@ -14,15 +10,15 @@ export type LegacyFilter = { externalType?: string } -export type SearchFilterChild = { - logicalOperator: FilterGroupLogicalOperator - groups?: SearchFilterChild[] +export type SearchFilterGroup = { + logicalOperator?: UILogicalOperator + groups?: SearchFilterGroup[] filters?: LegacyFilter[] } // this is a type purely used by the UI -export type SearchFilterGroup = { - logicalOperator: FilterGroupLogicalOperator - onEmptyFilter: EmptyFilterOption - groups: SearchFilterChild[] +export type UISearchFilter = { + logicalOperator?: UILogicalOperator + onEmptyFilter?: EmptyFilterOption + groups?: SearchFilterGroup[] } diff --git a/packages/types/src/documents/app/view.ts b/packages/types/src/documents/app/view.ts index 1b2372da857..3ae120fccac 100644 --- a/packages/types/src/documents/app/view.ts +++ b/packages/types/src/documents/app/view.ts @@ -1,4 +1,4 @@ -import { LegacyFilter, SearchFilterGroup, SortOrder, SortType } from "../../api" +import { LegacyFilter, UISearchFilter, SortOrder, SortType } from "../../api" import { UIFieldMetadata } from "./table" import { Document } from "../document" import { DBView, SearchFilters } from "../../sdk" @@ -92,7 +92,7 @@ export interface ViewV2 { tableId: string query?: LegacyFilter[] | SearchFilters // duplicate to store UI information about filters - queryUI?: SearchFilterGroup + queryUI?: UISearchFilter sort?: { field: string order?: SortOrder diff --git a/packages/types/src/sdk/search.ts b/packages/types/src/sdk/search.ts index d64e87d4347..a2d4b4760fd 100644 --- a/packages/types/src/sdk/search.ts +++ b/packages/types/src/sdk/search.ts @@ -203,7 +203,7 @@ export enum EmptyFilterOption { RETURN_NONE = "none", } -export enum FilterGroupLogicalOperator { +export enum UILogicalOperator { ALL = "all", ANY = "any", } From 3981cecc30208ca7d34fa31c62bc97cf5a946a89 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 18 Oct 2024 11:32:58 +0100 Subject: [PATCH 06/20] Remove buildQueryLegacy --- packages/server/src/sdk/app/rows/search.ts | 10 +- packages/shared-core/src/filters.ts | 128 --------------------- packages/types/src/api/web/searchFilter.ts | 11 +- 3 files changed, 18 insertions(+), 131 deletions(-) diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index 6abfe0c6818..caf85a78581 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -5,6 +5,7 @@ import { Row, RowSearchParams, SearchFilterKey, + SearchFilters, SearchResponse, SortOrder, Table, @@ -90,11 +91,16 @@ export async function search( if (options.viewId) { const view = source as ViewV2 + // Enrich saved query with ephemeral query params. // We prevent searching on any fields that are saved as part of the query, as // that could let users find rows they should not be allowed to access. - let viewQuery = await enrichSearchContext(view.query || {}, context) - viewQuery = dataFilters.buildQueryLegacy(viewQuery) || {} + let viewQuery = (await enrichSearchContext(view.query || {}, context)) as + | SearchFilters + | LegacyFilter[] + if (Array.isArray(viewQuery)) { + viewQuery = dataFilters.buildQuery(viewQuery) + } viewQuery = checkFilters(table, viewQuery) const sqsEnabled = await features.flags.isEnabled("SQS") diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 932d5a8ca02..eda1139c5ed 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -432,134 +432,6 @@ const buildCondition = (expression: LegacyFilter) => { return query } -export const buildQueryLegacy = ( - filter?: LegacyFilter[] | SearchFilters -): SearchFilters | undefined => { - // this is of type SearchFilters or is undefined - if (!Array.isArray(filter)) { - return filter - } - - let query: SearchFilters = { - string: {}, - fuzzy: {}, - range: {}, - equal: {}, - notEqual: {}, - empty: {}, - notEmpty: {}, - contains: {}, - notContains: {}, - oneOf: {}, - containsAny: {}, - } - - if (!Array.isArray(filter)) { - return query - } - - filter.forEach(expression => { - let { operator, field, type, value, externalType, onEmptyFilter } = - expression - const queryOperator = operator as SearchFilterOperator - const isHbs = - typeof value === "string" && (value.match(HBS_REGEX) || []).length > 0 - // Parse all values into correct types - if (operator === "allOr") { - query.allOr = true - return - } - if (onEmptyFilter) { - query.onEmptyFilter = onEmptyFilter - return - } - if ( - type === "datetime" && - !isHbs && - queryOperator !== "empty" && - queryOperator !== "notEmpty" - ) { - // Ensure date value is a valid date and parse into correct format - if (!value) { - return - } - try { - value = new Date(value).toISOString() - } catch (error) { - return - } - } - if (type === "number" && typeof value === "string" && !isHbs) { - if (queryOperator === "oneOf") { - value = value.split(",").map(item => parseFloat(item)) - } else { - value = parseFloat(value) - } - } - if (type === "boolean") { - value = `${value}`?.toLowerCase() === "true" - } - if ( - ["contains", "notContains", "containsAny"].includes( - operator.toLocaleString() - ) && - type === "array" && - typeof value === "string" - ) { - value = value.split(",") - } - if (operator.toLocaleString().startsWith("range") && query.range) { - const minint = - SqlNumberTypeRangeMap[ - externalType as keyof typeof SqlNumberTypeRangeMap - ]?.min || Number.MIN_SAFE_INTEGER - const maxint = - SqlNumberTypeRangeMap[ - externalType as keyof typeof SqlNumberTypeRangeMap - ]?.max || Number.MAX_SAFE_INTEGER - if (!query.range[field]) { - query.range[field] = { - low: type === "number" ? minint : "0000-00-00T00:00:00.000Z", - high: type === "number" ? maxint : "9999-00-00T00:00:00.000Z", - } - } - if (operator === "rangeLow" && value != null && value !== "") { - query.range[field] = { - ...query.range[field], - low: value, - } - } else if (operator === "rangeHigh" && value != null && value !== "") { - query.range[field] = { - ...query.range[field], - high: value, - } - } - } else if (isLogicalSearchOperator(queryOperator)) { - // ignore - } else if (query[queryOperator] && operator !== "onEmptyFilter") { - if (type === "boolean") { - // Transform boolean filters to cope with null. - // "equals false" needs to be "not equals true" - // "not equals false" needs to be "equals true" - if (queryOperator === "equal" && value === false) { - query.notEqual = query.notEqual || {} - query.notEqual[field] = true - } else if (queryOperator === "notEqual" && value === false) { - query.equal = query.equal || {} - query.equal[field] = true - } else { - query[queryOperator] ??= {} - query[queryOperator]![field] = value - } - } else { - query[queryOperator] ??= {} - query[queryOperator]![field] = value - } - } - }) - return query -} - /** * Converts a **UISearchFilter** filter definition into a grouped * search query of type **SearchFilters** diff --git a/packages/types/src/api/web/searchFilter.ts b/packages/types/src/api/web/searchFilter.ts index ef35217f9d1..428d5d364af 100644 --- a/packages/types/src/api/web/searchFilter.ts +++ b/packages/types/src/api/web/searchFilter.ts @@ -1,6 +1,8 @@ import { FieldType } from "../../documents" import { EmptyFilterOption, UILogicalOperator, SearchFilters } from "../../sdk" +// Prior to v2, this is the type the frontend sent us when filters were +// involved. We convert this to a SearchFilters before use with the search SDK. export type LegacyFilter = { operator: keyof SearchFilters | "rangeLow" | "rangeHigh" onEmptyFilter?: EmptyFilterOption @@ -16,7 +18,14 @@ export type SearchFilterGroup = { filters?: LegacyFilter[] } -// this is a type purely used by the UI +// As of v3, this is the format that the frontend always sends when search +// filters are involved. We convert this to SearchFilters before use with the +// search SDK. +// +// The reason we migrated was that we started to support "logical operators" in +// tests and SearchFilters because a recursive data structure. LegacyFilter[] +// wasn't able to support these sorts of recursive structures, so we changed the +// format. export type UISearchFilter = { logicalOperator?: UILogicalOperator onEmptyFilter?: EmptyFilterOption From 0fce17c3c06198505f8cddcf4c8f94d1511cacbb Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 18 Oct 2024 14:50:06 +0100 Subject: [PATCH 07/20] Fix allOr for everything except SQS. --- .../src/api/routes/tests/viewV2.spec.ts | 20 +++ packages/server/src/sdk/app/rows/search.ts | 9 +- packages/shared-core/src/filters.ts | 83 ++++++++----- packages/shared-core/src/utils.ts | 115 ++++-------------- packages/types/src/api/web/searchFilter.ts | 38 ++++-- 5 files changed, 135 insertions(+), 130 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index fa1f1690f66..6ba6fb8cb8e 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -3756,6 +3756,26 @@ describe.each([ }), expected: [], }, + { + name: "allOr", + insert: [{ string: "foo" }, { string: "bar" }], + query: simpleQuery( + { + operator: BasicOperator.EQUAL, + field: "string", + value: "foo", + }, + { + operator: BasicOperator.EQUAL, + field: "string", + value: "bar", + }, + { + operator: "allOr", + } + ), + expected: [{ string: "foo" }, { string: "bar" }], + }, ] it.only.each(testCases)( diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index caf85a78581..ee16058416d 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -114,11 +114,12 @@ export async function search( ? view.query : [] + const { filters } = dataFilters.splitFiltersArray(queryFilters) + // Extract existing fields - const existingFields = - queryFilters - ?.filter(filter => filter.field) - .map(filter => db.removeKeyNumbering(filter.field)) || [] + const existingFields = filters.map(filter => + db.removeKeyNumbering(filter.field) + ) // Carry over filters for unused fields Object.keys(options.query).forEach(key => { diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index eda1139c5ed..2d01c243638 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -24,6 +24,7 @@ import { isBasicSearchOperator, isArraySearchOperator, isRangeSearchOperator, + SearchFilter, } from "@budibase/types" import dayjs from "dayjs" import { OperatorOptions, SqlNumberTypeRangeMap } from "./constants" @@ -310,24 +311,17 @@ export class ColumnSplitter { * Builds a JSON query from the filter a SearchFilter definition * @param filter the builder filter structure */ -const buildCondition = (expression: LegacyFilter) => { - const query: SearchFilters = {} - const { operator, field, type, externalType, onEmptyFilter } = expression - let { value } = expression - - if (!operator || !field) { - return - } - if (operator === "allOr") { - query.allOr = true +function buildCondition(filter: undefined): undefined +function buildCondition(filter: SearchFilter): SearchFilters +function buildCondition(filter?: SearchFilter): SearchFilters | undefined { + if (!filter) { return } - if (onEmptyFilter) { - query.onEmptyFilter = onEmptyFilter - return - } + const query: SearchFilters = {} + const { operator, field, type, externalType } = filter + let { value } = filter // Default the value for noValue fields to ensure they are correctly added // to the final query @@ -432,16 +426,36 @@ const buildCondition = (expression: LegacyFilter) => { return query } +export interface LegacyFilterSplit { + allOr?: boolean + onEmptyFilter?: EmptyFilterOption + filters: SearchFilter[] +} + +export function splitFiltersArray(filters: LegacyFilter[]) { + const split: LegacyFilterSplit = { + filters: [], + } + + for (const filter of filters) { + if ("operator" in filter && filter.operator === "allOr") { + split.allOr = true + } else if ("onEmptyFilter" in filter) { + split.onEmptyFilter = filter.onEmptyFilter + } else { + split.filters.push(filter) + } + } + + return split +} + /** * Converts a **UISearchFilter** filter definition into a grouped * search query of type **SearchFilters** * * Legacy support remains for the old **SearchFilter[]** format. * These will be migrated to an appropriate **SearchFilters** object, if encountered - * - * @param filter - * - * @returns {SearchFilters} */ export function buildQuery(filter: undefined): undefined export function buildQuery( @@ -454,26 +468,33 @@ export function buildQuery( return } - let parsedFilter: UISearchFilter if (Array.isArray(filter)) { - parsedFilter = processSearchFilters(filter) - } else { - parsedFilter = filter + filter = processSearchFilters(filter) } const operator = logicalOperatorFromUI( - parsedFilter.logicalOperator || UILogicalOperator.ALL + filter.logicalOperator || UILogicalOperator.ALL ) - const groups = parsedFilter.groups || [] - const conditions: SearchFilters[] = groups.map(group => { - const filters = group.filters || [] - return { [operator]: { conditions: filters.map(x => buildCondition(x)) } } - }) - return { - onEmptyFilter: parsedFilter.onEmptyFilter, - [operator]: { conditions }, + const query: SearchFilters = {} + if (filter.onEmptyFilter) { + query.onEmptyFilter = filter.onEmptyFilter } + + query[operator] = { + conditions: (filter.groups || []).map(group => { + const { allOr, onEmptyFilter, filters } = splitFiltersArray( + group.filters || [] + ) + if (onEmptyFilter) { + query.onEmptyFilter = onEmptyFilter + } + const operator = allOr ? LogicalOperator.OR : LogicalOperator.AND + return { [operator]: { conditions: filters.map(buildCondition) } } + }), + } + + return query } function logicalOperatorFromUI(operator: UILogicalOperator): LogicalOperator { diff --git a/packages/shared-core/src/utils.ts b/packages/shared-core/src/utils.ts index 4f3c4f3deb5..dce1b8b960d 100644 --- a/packages/shared-core/src/utils.ts +++ b/packages/shared-core/src/utils.ts @@ -6,15 +6,22 @@ import { BasicOperator, ArrayOperator, isLogicalSearchOperator, - EmptyFilterOption, - SearchFilterGroup, + SearchFilter, } from "@budibase/types" import * as Constants from "./constants" -import { removeKeyNumbering } from "./filters" - -// an array of keys from filter type to properties that are in the type -// this can then be converted using .fromEntries to an object -type AllowedFilters = [keyof LegacyFilter, LegacyFilter[keyof LegacyFilter]][] +import { removeKeyNumbering, splitFiltersArray } from "./filters" +import _ from "lodash" + +const FILTER_ALLOWED_KEYS = [ + "field", + "operator", + "value", + "type", + "externalType", + "valueType", + "noValue", + "formulaType", +] export function unreachable( value: never, @@ -130,88 +137,20 @@ export function isSupportedUserSearch(query: SearchFilters) { return true } -/** - * Processes the filter config. Filters are migrated from - * SearchFilter[] to UISearchFilter - * - * If filters is not an array, the migration is skipped - * - * @param {LegacyFilter[] | UISearchFilter} filters - */ export const processSearchFilters = ( - filters: LegacyFilter[] + filterArray: LegacyFilter[] ): UISearchFilter => { - // Base search config. - const defaultCfg: UISearchFilter = { - logicalOperator: UILogicalOperator.ALL, - onEmptyFilter: EmptyFilterOption.RETURN_ALL, - groups: [], - } - - const filterAllowedKeys = [ - "field", - "operator", - "value", - "type", - "externalType", - "valueType", - "noValue", - "formulaType", - ] - - let baseGroup: SearchFilterGroup = { - logicalOperator: UILogicalOperator.ALL, - } - - return filters.reduce((acc: UISearchFilter, filter: LegacyFilter) => { - // Sort the properties for easier debugging - const filterPropertyKeys = (Object.keys(filter) as (keyof LegacyFilter)[]) - .sort((a, b) => { - return a.localeCompare(b) - }) - .filter(key => filter[key]) - - if (filterPropertyKeys.length == 1) { - const key = filterPropertyKeys[0], - value = filter[key] - // Global - if (key === "onEmptyFilter") { - // unset otherwise - acc.onEmptyFilter = value - } else if (key === "operator" && value === "allOr") { - // Group 1 logical operator - baseGroup.logicalOperator = UILogicalOperator.ANY - } - - return acc - } - - const allowedFilterSettings: AllowedFilters = filterPropertyKeys.reduce( - (acc: AllowedFilters, key) => { - const value = filter[key] - if (filterAllowedKeys.includes(key)) { - if (key === "field") { - acc.push([key, removeKeyNumbering(value)]) - } else { - acc.push([key, value]) - } - } - return acc + const { allOr, onEmptyFilter, filters } = splitFiltersArray(filterArray) + return { + onEmptyFilter, + groups: [ + { + logicalOperator: allOr ? UILogicalOperator.ANY : UILogicalOperator.ALL, + filters: filters.map(filter => { + filter.field = removeKeyNumbering(filter.field) + return _.pick(filter, FILTER_ALLOWED_KEYS) as SearchFilter + }), }, - [] - ) - - const migratedFilter: LegacyFilter = Object.fromEntries( - allowedFilterSettings - ) as LegacyFilter - - baseGroup.filters!.push(migratedFilter) - - if (!acc.groups || !acc.groups.length) { - // init the base group - acc.groups = [baseGroup] - } - - return acc - }, defaultCfg) + ], + } } diff --git a/packages/types/src/api/web/searchFilter.ts b/packages/types/src/api/web/searchFilter.ts index 428d5d364af..b21484f6edb 100644 --- a/packages/types/src/api/web/searchFilter.ts +++ b/packages/types/src/api/web/searchFilter.ts @@ -1,17 +1,41 @@ import { FieldType } from "../../documents" -import { EmptyFilterOption, UILogicalOperator, SearchFilters } from "../../sdk" +import { + EmptyFilterOption, + UILogicalOperator, + BasicOperator, + RangeOperator, + ArrayOperator, +} from "../../sdk" -// Prior to v2, this is the type the frontend sent us when filters were -// involved. We convert this to a SearchFilters before use with the search SDK. -export type LegacyFilter = { - operator: keyof SearchFilters | "rangeLow" | "rangeHigh" - onEmptyFilter?: EmptyFilterOption +type AllOr = { + operator: "allOr" +} + +type OnEmptyFilter = { + onEmptyFilter: EmptyFilterOption +} + +// TODO(samwho): this could be broken down further +export type SearchFilter = { + operator: + | BasicOperator + | RangeOperator + | ArrayOperator + | "rangeLow" + | "rangeHigh" + // Field name will often have a numerical prefix when coming from the frontend, + // use the ColumnSplitter class to remove it. field: string - type?: FieldType value: any + type?: FieldType externalType?: string + noValue?: boolean } +// Prior to v2, this is the type the frontend sent us when filters were +// involved. We convert this to a SearchFilters before use with the search SDK. +export type LegacyFilter = AllOr | OnEmptyFilter | SearchFilter + export type SearchFilterGroup = { logicalOperator?: UILogicalOperator groups?: SearchFilterGroup[] From f53e68f52667777846122ceee9c2508eab09b4ca Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 18 Oct 2024 14:53:20 +0100 Subject: [PATCH 08/20] Fix allOr test --- .../server/src/api/routes/tests/viewV2.spec.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 6ba6fb8cb8e..a738479f7da 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -3712,7 +3712,7 @@ describe.each([ query: UISearchFilter insert: Row[] expected: Row[] - searchOpts?: SearchViewRowRequest + searchOpts?: Partial } function simpleQuery(...filters: LegacyFilter[]): UISearchFilter { @@ -3758,7 +3758,7 @@ describe.each([ }, { name: "allOr", - insert: [{ string: "foo" }, { string: "bar" }], + insert: [{ string: "bar" }, { string: "foo" }], query: simpleQuery( { operator: BasicOperator.EQUAL, @@ -3774,7 +3774,11 @@ describe.each([ operator: "allOr", } ), - expected: [{ string: "foo" }, { string: "bar" }], + searchOpts: { + sort: "string", + sortOrder: SortOrder.ASCENDING, + }, + expected: [{ string: "bar" }, { string: "foo" }], }, ] @@ -3792,10 +3796,10 @@ describe.each([ }, }) - const { rows } = await config.api.viewV2.search( - view.id, - searchOpts - ) + const { rows } = await config.api.viewV2.search(view.id, { + query: {}, + ...searchOpts, + }) expect(rows).toEqual( expected.map(r => expect.objectContaining(r)) ) From 4b6a66627a865ef793d4484def99e9c214af2fbc Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 18 Oct 2024 15:28:03 +0100 Subject: [PATCH 09/20] More view search tests. --- .../src/api/routes/tests/viewV2.spec.ts | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index a738479f7da..a2251c6ffc6 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -3780,6 +3780,81 @@ describe.each([ }, expected: [{ string: "bar" }, { string: "foo" }], }, + { + name: "can find rows with fuzzy search", + insert: [{ string: "foo" }, { string: "bar" }], + query: simpleQuery({ + operator: BasicOperator.FUZZY, + field: "string", + value: "fo", + }), + expected: [{ string: "foo" }], + }, + { + name: "can find nothing with fuzzy search", + insert: [{ string: "foo" }, { string: "bar" }], + query: simpleQuery({ + operator: BasicOperator.FUZZY, + field: "string", + value: "baz", + }), + expected: [], + }, + { + name: "can find numeric rows", + insert: [{ number: 1 }, { number: 2 }], + query: simpleQuery({ + operator: BasicOperator.EQUAL, + field: "number", + value: 1, + }), + expected: [{ number: 1 }], + }, + { + name: "can find numeric values with rangeHigh", + insert: [{ number: 1 }, { number: 2 }, { number: 3 }], + query: simpleQuery({ + operator: "rangeHigh", + field: "number", + value: 2, + }), + searchOpts: { + sort: "number", + sortOrder: SortOrder.ASCENDING, + }, + expected: [{ number: 1 }, { number: 2 }], + }, + { + name: "can find numeric values with rangeLow", + insert: [{ number: 1 }, { number: 2 }, { number: 3 }], + query: simpleQuery({ + operator: "rangeLow", + field: "number", + value: 2, + }), + searchOpts: { + sort: "number", + sortOrder: SortOrder.ASCENDING, + }, + expected: [{ number: 2 }, { number: 3 }], + }, + { + name: "can find numeric values with full range", + insert: [{ number: 1 }, { number: 2 }, { number: 3 }], + query: simpleQuery( + { + operator: "rangeHigh", + field: "number", + value: 2, + }, + { + operator: "rangeLow", + field: "number", + value: 2, + } + ), + expected: [{ number: 2 }], + }, ] it.only.each(testCases)( @@ -3793,6 +3868,16 @@ describe.each([ queryUI: query, schema: { string: { visible: true }, + longform: { visible: true }, + options: { visible: true }, + array: { visible: true }, + number: { visible: true }, + bigint: { visible: true }, + datetime: { visible: true }, + timeOnly: { visible: true }, + boolean: { visible: true }, + user: { visible: true }, + users: { visible: true }, }, }) From 80875fd9a0375ee44598f8cf110b4d5e6ffd246d Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 18 Oct 2024 17:50:08 +0100 Subject: [PATCH 10/20] More tests. --- .../src/api/routes/tests/search.spec.ts | 4 +- .../src/api/routes/tests/viewV2.spec.ts | 137 ++++++++++++++++-- packages/shared-core/src/filters.ts | 10 +- 3 files changed, 133 insertions(+), 18 deletions(-) diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index e0143c59388..f9032b8b610 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -1602,7 +1602,7 @@ describe.each([ }) }) - describe.each([FieldType.ARRAY, FieldType.OPTIONS])("%s", () => { + describe("arrays", () => { beforeAll(async () => { tableOrViewId = await createTableOrView({ numbers: { @@ -2192,7 +2192,7 @@ describe.each([ }) describe("contains", () => { - it("successfully finds a row", async () => { + it.only("successfully finds a row", async () => { await expectQuery({ contains: { users: [user1._id] }, }).toContainExactly([ diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index a2251c6ffc6..52e28c879f3 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -33,6 +33,7 @@ import { UISearchFilter, LegacyFilter, SearchViewRowRequest, + ArrayOperator, } from "@budibase/types" import { generator, mocks } from "@budibase/backend-core/tests" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" @@ -3686,11 +3687,6 @@ describe.each([ number: { name: "number", type: FieldType.NUMBER }, bigint: { name: "bigint", type: FieldType.BIGINT }, datetime: { name: "datetime", type: FieldType.DATETIME }, - timeOnly: { - name: "timeOnly", - type: FieldType.DATETIME, - timeOnly: true, - }, boolean: { name: "boolean", type: FieldType.BOOLEAN }, user: { name: "user", @@ -3701,6 +3697,9 @@ describe.each([ name: "users", type: FieldType.BB_REFERENCE, subtype: BBReferenceFieldSubType.USER, + constraints: { + type: JsonFieldSubType.ARRAY, + }, }, }, }) @@ -3709,9 +3708,9 @@ describe.each([ interface TestCase { name: string - query: UISearchFilter - insert: Row[] - expected: Row[] + query: UISearchFilter | (() => UISearchFilter) + insert: Row[] | (() => Row[]) + expected: Row[] | (() => Row[]) searchOpts?: Partial } @@ -3855,11 +3854,130 @@ describe.each([ ), expected: [{ number: 2 }], }, + { + name: "can find longform values", + insert: [{ longform: "foo" }, { longform: "bar" }], + query: simpleQuery({ + operator: BasicOperator.EQUAL, + field: "longform", + value: "foo", + }), + expected: [{ longform: "foo" }], + }, + { + name: "can find options values", + insert: [{ options: "a" }, { options: "b" }], + query: simpleQuery({ + operator: BasicOperator.EQUAL, + field: "options", + value: "a", + }), + expected: [{ options: "a" }], + }, + { + name: "can find array values", + insert: [ + // Number field here is just to guarantee order. + { number: 1, array: ["a"] }, + { number: 2, array: ["b"] }, + { number: 3, array: ["a", "c"] }, + ], + query: simpleQuery({ + operator: ArrayOperator.CONTAINS, + field: "array", + value: "a", + }), + searchOpts: { + sort: "number", + sortOrder: SortOrder.ASCENDING, + }, + expected: [{ array: ["a"] }, { array: ["a", "c"] }], + }, + { + name: "can find bigint values", + insert: [{ bigint: "1" }, { bigint: "2" }], + query: simpleQuery({ + operator: BasicOperator.EQUAL, + field: "bigint", + type: FieldType.BIGINT, + value: "1", + }), + expected: [{ bigint: "1" }], + }, + { + name: "can find datetime values", + insert: [ + { datetime: "2021-01-01T00:00:00.000Z" }, + { datetime: "2021-01-02T00:00:00.000Z" }, + ], + query: simpleQuery({ + operator: BasicOperator.EQUAL, + field: "datetime", + type: FieldType.DATETIME, + value: "2021-01-01", + }), + expected: [{ datetime: "2021-01-01T00:00:00.000Z" }], + }, + { + name: "can find boolean values", + insert: [{ boolean: true }, { boolean: false }], + query: simpleQuery({ + operator: BasicOperator.EQUAL, + field: "boolean", + value: true, + }), + expected: [{ boolean: true }], + }, + { + name: "can find user values", + insert: () => [{ user: config.getUser() }], + query: () => + simpleQuery({ + operator: BasicOperator.EQUAL, + field: "user", + value: config.getUser()._id, + }), + expected: () => [ + { + user: expect.objectContaining({ _id: config.getUser()._id }), + }, + ], + }, + { + name: "can find users values", + insert: () => [{ users: [config.getUser()] }], + query: () => + simpleQuery({ + operator: ArrayOperator.CONTAINS, + field: "users", + value: [config.getUser()._id], + }), + expected: () => [ + { + users: [ + expect.objectContaining({ _id: config.getUser()._id }), + ], + }, + ], + }, ] - it.only.each(testCases)( + it.each(testCases)( "$name", async ({ query, insert, expected, searchOpts }) => { + // Some values can't be specified outside of a test (e.g. getting + // config.getUser(), it won't be initialised), so we use functions + // in those cases. + if (typeof insert === "function") { + insert = insert() + } + if (typeof expected === "function") { + expected = expected() + } + if (typeof query === "function") { + query = query() + } + await config.api.row.bulkImport(table._id!, { rows: insert }) const view = await config.api.viewV2.create({ @@ -3874,7 +3992,6 @@ describe.each([ number: { visible: true }, bigint: { visible: true }, datetime: { visible: true }, - timeOnly: { visible: true }, boolean: { visible: true }, user: { visible: true }, users: { visible: true }, diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index 2d01c243638..efdd3b0df2a 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -339,11 +339,7 @@ function buildCondition(filter?: SearchFilter): SearchFilters | undefined { if (!value) { return } - try { - value = new Date(value).toISOString() - } catch (error) { - return - } + value = new Date(value).toISOString() } break case FieldType.NUMBER: @@ -490,7 +486,9 @@ export function buildQuery( query.onEmptyFilter = onEmptyFilter } const operator = allOr ? LogicalOperator.OR : LogicalOperator.AND - return { [operator]: { conditions: filters.map(buildCondition) } } + return { + [operator]: { conditions: filters.map(buildCondition).filter(f => f) }, + } }), } From 915202badad5df9b43f4c47f3bcdd151150d7bf3 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 21 Oct 2024 09:48:51 +0100 Subject: [PATCH 11/20] More tests. --- .../src/api/routes/tests/viewV2.spec.ts | 41 +++++++++++++++++++ packages/server/src/sdk/app/views/internal.ts | 1 + 2 files changed, 42 insertions(+) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 52e28c879f3..5f9997c189f 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -219,6 +219,47 @@ describe.each([ expect(res).toEqual(expected) }) + it.only("can create a view with just a query field, no queryUI, for backwards compatibility", async () => { + const newView: Required> = { + name: generator.name(), + tableId: table._id!, + primaryDisplay: "id", + query: [ + { + operator: BasicOperator.EQUAL, + field: "field", + value: "value", + }, + ], + sort: { + field: "fieldToSort", + order: SortOrder.DESCENDING, + type: SortType.STRING, + }, + schema: { + id: { visible: true }, + Price: { + visible: true, + }, + }, + } + const res = await config.api.viewV2.create(newView) + + const expected: ViewV2 = { + ...newView, + schema: { + id: { visible: true }, + Price: { + visible: true, + }, + }, + id: expect.any(String), + version: 2, + } + + expect(res).toEqual(expected) + }) + it("persist only UI schema overrides", async () => { const newView: CreateViewRequest = { name: generator.name(), diff --git a/packages/server/src/sdk/app/views/internal.ts b/packages/server/src/sdk/app/views/internal.ts index 33b68759d72..fc6b59fefef 100644 --- a/packages/server/src/sdk/app/views/internal.ts +++ b/packages/server/src/sdk/app/views/internal.ts @@ -41,6 +41,7 @@ export async function create( } ensureQuerySet(view) + ensureQueryUISet(view) const db = context.getAppDB() From 064721cefa16617bf4c6121ee15c4ca7d9897566 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 21 Oct 2024 11:30:28 +0100 Subject: [PATCH 12/20] Make sure you can still create views with a query but no queryUI --- .../server/src/api/routes/tests/viewV2.spec.ts | 14 ++++++++++++++ packages/server/src/sdk/app/views/external.ts | 1 + 2 files changed, 15 insertions(+) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index fdb680c64e3..f1be2c1ae21 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -253,6 +253,20 @@ describe.each([ visible: true, }, }, + queryUI: { + groups: [ + { + logicalOperator: UILogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "field", + value: "value", + }, + ], + }, + ], + }, id: expect.any(String), version: 2, } diff --git a/packages/server/src/sdk/app/views/external.ts b/packages/server/src/sdk/app/views/external.ts index d5251122c9c..2484932232f 100644 --- a/packages/server/src/sdk/app/views/external.ts +++ b/packages/server/src/sdk/app/views/external.ts @@ -50,6 +50,7 @@ export async function create( } ensureQuerySet(view) + ensureQueryUISet(view) const db = context.getAppDB() From 205d6d0671b6041f881630bdd482aba18c3fc6ab Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 21 Oct 2024 11:39:22 +0100 Subject: [PATCH 13/20] Re-enable tests. --- packages/server/src/api/routes/tests/viewV2.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index f1be2c1ae21..8104d124c18 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -219,7 +219,7 @@ describe.each([ expect(res).toEqual(expected) }) - it.only("can create a view with just a query field, no queryUI, for backwards compatibility", async () => { + it("can create a view with just a query field, no queryUI, for backwards compatibility", async () => { const newView: Required> = { name: generator.name(), tableId: table._id!, From 67b814a5cadc73ba87fa97ffa72c5b432b5da27e Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 21 Oct 2024 13:35:53 +0100 Subject: [PATCH 14/20] Yet more tests. --- .../src/api/routes/tests/search.spec.ts | 2 +- .../src/api/routes/tests/viewV2.spec.ts | 238 +++++++++++++++++- packages/server/src/sdk/app/tables/getters.ts | 20 ++ packages/server/src/sdk/app/views/external.ts | 1 + packages/server/src/sdk/app/views/internal.ts | 2 + packages/server/src/sdk/app/views/utils.ts | 8 +- .../server/src/tests/utilities/api/viewV2.ts | 4 +- 7 files changed, 264 insertions(+), 11 deletions(-) diff --git a/packages/server/src/api/routes/tests/search.spec.ts b/packages/server/src/api/routes/tests/search.spec.ts index f9032b8b610..0aa2a80afe5 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -2192,7 +2192,7 @@ describe.each([ }) describe("contains", () => { - it.only("successfully finds a row", async () => { + it("successfully finds a row", async () => { await expectQuery({ contains: { users: [user1._id] }, }).toContainExactly([ diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 8104d124c18..a061a370ba4 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -39,7 +39,7 @@ import { generator, mocks } from "@budibase/backend-core/tests" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" import merge from "lodash/merge" import { quotas } from "@budibase/pro" -import { db, roles, features } from "@budibase/backend-core" +import { db, roles, features, context } from "@budibase/backend-core" describe.each([ ["lucene", undefined], @@ -56,7 +56,8 @@ describe.each([ const isInternal = isSqs || isLucene let table: Table - let datasource: Datasource + let rawDatasource: Datasource | undefined + let datasource: Datasource | undefined let envCleanup: (() => void) | undefined function saveTableRequest( @@ -113,8 +114,9 @@ describe.each([ }) if (dsProvider) { + rawDatasource = await dsProvider datasource = await config.createDatasource({ - datasource: await dsProvider, + datasource: rawDatasource, }) } table = await config.api.table.save(priceTable()) @@ -923,6 +925,7 @@ describe.each([ describe("update", () => { let view: ViewV2 + let table: Table beforeEach(async () => { table = await config.api.table.save(priceTable()) @@ -955,6 +958,21 @@ describe.each([ query: [ { operator: "equal", field: "newField", value: "thatValue" }, ], + // Should also update queryUI because query was not previously set. + queryUI: { + groups: [ + { + logicalOperator: "all", + filters: [ + { + operator: "equal", + field: "newField", + value: "thatValue", + }, + ], + }, + ], + }, schema: expect.anything(), }, }) @@ -974,8 +992,8 @@ describe.each([ query: [ { operator: BasicOperator.EQUAL, - field: generator.word(), - value: generator.word(), + field: "newField", + value: "newValue", }, ], sort: { @@ -999,6 +1017,21 @@ describe.each([ expect((await config.api.table.get(tableId)).views).toEqual({ [view.name]: { ...updatedData, + // queryUI gets generated from query + queryUI: { + groups: [ + { + logicalOperator: "all", + filters: [ + { + operator: "equal", + field: "newField", + value: "newValue", + }, + ], + }, + ], + }, schema: { ...table.schema, id: expect.objectContaining({ @@ -1244,6 +1277,145 @@ describe.each([ ) }) + it("can update queryUI field and query gets regenerated", async () => { + await config.api.viewV2.update({ + ...view, + queryUI: { + groups: [ + { + logicalOperator: UILogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "field", + value: "value", + }, + ], + }, + ], + }, + }) + + let updatedView = await config.api.viewV2.get(view.id) + expect(updatedView.query).toEqual({ + $and: { + conditions: [ + { + $and: { + conditions: [ + { + equal: { field: "value" }, + }, + ], + }, + }, + ], + }, + }) + + await config.api.viewV2.update({ + ...updatedView, + queryUI: { + groups: [ + { + logicalOperator: UILogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "newField", + value: "newValue", + }, + ], + }, + ], + }, + }) + + updatedView = await config.api.viewV2.get(view.id) + expect(updatedView.query).toEqual({ + $and: { + conditions: [ + { + $and: { + conditions: [ + { + equal: { newField: "newValue" }, + }, + ], + }, + }, + ], + }, + }) + }) + + it("can delete either query and it will get regenerated from queryUI", async () => { + await config.api.viewV2.update({ + ...view, + query: [ + { + operator: BasicOperator.EQUAL, + field: "field", + value: "value", + }, + ], + }) + + let updatedView = await config.api.viewV2.get(view.id) + expect(updatedView.queryUI).toBeDefined() + + await config.api.viewV2.update({ + ...updatedView, + query: undefined, + }) + + updatedView = await config.api.viewV2.get(view.id) + expect(updatedView.query).toBeDefined() + }) + + // This is because the conversion from queryUI -> query loses data, so you + // can't accurately reproduce the original queryUI from the query. If + // query is a LegacyFilter[] we allow it, because for Budibase v3 + // everything in the db had query set to a LegacyFilter[], and there's no + // loss of information converting from a LegacyFilter[] to a + // UISearchFilter. But we convert to a SearchFilters and that can't be + // accurately converted to a UISearchFilter. + it("can't regenerate queryUI from a query once it has been generated from a queryUI", async () => { + await config.api.viewV2.update({ + ...view, + queryUI: { + groups: [ + { + logicalOperator: UILogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "field", + value: "value", + }, + ], + }, + ], + }, + }) + + let updatedView = await config.api.viewV2.get(view.id) + expect(updatedView.query).toBeDefined() + + await config.api.viewV2.update( + { + ...updatedView, + queryUI: undefined, + }, + { + status: 400, + body: { + message: "view is missing queryUI field", + }, + } + ) + }) + !isLucene && describe("calculation views", () => { let table: Table @@ -1597,6 +1769,62 @@ describe.each([ expect.objectContaining({ visible: true, readonly: true }) ) }) + + it("should fill in the queryUI field if it's missing", async () => { + const res = await config.api.viewV2.create({ + name: generator.name(), + tableId: tableId, + query: [ + { + operator: BasicOperator.EQUAL, + field: "one", + value: "1", + }, + ], + schema: { + id: { visible: true }, + one: { visible: true }, + }, + }) + + const table = await config.api.table.get(tableId) + const rawView = table.views![res.name] as ViewV2 + delete rawView.queryUI + + await context.doInAppContext(config.getAppId(), async () => { + const db = context.getAppDB() + + if (!rawDatasource) { + await db.put(table) + } else { + const ds = await config.api.datasource.get(datasource!._id!) + ds.entities![table.name] = table + const updatedDs = { + ...rawDatasource, + _id: ds._id, + _rev: ds._rev, + entities: ds.entities, + } + await db.put(updatedDs) + } + }) + + const view = await getDelegate(res) + expect(view.queryUI).toEqual({ + groups: [ + { + logicalOperator: UILogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "one", + value: "1", + }, + ], + }, + ], + }) + }) }) describe("updating table schema", () => { diff --git a/packages/server/src/sdk/app/tables/getters.ts b/packages/server/src/sdk/app/tables/getters.ts index 49944bce850..fa87d3cfadf 100644 --- a/packages/server/src/sdk/app/tables/getters.ts +++ b/packages/server/src/sdk/app/tables/getters.ts @@ -12,9 +12,26 @@ import { TableResponse, TableSourceType, TableViewsResponse, + View, + ViewV2, } from "@budibase/types" import datasources from "../datasources" import sdk from "../../../sdk" +import { ensureQueryUISet } from "../views/utils" +import { isV2 } from "../views" + +function processView(view: ViewV2 | View) { + if (!isV2(view)) { + return + } + ensureQueryUISet(view) +} + +function processViews(views: (ViewV2 | View)[]) { + for (const view of views) { + processView(view) + } +} export async function processTable(table: Table): Promise { if (!table) { @@ -22,6 +39,9 @@ export async function processTable(table: Table): Promise
{ } table = { ...table } + if (table.views) { + processViews(Object.values(table.views)) + } if (table._id && isExternalTableID(table._id)) { // Old created external tables via Budibase might have a missing field name breaking some UI such as filters if (table.schema["id"] && !table.schema["id"].name) { diff --git a/packages/server/src/sdk/app/views/external.ts b/packages/server/src/sdk/app/views/external.ts index 2484932232f..74f2248f58a 100644 --- a/packages/server/src/sdk/app/views/external.ts +++ b/packages/server/src/sdk/app/views/external.ts @@ -81,6 +81,7 @@ export async function update(tableId: string, view: ViewV2): Promise { } ensureQuerySet(view) + ensureQueryUISet(view) delete views[existingView.name] views[view.name] = view diff --git a/packages/server/src/sdk/app/views/internal.ts b/packages/server/src/sdk/app/views/internal.ts index fc6b59fefef..efac4a9f8a8 100644 --- a/packages/server/src/sdk/app/views/internal.ts +++ b/packages/server/src/sdk/app/views/internal.ts @@ -5,6 +5,7 @@ import sdk from "../../../sdk" import * as utils from "../../../db/utils" import { enrichSchema, isV2 } from "." import { ensureQuerySet, ensureQueryUISet } from "./utils" +import { processTable } from "../tables/getters" export async function get(viewId: string): Promise { const { tableId } = utils.extractViewInfoFromID(viewId) @@ -70,6 +71,7 @@ export async function update(tableId: string, view: ViewV2): Promise { } ensureQuerySet(view) + ensureQueryUISet(view) delete table.views[existingView.name] table.views[view.name] = view diff --git a/packages/server/src/sdk/app/views/utils.ts b/packages/server/src/sdk/app/views/utils.ts index a110acf0722..ec942c617a5 100644 --- a/packages/server/src/sdk/app/views/utils.ts +++ b/packages/server/src/sdk/app/views/utils.ts @@ -1,6 +1,7 @@ import { ViewV2 } from "@budibase/types" import { utils, dataFilters } from "@budibase/shared-core" import { isPlainObject } from "lodash" +import { HTTPError } from "@budibase/backend-core" function isEmptyObject(obj: any) { return obj && isPlainObject(obj) && Object.keys(obj).length === 0 @@ -21,7 +22,7 @@ export function ensureQueryUISet(view: ViewV2) { // So despite the type saying that `view.query` is a LegacyFilter[] | // SearchFilters, it will never be a SearchFilters when a `view.queryUI` // is specified, making it "safe" to throw an error here. - throw new Error("view is missing queryUI field") + throw new HTTPError("view is missing queryUI field", 400) } view.queryUI = utils.processSearchFilters(view.query) @@ -29,7 +30,10 @@ export function ensureQueryUISet(view: ViewV2) { } export function ensureQuerySet(view: ViewV2) { - if (!view.query && view.queryUI && !isEmptyObject(view.queryUI)) { + // We consider queryUI to be the source of truth, so we don't check for the + // presence of query here. We will overwrite it regardless of whether it is + // present or not. + if (view.queryUI && !isEmptyObject(view.queryUI)) { view.query = dataFilters.buildQuery(view.queryUI) } } diff --git a/packages/server/src/tests/utilities/api/viewV2.ts b/packages/server/src/tests/utilities/api/viewV2.ts index ba07dbe5f04..9741240f27b 100644 --- a/packages/server/src/tests/utilities/api/viewV2.ts +++ b/packages/server/src/tests/utilities/api/viewV2.ts @@ -10,9 +10,7 @@ import { Expectations, TestAPI } from "./base" export class ViewV2API extends TestAPI { create = async ( - // The frontend changed in v3 from sending query to sending only queryUI, - // making sure tests reflect that. - view: Omit, + view: CreateViewRequest, expectations?: Expectations ): Promise => { const exp: Expectations = { From 439565486a56b141c4944032c1f82cd881c40e70 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 21 Oct 2024 13:41:28 +0100 Subject: [PATCH 15/20] Fix lint. --- packages/server/src/sdk/app/views/internal.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/server/src/sdk/app/views/internal.ts b/packages/server/src/sdk/app/views/internal.ts index efac4a9f8a8..77a81a16231 100644 --- a/packages/server/src/sdk/app/views/internal.ts +++ b/packages/server/src/sdk/app/views/internal.ts @@ -5,7 +5,6 @@ import sdk from "../../../sdk" import * as utils from "../../../db/utils" import { enrichSchema, isV2 } from "." import { ensureQuerySet, ensureQueryUISet } from "./utils" -import { processTable } from "../tables/getters" export async function get(viewId: string): Promise { const { tableId } = utils.extractViewInfoFromID(viewId) From f0fd81b752104b0b5f282e1fb3943ce9ebdf988f Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 21 Oct 2024 15:41:27 +0100 Subject: [PATCH 16/20] Update pro reference. --- packages/pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pro b/packages/pro index fc4c7f49251..297fdc937e9 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit fc4c7f4925139af078480217965c3d6338dc0a7f +Subproject commit 297fdc937e9c650b4964fc1a942b60022b195865 From 49477330c23f635b2157d1329d247349f0a9adcd Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Mon, 21 Oct 2024 16:18:39 +0100 Subject: [PATCH 17/20] Fix broken frontend test. --- .../backend/DataTable/modals/ExportModal.test.js | 2 ++ packages/shared-core/src/utils.ts | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/builder/src/components/backend/DataTable/modals/ExportModal.test.js b/packages/builder/src/components/backend/DataTable/modals/ExportModal.test.js index a5d24d74355..4927f51a8d5 100644 --- a/packages/builder/src/components/backend/DataTable/modals/ExportModal.test.js +++ b/packages/builder/src/components/backend/DataTable/modals/ExportModal.test.js @@ -79,6 +79,8 @@ describe("Export Modal", () => { props: propsCfg, }) + expect(propsCfg.filters[0].field).toBe("1:Cost") + expect(screen.getByTestId("filters-applied")).toBeVisible() expect(screen.getByTestId("filters-applied").textContent).toBe( "Filters applied" diff --git a/packages/shared-core/src/utils.ts b/packages/shared-core/src/utils.ts index dce1b8b960d..2bfd166414d 100644 --- a/packages/shared-core/src/utils.ts +++ b/packages/shared-core/src/utils.ts @@ -147,8 +147,12 @@ export const processSearchFilters = ( { logicalOperator: allOr ? UILogicalOperator.ANY : UILogicalOperator.ALL, filters: filters.map(filter => { - filter.field = removeKeyNumbering(filter.field) - return _.pick(filter, FILTER_ALLOWED_KEYS) as SearchFilter + const trimmedFilter = _.pick( + filter, + FILTER_ALLOWED_KEYS + ) as SearchFilter + trimmedFilter.field = removeKeyNumbering(trimmedFilter.field) + return trimmedFilter }), }, ], From 2cd3ab662793b19b182b3ef9aa540fbe7b97d742 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 22 Oct 2024 10:35:09 +0100 Subject: [PATCH 18/20] Respond to PR comments. --- packages/shared-core/src/utils.ts | 2 +- packages/types/src/api/web/searchFilter.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/shared-core/src/utils.ts b/packages/shared-core/src/utils.ts index 2bfd166414d..0af27d4e81f 100644 --- a/packages/shared-core/src/utils.ts +++ b/packages/shared-core/src/utils.ts @@ -12,7 +12,7 @@ import * as Constants from "./constants" import { removeKeyNumbering, splitFiltersArray } from "./filters" import _ from "lodash" -const FILTER_ALLOWED_KEYS = [ +const FILTER_ALLOWED_KEYS: (keyof SearchFilter)[] = [ "field", "operator", "value", diff --git a/packages/types/src/api/web/searchFilter.ts b/packages/types/src/api/web/searchFilter.ts index b21484f6edb..7a6920fb607 100644 --- a/packages/types/src/api/web/searchFilter.ts +++ b/packages/types/src/api/web/searchFilter.ts @@ -30,6 +30,8 @@ export type SearchFilter = { type?: FieldType externalType?: string noValue?: boolean + valueType?: string + formulaType?: string } // Prior to v2, this is the type the frontend sent us when filters were From 6f4e1dd711a8d0e85078b9ebcbb1bae10dbec994 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 23 Oct 2024 11:34:55 +0100 Subject: [PATCH 19/20] Respond to PR comment. --- .../src/api/routes/tests/viewV2.spec.ts | 167 +++++++++--------- packages/server/src/sdk/app/tables/getters.ts | 22 +-- packages/server/src/sdk/app/views/external.ts | 21 +-- packages/server/src/sdk/app/views/internal.ts | 21 +-- packages/server/src/sdk/app/views/utils.ts | 10 +- packages/shared-core/src/filters.ts | 2 + packages/shared-core/src/utils.ts | 6 +- 7 files changed, 122 insertions(+), 127 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index a061a370ba4..2af11b513bc 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -27,13 +27,14 @@ import { ViewV2Schema, ViewV2Type, JsonTypes, - UILogicalOperator, EmptyFilterOption, JsonFieldSubType, UISearchFilter, LegacyFilter, SearchViewRowRequest, ArrayOperator, + UILogicalOperator, + SearchFilters, } from "@budibase/types" import { generator, mocks } from "@budibase/backend-core/tests" import { DatabaseName, getDatasource } from "../../../integrations/tests/utils" @@ -159,11 +160,8 @@ describe.each([ tableId: table._id!, primaryDisplay: "id", queryUI: { - logicalOperator: UILogicalOperator.ALL, - onEmptyFilter: EmptyFilterOption.RETURN_ALL, groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -256,6 +254,8 @@ describe.each([ }, }, queryUI: { + logicalOperator: UILogicalOperator.ALL, + onEmptyFilter: EmptyFilterOption.RETURN_ALL, groups: [ { logicalOperator: UILogicalOperator.ALL, @@ -952,29 +952,37 @@ describe.each([ ], }) - expect((await config.api.table.get(tableId)).views).toEqual({ - [view.name]: { - ...view, - query: [ - { operator: "equal", field: "newField", value: "thatValue" }, - ], - // Should also update queryUI because query was not previously set. - queryUI: { - groups: [ - { - logicalOperator: "all", - filters: [ - { - operator: "equal", - field: "newField", - value: "thatValue", - }, - ], - }, - ], + const expected: ViewV2 = { + ...view, + query: [ + { + operator: BasicOperator.EQUAL, + field: "newField", + value: "thatValue", }, - schema: expect.anything(), + ], + // Should also update queryUI because query was not previously set. + queryUI: { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + logicalOperator: UILogicalOperator.ALL, + groups: [ + { + logicalOperator: UILogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "newField", + value: "thatValue", + }, + ], + }, + ], }, + schema: expect.anything(), + } + + expect((await config.api.table.get(tableId)).views).toEqual({ + [view.name]: expected, }) }) @@ -1014,38 +1022,42 @@ describe.each([ } await config.api.viewV2.update(updatedData) - expect((await config.api.table.get(tableId)).views).toEqual({ - [view.name]: { - ...updatedData, - // queryUI gets generated from query - queryUI: { - groups: [ - { - logicalOperator: "all", - filters: [ - { - operator: "equal", - field: "newField", - value: "newValue", - }, - ], - }, - ], - }, - schema: { - ...table.schema, - id: expect.objectContaining({ - visible: true, - }), - Category: expect.objectContaining({ - visible: false, - }), - Price: expect.objectContaining({ - visible: true, - readonly: true, - }), - }, + const expected: ViewV2 = { + ...updatedData, + // queryUI gets generated from query + queryUI: { + logicalOperator: UILogicalOperator.ALL, + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + groups: [ + { + logicalOperator: UILogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "newField", + value: "newValue", + }, + ], + }, + ], + }, + schema: { + ...table.schema, + id: expect.objectContaining({ + visible: true, + }), + Category: expect.objectContaining({ + visible: false, + }), + Price: expect.objectContaining({ + visible: true, + readonly: true, + }), }, + } + + expect((await config.api.table.get(tableId)).views).toEqual({ + [view.name]: expected, }) }) @@ -1283,7 +1295,6 @@ describe.each([ queryUI: { groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -1297,7 +1308,8 @@ describe.each([ }) let updatedView = await config.api.viewV2.get(view.id) - expect(updatedView.query).toEqual({ + let expected: SearchFilters = { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, $and: { conditions: [ { @@ -1311,14 +1323,14 @@ describe.each([ }, ], }, - }) + } + expect(updatedView.query).toEqual(expected) await config.api.viewV2.update({ ...updatedView, queryUI: { groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -1332,7 +1344,8 @@ describe.each([ }) updatedView = await config.api.viewV2.get(view.id) - expect(updatedView.query).toEqual({ + expected = { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, $and: { conditions: [ { @@ -1346,7 +1359,8 @@ describe.each([ }, ], }, - }) + } + expect(updatedView.query).toEqual(expected) }) it("can delete either query and it will get regenerated from queryUI", async () => { @@ -1386,7 +1400,6 @@ describe.each([ queryUI: { groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -1810,7 +1823,9 @@ describe.each([ }) const view = await getDelegate(res) - expect(view.queryUI).toEqual({ + const expected: UISearchFilter = { + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + logicalOperator: UILogicalOperator.ALL, groups: [ { logicalOperator: UILogicalOperator.ALL, @@ -1823,7 +1838,8 @@ describe.each([ ], }, ], - }) + } + expect(view.queryUI).toEqual(expected) }) }) @@ -2669,11 +2685,8 @@ describe.each([ tableId: table._id!, name: generator.guid(), queryUI: { - onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -2733,11 +2746,8 @@ describe.each([ tableId: table._id!, name: generator.guid(), queryUI: { - onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -3022,11 +3032,8 @@ describe.each([ tableId: table._id!, name: generator.guid(), queryUI: { - onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.NOT_EQUAL, @@ -3080,11 +3087,8 @@ describe.each([ tableId: table._id!, name: generator.guid(), queryUI: { - onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.NOT_EQUAL, @@ -3175,11 +3179,8 @@ describe.each([ tableId: table._id!, name: generator.guid(), queryUI: { - onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -3619,11 +3620,8 @@ describe.each([ name: generator.guid(), type: ViewV2Type.CALCULATION, queryUI: { - onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, @@ -3912,11 +3910,8 @@ describe.each([ tableId: table._id!, name: generator.guid(), queryUI: { - onEmptyFilter: EmptyFilterOption.RETURN_ALL, - logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: UILogicalOperator.ALL, filters: [ { operator: BasicOperator.EQUAL, diff --git a/packages/server/src/sdk/app/tables/getters.ts b/packages/server/src/sdk/app/tables/getters.ts index 270ae7fee09..a8ad606647f 100644 --- a/packages/server/src/sdk/app/tables/getters.ts +++ b/packages/server/src/sdk/app/tables/getters.ts @@ -12,8 +12,6 @@ import { TableResponse, TableSourceType, TableViewsResponse, - View, - ViewV2, FeatureFlag, } from "@budibase/types" import datasources from "../datasources" @@ -21,19 +19,6 @@ import sdk from "../../../sdk" import { ensureQueryUISet } from "../views/utils" import { isV2 } from "../views" -function processView(view: ViewV2 | View) { - if (!isV2(view)) { - return - } - ensureQueryUISet(view) -} - -function processViews(views: (ViewV2 | View)[]) { - for (const view of views) { - processView(view) - } -} - export async function processTable(table: Table): Promise
{ if (!table) { return table @@ -41,7 +26,12 @@ export async function processTable(table: Table): Promise
{ table = { ...table } if (table.views) { - processViews(Object.values(table.views)) + for (const [key, view] of Object.entries(table.views)) { + if (!isV2(view)) { + continue + } + table.views[key] = ensureQueryUISet(view) + } } if (table._id && isExternalTableID(table._id)) { // Old created external tables via Budibase might have a missing field name breaking some UI such as filters diff --git a/packages/server/src/sdk/app/views/external.ts b/packages/server/src/sdk/app/views/external.ts index 74f2248f58a..bee153a9107 100644 --- a/packages/server/src/sdk/app/views/external.ts +++ b/packages/server/src/sdk/app/views/external.ts @@ -19,8 +19,7 @@ export async function get(viewId: string): Promise { if (!found) { throw new Error("No view found") } - ensureQueryUISet(found) - return found + return ensureQueryUISet(found) } export async function getEnriched(viewId: string): Promise { @@ -35,22 +34,21 @@ export async function getEnriched(viewId: string): Promise { if (!found) { throw new Error("No view found") } - ensureQueryUISet(found) - return await enrichSchema(found, table.schema) + return await enrichSchema(ensureQueryUISet(found), table.schema) } export async function create( tableId: string, viewRequest: Omit ): Promise { - const view: ViewV2 = { + let view: ViewV2 = { ...viewRequest, id: utils.generateViewID(tableId), version: 2, } - ensureQuerySet(view) - ensureQueryUISet(view) + view = ensureQuerySet(view) + view = ensureQueryUISet(view) const db = context.getAppDB() @@ -62,7 +60,10 @@ export async function create( return view } -export async function update(tableId: string, view: ViewV2): Promise { +export async function update( + tableId: string, + view: Readonly +): Promise { const db = context.getAppDB() const { datasourceId, tableName } = breakExternalTableId(tableId) @@ -80,8 +81,8 @@ export async function update(tableId: string, view: ViewV2): Promise { throw new HTTPError(`Cannot update view type after creation`, 400) } - ensureQuerySet(view) - ensureQueryUISet(view) + view = ensureQuerySet(view) + view = ensureQueryUISet(view) delete views[existingView.name] views[view.name] = view diff --git a/packages/server/src/sdk/app/views/internal.ts b/packages/server/src/sdk/app/views/internal.ts index 77a81a16231..63807bcfd44 100644 --- a/packages/server/src/sdk/app/views/internal.ts +++ b/packages/server/src/sdk/app/views/internal.ts @@ -14,8 +14,7 @@ export async function get(viewId: string): Promise { if (!found) { throw new Error("No view found") } - ensureQueryUISet(found) - return found + return ensureQueryUISet(found) } export async function getEnriched(viewId: string): Promise { @@ -26,22 +25,21 @@ export async function getEnriched(viewId: string): Promise { if (!found) { throw new Error("No view found") } - ensureQueryUISet(found) - return await enrichSchema(found, table.schema) + return await enrichSchema(ensureQueryUISet(found), table.schema) } export async function create( tableId: string, viewRequest: Omit ): Promise { - const view: ViewV2 = { + let view: ViewV2 = { ...viewRequest, id: utils.generateViewID(tableId), version: 2, } - ensureQuerySet(view) - ensureQueryUISet(view) + view = ensureQuerySet(view) + view = ensureQueryUISet(view) const db = context.getAppDB() @@ -53,7 +51,10 @@ export async function create( return view } -export async function update(tableId: string, view: ViewV2): Promise { +export async function update( + tableId: string, + view: Readonly +): Promise { const db = context.getAppDB() const table = await sdk.tables.getTable(tableId) table.views ??= {} @@ -69,8 +70,8 @@ export async function update(tableId: string, view: ViewV2): Promise { throw new HTTPError(`Cannot update view type after creation`, 400) } - ensureQuerySet(view) - ensureQueryUISet(view) + view = ensureQuerySet(view) + view = ensureQueryUISet(view) delete table.views[existingView.name] table.views[view.name] = view diff --git a/packages/server/src/sdk/app/views/utils.ts b/packages/server/src/sdk/app/views/utils.ts index ec942c617a5..8ca5fd1098f 100644 --- a/packages/server/src/sdk/app/views/utils.ts +++ b/packages/server/src/sdk/app/views/utils.ts @@ -1,13 +1,14 @@ import { ViewV2 } from "@budibase/types" import { utils, dataFilters } from "@budibase/shared-core" -import { isPlainObject } from "lodash" +import { cloneDeep, isPlainObject } from "lodash" import { HTTPError } from "@budibase/backend-core" function isEmptyObject(obj: any) { return obj && isPlainObject(obj) && Object.keys(obj).length === 0 } -export function ensureQueryUISet(view: ViewV2) { +export function ensureQueryUISet(viewArg: Readonly): ViewV2 { + const view = cloneDeep(viewArg) if (!view.queryUI && view.query && !isEmptyObject(view.query)) { if (!Array.isArray(view.query)) { // In practice this should not happen. `view.query`, at the time this code @@ -27,13 +28,16 @@ export function ensureQueryUISet(view: ViewV2) { view.queryUI = utils.processSearchFilters(view.query) } + return view } -export function ensureQuerySet(view: ViewV2) { +export function ensureQuerySet(viewArg: Readonly): ViewV2 { + const view = cloneDeep(viewArg) // We consider queryUI to be the source of truth, so we don't check for the // presence of query here. We will overwrite it regardless of whether it is // present or not. if (view.queryUI && !isEmptyObject(view.queryUI)) { view.query = dataFilters.buildQuery(view.queryUI) } + return view } diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index efdd3b0df2a..a5d4e7ca4de 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -475,6 +475,8 @@ export function buildQuery( const query: SearchFilters = {} if (filter.onEmptyFilter) { query.onEmptyFilter = filter.onEmptyFilter + } else { + query.onEmptyFilter = EmptyFilterOption.RETURN_ALL } query[operator] = { diff --git a/packages/shared-core/src/utils.ts b/packages/shared-core/src/utils.ts index 0af27d4e81f..5db4ead1dc7 100644 --- a/packages/shared-core/src/utils.ts +++ b/packages/shared-core/src/utils.ts @@ -7,6 +7,7 @@ import { ArrayOperator, isLogicalSearchOperator, SearchFilter, + EmptyFilterOption, } from "@budibase/types" import * as Constants from "./constants" import { removeKeyNumbering, splitFiltersArray } from "./filters" @@ -139,10 +140,11 @@ export function isSupportedUserSearch(query: SearchFilters) { export const processSearchFilters = ( filterArray: LegacyFilter[] -): UISearchFilter => { +): Required => { const { allOr, onEmptyFilter, filters } = splitFiltersArray(filterArray) return { - onEmptyFilter, + logicalOperator: UILogicalOperator.ALL, + onEmptyFilter: onEmptyFilter || EmptyFilterOption.RETURN_ALL, groups: [ { logicalOperator: allOr ? UILogicalOperator.ANY : UILogicalOperator.ALL, From ff487735cc4a90e9b7e1cd38e41bc2ab8ebf98f4 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 23 Oct 2024 14:13:43 +0100 Subject: [PATCH 20/20] Update frontend type names to match changes I've made. --- packages/builder/src/stores/builder/viewsV2.js | 8 ++++---- .../frontend-core/src/components/CoreFilterBuilder.svelte | 4 ++-- .../frontend-core/src/components/grid/stores/filter.js | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/builder/src/stores/builder/viewsV2.js b/packages/builder/src/stores/builder/viewsV2.js index 7c0a940c1b9..3d67686344b 100644 --- a/packages/builder/src/stores/builder/viewsV2.js +++ b/packages/builder/src/stores/builder/viewsV2.js @@ -4,7 +4,7 @@ import { API } from "api" import { dataFilters } from "@budibase/shared-core" function convertToSearchFilters(view) { - // convert from SearchFilterGroup type + // convert from UISearchFilter type if (view?.query) { return { ...view, @@ -15,7 +15,7 @@ function convertToSearchFilters(view) { return view } -function convertToSearchFilterGroup(view) { +function convertToUISearchFilter(view) { if (view?.queryUI) { return { ...view, @@ -36,7 +36,7 @@ export function createViewsV2Store() { const views = Object.values(table?.views || {}).filter(view => { return view.version === 2 }) - list = list.concat(views.map(view => convertToSearchFilterGroup(view))) + list = list.concat(views.map(view => convertToUISearchFilter(view))) }) return { ...$store, @@ -77,7 +77,7 @@ export function createViewsV2Store() { if (!viewId) { return } - view = convertToSearchFilterGroup(view) + view = convertToUISearchFilter(view) const existingView = get(derivedStore).list.find(view => view.id === viewId) const tableIndex = get(tables).list.findIndex(table => { return table._id === view?.tableId || table._id === existingView?.tableId diff --git a/packages/frontend-core/src/components/CoreFilterBuilder.svelte b/packages/frontend-core/src/components/CoreFilterBuilder.svelte index c711a57e1c5..002d6fadb84 100644 --- a/packages/frontend-core/src/components/CoreFilterBuilder.svelte +++ b/packages/frontend-core/src/components/CoreFilterBuilder.svelte @@ -10,7 +10,7 @@ } from "@budibase/bbui" import { FieldType, - FilterGroupLogicalOperator, + UILogicalOperator, EmptyFilterOption, } from "@budibase/types" import { QueryUtils, Constants } from "@budibase/frontend-core" @@ -220,7 +220,7 @@ } else if (addGroup) { if (!editable?.groups?.length) { editable = { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, onEmptyFilter: EmptyFilterOption.RETURN_NONE, groups: [], } diff --git a/packages/frontend-core/src/components/grid/stores/filter.js b/packages/frontend-core/src/components/grid/stores/filter.js index 6e6c37da87c..e7adc356ae6 100644 --- a/packages/frontend-core/src/components/grid/stores/filter.js +++ b/packages/frontend-core/src/components/grid/stores/filter.js @@ -1,5 +1,5 @@ import { get, derived } from "svelte/store" -import { FieldType, FilterGroupLogicalOperator } from "@budibase/types" +import { FieldType, UILogicalOperator } from "@budibase/types" import { memo } from "../../../utils/memo" export const createStores = context => { @@ -25,10 +25,10 @@ export const deriveStores = context => { return $filter } let allFilters = { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, groups: [ { - logicalOperator: FilterGroupLogicalOperator.ALL, + logicalOperator: UILogicalOperator.ALL, filters: $inlineFilters, }, ],