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

View File

@ -1,8 +1,9 @@
import { ViewV2Type } from "@budibase/types"
import { SortOrder, UIView, ViewV2Type } from "@budibase/types"
import DataFetch from "./DataFetch.js"
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() {
return {
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
}
async getDefinition(datasource) {
async getDefinition(datasource: UIView | null): Promise<UIView | null> {
if (!datasource?.id) {
return null
}
try {
const res = await this.API.viewV2.fetchDefinition(datasource.id)
return res?.data
} catch (error) {
} catch (error: any) {
this.store.update(state => ({
...state,
error,
@ -31,7 +32,10 @@ export default class ViewV2Fetch extends DataFetch {
}
}
getDefaultSortColumn() {
getDefaultSortColumn(
_definition: { primaryDisplay?: string } | null,
_schema: Record<string, any>
) {
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 (
definition.type === ViewV2Type.CALCULATION &&
!Object.values(definition.schema || {}).some(x => x.calculationType)
definition?.type === ViewV2Type.CALCULATION &&
!Object.values(definition.schema || {}).some(isCalculationField)
) {
return {
rows: [],
@ -56,9 +60,9 @@ export default class ViewV2Fetch extends DataFetch {
// If sort/filter params are not defined, update options to store the
// params built in to this view. This ensures that we can accurately
// 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.sortOrder = definition.sort.order
this.options.sortOrder = definition.sort.order || SortOrder.ASCENDING
}
try {

View File

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

View File

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

View File

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