Budibase is an open-source low-code platform for creating internal apps in minutes. Supports PostgreSQL, MySQL, MSSQL, MongoDB, Rest API, Docker, K8s 🚀
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

54 lines
1.4 KiB

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()