1
0
Fork 0
mirror of synced 2024-06-29 11:31:06 +12:00
budibase/packages/builder/src/components/backend/DatasourceNavigator/modals/DatasourceConfigModal.svelte

80 lines
2.2 KiB
Svelte
Raw Normal View History

2021-09-24 08:47:22 +12:00
<script>
2021-09-24 21:19:48 +12:00
import { goto } from "@roxi/routify"
2021-09-24 22:28:56 +12:00
import { ModalContent, notifications, Body, Layout } from "@budibase/bbui"
2021-09-24 21:58:04 +12:00
import analytics, { Events } from "analytics"
2021-09-24 08:47:22 +12:00
import IntegrationConfigForm from "components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte"
import { datasources, tables } from "stores/backend"
2021-09-24 08:47:22 +12:00
import { IntegrationNames } from "constants"
import cloneDeep from "lodash/cloneDeepWith"
2021-09-24 08:47:22 +12:00
export let integration
export let modal
// kill the reference so the input isn't saved
let config = cloneDeep(integration)
2021-09-24 21:19:48 +12:00
2021-09-24 08:47:22 +12:00
function prepareData() {
let datasource = {}
let existingTypeCount = $datasources.list.filter(
ds => ds.source == config.type
2021-09-24 08:47:22 +12:00
).length
let baseName = IntegrationNames[config.type]
2021-09-24 08:47:22 +12:00
let name =
existingTypeCount === 0
? baseName
: `${baseName}-${existingTypeCount + 1}`
2021-09-24 08:47:22 +12:00
datasource.type = "datasource"
datasource.source = config.type
datasource.config = config.config
2021-09-24 08:47:22 +12:00
datasource.name = name
datasource.plus = config.plus
2021-09-24 08:47:22 +12:00
return datasource
}
async function saveDatasource() {
const datasource = prepareData()
2021-09-24 08:47:22 +12:00
try {
// Create datasource
const resp = await datasources.save(datasource, datasource.plus)
2021-09-24 21:01:53 +12:00
// update the tables incase data source plus
await tables.fetch()
2021-09-24 21:19:48 +12:00
await datasources.select(resp._id)
2021-09-27 22:04:01 +13:00
$goto(`./datasource/${resp._id}`)
2021-09-24 08:47:22 +12:00
notifications.success(`Datasource updated successfully.`)
2021-09-24 21:58:04 +12:00
analytics.captureEvent(Events.DATASOURCE.CREATED, {
name: resp.name,
source: resp.source,
})
return true
2021-09-24 08:47:22 +12:00
} catch (err) {
notifications.error(`Error saving datasource: ${err}`)
return false
2021-09-24 21:01:53 +12:00
}
}
2021-09-24 08:47:22 +12:00
</script>
<ModalContent
title={`Connect to ${IntegrationNames[config.type]}`}
2021-09-24 08:47:22 +12:00
onConfirm={() => saveDatasource()}
onCancel={() => modal.show()}
confirmText={config.plus
2021-09-24 21:01:53 +12:00
? "Fetch tables from database"
: "Save and continue to query"}
cancelText="Back"
size="L"
2021-09-24 08:47:22 +12:00
>
2021-09-24 22:28:56 +12:00
<Layout noPadding>
<Body size="XS"
>Connect your database to Budibase using the config below.
</Body>
</Layout>
<IntegrationConfigForm schema={config.schema} integration={config.config} />
2021-09-24 08:47:22 +12:00
</ModalContent>
<style>
</style>