diff --git a/packages/builder/src/builderStore/store/backend.js b/packages/builder/src/builderStore/store/backend.js index 0eb5aaaf7a..bc29e3ece3 100644 --- a/packages/builder/src/builderStore/store/backend.js +++ b/packages/builder/src/builderStore/store/backend.js @@ -20,9 +20,8 @@ export const getBackendUiStore = () => { reset: () => store.set({ ...INITIAL_BACKEND_UI_STATE }), database: { select: async db => { - const [tables, datasources, queries, integrations] = await Promise.all([ + const [tables, queries, integrations] = await Promise.all([ api.get(`/api/tables`).then(r => r.json()), - api.get(`/api/datasources`).then(r => r.json()), api.get(`/api/queries`).then(r => r.json()), api.get("/api/integrations").then(r => r.json()), ]) @@ -30,7 +29,6 @@ export const getBackendUiStore = () => { store.update(state => { state.selectedDatabase = db state.tables = tables - state.datasources = datasources state.queries = queries state.integrations = integrations return state @@ -49,56 +47,6 @@ export const getBackendUiStore = () => { return state }), }, - datasources: { - fetch: async () => { - const response = await api.get(`/api/datasources`) - const json = await response.json() - store.update(state => { - state.datasources = json - return state - }) - return json - }, - select: async datasourceId => { - store.update(state => { - state.selectedDatasourceId = datasourceId - state.selectedQueryId = null - return state - }) - }, - save: async datasource => { - const response = await api.post("/api/datasources", datasource) - const json = await response.json() - store.update(state => { - const currentIdx = state.datasources.findIndex( - ds => ds._id === json._id - ) - - if (currentIdx >= 0) { - state.datasources.splice(currentIdx, 1, json) - } else { - state.datasources.push(json) - } - - state.datasources = state.datasources - state.selectedDatasourceId = json._id - return state - }) - return json - }, - delete: async datasource => { - await api.delete( - `/api/datasources/${datasource._id}/${datasource._rev}` - ) - store.update(state => { - state.datasources = state.datasources.filter( - existing => existing._id !== datasource._id - ) - state.selectedDatasourceId = null - return state - }) - }, - }, queries: { fetch: async () => { const response = await api.get(`/api/queries`) diff --git a/packages/builder/src/builderStore/store/backend/datasources.js b/packages/builder/src/builderStore/store/backend/datasources.js new file mode 100644 index 0000000000..1a2cba18da --- /dev/null +++ b/packages/builder/src/builderStore/store/backend/datasources.js @@ -0,0 +1,54 @@ +import { writable } from "svelte/store" +import api from "../../api" + +function createDatasourcesStore() { + const { subscribe, update, set } = writable({ + sources: [], + selected: null, + }) + + return { + subscribe, + set, + update, + fetch: async () => { + const response = await api.get(`/api/datasources`) + const json = await response.json() + update(state => ({ ...state, sources: json })) + return json + }, + select: async datasourceId => { + update(state => ({ ...state, selected: datasourceId })) + }, + save: async datasource => { + const response = await api.post("/api/datasources", datasource) + const json = await response.json() + + update(state => { + const currentIdx = state.sources.findIndex(ds => ds._id === json._id) + + const sources = state.sources + + if (currentIdx >= 0) { + sources.splice(currentIdx, 1, json) + } else { + sources.push(json) + } + + return { sources, selected: json._id } + }) + return json + }, + delete: async datasource => { + await api.delete(`/api/datasources/${datasource._id}/${datasource._rev}`) + update(state => { + const sources = state.sources.filter( + existing => existing._id !== datasource._id + ) + return { sources, selected: null } + }) + }, + } +} + +export const datasources = createDatasourcesStore() diff --git a/packages/builder/src/builderStore/store/backend/index.js b/packages/builder/src/builderStore/store/backend/index.js index 011663961c..c2ae7f463c 100644 --- a/packages/builder/src/builderStore/store/backend/index.js +++ b/packages/builder/src/builderStore/store/backend/index.js @@ -1 +1,3 @@ export { permissions } from "./permissions" +export { roles } from "./roles" +export { datasources } from "./datasources" diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index 8877ccef69..a43382f3dd 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -9,6 +9,9 @@ import { selectedComponent, selectedAccessRole, } from "builderStore" +// Backendstores +import {datasources} from 'builderStore/store/backend/' + import { fetchComponentLibDefinitions } from "../loadComponentLibraries" import api from "../api" import { FrontendTypes } from "constants" @@ -57,6 +60,13 @@ export const getFrontendStore = () => { appInstance: application.instance, })) await hostingStore.actions.fetch() + + // Initialise backend stores + const [sources] = await Promise.all([ + api.get(`/api/datasources`).then(r => r.json()), + ]) + datasources.set({sources, selected: null}) + await backendUiStore.actions.database.select(application.instance) }, routing: { diff --git a/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte b/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte index 19fd7060ab..b026fe7631 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte @@ -2,13 +2,14 @@ import { onMount } from "svelte" import { goto } from "@sveltech/routify" import { backendUiStore } from "builderStore" + import { datasources } from 'builderStore/store/backend/' import EditDatasourcePopover from "./popovers/EditDatasourcePopover.svelte" import EditQueryPopover from "./popovers/EditQueryPopover.svelte" import NavItem from "components/common/NavItem.svelte" import ICONS from "./icons" function selectDatasource(datasource) { - backendUiStore.actions.datasources.select(datasource._id) + datasources.select(datasource._id) $goto(`./datasource/${datasource._id}`) } @@ -21,18 +22,18 @@ } onMount(() => { - backendUiStore.actions.datasources.fetch() + datasources.fetch() backendUiStore.actions.queries.fetch() }) {#if $backendUiStore.selectedDatabase && $backendUiStore.selectedDatabase._id}