From 64e31189b78499e37c38fa650875a829213e3e8a Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 7 Jan 2021 13:13:46 +0000 Subject: [PATCH] new datasource design --- .../DataTable/ExternalDataSourceTable.svelte | 11 +- .../components/backend/DataTable/Table.svelte | 1 - .../buttons/CreateQueryButton.svelte | 14 +- .../DatasourceNavigator.svelte | 1 - .../src/components/integration/Query.js | 9 ++ .../{Editor.svelte => QueryEditor.svelte} | 11 +- .../QueryViewer.svelte} | 139 ++++++++++-------- .../src/components/integration/codemirror.js | 1 + .../src/components/integration/index.svelte | 65 ++++++-- .../EventsEditor/actions/ExecuteQuery.svelte | 2 +- .../[selectedDatasource]/[query]/index.svelte | 36 ++++- .../[selectedDatasource]/index.svelte | 8 +- .../server/src/api/controllers/datasource.js | 1 + packages/server/src/api/controllers/query.js | 9 +- 14 files changed, 208 insertions(+), 100 deletions(-) create mode 100644 packages/builder/src/components/integration/Query.js rename packages/builder/src/components/integration/{Editor.svelte => QueryEditor.svelte} (77%) rename packages/builder/src/components/{backend/DataTable/modals/CreateEditQuery.svelte => integration/QueryViewer.svelte} (51%) diff --git a/packages/builder/src/components/backend/DataTable/ExternalDataSourceTable.svelte b/packages/builder/src/components/backend/DataTable/ExternalDataSourceTable.svelte index a340f7c3c9..e0f429cb3b 100644 --- a/packages/builder/src/components/backend/DataTable/ExternalDataSourceTable.svelte +++ b/packages/builder/src/components/backend/DataTable/ExternalDataSourceTable.svelte @@ -4,11 +4,10 @@ import { notifier } from "builderStore/store/notifications" import * as api from "./api" import Table from "./Table.svelte" - import CreateQueryButton from "components/backend/DataTable/buttons/CreateQueryButton.svelte" export let query = {} + export let data = [] - let data = [] let loading = false let error = false @@ -17,7 +16,7 @@ loading = true const response = await api.fetchDataForQuery( $params.selectedDatasource, - $params.query + query._id ) data = response.rows || [] error = false @@ -30,15 +29,13 @@ } // Fetch rows for specified query - $: query && fetchData() + // $: query && fetchData() {#if error}
{error}
{/if} - - -
+ diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditQuery.svelte b/packages/builder/src/components/integration/QueryViewer.svelte similarity index 51% rename from packages/builder/src/components/backend/DataTable/modals/CreateEditQuery.svelte rename to packages/builder/src/components/integration/QueryViewer.svelte index 024646757c..c8a34eb423 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditQuery.svelte +++ b/packages/builder/src/components/integration/QueryViewer.svelte @@ -14,33 +14,40 @@ import api from "builderStore/api" import { FIELDS } from "constants/backend" import IntegrationQueryEditor from "components/integration/index.svelte" + import ExternalDataSourceTable from "components/backend/DataTable/ExternalDataSourceTable.svelte" import { backendUiStore } from "builderStore" const PREVIEW_HEADINGS = [ { - title: "Preview", - key: "PREVIEW", + title: "JSON", + key: "JSON", }, { title: "Schema", key: "SCHEMA", }, + { + title: "Results", + key: "RESULTS", + }, + { + title: "Preview", + key: "PREVIEW", + }, ] export let query export let fields = [] let config = {} - let previewTab = "PREVIEW" - let preview + let tab = "JSON" + let parameters + let data $: datasource = $backendUiStore.datasources.find( ds => ds._id === query.datasourceId ) - $: query.datasourceId = - query.datasourceId || $backendUiStore.selectedDatasourceId - $: query.schema = fields.reduce( (acc, next) => ({ ...acc, @@ -52,6 +59,9 @@ {} ) + $: datasourceType = datasource.source + $: datasourceType && fetchQueryConfig() + function newField() { fields = [...fields, {}] } @@ -76,17 +86,18 @@ async function previewQuery() { try { const response = await api.post(`/api/queries/preview`, { + parameters: query.parameters, datasourceId: datasource._id, query: query.queryString, }) const json = await response.json() - if (response.status !== 200) { - throw new Error(json.message) - } - preview = json[0] || {} + + if (response.status !== 200) throw new Error(json.message) + + data = json || [] // TODO: refactor - fields = Object.keys(preview).map(field => ({ + fields = Object.keys(json[0]).map(field => ({ name: field, type: "STRING", })) @@ -96,15 +107,21 @@ } } - onMount(() => { - fetchQueryConfig() - }) + async function saveQuery() { + try { + await backendUiStore.actions.queries.save(query.datasourceId, query) + notifier.success(`Query created successfully.`) + } catch (err) { + console.error(err) + notifier.danger(`Error creating query. ${err.message}`) + } + }
- + @@ -118,40 +135,43 @@ - + - + - +
+
+ + +
- - - {#if preview} - - {#if previewTab === 'PREVIEW'} -
{JSON.stringify(preview, undefined, 2)}
- {:else if previewTab === 'SCHEMA'} - {#each fields as field, idx} -
- - - deleteField(idx)} /> -
- {/each} - - {/if} -
- {/if} + {#if data} + + {#if tab === 'JSON'} +
{JSON.stringify(data[0], undefined, 2)}
+ {:else if tab === 'PREVIEW'} + + {:else if tab === 'SCHEMA'} + {#each fields as field, idx} +
+ + + deleteField(idx)} /> +
+ {/each} + + {/if} +
+ {/if} +
@@ -163,17 +183,6 @@ margin-bottom: var(--spacing-m); } - h6 { - font-family: var(--font-sans); - font-weight: 600; - text-rendering: var(--text-render); - color: var(--ink); - font-size: var(--heading-font-size-xs); - color: var(--ink); - margin-bottom: var(--spacing-xs); - margin-top: var(--spacing-xs); - } - .config { margin-bottom: var(--spacing-s); } @@ -185,7 +194,21 @@ .preview { width: 800px; + height: 100%; + overflow-y: auto; overflow-wrap: break-word; white-space: pre-wrap; } + + .viewer { + position: relative; + } + + .viewer-controls { + position: absolute; + right: 0; + display: grid; + grid-gap: var(--spacing-m); + grid-auto-flow: column; + } diff --git a/packages/builder/src/components/integration/codemirror.js b/packages/builder/src/components/integration/codemirror.js index 3b823a1c97..6d731ab9af 100644 --- a/packages/builder/src/components/integration/codemirror.js +++ b/packages/builder/src/components/integration/codemirror.js @@ -1,6 +1,7 @@ import CodeMirror from "codemirror" import "codemirror/lib/codemirror.css" import "codemirror/mode/sql/sql" +import "codemirror/mode/css/css" import "codemirror/mode/handlebars/handlebars" import "codemirror/mode/javascript/javascript" diff --git a/packages/builder/src/components/integration/index.svelte b/packages/builder/src/components/integration/index.svelte index da301e3458..c133f4ff24 100644 --- a/packages/builder/src/components/integration/index.svelte +++ b/packages/builder/src/components/integration/index.svelte @@ -1,6 +1,6 @@ - +Parameters -{#if type === QueryTypes.SQL} - +{#if query.queryType === QueryTypes.SQL} +
+
+ + + + + {#each parameters as parameter, idx} + + + deleteQueryParameter(idx)} /> + {/each} + +
+
+ + {/if} + + diff --git a/packages/builder/src/components/userInterface/EventsEditor/actions/ExecuteQuery.svelte b/packages/builder/src/components/userInterface/EventsEditor/actions/ExecuteQuery.svelte index 8195e16258..ccc15a743f 100644 --- a/packages/builder/src/components/userInterface/EventsEditor/actions/ExecuteQuery.svelte +++ b/packages/builder/src/components/userInterface/EventsEditor/actions/ExecuteQuery.svelte @@ -25,7 +25,7 @@ {/if} diff --git a/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/[query]/index.svelte b/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/[query]/index.svelte index 27e3845584..0f6c4825ab 100644 --- a/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/[query]/index.svelte +++ b/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/[query]/index.svelte @@ -1,12 +1,36 @@ -{#if $backendUiStore.selectedDatabase._id && query} - -{/if} +
+ {#if $backendUiStore.selectedDatabase._id && query} + + {/if} +
+ + diff --git a/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/index.svelte b/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/index.svelte index c2f615c511..82756a2c2b 100644 --- a/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/index.svelte +++ b/packages/builder/src/pages/[application]/data/datasource/[selectedDatasource]/index.svelte @@ -1,5 +1,6 @@ {#if datasource} - + $goto('../new')}> + + Create Query +

{datasource.name}: Configuration

diff --git a/packages/server/src/api/controllers/datasource.js b/packages/server/src/api/controllers/datasource.js index 87e22d2e8e..2cbe4fb107 100644 --- a/packages/server/src/api/controllers/datasource.js +++ b/packages/server/src/api/controllers/datasource.js @@ -60,6 +60,7 @@ exports.update = async function(ctx) { } exports.destroy = async function(ctx) { + // TODO: destroy all queries as well const database = new CouchDB(ctx.user.appId) await database.destroy(ctx.params.datasourceId) ctx.message = `Datasource deleted.` diff --git a/packages/server/src/api/controllers/query.js b/packages/server/src/api/controllers/query.js index 229897debb..9c4b2cc8a8 100644 --- a/packages/server/src/api/controllers/query.js +++ b/packages/server/src/api/controllers/query.js @@ -51,7 +51,12 @@ exports.save = async function(ctx) { } exports.preview = async function(ctx) { - const { query, datasourceId } = ctx.request.body + const { query, datasourceId, parameters } = ctx.request.body + + const queryTemplate = handlebars.compile(query) + const parsedQuery = queryTemplate(parameters) + + console.log(parsedQuery) const db = new CouchDB(ctx.user.appId) @@ -64,7 +69,7 @@ exports.preview = async function(ctx) { return } - ctx.body = await new Integration(datasource.config, query).query() + ctx.body = await new Integration(datasource.config, parsedQuery).query() } exports.execute = async function(ctx) {