fixes and splits out database to it's separate store

This commit is contained in:
Keviin Åberg Kultalahti 2021-03-22 16:33:08 +01:00
parent cb3a4b549a
commit 526283d406
8 changed files with 17 additions and 79 deletions

View File

@ -6,7 +6,6 @@ const INITIAL_BACKEND_UI_STATE = {
tables: [], tables: [],
views: [], views: [],
datasources: [], datasources: [],
queries: [],
selectedDatabase: {}, selectedDatabase: {},
selectedTable: {}, selectedTable: {},
draftTable: {}, draftTable: {},
@ -19,12 +18,11 @@ export const getBackendUiStore = () => {
reset: () => store.set({ ...INITIAL_BACKEND_UI_STATE }), reset: () => store.set({ ...INITIAL_BACKEND_UI_STATE }),
database: { database: {
select: async db => { select: async db => {
const [tables, queries] = await Promise.all([ const [tables] = await Promise.all([
api.get(`/api/tables`).then(r => r.json()), api.get(`/api/tables`).then(r => r.json()),
]) ])
store.update(state => { store.update(state => {
state.selectedDatabase = db
state.tables = tables state.tables = tables
return state return state
}) })
@ -42,72 +40,6 @@ export const getBackendUiStore = () => {
return state 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: { tables: {
fetch: async () => { fetch: async () => {
const tablesResponse = await api.get(`/api/tables`) const tablesResponse = await api.get(`/api/tables`)

View File

@ -0,0 +1,3 @@
import { writable } from "svelte/store"
export const database = writable({})

View File

@ -1,4 +1,5 @@
import { writable } from "svelte/store" import { writable } from "svelte/store"
import { queries } from "./"
import api from "../../api" import api from "../../api"
function createDatasourcesStore() { function createDatasourcesStore() {
@ -19,6 +20,7 @@ function createDatasourcesStore() {
}, },
select: async datasourceId => { select: async datasourceId => {
update(state => ({ ...state, selected: datasourceId })) update(state => ({ ...state, selected: datasourceId }))
queries.update(state => ({...state, selected: null}))
}, },
save: async datasource => { save: async datasource => {
const response = await api.post("/api/datasources", datasource) const response = await api.post("/api/datasources", datasource)
@ -35,7 +37,7 @@ function createDatasourcesStore() {
sources.push(json) sources.push(json)
} }
return { sources, selected: json._id } return { list: sources, selected: json._id }
}) })
return json return json
}, },

View File

@ -1,3 +1,4 @@
export { database } from "./database"
export { permissions } from "./permissions" export { permissions } from "./permissions"
export { roles } from "./roles" export { roles } from "./roles"
export { datasources } from "./datasources" export { datasources } from "./datasources"

View File

@ -8,6 +8,7 @@ function createQueriesStore() {
return { return {
subscribe, subscribe,
set, set,
update,
fetch: async () => { fetch: async () => {
const response = await api.get(`/api/queries`) const response = await api.get(`/api/queries`)
const json = await response.json() const json = await response.json()

View File

@ -10,7 +10,7 @@ import {
selectedAccessRole, selectedAccessRole,
} from "builderStore" } from "builderStore"
// Backendstores // Backendstores
import { datasources, integrations, queries } from 'builderStore/store/backend/' import { datasources, integrations, queries, database } from 'builderStore/store/backend/'
import { fetchComponentLibDefinitions } from "../loadComponentLibraries" import { fetchComponentLibDefinitions } from "../loadComponentLibraries"
import api from "../api" import api from "../api"
@ -70,8 +70,8 @@ export const getFrontendStore = () => {
datasources.set({ list: _datasources, selected: null }) datasources.set({ list: _datasources, selected: null })
integrations.set(_integrations) integrations.set(_integrations)
queries.set({ list: _queries, selected: null }) queries.set({ list: _queries, selected: null })
database.set(application.instance)
await backendUiStore.actions.database.select(application.instance)
}, },
routing: { routing: {
fetch: async () => { fetch: async () => {

View File

@ -1,8 +1,7 @@
<script> <script>
import { onMount } from "svelte" import { onMount } from "svelte"
import { goto } from "@sveltech/routify" import { goto } from "@sveltech/routify"
import { backendUiStore } from "builderStore" import { database, datasources, queries } from 'builderStore/store/backend/'
import { datasources, queries } from 'builderStore/store/backend/'
import EditDatasourcePopover from "./popovers/EditDatasourcePopover.svelte" import EditDatasourcePopover from "./popovers/EditDatasourcePopover.svelte"
import EditQueryPopover from "./popovers/EditQueryPopover.svelte" import EditQueryPopover from "./popovers/EditQueryPopover.svelte"
import NavItem from "components/common/NavItem.svelte" import NavItem from "components/common/NavItem.svelte"
@ -14,7 +13,7 @@
} }
function onClickQuery(query) { function onClickQuery(query) {
if ($backendUiStore.selectedQueryId === query._id) { if ($queries.selected === query._id) {
return return
} }
queries.select(query) queries.select(query)
@ -27,7 +26,7 @@
}) })
</script> </script>
{#if $backendUiStore.selectedDatabase && $backendUiStore.selectedDatabase._id} {#if $database?._id}
<div class="hierarchy-items-container"> <div class="hierarchy-items-container">
{#each $datasources.list as datasource, idx} {#each $datasources.list as datasource, idx}
<NavItem <NavItem

View File

@ -1,5 +1,5 @@
<script> <script>
import { goto, params } from "@sveltech/routify" import { goto } from "@sveltech/routify"
import { datasources } from 'builderStore/store/backend/' import { datasources } from 'builderStore/store/backend/'
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { Input, Label, ModalContent } from "@budibase/bbui" import { Input, Label, ModalContent } from "@budibase/bbui"
@ -14,7 +14,7 @@
function checkValid(evt) { function checkValid(evt) {
const datasourceName = evt.target.value const datasourceName = evt.target.value
if ( if (
$datasources?.sources.some( $datasources?.list.some(
datasource => datasource.name === datasourceName datasource => datasource.name === datasourceName
) )
) { ) {