budibase/packages/frontend-core/src/fetch/ViewV2Fetch.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-07-19 10:16:12 +02:00
import DataFetch from "./DataFetch.js"
2023-08-08 14:13:27 +02:00
import { get } from "svelte/store"
2023-07-19 10:16:12 +02:00
export default class ViewV2Fetch extends DataFetch {
2023-08-01 16:34:02 +02:00
determineFeatureFlags() {
return {
2023-08-08 14:13:27 +02:00
// The API does not actually support dynamic filtering, but since views
// have filters built in we don't want to perform client side filtering
// which would happen if we marked this as false
2023-08-01 16:34:02 +02:00
supportsSearch: true,
supportsSort: true,
supportsPagination: true,
}
}
2023-07-19 10:16:12 +02:00
async getSchema(datasource, definition) {
return definition?.schema
2023-07-19 10:16:12 +02:00
}
async getDefinition(datasource) {
try {
2023-07-21 13:25:13 +02:00
const table = await this.API.fetchTableDefinition(datasource.tableId)
return Object.values(table.views || {}).find(
view => view.id === datasource.id
)
2023-07-19 10:16:12 +02:00
} catch (error) {
this.store.update(state => ({
...state,
error,
}))
return null
}
}
async getData() {
2023-08-08 14:13:27 +02:00
const { datasource, limit, sortColumn, sortOrder, sortType, paginate } =
this.options
const { cursor } = get(this.store)
2023-07-19 10:16:12 +02:00
try {
2023-08-08 14:13:27 +02:00
const res = await this.API.viewV2.fetch({
viewId: datasource.id,
paginate,
limit,
bookmark: cursor,
sort: sortColumn,
sortOrder,
sortType,
})
return {
rows: res?.rows || [],
hasNextPage: res?.hasNextPage || false,
cursor: res?.bookmark || null,
}
2023-07-19 10:16:12 +02:00
} catch (error) {
2023-08-08 14:13:27 +02:00
return {
rows: [],
hasNextPage: false,
error,
}
2023-07-19 10:16:12 +02:00
}
}
}