From 526283d40614854b48962e132d1d7a77bef2a148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keviin=20=C3=85berg=20Kultalahti?= Date: Mon, 22 Mar 2021 16:33:08 +0100 Subject: [PATCH] fixes and splits out database to it's separate store --- .../builder/src/builderStore/store/backend.js | 70 +------------------ .../builderStore/store/backend/database.js | 3 + .../builderStore/store/backend/datasources.js | 4 +- .../src/builderStore/store/backend/index.js | 1 + .../src/builderStore/store/backend/queries.js | 1 + .../src/builderStore/store/frontend.js | 6 +- .../DatasourceNavigator.svelte | 7 +- .../modals/CreateDatasourceModal.svelte | 4 +- 8 files changed, 17 insertions(+), 79 deletions(-) create mode 100644 packages/builder/src/builderStore/store/backend/database.js diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index 9a13471ec8..8f12661284 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -6,7 +6,6 @@ const INITIAL_BACKEND_UI_STATE = { tables: [], views: [], datasources: [], - queries: [], selectedDatabase: {}, selectedTable: {}, draftTable: {}, @@ -19,12 +18,11 @@ export const getBackendUiStore = () => { reset: () => store.set({ ...INITIAL_BACKEND_UI_STATE }), database: { select: async db => { - const [tables, queries] = await Promise.all([ + const [tables] = await Promise.all([ api.get(`/api/tables`).then(r => r.json()), ]) store.update(state => { - state.selectedDatabase = db state.tables = tables return state }) @@ -42,72 +40,6 @@ export const getBackendUiStore = () => { return state }), }, - queries: { - fetch: async () => { - const response = await api.get(`/api/queries`) - const json = await response.json() - store.update(state => { - state.queries = json - return state - }) - return json - }, - save: async (datasourceId, query) => { - const integrations = get(store).integrations - const dataSource = get(store).datasources.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() - store.update(state => { - const currentIdx = state.queries.findIndex( - query => query._id === json._id - ) - - if (currentIdx >= 0) { - state.queries.splice(currentIdx, 1, json) - } else { - state.queries.push(json) - } - - state.queries = state.queries - state.selectedQueryId = json._id - return state - }) - return json - }, - select: query => - store.update(state => { - state.selectedDatasourceId = query.datasourceId - state.selectedQueryId = query._id - return state - }), - delete: async query => { - await api.delete(`/api/queries/${query._id}/${query._rev}`) - store.update(state => { - state.queries = state.queries.filter( - existing => existing._id !== query._id - ) - if (state.selectedQueryId === query._id) { - state.selectedQueryId = null - } - - return state - }) - }, - }, tables: { fetch: async () => { const tablesResponse = await api.get(`/api/tables`) diff --git a/packages/builder/src/builderStore/store/backend/database.js b/packages/builder/src/builderStore/store/backend/database.js new file mode 100644 index 0000000000..c619ca200d --- /dev/null +++ b/packages/builder/src/builderStore/store/backend/database.js @@ -0,0 +1,3 @@ +import { writable } from "svelte/store" + +export const database = writable({}) \ No newline at end of file diff --git a/packages/builder/src/builderStore/store/backend/datasources.js b/packages/builder/src/builderStore/store/backend/datasources.js index 509c6479ae..d84e66d344 100644 --- a/packages/builder/src/builderStore/store/backend/datasources.js +++ b/packages/builder/src/builderStore/store/backend/datasources.js @@ -1,4 +1,5 @@ import { writable } from "svelte/store" +import { queries } from "./" import api from "../../api" function createDatasourcesStore() { @@ -19,6 +20,7 @@ function createDatasourcesStore() { }, select: async datasourceId => { update(state => ({ ...state, selected: datasourceId })) + queries.update(state => ({...state, selected: null})) }, save: async datasource => { const response = await api.post("/api/datasources", datasource) @@ -35,7 +37,7 @@ function createDatasourcesStore() { sources.push(json) } - return { sources, selected: json._id } + return { list: sources, selected: json._id } }) return json }, diff --git a/packages/builder/src/builderStore/store/backend/index.js b/packages/builder/src/builderStore/store/backend/index.js index 3022f6791e..11a5613459 100644 --- a/packages/builder/src/builderStore/store/backend/index.js +++ b/packages/builder/src/builderStore/store/backend/index.js @@ -1,3 +1,4 @@ +export { database } from "./database" export { permissions } from "./permissions" export { roles } from "./roles" export { datasources } from "./datasources" diff --git a/packages/builder/src/builderStore/store/backend/queries.js b/packages/builder/src/builderStore/store/backend/queries.js index edb0c0577b..68851f59ee 100644 --- a/packages/builder/src/builderStore/store/backend/queries.js +++ b/packages/builder/src/builderStore/store/backend/queries.js @@ -8,6 +8,7 @@ function createQueriesStore() { return { subscribe, set, + update, fetch: async () => { const response = await api.get(`/api/queries`) const json = await response.json() diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index 270a24c295..e506ecb4f1 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, queries } from 'builderStore/store/backend/' +import { datasources, integrations, queries, database } from 'builderStore/store/backend/' import { fetchComponentLibDefinitions } from "../loadComponentLibraries" import api from "../api" @@ -70,8 +70,8 @@ export const getFrontendStore = () => { datasources.set({ list: _datasources, selected: null }) integrations.set(_integrations) queries.set({ list: _queries, selected: null }) - - await backendUiStore.actions.database.select(application.instance) + database.set(application.instance) + }, routing: { fetch: async () => { diff --git a/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte b/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte index f94967dee9..f101bee181 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte @@ -1,8 +1,7 @@ -{#if $backendUiStore.selectedDatabase && $backendUiStore.selectedDatabase._id} +{#if $database?._id}
{#each $datasources.list as datasource, idx} - import { goto, params } from "@sveltech/routify" + import { goto } from "@sveltech/routify" import { datasources } from 'builderStore/store/backend/' import { notifier } from "builderStore/store/notifications" import { Input, Label, ModalContent } from "@budibase/bbui" @@ -14,7 +14,7 @@ function checkValid(evt) { const datasourceName = evt.target.value if ( - $datasources?.sources.some( + $datasources?.list.some( datasource => datasource.name === datasourceName ) ) {