Remove static functions from data fetch models

This commit is contained in:
Andrew Kingston 2022-01-20 11:50:18 +00:00
parent a3d6af7b9f
commit d494e53786
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 {
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,
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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") {

View File

@ -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 {

View File

@ -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,

View File

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

View File

@ -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
}

View File

@ -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"