From c0badb9c2a10f1c6b4a08e2b97dfc35a6d0603ae Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 20 Jan 2022 11:50:18 +0000 Subject: [PATCH] Remove static functions from data fetch models --- packages/client/src/sdk.js | 6 ++-- .../src/utils/schema.js | 28 ++++++++++--------- packages/frontend-core/src/fetch/DataFetch.js | 12 ++++---- .../frontend-core/src/fetch/FieldFetch.js | 2 +- .../frontend-core/src/fetch/JSONArrayFetch.js | 2 +- .../src/fetch/NestedProviderFetch.js | 2 +- .../frontend-core/src/fetch/QueryFetch.js | 2 +- packages/frontend-core/src/fetch/ViewFetch.js | 2 +- packages/frontend-core/src/index.js | 1 - 9 files changed, 29 insertions(+), 28 deletions(-) rename packages/{frontend-core => client}/src/utils/schema.js (66%) diff --git a/packages/client/src/sdk.js b/packages/client/src/sdk.js index aa87330edb..c5e023832f 100644 --- a/packages/client/src/sdk.js +++ b/packages/client/src/sdk.js @@ -1,4 +1,3 @@ -import { SchemaUtils } from "@budibase/frontend-core" import { API } from "./api.js" import { authStore, @@ -11,7 +10,8 @@ import { styleable } from "utils/styleable" import { linkable } from "utils/linkable" import { getAction } from "utils/getAction" import Provider from "components/context/Provider.svelte" -import { ActionTypes } from "constants" +import { ActionTypes } from "./constants" +import { fetchDatasourceSchema } from "./utils/schema.js" export default { API, @@ -23,7 +23,7 @@ export default { styleable, linkable, getAction, - fetchDatasourceSchema: SchemaUtils.fetchDatasourceSchema, + fetchDatasourceSchema, Provider, ActionTypes, } diff --git a/packages/frontend-core/src/utils/schema.js b/packages/client/src/utils/schema.js similarity index 66% rename from packages/frontend-core/src/utils/schema.js rename to packages/client/src/utils/schema.js index 085ed482d7..46beaa5de7 100644 --- a/packages/frontend-core/src/utils/schema.js +++ b/packages/client/src/utils/schema.js @@ -1,12 +1,13 @@ -import { convertJSONSchemaToTableSchema } from "./json" -import DataFetch from "../fetch/DataFetch.js" -import TableFetch from "../fetch/TableFetch.js" -import ViewFetch from "../fetch/ViewFetch.js" -import QueryFetch from "../fetch/QueryFetch.js" -import RelationshipFetch from "../fetch/RelationshipFetch.js" -import NestedProviderFetch from "../fetch/NestedProviderFetch.js" -import FieldFetch from "../fetch/FieldFetch.js" -import JSONArrayFetch from "../fetch/JSONArrayFetch.js" +import { API } from "../api.js" +import { JSONUtils } from "@budibase/frontend-core" +import DataFetch from "@budibase/frontend-core/src/fetch/DataFetch.js" +import TableFetch from "@budibase/frontend-core/src/fetch/TableFetch.js" +import ViewFetch from "@budibase/frontend-core/src/fetch/ViewFetch.js" +import QueryFetch from "@budibase/frontend-core/src/fetch/QueryFetch.js" +import RelationshipFetch from "@budibase/frontend-core/src/fetch/RelationshipFetch.js" +import NestedProviderFetch from "@budibase/frontend-core/src/fetch/NestedProviderFetch.js" +import FieldFetch from "@budibase/frontend-core/src/fetch/FieldFetch.js" +import JSONArrayFetch from "@budibase/frontend-core/src/fetch/JSONArrayFetch.js" /** * Fetches the schema of any kind of datasource. @@ -31,10 +32,11 @@ export const fetchDatasourceSchema = async ( if (!handler) { return null } + const instance = new handler({ API }) // Get the datasource definition and then schema - const definition = await handler.getDefinition(datasource) - let schema = handler.getSchema(datasource, definition) + const definition = await instance.getDefinition(datasource) + let schema = instance.getSchema(datasource, definition) if (!schema) { return null } @@ -44,7 +46,7 @@ export const fetchDatasourceSchema = async ( Object.keys(schema).forEach(fieldKey => { const fieldSchema = schema[fieldKey] if (fieldSchema?.type === "json") { - const jsonSchema = convertJSONSchemaToTableSchema(fieldSchema, { + const jsonSchema = JSONUtils.convertJSONSchemaToTableSchema(fieldSchema, { squashObjects: true, }) Object.keys(jsonSchema).forEach(jsonKey => { @@ -78,5 +80,5 @@ export const fetchDatasourceSchema = async ( } // Ensure schema structure is correct - return DataFetch.enrichSchema(schema) + return instance.enrichSchema(schema) } diff --git a/packages/frontend-core/src/fetch/DataFetch.js b/packages/frontend-core/src/fetch/DataFetch.js index cf47f984cf..72e1d84f00 100644 --- a/packages/frontend-core/src/fetch/DataFetch.js +++ b/packages/frontend-core/src/fetch/DataFetch.js @@ -118,7 +118,7 @@ export default class DataFetch { const { datasource, filter, sortColumn, paginate } = this.options // Fetch datasource definition and determine feature flags - const definition = await this.constructor.getDefinition(datasource) + const definition = await this.getDefinition(datasource) const features = this.determineFeatureFlags(definition) this.featureStore.set({ supportsSearch: !!features?.supportsSearch, @@ -127,8 +127,8 @@ export default class DataFetch { }) // Fetch and enrich schema - let schema = this.constructor.getSchema(datasource, definition) - schema = DataFetch.enrichSchema(schema) + let schema = this.getSchema(datasource, definition) + schema = this.enrichSchema(schema) if (!schema) { return } @@ -224,7 +224,7 @@ export default class DataFetch { * @param datasource * @return {object} the definition */ - static async getDefinition(datasource) { + async getDefinition(datasource) { if (!datasource?.tableId) { return null } @@ -242,7 +242,7 @@ export default class DataFetch { * @param definition the datasource definition * @return {object} the schema */ - static getSchema(datasource, definition) { + getSchema(datasource, definition) { return definition?.schema } @@ -251,7 +251,7 @@ export default class DataFetch { * @param schema the datasource schema * @return {object} the enriched datasource schema */ - static enrichSchema(schema) { + enrichSchema(schema) { if (schema == null) { return null } diff --git a/packages/frontend-core/src/fetch/FieldFetch.js b/packages/frontend-core/src/fetch/FieldFetch.js index 755e367ac4..9402a45a83 100644 --- a/packages/frontend-core/src/fetch/FieldFetch.js +++ b/packages/frontend-core/src/fetch/FieldFetch.js @@ -1,7 +1,7 @@ import DataFetch from "./DataFetch.js" export default class FieldFetch extends DataFetch { - static async getDefinition(datasource) { + async getDefinition(datasource) { // Field sources have their schema statically defined let schema if (datasource.fieldType === "attachment") { diff --git a/packages/frontend-core/src/fetch/JSONArrayFetch.js b/packages/frontend-core/src/fetch/JSONArrayFetch.js index 783ad9973c..ab2af3e2c7 100644 --- a/packages/frontend-core/src/fetch/JSONArrayFetch.js +++ b/packages/frontend-core/src/fetch/JSONArrayFetch.js @@ -2,7 +2,7 @@ import FieldFetch from "./FieldFetch.js" import { getJSONArrayDatasourceSchema } from "../utils/json" export default class JSONArrayFetch extends FieldFetch { - static async getDefinition(datasource) { + async getDefinition(datasource) { // JSON arrays need their table definitions fetched. // We can then extract their schema as a subset of the table schema. try { diff --git a/packages/frontend-core/src/fetch/NestedProviderFetch.js b/packages/frontend-core/src/fetch/NestedProviderFetch.js index 163d7e9930..01c22b6ba0 100644 --- a/packages/frontend-core/src/fetch/NestedProviderFetch.js +++ b/packages/frontend-core/src/fetch/NestedProviderFetch.js @@ -1,7 +1,7 @@ import DataFetch from "./DataFetch.js" export default class NestedProviderFetch extends DataFetch { - static async getDefinition(datasource) { + async getDefinition(datasource) { // Nested providers should already have exposed their own schema return { schema: datasource?.value?.schema, diff --git a/packages/frontend-core/src/fetch/QueryFetch.js b/packages/frontend-core/src/fetch/QueryFetch.js index a95ede95de..19aea6dfde 100644 --- a/packages/frontend-core/src/fetch/QueryFetch.js +++ b/packages/frontend-core/src/fetch/QueryFetch.js @@ -11,7 +11,7 @@ export default class QueryFetch extends DataFetch { return { supportsPagination } } - static async getDefinition(datasource) { + async getDefinition(datasource) { if (!datasource?._id) { return null } diff --git a/packages/frontend-core/src/fetch/ViewFetch.js b/packages/frontend-core/src/fetch/ViewFetch.js index 431b2046df..981969f46c 100644 --- a/packages/frontend-core/src/fetch/ViewFetch.js +++ b/packages/frontend-core/src/fetch/ViewFetch.js @@ -1,7 +1,7 @@ import DataFetch from "./DataFetch.js" export default class ViewFetch extends DataFetch { - static getSchema(datasource, definition) { + getSchema(datasource, definition) { return definition?.views?.[datasource.name]?.schema } diff --git a/packages/frontend-core/src/index.js b/packages/frontend-core/src/index.js index 7e474db757..53822fdfb4 100644 --- a/packages/frontend-core/src/index.js +++ b/packages/frontend-core/src/index.js @@ -5,4 +5,3 @@ export * as Constants from "./constants" export * as LuceneUtils from "./utils/lucene" export * as JSONUtils from "./utils/json" export * as CookieUtils from "./utils/cookies" -export * as SchemaUtils from "./utils/schema"