update the name of the datasource

This commit is contained in:
Maurits Lourens 2021-08-17 23:57:11 +02:00
parent 34edf4fb14
commit c86a878b27
6 changed files with 107 additions and 4 deletions

View File

@ -23,8 +23,11 @@
dropdown.show() dropdown.show()
} }
const openMenu = () => { const openMenu = (event) => {
if (!disabled) show() if (!disabled) {
event.stopPropagation()
show()
}
} }
setContext("actionMenu", { show, hide }) setContext("actionMenu", { show, hide })

View File

@ -0,0 +1,62 @@
<script>
import { datasources } from "stores/backend"
import { notifications } from "@budibase/bbui"
import { Input, ModalContent, Modal } from "@budibase/bbui"
import analytics from "analytics"
let error = ""
let modal
let name
export let datasource
export let onCancel = undefined
export const show = () => {
name = datasource?.name
modal.show()
}
export const hide = () => {
modal.hide()
}
function checkValid(evt) {
const datasourceName = evt.target.value
if (
$datasources?.list.some(ds => ds.name === datasourceName)
) {
error = `Datasource with name ${datasourceName} already exists. Please choose another name.`
return
}
error = ""
}
async function updateDatasource() {
const updatedDatasource = {
...datasource,
name,
}
await datasources.save(updatedDatasource)
notifications.success(`Datasource ${name} updated successfully.`)
analytics.captureEvent("Datasource Updated", updatedDatasource)
hide()
}
</script>
<Modal bind:this={modal} on:hide={onCancel}>
<ModalContent
title="Update Datasource"
size="L"
confirmText="Update"
onConfirm={updateDatasource}
disabled={error || !name || !datasource?.type}
>
<Input
data-cy="datasource-name-input"
label="Datasource Name"
on:input={checkValid}
bind:value={name}
{error}
/>
</ModalContent>
</Modal>

View File

@ -4,10 +4,12 @@
import { notifications } from "@budibase/bbui" import { notifications } from "@budibase/bbui"
import { ActionMenu, MenuItem, Icon } from "@budibase/bbui" import { ActionMenu, MenuItem, Icon } from "@budibase/bbui"
import ConfirmDialog from "components/common/ConfirmDialog.svelte" import ConfirmDialog from "components/common/ConfirmDialog.svelte"
import UpdateDatasourceModal from "components/backend/DatasourceNavigator/modals/UpdateDatasourceModal.svelte"
export let datasource export let datasource
let confirmDeleteDialog let confirmDeleteDialog
let updateDatasourceDialog
async function deleteDatasource() { async function deleteDatasource() {
const wasSelectedSource = $datasources.selected const wasSelectedSource = $datasources.selected
@ -24,6 +26,7 @@
<div slot="control" class="icon"> <div slot="control" class="icon">
<Icon size="S" hoverable name="MoreSmallList" /> <Icon size="S" hoverable name="MoreSmallList" />
</div> </div>
<MenuItem icon="Edit" on:click={updateDatasourceDialog.show}>Update</MenuItem>
<MenuItem icon="Delete" on:click={confirmDeleteDialog.show}>Delete</MenuItem> <MenuItem icon="Delete" on:click={confirmDeleteDialog.show}>Delete</MenuItem>
</ActionMenu> </ActionMenu>
@ -37,6 +40,7 @@
<i>{datasource.name}?</i> <i>{datasource.name}?</i>
This action cannot be undone. This action cannot be undone.
</ConfirmDialog> </ConfirmDialog>
<UpdateDatasourceModal {datasource} bind:this={updateDatasourceDialog} />
<style> <style>
div.icon { div.icon {

View File

@ -59,9 +59,16 @@ export function createDatasourcesStore() {
return json return json
}, },
save: async datasource => { save: async datasource => {
let url = "/api/datasources" let response
if (datasource._id) {
response = await api.put(
`/api/datasources/${datasource._id}`,
datasource
)
} else {
response = await api.post("/api/datasources", datasource)
}
const response = await api.post(url, datasource)
const json = await response.json() const json = await response.json()
if (response.status !== 200) { if (response.status !== 200) {

View File

@ -57,6 +57,28 @@ exports.buildSchemaFromDb = async function (ctx) {
ctx.body = datasource ctx.body = datasource
} }
exports.update = async function (ctx) {
const db = new CouchDB(ctx.appId)
const datasourceId = ctx.params.datasourceId
const datasource = await db.get(datasourceId)
datasource.name = ctx.request.body.name
const response = await db.put(datasource)
datasource._rev = response.rev
// Drain connection pools when configuration is changed
if (datasource.source) {
const source = integrations[datasource.source]
if (source && source.pool) {
await source.pool.end()
}
}
ctx.status = 200
ctx.message = "Datasource saved successfully."
ctx.body = datasource
}
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.plus

View File

@ -66,6 +66,11 @@ router
authorized(PermissionTypes.TABLE, PermissionLevels.READ), authorized(PermissionTypes.TABLE, PermissionLevels.READ),
datasourceController.find datasourceController.find
) )
.put(
"/api/datasources/:datasourceId",
authorized(PermissionTypes.TABLE, PermissionLevels.READ),
datasourceController.update
)
.post( .post(
"/api/datasources/query", "/api/datasources/query",
authorized(PermissionTypes.TABLE, PermissionLevels.READ), authorized(PermissionTypes.TABLE, PermissionLevels.READ),