Remove static functions from data fetch models

This commit is contained in:
Andrew Kingston 2022-01-20 11:50:18 +00:00
parent 7baba13704
commit c0badb9c2a
9 changed files with 29 additions and 28 deletions

View File

@ -1,4 +1,3 @@
import { SchemaUtils } from "@budibase/frontend-core"
import { API } from "./api.js" import { API } from "./api.js"
import { import {
authStore, authStore,
@ -11,7 +10,8 @@ import { styleable } from "utils/styleable"
import { linkable } from "utils/linkable" import { linkable } from "utils/linkable"
import { getAction } from "utils/getAction" import { getAction } from "utils/getAction"
import Provider from "components/context/Provider.svelte" import Provider from "components/context/Provider.svelte"
import { ActionTypes } from "constants" import { ActionTypes } from "./constants"
import { fetchDatasourceSchema } from "./utils/schema.js"
export default { export default {
API, API,
@ -23,7 +23,7 @@ export default {
styleable, styleable,
linkable, linkable,
getAction, getAction,
fetchDatasourceSchema: SchemaUtils.fetchDatasourceSchema, fetchDatasourceSchema,
Provider, Provider,
ActionTypes, ActionTypes,
} }

View File

@ -1,12 +1,13 @@
import { convertJSONSchemaToTableSchema } from "./json" import { API } from "../api.js"
import DataFetch from "../fetch/DataFetch.js" import { JSONUtils } from "@budibase/frontend-core"
import TableFetch from "../fetch/TableFetch.js" import DataFetch from "@budibase/frontend-core/src/fetch/DataFetch.js"
import ViewFetch from "../fetch/ViewFetch.js" import TableFetch from "@budibase/frontend-core/src/fetch/TableFetch.js"
import QueryFetch from "../fetch/QueryFetch.js" import ViewFetch from "@budibase/frontend-core/src/fetch/ViewFetch.js"
import RelationshipFetch from "../fetch/RelationshipFetch.js" import QueryFetch from "@budibase/frontend-core/src/fetch/QueryFetch.js"
import NestedProviderFetch from "../fetch/NestedProviderFetch.js" import RelationshipFetch from "@budibase/frontend-core/src/fetch/RelationshipFetch.js"
import FieldFetch from "../fetch/FieldFetch.js" import NestedProviderFetch from "@budibase/frontend-core/src/fetch/NestedProviderFetch.js"
import JSONArrayFetch from "../fetch/JSONArrayFetch.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. * Fetches the schema of any kind of datasource.
@ -31,10 +32,11 @@ export const fetchDatasourceSchema = async (
if (!handler) { if (!handler) {
return null return null
} }
const instance = new handler({ API })
// Get the datasource definition and then schema // Get the datasource definition and then schema
const definition = await handler.getDefinition(datasource) const definition = await instance.getDefinition(datasource)
let schema = handler.getSchema(datasource, definition) let schema = instance.getSchema(datasource, definition)
if (!schema) { if (!schema) {
return null return null
} }
@ -44,7 +46,7 @@ export const fetchDatasourceSchema = async (
Object.keys(schema).forEach(fieldKey => { Object.keys(schema).forEach(fieldKey => {
const fieldSchema = schema[fieldKey] const fieldSchema = schema[fieldKey]
if (fieldSchema?.type === "json") { if (fieldSchema?.type === "json") {
const jsonSchema = convertJSONSchemaToTableSchema(fieldSchema, { const jsonSchema = JSONUtils.convertJSONSchemaToTableSchema(fieldSchema, {
squashObjects: true, squashObjects: true,
}) })
Object.keys(jsonSchema).forEach(jsonKey => { Object.keys(jsonSchema).forEach(jsonKey => {
@ -78,5 +80,5 @@ export const fetchDatasourceSchema = async (
} }
// Ensure schema structure is correct // Ensure schema structure is correct
return DataFetch.enrichSchema(schema) return instance.enrichSchema(schema)
} }

View File

@ -118,7 +118,7 @@ export default class DataFetch {
const { datasource, filter, sortColumn, paginate } = this.options const { datasource, filter, sortColumn, paginate } = this.options
// Fetch datasource definition and determine feature flags // 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) const features = this.determineFeatureFlags(definition)
this.featureStore.set({ this.featureStore.set({
supportsSearch: !!features?.supportsSearch, supportsSearch: !!features?.supportsSearch,
@ -127,8 +127,8 @@ export default class DataFetch {
}) })
// Fetch and enrich schema // Fetch and enrich schema
let schema = this.constructor.getSchema(datasource, definition) let schema = this.getSchema(datasource, definition)
schema = DataFetch.enrichSchema(schema) schema = this.enrichSchema(schema)
if (!schema) { if (!schema) {
return return
} }
@ -224,7 +224,7 @@ export default class DataFetch {
* @param datasource * @param datasource
* @return {object} the definition * @return {object} the definition
*/ */
static async getDefinition(datasource) { async getDefinition(datasource) {
if (!datasource?.tableId) { if (!datasource?.tableId) {
return null return null
} }
@ -242,7 +242,7 @@ export default class DataFetch {
* @param definition the datasource definition * @param definition the datasource definition
* @return {object} the schema * @return {object} the schema
*/ */
static getSchema(datasource, definition) { getSchema(datasource, definition) {
return definition?.schema return definition?.schema
} }
@ -251,7 +251,7 @@ export default class DataFetch {
* @param schema the datasource schema * @param schema the datasource schema
* @return {object} the enriched datasource schema * @return {object} the enriched datasource schema
*/ */
static enrichSchema(schema) { enrichSchema(schema) {
if (schema == null) { if (schema == null) {
return null return null
} }

View File

@ -1,7 +1,7 @@
import DataFetch from "./DataFetch.js" import DataFetch from "./DataFetch.js"
export default class FieldFetch extends DataFetch { export default class FieldFetch extends DataFetch {
static async getDefinition(datasource) { async getDefinition(datasource) {
// Field sources have their schema statically defined // Field sources have their schema statically defined
let schema let schema
if (datasource.fieldType === "attachment") { if (datasource.fieldType === "attachment") {

View File

@ -2,7 +2,7 @@ import FieldFetch from "./FieldFetch.js"
import { getJSONArrayDatasourceSchema } from "../utils/json" import { getJSONArrayDatasourceSchema } from "../utils/json"
export default class JSONArrayFetch extends FieldFetch { export default class JSONArrayFetch extends FieldFetch {
static async getDefinition(datasource) { async getDefinition(datasource) {
// JSON arrays need their table definitions fetched. // JSON arrays need their table definitions fetched.
// We can then extract their schema as a subset of the table schema. // We can then extract their schema as a subset of the table schema.
try { try {

View File

@ -1,7 +1,7 @@
import DataFetch from "./DataFetch.js" import DataFetch from "./DataFetch.js"
export default class NestedProviderFetch extends DataFetch { export default class NestedProviderFetch extends DataFetch {
static async getDefinition(datasource) { async getDefinition(datasource) {
// Nested providers should already have exposed their own schema // Nested providers should already have exposed their own schema
return { return {
schema: datasource?.value?.schema, schema: datasource?.value?.schema,

View File

@ -11,7 +11,7 @@ export default class QueryFetch extends DataFetch {
return { supportsPagination } return { supportsPagination }
} }
static async getDefinition(datasource) { async getDefinition(datasource) {
if (!datasource?._id) { if (!datasource?._id) {
return null return null
} }

View File

@ -1,7 +1,7 @@
import DataFetch from "./DataFetch.js" import DataFetch from "./DataFetch.js"
export default class ViewFetch extends DataFetch { export default class ViewFetch extends DataFetch {
static getSchema(datasource, definition) { getSchema(datasource, definition) {
return definition?.views?.[datasource.name]?.schema return definition?.views?.[datasource.name]?.schema
} }

View File

@ -5,4 +5,3 @@ export * as Constants from "./constants"
export * as LuceneUtils from "./utils/lucene" export * as LuceneUtils from "./utils/lucene"
export * as JSONUtils from "./utils/json" export * as JSONUtils from "./utils/json"
export * as CookieUtils from "./utils/cookies" export * as CookieUtils from "./utils/cookies"
export * as SchemaUtils from "./utils/schema"