2025-01-02 16:25:37 +01:00
|
|
|
import { Table, View } from "@budibase/types"
|
2025-01-02 16:27:43 +01:00
|
|
|
import DataFetch from "./DataFetch"
|
2021-12-17 09:22:04 +01:00
|
|
|
|
2025-01-02 16:25:37 +01:00
|
|
|
type ViewV1 = View & { name: string }
|
|
|
|
|
|
|
|
export default class ViewFetch extends DataFetch<ViewV1, Table> {
|
2025-01-08 14:27:13 +01:00
|
|
|
async getDefinition() {
|
|
|
|
const { datasource } = this.options
|
|
|
|
|
2025-01-02 16:25:37 +01:00
|
|
|
if (!datasource?.tableId) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return await this.API.fetchTableDefinition(datasource.tableId)
|
|
|
|
} catch (error: any) {
|
|
|
|
this.store.update(state => ({
|
|
|
|
...state,
|
|
|
|
error,
|
|
|
|
}))
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-08 12:57:25 +01:00
|
|
|
getSchema(definition: Table) {
|
|
|
|
const { datasource } = this.options
|
2021-12-17 19:48:44 +01:00
|
|
|
return definition?.views?.[datasource.name]?.schema
|
2021-12-17 09:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async getData() {
|
|
|
|
const { datasource } = this.options
|
2022-01-20 10:40:53 +01:00
|
|
|
try {
|
2025-01-07 16:52:01 +01:00
|
|
|
const res = await this.API.fetchViewData(datasource.name, {
|
|
|
|
calculation: datasource.calculation,
|
|
|
|
field: datasource.field,
|
|
|
|
groupBy: datasource.groupBy,
|
|
|
|
tableId: datasource.tableId,
|
|
|
|
})
|
2022-01-20 10:40:53 +01:00
|
|
|
return { rows: res || [] }
|
|
|
|
} catch (error) {
|
2025-01-08 12:28:36 +01:00
|
|
|
console.error(error, { datasource })
|
2022-01-20 10:40:53 +01:00
|
|
|
return { rows: [] }
|
2021-12-17 09:22:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|