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"
|
2021-10-07 03:31:32 +13:00
|
|
|
import { datasources, tables } from "stores/backend"
|
2021-09-24 08:47:22 +12:00
|
|
|
import { IntegrationNames } from "constants"
|
2021-10-07 03:06:48 +13:00
|
|
|
import cloneDeep from "lodash/cloneDeepWith"
|
2021-09-24 08:47:22 +12:00
|
|
|
|
|
|
|
export let integration
|
2021-10-07 03:06:48 +13:00
|
|
|
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(
|
2021-10-07 03:06:48 +13:00
|
|
|
ds => ds.source == config.type
|
2021-09-24 08:47:22 +12:00
|
|
|
).length
|
|
|
|
|
2021-10-07 03:06:48 +13:00
|
|
|
let baseName = IntegrationNames[config.type]
|
2021-09-24 08:47:22 +12:00
|
|
|
let name =
|
2021-10-07 04:27:46 +13:00
|
|
|
existingTypeCount === 0
|
|
|
|
? baseName
|
|
|
|
: `${baseName}-${existingTypeCount + 1}`
|
2021-09-24 08:47:22 +12:00
|
|
|
|
|
|
|
datasource.type = "datasource"
|
2021-10-07 03:06:48 +13:00
|
|
|
datasource.source = config.type
|
|
|
|
datasource.config = config.config
|
2021-09-24 08:47:22 +12:00
|
|
|
datasource.name = name
|
2021-10-07 03:06:48 +13:00
|
|
|
datasource.plus = config.plus
|
2021-09-24 08:47:22 +12:00
|
|
|
|
|
|
|
return datasource
|
|
|
|
}
|
|
|
|
async function saveDatasource() {
|
2021-09-29 00:25:57 +13:00
|
|
|
const datasource = prepareData()
|
2021-09-24 08:47:22 +12:00
|
|
|
try {
|
|
|
|
// Create datasource
|
2021-09-29 00:25:57 +13:00
|
|
|
const resp = await datasources.save(datasource, datasource.plus)
|
2021-09-24 21:01:53 +12:00
|
|
|
|
2021-10-07 03:31:32 +13: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,
|
|
|
|
})
|
2021-09-29 00:25:57 +13:00
|
|
|
return true
|
2021-09-24 08:47:22 +12:00
|
|
|
} catch (err) {
|
|
|
|
notifications.error(`Error saving datasource: ${err}`)
|
2021-09-29 00:25:57 +13:00
|
|
|
return false
|
2021-09-24 21:01:53 +12:00
|
|
|
}
|
|
|
|
}
|
2021-09-24 08:47:22 +12:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<ModalContent
|
2021-10-07 03:06:48 +13:00
|
|
|
title={`Connect to ${IntegrationNames[config.type]}`}
|
2021-09-24 08:47:22 +12:00
|
|
|
onConfirm={() => saveDatasource()}
|
2021-10-07 03:06:48 +13:00
|
|
|
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"
|
2021-10-13 04:13:54 +13:00
|
|
|
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>
|
|
|
|
|
2021-10-07 03:06:48 +13:00
|
|
|
<IntegrationConfigForm schema={config.schema} integration={config.config} />
|
2021-09-24 08:47:22 +12:00
|
|
|
</ModalContent>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
</style>
|