From 507fa2b417810f15c9baa1e32e436dc39bfc3546 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 8 Mar 2024 16:50:59 +0000 Subject: [PATCH] Quick fix for #12502 - the schema was never updating in the UI no matter what was input despite the response - cleaned up the key parsing a little on the backend and made sure there is simply one schema that the frontend always uses, respecting whatever the backend responds with. --- .../components/integration/QueryViewer.svelte | 22 ++++++------------- packages/server/src/threads/query.ts | 11 +++++++--- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/builder/src/components/integration/QueryViewer.svelte b/packages/builder/src/components/integration/QueryViewer.svelte index b3f8ddb09b..99635323ca 100644 --- a/packages/builder/src/components/integration/QueryViewer.svelte +++ b/packages/builder/src/components/integration/QueryViewer.svelte @@ -39,7 +39,7 @@ let integration let schemaType - let autoSchema = {} + let schema = {} let nestedSchemaFields = {} let rows = [] let keys = {} @@ -52,6 +52,8 @@ schemaType = integration.query[query.queryVerb].type newQuery = cloneDeep(query) + // init schema from the query if one already exists + schema = newQuery.schema // Set the location where the query code will be written to an empty string so that it doesn't // get changed from undefined -> "" by the input, breaking our unsaved changes checks newQuery.fields[schemaType] ??= "" @@ -86,12 +88,7 @@ nestedSchemaFields = response.nestedSchemaFields - if (Object.keys(newQuery.schema).length === 0) { - // Assign this to a variable instead of directly to the newQuery.schema so that a user - // can change the table they're querying and have the schema update until they first - // edit it - autoSchema = response.schema - } + schema = response.schema rows = response.rows notifications.success("Query executed successfully") @@ -118,10 +115,7 @@ loading = true const response = await queries.save(newQuery.datasourceId, { ...newQuery, - schema: - Object.keys(newQuery.schema).length === 0 - ? autoSchema - : newQuery.schema, + schema, nestedSchemaFields, }) @@ -320,12 +314,10 @@ (showSidePanel = false)} onSchemaChange={newSchema => { - newQuery.schema = newSchema + schema = newSchema }} {rows} - schema={Object.keys(newQuery.schema).length === 0 - ? autoSchema - : newQuery.schema} + {schema} /> diff --git a/packages/server/src/threads/query.ts b/packages/server/src/threads/query.ts index 9d7b7062a5..baee2d5c05 100644 --- a/packages/server/src/threads/query.ts +++ b/packages/server/src/threads/query.ts @@ -15,7 +15,7 @@ import { context, cache, auth } from "@budibase/backend-core" import { getGlobalIDFromUserMetadataID } from "../db/utils" import sdk from "../sdk" import { cloneDeep } from "lodash/fp" -import { Datasource, Query, SourceName } from "@budibase/types" +import { Datasource, Query, SourceName, Row } from "@budibase/types" import { isSQL } from "../integrations/utils" import { interpolateSQL } from "../integrations/queries/sql" @@ -115,7 +115,7 @@ class QueryRunner { } let output = threadUtils.formatResponse(await integration[queryVerb](query)) - let rows = output, + let rows = output as Row[], info = undefined, extra = undefined, pagination = undefined @@ -170,7 +170,12 @@ class QueryRunner { } // get all the potential fields in the schema - let keys = rows.flatMap(Object.keys) + const keysSet: Set = new Set() + rows.forEach(row => { + const keys = Object.keys(row) + keys.forEach(key => keysSet.add(key)) + }) + const keys: string[] = [...keysSet] if (integration.end) { integration.end()