2025-01-02 16:27:43 +01:00
|
|
|
import DataFetch from "./DataFetch"
|
2022-01-31 20:02:59 +01:00
|
|
|
import { Helpers } from "@budibase/bbui"
|
2025-01-08 13:37:28 +01:00
|
|
|
import { ExecuteQueryRequest, Query } from "@budibase/types"
|
2021-12-17 19:39:48 +01:00
|
|
|
import { get } from "svelte/store"
|
2021-12-17 09:22:04 +01:00
|
|
|
|
2025-01-07 11:47:10 +01:00
|
|
|
interface QueryDatasource {
|
2025-01-09 15:23:20 +01:00
|
|
|
type: "query"
|
2025-01-07 11:47:10 +01:00
|
|
|
_id: string
|
2025-01-08 13:23:18 +01:00
|
|
|
fields: Record<string, any> & {
|
|
|
|
pagination?: {
|
|
|
|
type: string
|
|
|
|
location: string
|
|
|
|
pageParam: string
|
|
|
|
}
|
|
|
|
}
|
2025-01-08 13:37:28 +01:00
|
|
|
queryParams?: Record<string, string>
|
2025-01-08 13:23:18 +01:00
|
|
|
parameters: { name: string; default: string }[]
|
2025-01-07 11:47:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class QueryFetch extends DataFetch<QueryDatasource, Query> {
|
2025-01-08 12:49:17 +01:00
|
|
|
async determineFeatureFlags() {
|
2025-01-08 14:27:13 +01:00
|
|
|
const definition = await this.getDefinition()
|
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
|
|
|
}
|
|
|
|
|
2025-01-08 14:27:13 +01:00
|
|
|
async getDefinition() {
|
|
|
|
const { datasource } = this.options
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-21 12:56:58 +02:00
|
|
|
getDefaultSortColumn() {
|
|
|
|
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
|
2025-01-08 13:37:28 +01:00
|
|
|
const parameters = Helpers.cloneDeep(datasource.queryParams || {})
|
2025-01-08 13:23:18 +01:00
|
|
|
for (const param of datasource?.parameters || []) {
|
2021-12-17 09:22:04 +01:00
|
|
|
if (!parameters[param.name]) {
|
|
|
|
parameters[param.name] = param.default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 19:39:48 +01:00
|
|
|
// Add pagination to query if supported
|
2025-01-08 13:37:28 +01:00
|
|
|
const queryPayload: ExecuteQueryRequest = { parameters }
|
2022-01-07 12:30:47 +01:00
|
|
|
if (paginate && supportsPagination) {
|
2025-01-07 11:47:10 +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 {
|
2024-12-03 10:59:08 +01:00
|
|
|
const res = await this.API.executeQuery(datasource?._id, queryPayload)
|
2022-01-20 10:40:53 +01:00
|
|
|
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
|
2025-01-07 12:06:37 +01:00
|
|
|
nextCursor = queryPayload.pagination!.page! + 1
|
2022-01-20 10:40:53 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|