From 1161c185e21b3eb48cd357920382914b5b8fe17f Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 13 Jun 2024 17:46:03 +0100 Subject: [PATCH] Down to 0 failures. --- .../src/api/routes/tests/search.spec.ts | 25 ++++++------------- packages/shared-core/src/filters.ts | 20 +++++++++++++++ 2 files changed, 27 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 afc575b2c4..22971c9c1f 100644 --- a/packages/server/src/api/routes/tests/search.spec.ts +++ b/packages/server/src/api/routes/tests/search.spec.ts @@ -26,12 +26,12 @@ import { dataFilters } from "@budibase/shared-core" describe.each([ ["in-memory", undefined], - // ["lucene", undefined], - // ["sqs", undefined], - // [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], - // [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], - // [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], - // [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], + ["lucene", undefined], + ["sqs", undefined], + [DatabaseName.POSTGRES, getDatasource(DatabaseName.POSTGRES)], + [DatabaseName.MYSQL, getDatasource(DatabaseName.MYSQL)], + [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], + [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], ])("search (%s)", (name, dsProvider) => { const isSqs = name === "sqs" const isLucene = name === "lucene" @@ -94,18 +94,7 @@ describe.each([ private async performSearch(): Promise { if (isInMemory) { - let result = dataFilters.runQuery(_.cloneDeep(rows), this.query.query) - if (this.query.sort) { - result = dataFilters.sort( - result, - this.query.sort, - this.query.sortOrder || SortOrder.ASCENDING - ) - } - if (this.query.limit) { - result = dataFilters.limit(result, this.query.limit.toString()) - } - return result + return dataFilters.search(_.cloneDeep(rows), this.query) } else { return ( await config.api.row.search(table._id!, { diff --git a/packages/shared-core/src/filters.ts b/packages/shared-core/src/filters.ts index d63975f8fc..14b9f004d3 100644 --- a/packages/shared-core/src/filters.ts +++ b/packages/shared-core/src/filters.ts @@ -10,6 +10,8 @@ import { SortType, FieldConstraints, SortOrder, + RowSearchParams, + EmptyFilterOption, } from "@budibase/types" import dayjs from "dayjs" import { OperatorOptions, SqlNumberTypeRangeMap } from "./constants" @@ -260,6 +262,17 @@ export const buildQuery = (filter: SearchFilter[]) => { return query } +export const search = (docs: Record[], query: RowSearchParams) => { + let result = runQuery(docs, query.query) + if (query.sort) { + result = sort(result, query.sort, query.sortOrder || SortOrder.ASCENDING) + } + if (query.limit) { + result = limit(result, query.limit.toString()) + } + return result +} + /** * Performs a client-side search on an array of data * @param docs the data @@ -278,6 +291,13 @@ export const runQuery = ( query = cleanupQuery(query) + if ( + !hasFilters(query) && + query.onEmptyFilter === EmptyFilterOption.RETURN_NONE + ) { + return [] + } + const match = ( type: SearchFilterOperator,