From cb3a4b549ac2a36af90fca3c4982b7b4489ca1ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keviin=20=C3=85berg=20Kultalahti?= Date: Mon, 22 Mar 2021 14:47:21 +0100 Subject: [PATCH] moves queries part of backendstore to separate store --- .../builder/src/builderStore/store/backend.js | 2 - .../builderStore/store/backend/datasources.js | 10 +-- .../src/builderStore/store/backend/index.js | 1 + .../src/builderStore/store/backend/queries.js | 69 +++++++++++++++++++ .../src/builderStore/store/frontend.js | 10 +-- .../DatasourceNavigator.svelte | 12 ++-- .../popovers/EditQueryPopover.svelte | 2 +- .../PropertyControls/DatasourceSelect.svelte | 6 +- .../EventsEditor/actions/ExecuteQuery.svelte | 14 ++-- .../components/integration/QueryViewer.svelte | 4 +- .../[query]/_layout.svelte | 6 +- .../[selectedDatasource]/[query]/index.svelte | 3 +- .../[selectedDatasource]/_layout.svelte | 2 +- .../[selectedDatasource]/index.svelte | 8 +-- .../data/datasource/_layout.svelte | 4 +- .../data/datasource/index.svelte | 2 +- 16 files changed, 113 insertions(+), 42 deletions(-) create mode 100644 packages/builder/src/builderStore/store/backend/queries.js diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index fb01bd5f95..9a13471ec8 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -21,13 +21,11 @@ export const getBackendUiStore = () => { select: async db => { const [tables, queries] = await Promise.all([ api.get(`/api/tables`).then(r => r.json()), - api.get(`/api/queries`).then(r => r.json()), ]) store.update(state => { state.selectedDatabase = db state.tables = tables - state.queries = queries return state }) }, diff --git a/packages/builder/src/builderStore/store/backend/datasources.js b/packages/builder/src/builderStore/store/backend/datasources.js index 1a2cba18da..509c6479ae 100644 --- a/packages/builder/src/builderStore/store/backend/datasources.js +++ b/packages/builder/src/builderStore/store/backend/datasources.js @@ -3,7 +3,7 @@ import api from "../../api" function createDatasourcesStore() { const { subscribe, update, set } = writable({ - sources: [], + list: [], selected: null, }) @@ -14,7 +14,7 @@ function createDatasourcesStore() { fetch: async () => { const response = await api.get(`/api/datasources`) const json = await response.json() - update(state => ({ ...state, sources: json })) + update(state => ({ ...state, list: json })) return json }, select: async datasourceId => { @@ -25,9 +25,9 @@ function createDatasourcesStore() { const json = await response.json() update(state => { - const currentIdx = state.sources.findIndex(ds => ds._id === json._id) + const currentIdx = state.list.findIndex(ds => ds._id === json._id) - const sources = state.sources + const sources = state.list if (currentIdx >= 0) { sources.splice(currentIdx, 1, json) @@ -42,7 +42,7 @@ function createDatasourcesStore() { delete: async datasource => { await api.delete(`/api/datasources/${datasource._id}/${datasource._rev}`) update(state => { - const sources = state.sources.filter( + const sources = state.list.filter( existing => existing._id !== datasource._id ) return { sources, selected: null } diff --git a/packages/builder/src/builderStore/store/backend/index.js b/packages/builder/src/builderStore/store/backend/index.js index 8cdbffbe4a..3022f6791e 100644 --- a/packages/builder/src/builderStore/store/backend/index.js +++ b/packages/builder/src/builderStore/store/backend/index.js @@ -2,3 +2,4 @@ export { permissions } from "./permissions" export { roles } from "./roles" export { datasources } from "./datasources" export { integrations } from "./integrations" +export { queries } from "./queries" diff --git a/packages/builder/src/builderStore/store/backend/queries.js b/packages/builder/src/builderStore/store/backend/queries.js new file mode 100644 index 0000000000..edb0c0577b --- /dev/null +++ b/packages/builder/src/builderStore/store/backend/queries.js @@ -0,0 +1,69 @@ +import { writable } from "svelte/store" +import { datasources, integrations } from "./" +import api from "../../api" + +function createQueriesStore() { + const { subscribe, set, update } = writable({list: [], selected: null}) + + return { + subscribe, + set, + fetch: async () => { + const response = await api.get(`/api/queries`) + const json = await response.json() + update(state => ({...state, list: json})) + return json + }, + save: async (datasourceId, query) => { + const _integrations = get(integrations) + const dataSource = get(datasources).list.filter( + ds => ds._id === datasourceId + ) + // check if readable attribute is found + if (dataSource.length !== 0) { + const integration = _integrations[dataSource[0].source] + const readable = integration.query[query.queryVerb].readable + if (readable) { + query.readable = readable + } + } + query.datasourceId = datasourceId + const response = await api.post(`/api/queries`, query) + if (response.status !== 200) { + throw new Error("Failed saving query.") + } + const json = await response.json() + update(state => { + const currentIdx = state.list.findIndex(query => query._id === json._id) + + const queries = state.list + + if (currentIdx >= 0) { + queries.splice(currentIdx, 1, json) + } else { + queries.push(json) + } + return { list: queries, selected: json._id} + }) + return json + }, + select: query => { + datasources.update(state => ({ ...state, selected: query.datasourceId })) + }, + delete: async query => { + await api.delete(`/api/queries/${query._id}/${query._rev}`) + update(state => { + state.list = state.list.filter( + existing => existing._id !== query._id + ) + if (state.selected === query._id) { + state.selected = null + } + + return state + }) + }, + } +} + +export const queries = createQueriesStore() diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index f4977db705..270a24c295 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -10,7 +10,7 @@ import { selectedAccessRole, } from "builderStore" // Backendstores -import { datasources, integrations } from 'builderStore/store/backend/' +import { datasources, integrations, queries } from 'builderStore/store/backend/' import { fetchComponentLibDefinitions } from "../loadComponentLibraries" import api from "../api" @@ -62,12 +62,14 @@ export const getFrontendStore = () => { await hostingStore.actions.fetch() // Initialise backend stores - const [_datasources, _integrations] = await Promise.all([ + const [_datasources, _integrations, _queries] = await Promise.all([ api.get(`/api/datasources`).then(r => r.json()), - api.get("/api/integrations").then(r => r.json()) + api.get("/api/integrations").then(r => r.json()), + api.get(`/api/queries`).then(r => r.json()) ]) - datasources.set({sources: _datasources, selected: null}) + datasources.set({ list: _datasources, selected: null }) integrations.set(_integrations) + queries.set({ list: _queries, selected: null }) await backendUiStore.actions.database.select(application.instance) }, diff --git a/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte b/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte index b026fe7631..f94967dee9 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte @@ -2,7 +2,7 @@ import { onMount } from "svelte" import { goto } from "@sveltech/routify" import { backendUiStore } from "builderStore" - import { datasources } from 'builderStore/store/backend/' + import { datasources, queries } from 'builderStore/store/backend/' import EditDatasourcePopover from "./popovers/EditDatasourcePopover.svelte" import EditQueryPopover from "./popovers/EditQueryPopover.svelte" import NavItem from "components/common/NavItem.svelte" @@ -17,19 +17,19 @@ if ($backendUiStore.selectedQueryId === query._id) { return } - backendUiStore.actions.queries.select(query) + queries.select(query) $goto(`./datasource/${query.datasourceId}/${query._id}`) } onMount(() => { datasources.fetch() - backendUiStore.actions.queries.fetch() + queries.fetch() }) {#if $backendUiStore.selectedDatabase && $backendUiStore.selectedDatabase._id}
- {#each $datasources.sources as datasource, idx} + {#each $datasources.list as datasource, idx} 0} text={datasource.name} @@ -43,12 +43,12 @@
- {#each $backendUiStore.queries.filter(query => query.datasourceId === datasource._id) as query} + {#each $queries.list.filter(query => query.datasourceId === datasource._id) as query} onClickQuery(query)}> diff --git a/packages/builder/src/components/backend/DatasourceNavigator/popovers/EditQueryPopover.svelte b/packages/builder/src/components/backend/DatasourceNavigator/popovers/EditQueryPopover.svelte index dc07b31645..517e7b1297 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/popovers/EditQueryPopover.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/popovers/EditQueryPopover.svelte @@ -23,7 +23,7 @@ } async function deleteQuery() { - await backendUiStore.actions.queries.delete(query) + await queries.delete(query) notifier.success("Query deleted") hideEditor() } diff --git a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/DatasourceSelect.svelte b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/DatasourceSelect.svelte index 24422150fc..344564d15e 100644 --- a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/DatasourceSelect.svelte +++ b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/DatasourceSelect.svelte @@ -37,7 +37,7 @@ })) return [...acc, ...viewsArr] }, []) - $: queries = $backendUiStore.queries + $: queries = $queries.list .filter( query => showAllQueries || query.queryVerb === "read" || query.readable ) @@ -82,7 +82,7 @@ } function fetchQueryDefinition(query) { - const source = $datasources.sources.find( + const source = $datasources.list.find( ds => ds._id === query.datasourceId ).source return $integrations[source].query[query.queryVerb] @@ -124,7 +124,7 @@ height={200} query={value} schema={fetchQueryDefinition(value)} - datasource={$datasources.sources.find(ds => ds._id === value.datasourceId)} + datasource={$datasources.list.find(ds => ds._id === value.datasourceId)} editable={false} /> diff --git a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/EventsEditor/actions/ExecuteQuery.svelte b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/EventsEditor/actions/ExecuteQuery.svelte index 2187f384e9..bb60eff64c 100644 --- a/packages/builder/src/components/design/PropertiesPanel/PropertyControls/EventsEditor/actions/ExecuteQuery.svelte +++ b/packages/builder/src/components/design/PropertiesPanel/PropertyControls/EventsEditor/actions/ExecuteQuery.svelte @@ -1,15 +1,15 @@ 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 2080480fce..62332f405d 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,6 +1,7 @@ diff --git a/packages/builder/src/pages/[application]/data/datasource/index.svelte b/packages/builder/src/pages/[application]/data/datasource/index.svelte index 7f85d4ed14..62bd7acd47 100644 --- a/packages/builder/src/pages/[application]/data/datasource/index.svelte +++ b/packages/builder/src/pages/[application]/data/datasource/index.svelte @@ -6,7 +6,7 @@ onMount(async () => { // navigate to first table in list, if not already selected - $datasources.sources.length > 0 && $goto(`../${$datasources.sources[0]._id}`) + $datasources.list.length > 0 && $goto(`../${$datasources.list[0]._id}`) })