From 5c26f07d261b0dc47ff3608216a05c090ca2862b Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Fri, 17 Dec 2021 10:37:06 +0000 Subject: [PATCH] Add back in query extension capability to new data provider --- .../src/components/app/DataProvider.svelte | 58 +++++++++++++++---- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/packages/client/src/components/app/DataProvider.svelte b/packages/client/src/components/app/DataProvider.svelte index 93347c3fb0..8d4218c3b2 100644 --- a/packages/client/src/components/app/DataProvider.svelte +++ b/packages/client/src/components/app/DataProvider.svelte @@ -3,6 +3,7 @@ import { ProgressCircle, Pagination } from "@budibase/bbui" import Placeholder from "./Placeholder.svelte" import { fetchData } from "utils/fetch/fetchData.js" + import { buildLuceneQuery } from "builder/src/helpers/lucene" export let dataSource export let filter @@ -14,9 +15,16 @@ const { styleable, Provider, ActionTypes } = getContext("sdk") const component = getContext("component") + // We need to manage our lucene query manually as we want to allow components + // to extend it + let queryExtensions = {} + $: defaultQuery = buildLuceneQuery(filter) + $: query = extendQuery(defaultQuery, queryExtensions) + + // Keep our data fetch instance up to date $: fetch = createFetch(dataSource) $: fetch.update({ - filter, + query, sortColumn, sortOrder, limit, @@ -30,14 +38,14 @@ callback: () => fetch.refresh(), metadata: { dataSource }, }, - // { - // type: ActionTypes.AddDataProviderQueryExtension, - // callback: addQueryExtension, - // }, - // { - // type: ActionTypes.RemoveDataProviderQueryExtension, - // callback: removeQueryExtension, - // }, + { + type: ActionTypes.AddDataProviderQueryExtension, + callback: addQueryExtension, + }, + { + type: ActionTypes.RemoveDataProviderQueryExtension, + callback: removeQueryExtension, + }, { type: ActionTypes.SetDataProviderSorting, callback: ({ column, order }) => { @@ -74,13 +82,43 @@ const createFetch = datasource => { return fetchData(datasource, { - filter, + query, sortColumn, sortOrder, limit, paginate, }) } + + const addQueryExtension = (key, extension) => { + if (!key || !extension) { + return + } + queryExtensions = { ...queryExtensions, [key]: extension } + } + + const removeQueryExtension = key => { + if (!key) { + return + } + const newQueryExtensions = { ...queryExtensions } + delete newQueryExtensions[key] + queryExtensions = newQueryExtensions + } + + const extendQuery = (defaultQuery, extensions) => { + const extensionValues = Object.values(extensions || {}) + let extendedQuery = { ...defaultQuery } + extensionValues.forEach(extension => { + Object.entries(extension || {}).forEach(([operator, fields]) => { + extendedQuery[operator] = { + ...extendedQuery[operator], + ...fields, + } + }) + }) + return extendedQuery + }