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

89 lines
2.1 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 {
supportsSearch: true,
supportsSort: true,
supportsPagination: true,
}
}
getSchema(datasource, definition) {
return definition?.schema
2023-07-19 10:16:12 +02:00
}
async getDefinition(datasource) {
if (!datasource?.id) {
return null
}
2023-07-19 10:16:12 +02:00
try {
const res = await this.API.viewV2.fetchDefinition(datasource.id)
return res?.data
2023-07-19 10:16:12 +02:00
} catch (error) {
this.store.update(state => ({
...state,
error,
}))
2023-07-19 10:16:12 +02:00
return null
}
}
getDefaultSortColumn() {
return null
}
2023-07-19 10:16:12 +02:00
async getData() {
const {
datasource,
limit,
sortColumn,
sortOrder,
sortType,
paginate,
filter,
} = this.options
const { cursor, query, definition } = get(this.store)
// If sort params are not defined, update options to store the sorting
// params built in to this view. This ensures that we can accurately
// compare old and new sorting params and skip a redundant API call.
if (!sortColumn && definition.sort?.field) {
this.options.sortColumn = definition.sort.field
this.options.sortOrder = definition.sort.order
}
// If sort params are not defined, update options to store the sorting
// params built in to this view. This ensures that we can accurately
// compare old and new sorting params and skip a redundant API call.
if (!filter?.length && definition.query?.length) {
this.options.filter = definition.query
}
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,
query,
2023-08-08 14:13:27 +02:00
paginate,
limit,
bookmark: cursor,
sort: sortColumn,
sortOrder: sortOrder?.toLowerCase(),
2023-08-08 14:13:27 +02:00
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
}
}
}