Verify datasouce before saving

This commit is contained in:
Adria Navarro 2023-05-23 15:21:33 +02:00
parent 22dde1fb79
commit 886b726627
3 changed files with 45 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import { datasources, tables } from "../stores/backend"
import { IntegrationNames } from "../constants/backend"
import { get } from "svelte/store"
import cloneDeep from "lodash/cloneDeepWith"
import { API } from "api"
function prepareData(config) {
let datasource = {}
@ -37,3 +38,9 @@ export async function createRestDatasource(integration) {
const config = cloneDeep(integration)
return saveDatasource(config)
}
export async function validateDatasourceConfig(config) {
const datasource = prepareData(config)
const resp = await API.validateDatasource(datasource)
return resp
}

View File

@ -4,7 +4,10 @@
import IntegrationConfigForm from "components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte"
import { IntegrationNames } from "constants/backend"
import cloneDeep from "lodash/cloneDeepWith"
import { saveDatasource as save } from "builderStore/datasource"
import {
saveDatasource as save,
validateDatasourceConfig,
} from "builderStore/datasource"
import { onMount } from "svelte"
export let integration
@ -18,6 +21,27 @@
$: name =
IntegrationNames[datasource.type] || datasource.name || datasource.type
async function validateConfig() {
function displayError(message) {
notifications.error(message ?? "Error validating datasource")
}
let connected = false
try {
if (!datasource.name) {
datasource.name = name
}
const resp = await validateDatasourceConfig(datasource)
if (!resp.connected) {
displayError(resp.error)
}
connected = resp.connected
} catch (err) {
displayError(err?.message)
}
return connected
}
async function saveDatasource() {
try {
if (!datasource.name) {
@ -40,7 +64,8 @@
<ModalContent
title={`Connect to ${name}`}
onConfirm={() => saveDatasource()}
onConfirm={() =>
validateConfig().then(connected => connected && saveDatasource())}
onCancel={() => modal.show()}
confirmText={datasource.plus
? "Save and fetch tables"

View File

@ -58,4 +58,15 @@ export const buildDatasourceEndpoints = API => ({
url: `/api/datasources/${datasourceId}/${datasourceRev}`,
})
},
/**
* Validate a datasource configuration
* @param datasource the datasource configuration to validate
*/
validateDatasource: async datasource => {
return await API.post({
url: `/api/datasources/verify`,
body: { datasource },
})
},
})