Cleanups
This commit is contained in:
parent
819ca2129e
commit
10fca945d2
|
@ -100,7 +100,9 @@ export default class CustomFetch extends DataFetch<
|
|||
return this.enrichCustomData(this.parseCustomData(datasource?.data))
|
||||
}
|
||||
|
||||
async getDefinition(datasource: CustomDatasource) {
|
||||
async getDefinition() {
|
||||
const { datasource } = this.options
|
||||
|
||||
// Try and work out the schema from the array provided
|
||||
const schema: CustomDefinition = {}
|
||||
const data = this.getCustomData(datasource)
|
||||
|
|
|
@ -11,6 +11,7 @@ import {
|
|||
SortType,
|
||||
TableSchema,
|
||||
UISearchFilter,
|
||||
ViewSchema,
|
||||
} from "@budibase/types"
|
||||
import { APIClient } from "../api/types"
|
||||
|
||||
|
@ -210,10 +211,10 @@ export default abstract class DataFetch<
|
|||
* Fetches a fresh set of data from the server, resetting pagination
|
||||
*/
|
||||
async getInitialData() {
|
||||
const { datasource, filter, paginate } = this.options
|
||||
const { filter, paginate } = this.options
|
||||
|
||||
// Fetch datasource definition and extract sort properties if configured
|
||||
const definition = await this.getDefinition(datasource)
|
||||
const definition = await this.getDefinition()
|
||||
|
||||
// Determine feature flags
|
||||
const features = await this.determineFeatureFlags()
|
||||
|
@ -351,19 +352,19 @@ export default abstract class DataFetch<
|
|||
|
||||
/**
|
||||
* Gets the definition for this datasource.
|
||||
* @param datasource
|
||||
|
||||
* @return {object} the definition
|
||||
*/
|
||||
abstract getDefinition(
|
||||
datasource: TDatasource | null
|
||||
): Promise<TDefinition | null>
|
||||
abstract getDefinition(): Promise<TDefinition | null>
|
||||
|
||||
/**
|
||||
* Gets the schema definition for a datasource.
|
||||
* @param definition the datasource definition
|
||||
* @return {object} the schema
|
||||
*/
|
||||
getSchema(definition: TDefinition | null): Record<string, any> | undefined {
|
||||
getSchema(
|
||||
definition: TDefinition | null
|
||||
): ViewSchema | Record<string, any> | undefined {
|
||||
return definition?.schema ?? undefined
|
||||
}
|
||||
|
||||
|
|
|
@ -19,9 +19,9 @@ export default class FieldFetch extends DataFetch<
|
|||
FieldDatasource,
|
||||
FieldDefinition
|
||||
> {
|
||||
async getDefinition(
|
||||
datasource: FieldDatasource
|
||||
): Promise<FieldDefinition | null> {
|
||||
async getDefinition(): Promise<FieldDefinition | null> {
|
||||
const { datasource } = this.options
|
||||
|
||||
// Field sources have their schema statically defined
|
||||
let schema
|
||||
if (datasource.fieldType === "attachment") {
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
import FieldFetch, { FieldDatasource } from "./FieldFetch"
|
||||
import FieldFetch from "./FieldFetch"
|
||||
import { getJSONArrayDatasourceSchema } from "../utils/json"
|
||||
|
||||
export default class JSONArrayFetch extends FieldFetch {
|
||||
async getDefinition(datasource: FieldDatasource) {
|
||||
async getDefinition() {
|
||||
const { datasource } = this.options
|
||||
|
||||
// JSON arrays need their table definitions fetched.
|
||||
// We can then extract their schema as a subset of the table schema.
|
||||
try {
|
||||
|
|
|
@ -17,7 +17,9 @@ export default class NestedProviderFetch extends DataFetch<
|
|||
NestedProviderDatasource,
|
||||
NestedProviderDefinition
|
||||
> {
|
||||
async getDefinition(datasource: NestedProviderDatasource) {
|
||||
async getDefinition() {
|
||||
const { datasource } = this.options
|
||||
|
||||
// Nested providers should already have exposed their own schema
|
||||
return {
|
||||
schema: datasource?.value?.schema,
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
import FieldFetch, { FieldDatasource } from "./FieldFetch"
|
||||
import FieldFetch from "./FieldFetch"
|
||||
import {
|
||||
getJSONArrayDatasourceSchema,
|
||||
generateQueryArraySchemas,
|
||||
} from "../utils/json"
|
||||
|
||||
export default class QueryArrayFetch extends FieldFetch {
|
||||
async getDefinition(datasource: FieldDatasource) {
|
||||
async getDefinition() {
|
||||
const { datasource } = this.options
|
||||
|
||||
if (!datasource?.tableId) {
|
||||
return null
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ interface QueryDatasource {
|
|||
|
||||
export default class QueryFetch extends DataFetch<QueryDatasource, Query> {
|
||||
async determineFeatureFlags() {
|
||||
const definition = await this.getDefinition(this.options.datasource)
|
||||
const definition = await this.getDefinition()
|
||||
const supportsPagination =
|
||||
!!definition?.fields?.pagination?.type &&
|
||||
!!definition?.fields?.pagination?.location &&
|
||||
|
@ -26,7 +26,9 @@ export default class QueryFetch extends DataFetch<QueryDatasource, Query> {
|
|||
return { supportsPagination }
|
||||
}
|
||||
|
||||
async getDefinition(datasource: QueryDatasource) {
|
||||
async getDefinition() {
|
||||
const { datasource } = this.options
|
||||
|
||||
if (!datasource?._id) {
|
||||
return null
|
||||
}
|
||||
|
|
|
@ -12,7 +12,9 @@ export default class RelationshipFetch extends DataFetch<
|
|||
RelationshipDatasource,
|
||||
Table
|
||||
> {
|
||||
async getDefinition(datasource: RelationshipDatasource) {
|
||||
async getDefinition() {
|
||||
const { datasource } = this.options
|
||||
|
||||
if (!datasource?.tableId) {
|
||||
return null
|
||||
}
|
||||
|
|
|
@ -11,7 +11,9 @@ export default class TableFetch extends DataFetch<UITable, Table> {
|
|||
}
|
||||
}
|
||||
|
||||
async getDefinition(datasource: UITable) {
|
||||
async getDefinition() {
|
||||
const { datasource } = this.options
|
||||
|
||||
if (!datasource?.tableId) {
|
||||
return null
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@ import DataFetch from "./DataFetch"
|
|||
type ViewV1 = View & { name: string }
|
||||
|
||||
export default class ViewFetch extends DataFetch<ViewV1, Table> {
|
||||
async getDefinition(datasource: ViewV1) {
|
||||
async getDefinition() {
|
||||
const { datasource } = this.options
|
||||
|
||||
if (!datasource?.tableId) {
|
||||
return null
|
||||
}
|
||||
|
|
|
@ -12,7 +12,9 @@ export default class ViewV2Fetch extends DataFetch<UIView, ViewV2> {
|
|||
}
|
||||
}
|
||||
|
||||
async getDefinition(datasource: UIView) {
|
||||
async getDefinition() {
|
||||
const { datasource } = this.options
|
||||
|
||||
try {
|
||||
const res = await this.API.viewV2.fetchDefinition(datasource.id)
|
||||
return res?.data
|
||||
|
|
|
@ -10,7 +10,6 @@ import UserFetch from "./UserFetch.js"
|
|||
import GroupUserFetch from "./GroupUserFetch"
|
||||
import CustomFetch from "./CustomFetch"
|
||||
import QueryArrayFetch from "./QueryArrayFetch.js"
|
||||
import { UIDatasource } from "@budibase/types"
|
||||
import { APIClient } from "../api/types.js"
|
||||
|
||||
const DataFetchMap = {
|
||||
|
@ -39,12 +38,16 @@ export const fetchData = ({ API, datasource, options }: any) => {
|
|||
|
||||
// Creates an empty fetch instance with no datasource configured, so no data
|
||||
// will initially be loaded
|
||||
const createEmptyFetchInstance = ({
|
||||
const createEmptyFetchInstance = <
|
||||
TDatasource extends {
|
||||
type: keyof typeof DataFetchMap
|
||||
}
|
||||
>({
|
||||
API,
|
||||
datasource,
|
||||
}: {
|
||||
API: APIClient
|
||||
datasource: any
|
||||
datasource: TDatasource
|
||||
}) => {
|
||||
const handler = DataFetchMap[datasource?.type as keyof typeof DataFetchMap]
|
||||
if (!handler) {
|
||||
|
@ -54,25 +57,33 @@ const createEmptyFetchInstance = ({
|
|||
}
|
||||
|
||||
// Fetches the definition of any type of datasource
|
||||
export const getDatasourceDefinition = async ({
|
||||
export const getDatasourceDefinition = async <
|
||||
TDatasource extends {
|
||||
type: keyof typeof DataFetchMap
|
||||
}
|
||||
>({
|
||||
API,
|
||||
datasource,
|
||||
}: {
|
||||
API: APIClient
|
||||
datasource: any
|
||||
datasource: TDatasource
|
||||
}) => {
|
||||
const instance = createEmptyFetchInstance({ API, datasource })
|
||||
return await instance?.getDefinition(datasource)
|
||||
return await instance?.getDefinition()
|
||||
}
|
||||
|
||||
// Fetches the schema of any type of datasource
|
||||
export const getDatasourceSchema = ({
|
||||
export const getDatasourceSchema = <
|
||||
TDatasource extends {
|
||||
type: keyof typeof DataFetchMap
|
||||
}
|
||||
>({
|
||||
API,
|
||||
datasource,
|
||||
definition,
|
||||
}: {
|
||||
API: APIClient
|
||||
datasource: UIDatasource
|
||||
datasource: TDatasource
|
||||
definition?: any
|
||||
}) => {
|
||||
const instance = createEmptyFetchInstance({ API, datasource })
|
||||
|
|
Loading…
Reference in New Issue