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

45 lines
1.1 KiB
TypeScript
Raw Normal View History

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"
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
}
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-08 12:28:36 +01:00
console.error(error, { datasource })
return { rows: [] }
}
}
}