From 97eb4a2e79824923b41c47d3a51f9f2d76175a2a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 2 Jan 2025 12:33:54 +0100 Subject: [PATCH] Type view fetch --- packages/frontend-core/src/fetch/DataFetch.ts | 22 ++++++++++------- .../fetch/{ViewV2Fetch.js => ViewV2Fetch.ts} | 24 +++++++++++-------- .../types/src/ui/stores/grid/datasource.ts | 4 +--- packages/types/src/ui/stores/grid/fetch.ts | 9 +++++-- packages/types/src/ui/stores/grid/view.ts | 3 ++- 5 files changed, 37 insertions(+), 25 deletions(-) rename packages/frontend-core/src/fetch/{ViewV2Fetch.js => ViewV2Fetch.ts} (70%) diff --git a/packages/frontend-core/src/fetch/DataFetch.ts b/packages/frontend-core/src/fetch/DataFetch.ts index 7355afd967..ea28cd7240 100644 --- a/packages/frontend-core/src/fetch/DataFetch.ts +++ b/packages/frontend-core/src/fetch/DataFetch.ts @@ -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 { 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 + extends DataFetchStore { hasNextPage: boolean hasPrevPage: boolean supportsSearch: boolean @@ -69,8 +70,8 @@ export default abstract class DataFetch { clientSideSorting: boolean clientSideLimiting: boolean } - store: Writable - derivedStore: Readable + store: Writable> + derivedStore: Readable> /** * Constructs a new DataFetch instance. @@ -335,7 +336,7 @@ export default abstract class DataFetch { 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 { * @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 { * 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 { * @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): boolean { return state.cursors[state.pageNumber + 1] != null } diff --git a/packages/frontend-core/src/fetch/ViewV2Fetch.js b/packages/frontend-core/src/fetch/ViewV2Fetch.ts similarity index 70% rename from packages/frontend-core/src/fetch/ViewV2Fetch.js rename to packages/frontend-core/src/fetch/ViewV2Fetch.ts index 8436646077..337c090c66 100644 --- a/packages/frontend-core/src/fetch/ViewV2Fetch.js +++ b/packages/frontend-core/src/fetch/ViewV2Fetch.ts @@ -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 { 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 { 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 + ) { 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 { diff --git a/packages/types/src/ui/stores/grid/datasource.ts b/packages/types/src/ui/stores/grid/datasource.ts index 9533bbb8f0..9927518133 100644 --- a/packages/types/src/ui/stores/grid/datasource.ts +++ b/packages/types/src/ui/stores/grid/datasource.ts @@ -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 diff --git a/packages/types/src/ui/stores/grid/fetch.ts b/packages/types/src/ui/stores/grid/fetch.ts index a81f436fde..a8732c66e3 100644 --- a/packages/types/src/ui/stores/grid/fetch.ts +++ b/packages/types/src/ui/stores/grid/fetch.ts @@ -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 + fetch: (datasourceId: string, options: SearchOptions) => any + } + resetKey: string | null error: any diff --git a/packages/types/src/ui/stores/grid/view.ts b/packages/types/src/ui/stores/grid/view.ts index f81cc34aaf..270faaa160 100644 --- a/packages/types/src/ui/stores/grid/view.ts +++ b/packages/types/src/ui/stores/grid/view.ts @@ -1,6 +1,7 @@ import { ViewV2 } from "@budibase/types" import { UIFieldSchema } from "./table" -export interface UIView extends ViewV2 { +export interface UIView extends Omit { + type: string schema: Record }