budibase/packages/frontend-core/src/fetch/ViewFetch.ts

42 lines
1.0 KiB
TypeScript
Raw Normal View History

2025-01-02 16:25:37 +01:00
import { Table, View } from "@budibase/types"
import DataFetch from "./DataFetch.js"
2025-01-02 16:25:37 +01:00
type ViewV1 = View & { name: string }
export default class ViewFetch extends DataFetch<ViewV1, Table> {
async getDefinition(datasource: ViewV1) {
if (!datasource?.tableId) {
return null
}
try {
return await this.API.fetchTableDefinition(datasource.tableId)
} catch (error: any) {
this.store.update(state => ({
...state,
error,
}))
return null
}
}
getSchema(datasource: ViewV1, definition: Table) {
2021-12-17 19:48:44 +01:00
return definition?.views?.[datasource.name]?.schema
}
async getData() {
const { datasource } = this.options
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,
})
return { rows: res || [] }
} catch (error) {
2025-01-07 16:52:01 +01:00
console.error(error)
return { rows: [] }
}
}
}