More types

This commit is contained in:
Adria Navarro 2025-01-02 13:33:24 +01:00
parent 163ca349a9
commit 1899af9190
3 changed files with 49 additions and 39 deletions

View File

@ -17,7 +17,7 @@ import {
const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils const { buildQuery, limit: queryLimit, runQuery, sort } = QueryUtils
interface DataFetchStore<T extends UIDatasource | null> { interface DataFetchStore<T> {
rows: UIRow[] rows: UIRow[]
info: null info: null
schema: TableSchema | null schema: TableSchema | null
@ -32,8 +32,7 @@ interface DataFetchStore<T extends UIDatasource | null> {
definition?: T | null definition?: T | null
} }
interface DataFetchDerivedStore<T extends UIDatasource | null> interface DataFetchDerivedStore<T> extends DataFetchStore<T> {
extends DataFetchStore<T> {
hasNextPage: boolean hasNextPage: boolean
hasPrevPage: boolean hasPrevPage: boolean
supportsSearch: boolean supportsSearch: boolean
@ -46,7 +45,10 @@ interface DataFetchDerivedStore<T extends UIDatasource | null>
* internal table or datasource plus. * internal table or datasource plus.
* For other types of datasource, this class is overridden and extended. * For other types of datasource, this class is overridden and extended.
*/ */
export default abstract class DataFetch<T extends UIDatasource | null> { export default abstract class DataFetch<
TDatasource extends UIDatasource | null,
TDefinition extends { primaryDisplay?: string }
> {
API: UIFetchAPI API: UIFetchAPI
features: { features: {
supportsSearch: boolean supportsSearch: boolean
@ -54,7 +56,7 @@ export default abstract class DataFetch<T extends UIDatasource | null> {
supportsPagination: boolean supportsPagination: boolean
} }
options: { options: {
datasource: T datasource: TDatasource
limit: number limit: number
// Search config // Search config
filter: UISearchFilter | LegacyFilter[] | null filter: UISearchFilter | LegacyFilter[] | null
@ -70,14 +72,18 @@ export default abstract class DataFetch<T extends UIDatasource | null> {
clientSideSorting: boolean clientSideSorting: boolean
clientSideLimiting: boolean clientSideLimiting: boolean
} }
store: Writable<DataFetchStore<T>> store: Writable<DataFetchStore<TDefinition>>
derivedStore: Readable<DataFetchDerivedStore<T>> derivedStore: Readable<DataFetchDerivedStore<TDefinition>>
/** /**
* Constructs a new DataFetch instance. * Constructs a new DataFetch instance.
* @param opts the fetch options * @param opts the fetch options
*/ */
constructor(opts: { API: UIFetchAPI; datasource: T; options?: {} }) { constructor(opts: {
API: UIFetchAPI
datasource: TDatasource
options?: {}
}) {
// Feature flags // Feature flags
this.features = { this.features = {
supportsSearch: false, supportsSearch: false,
@ -327,38 +333,23 @@ export default abstract class DataFetch<T extends UIDatasource | null> {
/** /**
* Gets the definition for this datasource. * Gets the definition for this datasource.
* Defaults to fetching a table definition.
* @param datasource * @param datasource
* @return {object} the definition * @return {object} the definition
*/ */
async getDefinition(datasource: UIDatasource | null) { abstract getDefinition(
if (!datasource?.tableId) { datasource: UIDatasource | null
return null ): Promise<TDefinition | null>
}
try {
return (await this.API.fetchTableDefinition(datasource.tableId)) as T
} catch (error: any) {
this.store.update(state => ({
...state,
error,
}))
return null
}
}
/** /**
* Gets the schema definition for a datasource. * Gets the schema definition for a datasource.
* Defaults to getting the "schema" property of the definition. * @param datasource the datasource
* @param _datasource the datasource
* @param definition the datasource definition * @param definition the datasource definition
* @return {object} the schema * @return {object} the schema
*/ */
getSchema( abstract getSchema(
_datasource: UIDatasource | null, datasource: UIDatasource | null,
definition: T | null definition: TDefinition | null
): TableSchema | undefined { ): any
return definition?.schema
}
/** /**
* Enriches a datasource schema with nested fields and ensures the structure * Enriches a datasource schema with nested fields and ensures the structure
@ -416,7 +407,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: T | null) { determineFeatureFlags(_definition: TDefinition | null) {
return { return {
supportsSearch: false, supportsSearch: false,
supportsSort: false, supportsSort: false,
@ -499,7 +490,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<T>): boolean { hasNextPage(state: DataFetchStore<TDefinition>): boolean {
return state.cursors[state.pageNumber + 1] != null return state.cursors[state.pageNumber + 1] != null
} }

View File

@ -1,8 +1,8 @@
import { get } from "svelte/store" import { get } from "svelte/store"
import DataFetch from "./DataFetch.js" import DataFetch from "./DataFetch.js"
import { SortOrder, UITable } from "@budibase/types" import { SortOrder, Table, UITable } from "@budibase/types"
export default class TableFetch extends DataFetch<UITable> { export default class TableFetch extends DataFetch<UITable, Table> {
determineFeatureFlags() { determineFeatureFlags() {
return { return {
supportsSearch: true, supportsSearch: true,
@ -11,6 +11,25 @@ export default class TableFetch extends DataFetch<UITable> {
} }
} }
async getDefinition(datasource: UITable | null) {
if (!datasource?.tableId) {
return null
}
try {
return await this.API.fetchTableDefinition(datasource.tableId)
} catch (error: any) {
this.store.update(state => ({
...state,
error,
}))
return null
}
}
getSchema(_datasource: UITable | null, definition: Table | null) {
return definition?.schema
}
async getData() { async getData() {
const { datasource, limit, sortColumn, sortOrder, sortType, paginate } = const { datasource, limit, sortColumn, sortOrder, sortType, paginate } =
this.options this.options

View File

@ -1,9 +1,9 @@
import { SortOrder, UIView, ViewV2Type } from "@budibase/types" import { SortOrder, UIView, ViewV2, 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" import { isCalculationField } from "packages/shared-core/src/helpers/views.js"
export default class ViewV2Fetch extends DataFetch<UIView> { export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> {
determineFeatureFlags() { determineFeatureFlags() {
return { return {
supportsSearch: true, supportsSearch: true,
@ -12,11 +12,11 @@ export default class ViewV2Fetch extends DataFetch<UIView> {
} }
} }
getSchema(_datasource: UIView | null, definition: UIView | null) { getSchema(_datasource: UIView, definition: ViewV2) {
return definition?.schema return definition?.schema
} }
async getDefinition(datasource: UIView | null): Promise<UIView | null> { async getDefinition(datasource: UIView | null): Promise<ViewV2 | null> {
if (!datasource?.id) { if (!datasource?.id) {
return null return null
} }