diff --git a/packages/auth/src/redis/utils.js b/packages/auth/src/redis/utils.js index 466b117e96..3461d2a511 100644 --- a/packages/auth/src/redis/utils.js +++ b/packages/auth/src/redis/utils.js @@ -16,6 +16,7 @@ exports.Databases = { USER_CACHE: "users", FLAGS: "flags", APP_METADATA: "appMetadata", + QUERY_VARS: "queryVars", } exports.SEPARATOR = SEPARATOR 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 0272463d92..4a104a1987 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/rest/RestExtraConfigForm.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/rest/RestExtraConfigForm.svelte @@ -1,9 +1,18 @@ @@ -43,6 +52,36 @@ + +
+
+ Variables + Optional +
+
+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 query +Static + + + +
+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. + + + diff --git a/packages/builder/src/helpers/data/utils.js b/packages/builder/src/helpers/data/utils.js index 1b8f1e04c5..d41745050f 100644 --- a/packages/builder/src/helpers/data/utils.js +++ b/packages/builder/src/helpers/data/utils.js @@ -119,3 +119,62 @@ export function flipHeaderState(headersActivity) { }) return enabled } + +// convert dynamic variables list to simple key/val object +export function variablesToObject(datasource) { + const variablesList = datasource?.config?.dynamicVariables + if (variablesList && variablesList.length > 0) { + return variablesList.reduce( + (acc, next) => ({ ...acc, [next.name]: next.value }), + {} + ) + } + return {} +} + +// convert dynamic variables object back to a list, enrich with query id +export function rebuildVariables(queryId, variables) { + let vars = [] + if (variables) { + vars = Object.entries(variables).map(entry => { + return { + name: entry[0], + value: entry[1], + queryId, + } + }) + } + return vars +} + +export function shouldShowVariables(dynamicVariables, variablesReadOnly) { + return !!( + dynamicVariables && + // show when editable or when read only and not empty + (!variablesReadOnly || Object.keys(dynamicVariables).length > 0) + ) +} + +export function buildAuthConfigs(datasource) { + if (datasource?.config?.authConfigs) { + return datasource.config.authConfigs.map(c => ({ + label: c.name, + value: c._id, + })) + } + return [] +} + +export default { + breakQueryString, + buildQueryString, + fieldsToSchema, + flipHeaderState, + keyValueToQueryParameters, + queryParametersToKeyValue, + schemaToFields, + variablesToObject, + 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 new file mode 100644 index 0000000000..1fdbc98483 --- /dev/null +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/_components/DynamicVariableModal.svelte @@ -0,0 +1,45 @@ + + + + + + + diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/index.svelte index 94cf2d8604..98b7859305 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/index.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[selectedDatasource]/index.svelte @@ -21,11 +21,11 @@ import { cloneDeep } from "lodash/fp" import ImportRestQueriesModal from "components/backend/DatasourceNavigator/modals/ImportRestQueriesModal.svelte" - import { onMount } from "svelte" let importQueriesModal let changed - let datasource + let integration, baseDatasource, datasource + let queryList const querySchema = { name: {}, queryVerb: { displayName: "Method" }, @@ -34,11 +34,12 @@ $: baseDatasource = $datasources.list.find( ds => ds._id === $datasources.selected ) - $: integration = datasource && $integrations[datasource.source] + $: queryList = $queries.list.filter( query => query.datasourceId === datasource?._id ) $: hasChanged(baseDatasource, datasource) + $: updateDatasource(baseDatasource) function hasChanged(base, ds) { if (base && ds) { @@ -66,9 +67,12 @@ $goto(`./${query._id}`) } - onMount(() => { - datasource = cloneDeep(baseDatasource) - }) + function updateDatasource(base) { + if (base) { + datasource = cloneDeep(base) + integration = $integrations[datasource.source] + } + } @@ -142,7 +146,11 @@
{/if} {#if datasource?.source === IntegrationTypes.REST} - + {/if} 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 5a192099ba..37c5fce61e 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 @@ -1,22 +1,22 @@ + {#if query && queryConfig}
@@ -317,7 +334,10 @@ {#if !$flags.queryTransformerBanner} + window.open( + "https://docs.budibase.com/building-apps/data/transformers" + )} on:change={() => flags.updateFlag("queryTransformerBanner", true)} > @@ -383,6 +403,9 @@ name="schema" headings options={SchemaTypeOptions} + menuItems={schemaMenuItems} + showMenu={!schemaReadOnly} + readOnly={schemaReadOnly} /> {/if} @@ -391,7 +414,12 @@