2021-12-17 10:52:12 +01:00
|
|
|
import DataFetch from "./DataFetch.js"
|
2022-01-31 20:02:59 +01:00
|
|
|
import { Helpers } from "@budibase/bbui"
|
2021-12-17 19:39:48 +01:00
|
|
|
import { get } from "svelte/store"
|
2021-12-17 09:22:04 +01:00
|
|
|
|
2021-12-17 19:39:48 +01:00
|
|
|
export default class QueryFetch extends DataFetch {
|
|
|
|
determineFeatureFlags(definition) {
|
2022-01-05 10:16:10 +01:00
|
|
|
const supportsPagination =
|
2022-01-07 12:03:55 +01:00
|
|
|
!!definition?.fields?.pagination?.type &&
|
|
|
|
!!definition?.fields?.pagination?.location &&
|
|
|
|
!!definition?.fields?.pagination?.pageParam
|
2022-01-05 10:16:10 +01:00
|
|
|
return { supportsPagination }
|
2021-12-17 19:39:48 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 12:50:18 +01:00
|
|
|
async getDefinition(datasource) {
|
2021-12-17 09:22:04 +01:00
|
|
|
if (!datasource?._id) {
|
|
|
|
return null
|
|
|
|
}
|
2022-01-20 10:40:53 +01:00
|
|
|
try {
|
2022-02-01 17:46:00 +01:00
|
|
|
const definition = await this.API.fetchQueryDefinition(datasource._id)
|
|
|
|
// After getting the definition of query, it loses "fields" attribute
|
|
|
|
// because of security reason from the server. However, this attribute
|
|
|
|
// needs to be inside the definition for pagination.
|
|
|
|
if (!definition.fields) {
|
|
|
|
definition.fields = datasource.fields
|
|
|
|
}
|
|
|
|
return definition
|
2022-01-20 10:40:53 +01:00
|
|
|
} catch (error) {
|
|
|
|
return null
|
|
|
|
}
|
2021-12-17 09:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async getData() {
|
2022-01-07 12:30:47 +01:00
|
|
|
const { datasource, limit, paginate } = this.options
|
2023-03-10 17:23:56 +01:00
|
|
|
const { supportsPagination } = this.features
|
2022-01-05 10:16:10 +01:00
|
|
|
const { cursor, definition } = get(this.store)
|
2022-01-10 13:45:30 +01:00
|
|
|
const type = definition?.fields?.pagination?.type
|
2021-12-17 09:22:04 +01:00
|
|
|
|
|
|
|
// Set the default query params
|
2022-01-31 20:02:59 +01:00
|
|
|
let parameters = Helpers.cloneDeep(datasource?.queryParams || {})
|
2021-12-17 09:22:04 +01:00
|
|
|
for (let param of datasource?.parameters || {}) {
|
|
|
|
if (!parameters[param.name]) {
|
|
|
|
parameters[param.name] = param.default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 19:39:48 +01:00
|
|
|
// Add pagination to query if supported
|
|
|
|
let queryPayload = { queryId: datasource?._id, parameters }
|
2022-01-07 12:30:47 +01:00
|
|
|
if (paginate && supportsPagination) {
|
2022-01-05 18:28:57 +01:00
|
|
|
const requestCursor = type === "page" ? parseInt(cursor || 1) : cursor
|
2022-01-05 10:16:10 +01:00
|
|
|
queryPayload.pagination = { page: requestCursor, limit }
|
2021-12-17 19:39:48 +01:00
|
|
|
}
|
|
|
|
|
2022-01-05 10:16:10 +01:00
|
|
|
// Execute query
|
2022-01-20 10:40:53 +01:00
|
|
|
try {
|
|
|
|
const res = await this.API.executeQuery(queryPayload)
|
|
|
|
const { data, pagination, ...rest } = res
|
2022-01-05 10:16:10 +01:00
|
|
|
|
2022-01-20 10:40:53 +01:00
|
|
|
// Derive pagination info from response
|
|
|
|
let nextCursor = null
|
|
|
|
let hasNextPage = false
|
|
|
|
if (paginate && supportsPagination) {
|
|
|
|
if (type === "page") {
|
|
|
|
// For "page number" pagination, increment the existing page number
|
|
|
|
nextCursor = queryPayload.pagination.page + 1
|
|
|
|
hasNextPage = data?.length === limit && limit > 0
|
|
|
|
} else {
|
|
|
|
// For "cursor" pagination, the cursor should be in the response
|
|
|
|
nextCursor = pagination?.cursor
|
|
|
|
hasNextPage = nextCursor != null
|
|
|
|
}
|
2022-01-05 10:16:10 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 10:40:53 +01:00
|
|
|
return {
|
|
|
|
rows: data || [],
|
|
|
|
info: rest,
|
|
|
|
cursor: nextCursor,
|
|
|
|
hasNextPage,
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
rows: [],
|
|
|
|
hasNextPage: false,
|
|
|
|
}
|
2021-12-17 09:22:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|