budibase/packages/client/src/api/queries.js

35 lines
989 B
JavaScript
Raw Normal View History

import { notificationStore, dataSourceStore } from "stores"
2020-12-30 13:58:39 +01:00
import API from "./api"
/**
* Executes a query against an external data connector.
*/
export const executeQuery = async ({ queryId, pagination, parameters }) => {
const query = await fetchQueryDefinition(queryId)
if (query?.datasourceId == null) {
notificationStore.actions.error("That query couldn't be found")
return
}
2021-01-25 12:57:04 +01:00
const res = await API.post({
url: `/api/v2/queries/${queryId}`,
body: {
2021-01-08 19:22:03 +01:00
parameters,
pagination,
},
})
if (res.error) {
notificationStore.actions.error("An error has occurred")
} else if (!query.readable) {
notificationStore.actions.success("Query executed successfully")
await dataSourceStore.actions.invalidateDataSource(query.datasourceId)
}
2021-01-25 13:36:35 +01:00
return res
}
/**
* Fetches the definition of an external query.
*/
export const fetchQueryDefinition = async queryId => {
return await API.get({ url: `/api/queries/${queryId}`, cache: true })
}