Type view fetch

This commit is contained in:
Adria Navarro 2025-01-02 12:33:54 +01:00
parent 89eba31897
commit 97eb4a2e79
5 changed files with 37 additions and 25 deletions

View File

@ -8,7 +8,6 @@ import {
SearchFilters, SearchFilters,
SortOrder, SortOrder,
SortType, SortType,
Table,
TableSchema, TableSchema,
UIDatasource, UIDatasource,
UIFetchAPI, UIFetchAPI,
@ -18,7 +17,7 @@ import {
const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils
interface DataFetchStore { interface DataFetchStore<T extends UIDatasource | null> {
rows: UIRow[] rows: UIRow[]
info: null info: null
schema: TableSchema | null schema: TableSchema | null
@ -30,9 +29,11 @@ interface DataFetchStore {
cursors: any[] cursors: any[]
resetKey: number resetKey: number
error: null error: null
definition?: T | null
} }
interface DataFetchDerivedStore extends DataFetchStore { interface DataFetchDerivedStore<T extends UIDatasource | null>
extends DataFetchStore<T> {
hasNextPage: boolean hasNextPage: boolean
hasPrevPage: boolean hasPrevPage: boolean
supportsSearch: boolean supportsSearch: boolean
@ -69,8 +70,8 @@ export default abstract class DataFetch<T extends UIDatasource | null> {
clientSideSorting: boolean clientSideSorting: boolean
clientSideLimiting: boolean clientSideLimiting: boolean
} }
store: Writable<DataFetchStore> store: Writable<DataFetchStore<T>>
derivedStore: Readable<DataFetchDerivedStore> derivedStore: Readable<DataFetchDerivedStore<T>>
/** /**
* Constructs a new DataFetch instance. * Constructs a new DataFetch instance.
@ -335,7 +336,7 @@ export default abstract class DataFetch<T extends UIDatasource | null> {
return null return null
} }
try { try {
return await this.API.fetchTableDefinition(datasource.tableId) return (await this.API.fetchTableDefinition(datasource.tableId)) as T
} catch (error: any) { } catch (error: any) {
this.store.update(state => ({ this.store.update(state => ({
...state, ...state,
@ -352,7 +353,10 @@ export default abstract class DataFetch<T extends UIDatasource | null> {
* @param definition the datasource definition * @param definition the datasource definition
* @return {object} the schema * @return {object} the schema
*/ */
getSchema(_datasource: UIDatasource | null, definition: Table | null) { getSchema(
_datasource: UIDatasource | null,
definition: T | null
): TableSchema | undefined {
return definition?.schema return definition?.schema
} }
@ -412,7 +416,7 @@ export default abstract class DataFetch<T extends UIDatasource | null> {
* Determine the feature flag for this datasource definition * Determine the feature flag for this datasource definition
* @param definition * @param definition
*/ */
determineFeatureFlags(_definition: Table | null) { determineFeatureFlags(_definition: T | null) {
return { return {
supportsSearch: false, supportsSearch: false,
supportsSort: false, supportsSort: false,
@ -495,7 +499,7 @@ export default abstract class DataFetch<T extends UIDatasource | null> {
* @param state the current store state * @param state the current store state
* @return {boolean} whether there is a next page of data or not * @return {boolean} whether there is a next page of data or not
*/ */
hasNextPage(state: DataFetchStore): boolean { hasNextPage(state: DataFetchStore<T>): boolean {
return state.cursors[state.pageNumber + 1] != null return state.cursors[state.pageNumber + 1] != null
} }

View File

@ -1,8 +1,9 @@
import { ViewV2Type } from "@budibase/types" import { SortOrder, UIView, ViewV2Type } from "@budibase/types"
import DataFetch from "./DataFetch.js" import DataFetch from "./DataFetch.js"
import { get } from "svelte/store" import { get } from "svelte/store"
import { isCalculationField } from "packages/shared-core/src/helpers/views.js"
export default class ViewV2Fetch extends DataFetch { export default class ViewV2Fetch extends DataFetch<UIView> {
determineFeatureFlags() { determineFeatureFlags() {
return { return {
supportsSearch: true, supportsSearch: true,
@ -11,18 +12,18 @@ export default class ViewV2Fetch extends DataFetch {
} }
} }
getSchema(datasource, definition) { getSchema(_datasource: UIView | null, definition: UIView | null) {
return definition?.schema return definition?.schema
} }
async getDefinition(datasource) { async getDefinition(datasource: UIView | null): Promise<UIView | null> {
if (!datasource?.id) { if (!datasource?.id) {
return null return null
} }
try { try {
const res = await this.API.viewV2.fetchDefinition(datasource.id) const res = await this.API.viewV2.fetchDefinition(datasource.id)
return res?.data return res?.data
} catch (error) { } catch (error: any) {
this.store.update(state => ({ this.store.update(state => ({
...state, ...state,
error, error,
@ -31,7 +32,10 @@ export default class ViewV2Fetch extends DataFetch {
} }
} }
getDefaultSortColumn() { getDefaultSortColumn(
_definition: { primaryDisplay?: string } | null,
_schema: Record<string, any>
) {
return null return null
} }
@ -42,8 +46,8 @@ export default class ViewV2Fetch extends DataFetch {
// If this is a calculation view and we have no calculations, return nothing // If this is a calculation view and we have no calculations, return nothing
if ( if (
definition.type === ViewV2Type.CALCULATION && definition?.type === ViewV2Type.CALCULATION &&
!Object.values(definition.schema || {}).some(x => x.calculationType) !Object.values(definition.schema || {}).some(isCalculationField)
) { ) {
return { return {
rows: [], rows: [],
@ -56,9 +60,9 @@ export default class ViewV2Fetch extends DataFetch {
// If sort/filter params are not defined, update options to store the // If sort/filter params are not defined, update options to store the
// params built in to this view. This ensures that we can accurately // params built in to this view. This ensures that we can accurately
// compare old and new params and skip a redundant API call. // compare old and new params and skip a redundant API call.
if (!sortColumn && definition.sort?.field) { if (!sortColumn && definition?.sort?.field) {
this.options.sortColumn = definition.sort.field this.options.sortColumn = definition.sort.field
this.options.sortOrder = definition.sort.order this.options.sortOrder = definition.sort.order || SortOrder.ASCENDING
} }
try { try {

View File

@ -1,8 +1,6 @@
import { UITable, UIView } from "@budibase/types" import { UITable, UIView } from "@budibase/types"
export type UIDatasource = (UITable | UIView) & { export type UIDatasource = UITable | UIView
type: string
}
export interface UIFieldMutation { export interface UIFieldMutation {
visible?: boolean visible?: boolean

View File

@ -10,10 +10,10 @@ import {
} from "@budibase/types" } from "@budibase/types"
interface SearchOptions { interface SearchOptions {
query: SearchFilters | null query?: SearchFilters | null | undefined
limit: number limit: number
sort: string | null sort: string | null
sortOrder: string sortOrder: string | undefined
sortType: SortType | null sortType: SortType | null
paginate: boolean paginate: boolean
bookmark: null bookmark: null
@ -29,6 +29,11 @@ export interface UIFetchAPI {
searchTable(tableId: string, options: SearchOptions): any searchTable(tableId: string, options: SearchOptions): any
viewV2: {
fetchDefinition: (datasourceId: string) => Promise<any>
fetch: (datasourceId: string, options: SearchOptions) => any
}
resetKey: string | null resetKey: string | null
error: any error: any

View File

@ -1,6 +1,7 @@
import { ViewV2 } from "@budibase/types" import { ViewV2 } from "@budibase/types"
import { UIFieldSchema } from "./table" import { UIFieldSchema } from "./table"
export interface UIView extends ViewV2 { export interface UIView extends Omit<ViewV2, "type"> {
type: string
schema: Record<string, UIFieldSchema> schema: Record<string, UIFieldSchema>
} }