From 93bf76e1fe50c2225997f5d0c24895a7318e9933 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 20 Nov 2023 14:22:39 +0100 Subject: [PATCH] Filter views --- .../DatasourceNavigator/datasourceUtils.js | 48 ++++++++++++++----- .../tests/datasourceUtils.spec.js | 2 +- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/packages/builder/src/components/backend/DatasourceNavigator/datasourceUtils.js b/packages/builder/src/components/backend/DatasourceNavigator/datasourceUtils.js index 543fa1f204..bc7fa53b49 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/datasourceUtils.js +++ b/packages/builder/src/components/backend/DatasourceNavigator/datasourceUtils.js @@ -121,16 +121,10 @@ export const enrichDatasources = ( const dsTables = tables.list.filter( table => - table.sourceId === datasource._id && - table._id !== TableNames.USERS && - (!searchTerm || - table.name?.toLowerCase()?.indexOf(searchTerm.toLowerCase()) > -1) + table.sourceId === datasource._id && table._id !== TableNames.USERS ) const dsQueries = queries.list.filter( - query => - query.datasourceId === datasource._id && - (!searchTerm || - query.name?.toLowerCase()?.indexOf(searchTerm.toLowerCase()) > -1) + query => query.datasourceId === datasource._id ) const open = showDatasourceOpen({ @@ -141,14 +135,46 @@ export const enrichDatasources = ( onlyOneSource: onlySource, }) - const show = !!(!searchTerm || dsQueries.length || dsTables.length) + const visibleDsQueries = dsQueries.filter( + q => + !searchTerm || + q.name?.toLowerCase()?.indexOf(searchTerm.toLowerCase()) > -1 + ) + + const visibleDsTables = dsTables + .map(t => ({ + ...t, + views: !searchTerm + ? t.views + : Object.keys(t.views || {}) + .filter( + viewName => + viewName.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1 + ) + .reduce( + (acc, viewName) => ({ ...acc, [viewName]: t.views[viewName] }), + {} + ), + })) + .filter( + table => + !searchTerm || + table.name?.toLowerCase()?.indexOf(searchTerm.toLowerCase()) > -1 || + Object.keys(table.views).length + ) + + const show = !!( + !searchTerm || + visibleDsQueries.length || + visibleDsTables.length + ) return { ...datasource, selected, containsSelected, open, - queries: dsQueries, - tables: dsTables, + queries: visibleDsQueries, + tables: visibleDsTables, show, } }) diff --git a/packages/builder/src/components/backend/DatasourceNavigator/tests/datasourceUtils.spec.js b/packages/builder/src/components/backend/DatasourceNavigator/tests/datasourceUtils.spec.js index 7e735b5653..f71c647490 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/tests/datasourceUtils.spec.js +++ b/packages/builder/src/components/backend/DatasourceNavigator/tests/datasourceUtils.spec.js @@ -142,7 +142,7 @@ describe("datasourceUtils", () => { ) }) - it("given a valid search term, all entities are correctly filtered", () => { + it("given a valid search term, all tables are correctly filtered", () => { const searchTerm = "ex" const result = enrichDatasources(