From a9437302b74a6693fca2681fbc023133938b25ed Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 16 Dec 2021 11:41:28 +0000 Subject: [PATCH] Fixing issue with switching between datasources and making sure old client apps can still use the deprecated API. --- .../datasource/[selectedDatasource]/index.svelte | 16 ++++++++++------ packages/client/src/api/queries.js | 2 +- .../server/src/api/controllers/query/index.js | 16 ++++++++++++++-- packages/server/src/api/routes/query.js | 9 ++++++++- 4 files changed, 33 insertions(+), 10 deletions(-) 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 ab3ac18dc7..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] + } + } diff --git a/packages/client/src/api/queries.js b/packages/client/src/api/queries.js index 257d07f8ef..834282fe73 100644 --- a/packages/client/src/api/queries.js +++ b/packages/client/src/api/queries.js @@ -11,7 +11,7 @@ export const executeQuery = async ({ queryId, parameters }) => { return } const res = await API.post({ - url: `/api/queries/${queryId}`, + url: `/api/v2/queries/${queryId}`, body: { parameters, }, diff --git a/packages/server/src/api/controllers/query/index.js b/packages/server/src/api/controllers/query/index.js index ff9d814bc9..198d68d081 100644 --- a/packages/server/src/api/controllers/query/index.js +++ b/packages/server/src/api/controllers/query/index.js @@ -169,7 +169,7 @@ exports.preview = async function (ctx) { } } -exports.execute = async function (ctx) { +async function execute(ctx, opts = { rowsOnly: false }) { const db = new CouchDB(ctx.appId) const query = await db.get(ctx.params.queryId) @@ -188,12 +188,24 @@ exports.execute = async function (ctx) { query: enrichedQuery, transformer: query.transformer, }) - ctx.body = { data: rows, ...extra } + if (opts && opts.rowsOnly) { + ctx.body = rows + } else { + ctx.body = { data: rows, ...extra } + } } catch (err) { ctx.throw(400, err) } } +exports.executeV1 = async function (ctx) { + return execute(ctx, { rowsOnly: true }) +} + +exports.executeV2 = async function (ctx) { + return execute(ctx, { rowsOnly: false }) +} + exports.destroy = async function (ctx) { const db = new CouchDB(ctx.appId) await db.remove(ctx.params.queryId, ctx.params.revId) diff --git a/packages/server/src/api/routes/query.js b/packages/server/src/api/routes/query.js index 4a3cfeeb97..466fb1efaf 100644 --- a/packages/server/src/api/routes/query.js +++ b/packages/server/src/api/routes/query.js @@ -41,11 +41,18 @@ router authorized(PermissionTypes.QUERY, PermissionLevels.READ), queryController.find ) + // DEPRECATED - use new query endpoint for future work .post( "/api/queries/:queryId", paramResource("queryId"), authorized(PermissionTypes.QUERY, PermissionLevels.WRITE), - queryController.execute + queryController.executeV1 + ) + .post( + "/api/v2/queries/:queryId", + paramResource("queryId"), + authorized(PermissionTypes.QUERY, PermissionLevels.WRITE), + queryController.executeV2 ) .delete( "/api/queries/:queryId/:revId",