From 58734cd1f2e6283cfd267bcd6a364aef193284a9 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 17 Dec 2021 17:16:06 +0000 Subject: [PATCH] Fixing issues with query dynamic variables being able to overwrite/appearing in other queries. --- .../rest/RestExtraConfigForm.svelte | 8 ++++---- packages/builder/src/helpers/data/utils.js | 17 ++++++++++------- .../_components/DynamicVariableModal.svelte | 11 ++++++++--- .../rest/[query]/index.svelte | 10 ++++++---- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/rest/RestExtraConfigForm.svelte b/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/rest/RestExtraConfigForm.svelte index 4a104a1987..2c8b699849 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/rest/RestExtraConfigForm.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/rest/RestExtraConfigForm.svelte @@ -60,9 +60,9 @@ Variables enabled you to store and reuse values in queries. Static variables - use constant values while dynamic values can be bound to the response headers - or body of a queryVariables enable you to store and re-use values in queries, with the choice + of a static value such as a token using static variables, or a value from a + query response using dynamic variables. Static @@ -78,7 +78,7 @@ Dynamic Dynamic variables are evaluated when a dependant query is executed. The value - is cached for 24 hours and will re-evaluate if the dependendent query fails. + is cached for a period of time and will be refreshed if a query fails. diff --git a/packages/builder/src/helpers/data/utils.js b/packages/builder/src/helpers/data/utils.js index e163043470..c8eb7a4d3b 100644 --- a/packages/builder/src/helpers/data/utils.js +++ b/packages/builder/src/helpers/data/utils.js @@ -121,10 +121,13 @@ export function flipHeaderState(headersActivity) { } // convert dynamic variables list to simple key/val object -export function variablesToObject(datasource) { +export function getDynamicVariables(datasource, queryId) { const variablesList = datasource?.config?.dynamicVariables if (variablesList && variablesList.length > 0) { - return variablesList.reduce( + const filtered = queryId + ? variablesList.filter(variable => variable.queryId === queryId) + : variablesList + return filtered.reduce( (acc, next) => ({ ...acc, [next.name]: next.value }), {} ) @@ -133,10 +136,10 @@ export function variablesToObject(datasource) { } // convert dynamic variables object back to a list, enrich with query id -export function rebuildVariables(queryId, variables) { - let vars = [] +export function rebuildVariables(datasource, queryId, variables) { + let finalVars = [] if (variables) { - vars = Object.entries(variables).map(entry => { + finalVars = Object.entries(variables).map(entry => { return { name: entry[0], value: entry[1], @@ -144,7 +147,7 @@ export function rebuildVariables(queryId, variables) { } }) } - return vars + return [...(datasource?.config?.dynamicVariables || []), ...finalVars] } export function shouldShowVariables(dynamicVariables, variablesReadOnly) { @@ -173,7 +176,7 @@ export default { keyValueToQueryParameters, queryParametersToKeyValue, schemaToFields, - variablesToObject, + getDynamicVariables, rebuildVariables, shouldShowVariables, buildAuthConfigs, diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/_components/DynamicVariableModal.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/_components/DynamicVariableModal.svelte index 1fdbc98483..2941826427 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/_components/DynamicVariableModal.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/_components/DynamicVariableModal.svelte @@ -2,9 +2,10 @@ import { Input, ModalContent, Modal } from "@budibase/bbui" export let dynamicVariables + export let datasource export let binding - let name, modal, valid + let name, modal, valid, allVariableNames export const show = () => { modal.show() @@ -17,11 +18,15 @@ if (!name) { return false } - const varKeys = Object.keys(vars || {}) - return varKeys.find(key => key.toLowerCase() === name.toLowerCase()) == null + return !allVariableNames.find( + varName => varName.toLowerCase() === name.toLowerCase() + ) } $: valid = checkValid(dynamicVariables, name) + $: allVariableNames = (datasource?.config?.dynamicVariables || []).map( + variable => variable.name + ) $: error = name && !valid ? "Variable name is already in use." : null async function saveVariable() { diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/rest/[query]/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/rest/[query]/index.svelte index 62a16e9316..a54a4f205a 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/rest/[query]/index.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/rest/[query]/index.svelte @@ -116,6 +116,7 @@ if (dynamicVariables) { datasource.config.dynamicVariables = restUtils.rebuildVariables( + datasource, saveId, dynamicVariables ) @@ -213,7 +214,7 @@ if (query && !query.fields.bodyType) { query.fields.bodyType = "none" } - dynamicVariables = restUtils.variablesToObject(datasource) + dynamicVariables = restUtils.getDynamicVariables(datasource, query._id) }) @@ -396,9 +397,10 @@ {#if showVariablesTab} - {"Create dynamic variables to use body and headers results in other queries"} + + Create dynamic variables based on response body or headers + from other queries. +