2021-09-01 12:41:48 +02:00
|
|
|
import { notificationStore, dataSourceStore } from "stores"
|
2020-12-30 13:58:39 +01:00
|
|
|
import API from "./api"
|
|
|
|
|
2021-01-04 19:57:16 +01:00
|
|
|
/**
|
|
|
|
* Executes a query against an external data connector.
|
|
|
|
*/
|
2021-02-08 10:51:20 +01:00
|
|
|
export const executeQuery = async ({ queryId, parameters }) => {
|
|
|
|
const query = await API.get({ url: `/api/queries/${queryId}` })
|
|
|
|
if (query?.datasourceId == null) {
|
2021-07-30 15:01:01 +02:00
|
|
|
notificationStore.actions.error("That query couldn't be found")
|
2021-02-08 10:51:20 +01:00
|
|
|
return
|
|
|
|
}
|
2021-01-25 12:57:04 +01:00
|
|
|
const res = await API.post({
|
2021-01-08 13:06:37 +01:00
|
|
|
url: `/api/queries/${queryId}`,
|
|
|
|
body: {
|
2021-01-08 19:22:03 +01:00
|
|
|
parameters,
|
2021-01-08 13:06:37 +01:00
|
|
|
},
|
2021-01-04 19:57:16 +01:00
|
|
|
})
|
2021-01-26 10:31:41 +01:00
|
|
|
if (res.error) {
|
2021-07-30 15:01:01 +02:00
|
|
|
notificationStore.actions.error("An error has occurred")
|
2021-02-08 10:51:20 +01:00
|
|
|
} else if (!query.readable) {
|
2021-07-30 15:01:01 +02:00
|
|
|
notificationStore.actions.success("Query executed successfully")
|
2021-03-16 14:54:34 +01:00
|
|
|
dataSourceStore.actions.invalidateDataSource(query.datasourceId)
|
2021-01-26 10:31:41 +01:00
|
|
|
}
|
2021-01-25 13:36:35 +01:00
|
|
|
return res
|
2021-01-04 19:57:16 +01:00
|
|
|
}
|