check tables can be fetched before saving config

This commit is contained in:
Peter Clement 2021-09-28 12:25:57 +01:00
parent 2a4d6ce645
commit 5ca4da1146
4 changed files with 32 additions and 32 deletions

View File

@ -3,7 +3,7 @@
import { ModalContent, notifications, Body, Layout } from "@budibase/bbui" import { ModalContent, notifications, Body, Layout } from "@budibase/bbui"
import analytics, { Events } from "analytics" import analytics, { Events } from "analytics"
import IntegrationConfigForm from "components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte" import IntegrationConfigForm from "components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte"
import { datasources, tables } from "stores/backend" import { datasources } from "stores/backend"
import { IntegrationNames } from "constants" import { IntegrationNames } from "constants"
export let integration export let integration
@ -27,13 +27,11 @@
return datasource return datasource
} }
async function saveDatasource() { async function saveDatasource() {
const datasource = prepareData()
try { try {
// Create datasource // Create datasource
const resp = await datasources.save(prepareData()) const resp = await datasources.save(datasource, datasource.plus)
if (integration.plus) {
updateDatasourceSchema(resp)
}
await datasources.select(resp._id) await datasources.select(resp._id)
$goto(`./datasource/${resp._id}`) $goto(`./datasource/${resp._id}`)
notifications.success(`Datasource updated successfully.`) notifications.success(`Datasource updated successfully.`)
@ -41,17 +39,10 @@
name: resp.name, name: resp.name,
source: resp.source, source: resp.source,
}) })
return true
} catch (err) { } catch (err) {
notifications.error(`Error saving datasource: ${err}`) notifications.error(`Error saving datasource: ${err}`)
} return false
}
async function updateDatasourceSchema(datasourceJson) {
try {
await datasources.updateSchema(datasourceJson)
await tables.fetch()
} catch (err) {
notifications.error(`Error updating datasource schema: ${err}`)
} }
} }
</script> </script>

View File

@ -9,12 +9,6 @@
$datasources.list.find(x => (x._id = "bb_internal")).entities.length > 1 || $datasources.list.find(x => (x._id = "bb_internal")).entities.length > 1 ||
$datasources.list.length > 1 $datasources.list.length > 1
$: console.log(
$datasources.list.find(x => (x._id = "bb_internal")).entities.length > 1
)
$: console.log($datasources.list.length >= 1)
$: console.log($datasources.list)
onMount(() => { onMount(() => {
if (!setupComplete) { if (!setupComplete) {
modal.show() modal.show()

View File

@ -58,7 +58,7 @@ export function createDatasourcesStore() {
}) })
return json return json
}, },
save: async datasource => { save: async (datasource, fetchSchema = false) => {
let response let response
if (datasource._id) { if (datasource._id) {
response = await api.put( response = await api.put(
@ -66,7 +66,10 @@ export function createDatasourcesStore() {
datasource datasource
) )
} else { } else {
response = await api.post("/api/datasources", datasource) response = await api.post("/api/datasources", {
datasource: datasource,
fetchSchema,
})
} }
const json = await response.json() const json = await response.json()

View File

@ -41,15 +41,10 @@ exports.fetch = async function (ctx) {
exports.buildSchemaFromDb = async function (ctx) { exports.buildSchemaFromDb = async function (ctx) {
const db = new CouchDB(ctx.appId) const db = new CouchDB(ctx.appId)
const datasourceId = ctx.params.datasourceId const datasource = await db.get(ctx.params.datasourceId)
const datasource = await db.get(datasourceId)
const Connector = integrations[datasource.source] const tables = await buildSchemaHelper(datasource)
datasource.entities = tables
// Connect to the DB and build the schema
const connector = new Connector(datasource.config)
await connector.buildSchema(datasource._id, datasource.entities)
datasource.entities = connector.tables
const response = await db.put(datasource) const response = await db.put(datasource)
datasource._rev = response.rev datasource._rev = response.rev
@ -81,12 +76,18 @@ exports.update = async function (ctx) {
exports.save = async function (ctx) { exports.save = async function (ctx) {
const db = new CouchDB(ctx.appId) const db = new CouchDB(ctx.appId)
const plus = ctx.request.body.plus const plus = ctx.request.body.datasource.plus
const fetchSchema = ctx.request.body.fetchSchema
const datasource = { const datasource = {
_id: generateDatasourceID({ plus }), _id: generateDatasourceID({ plus }),
type: plus ? DocumentTypes.DATASOURCE_PLUS : DocumentTypes.DATASOURCE, type: plus ? DocumentTypes.DATASOURCE_PLUS : DocumentTypes.DATASOURCE,
...ctx.request.body, ...ctx.request.body.datasource,
}
if (fetchSchema) {
let tables = await buildSchemaHelper(datasource)
datasource.entities = tables
} }
const response = await db.put(datasource) const response = await db.put(datasource)
@ -133,3 +134,14 @@ exports.query = async function (ctx) {
ctx.throw(400, err) ctx.throw(400, err)
} }
} }
const buildSchemaHelper = async datasource => {
const Connector = integrations[datasource.source]
// Connect to the DB and build the schema
const connector = new Connector(datasource.config)
await connector.buildSchema(datasource._id, datasource.entities)
datasource.entities = connector.tables
return connector.tables
}