2021-12-17 10:52:12 +01:00
|
|
|
import DataFetch from "./DataFetch.js"
|
2021-12-17 09:22:04 +01:00
|
|
|
import { executeQuery, fetchQueryDefinition } from "api"
|
2021-12-17 10:52:12 +01:00
|
|
|
import { cloneDeep } from "lodash/fp"
|
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 =
|
2021-12-17 19:39:48 +01:00
|
|
|
definition?.fields?.pagination?.type != null &&
|
|
|
|
definition?.fields?.pagination?.pageParam != null
|
2022-01-05 10:16:10 +01:00
|
|
|
return { supportsPagination }
|
2021-12-17 19:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static async getDefinition(datasource) {
|
2021-12-17 09:22:04 +01:00
|
|
|
if (!datasource?._id) {
|
|
|
|
return null
|
|
|
|
}
|
2021-12-17 19:39:48 +01:00
|
|
|
return await fetchQueryDefinition(datasource._id)
|
2021-12-17 09:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async getData() {
|
2021-12-17 19:39:48 +01:00
|
|
|
const { datasource, limit } = this.options
|
2022-01-05 10:16:10 +01:00
|
|
|
const { supportsPagination } = get(this.featureStore)
|
|
|
|
const { cursor, definition } = get(this.store)
|
|
|
|
const { type } = definition.fields.pagination
|
2021-12-17 09:22:04 +01:00
|
|
|
|
|
|
|
// Set the default query params
|
|
|
|
let parameters = cloneDeep(datasource?.queryParams || {})
|
|
|
|
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-05 10:16:10 +01:00
|
|
|
if (supportsPagination) {
|
|
|
|
const requestCursor = type === "page" ? parseInt(cursor || 0) : cursor
|
|
|
|
queryPayload.pagination = { page: requestCursor, limit }
|
2021-12-17 19:39:48 +01:00
|
|
|
}
|
|
|
|
|
2022-01-05 10:16:10 +01:00
|
|
|
// Execute query
|
2021-12-17 19:39:48 +01:00
|
|
|
const { data, pagination, ...rest } = await executeQuery(queryPayload)
|
2022-01-05 10:16:10 +01:00
|
|
|
|
|
|
|
// Derive pagination info from response
|
|
|
|
let nextCursor = null
|
|
|
|
let hasNextPage = false
|
|
|
|
if (supportsPagination) {
|
|
|
|
if (type === "page") {
|
|
|
|
// For "page number" pagination, increment the existing page number
|
|
|
|
nextCursor = queryPayload.pagination.page + 1
|
|
|
|
} else {
|
|
|
|
// For "cursor" pagination, the cursor should be in the response
|
|
|
|
nextCursor = pagination.cursor
|
|
|
|
}
|
|
|
|
hasNextPage = data?.length === limit && limit > 0
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:22:04 +01:00
|
|
|
return {
|
2021-12-17 14:12:28 +01:00
|
|
|
rows: data || [],
|
|
|
|
info: rest,
|
2022-01-05 10:16:10 +01:00
|
|
|
cursor: nextCursor,
|
|
|
|
hasNextPage,
|
2021-12-17 09:22:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|