Fix types
This commit is contained in:
parent
4f06592685
commit
eb73370460
|
@ -1,5 +1,5 @@
|
|||
import { API } from "api"
|
||||
import { DataFetchMap } from "@budibase/frontend-core"
|
||||
import { DataFetchMap, DataFetchType } from "@budibase/frontend-core"
|
||||
|
||||
/**
|
||||
* Constructs a fetch instance for a given datasource.
|
||||
|
@ -8,12 +8,20 @@ import { DataFetchMap } from "@budibase/frontend-core"
|
|||
* @param datasource the datasource
|
||||
* @returns
|
||||
*/
|
||||
const getDatasourceFetchInstance = (datasource: { type: string }) => {
|
||||
const getDatasourceFetchInstance = <
|
||||
TDatasource extends { type: DataFetchType }
|
||||
>(
|
||||
datasource: TDatasource
|
||||
) => {
|
||||
const handler = DataFetchMap[datasource?.type]
|
||||
if (!handler) {
|
||||
return null
|
||||
}
|
||||
return new handler({ API, datasource })
|
||||
return new handler({
|
||||
API,
|
||||
datasource: datasource as never,
|
||||
query: null as any,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -21,21 +29,23 @@ const getDatasourceFetchInstance = (datasource: { type: string }) => {
|
|||
* @param datasource the datasource to fetch the schema for
|
||||
* @param options options for enriching the schema
|
||||
*/
|
||||
export const fetchDatasourceSchema = async (
|
||||
datasource,
|
||||
export const fetchDatasourceSchema = async <
|
||||
TDatasource extends { type: DataFetchType }
|
||||
>(
|
||||
datasource: TDatasource,
|
||||
options = { enrichRelationships: false, formSchema: false }
|
||||
) => {
|
||||
const instance = getDatasourceFetchInstance(datasource)
|
||||
const definition = await instance?.getDefinition(datasource)
|
||||
if (!definition) {
|
||||
const definition = await instance?.getDefinition()
|
||||
if (!instance || !definition) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Get the normal schema as long as we aren't wanting a form schema
|
||||
let schema
|
||||
let schema: any
|
||||
if (datasource?.type !== "query" || !options?.formSchema) {
|
||||
schema = instance.getSchema(definition)
|
||||
} else if (definition.parameters?.length) {
|
||||
schema = instance.getSchema(definition as any)
|
||||
} else if ("parameters" in definition && definition.parameters?.length) {
|
||||
schema = {}
|
||||
definition.parameters.forEach(param => {
|
||||
schema[param.name] = { ...param, type: "string" }
|
||||
|
@ -55,7 +65,12 @@ export const fetchDatasourceSchema = async (
|
|||
}
|
||||
|
||||
// Enrich schema with relationships if required
|
||||
if (definition?.sql && options?.enrichRelationships) {
|
||||
if (
|
||||
definition &&
|
||||
"sql" in definition &&
|
||||
definition.sql &&
|
||||
options?.enrichRelationships
|
||||
) {
|
||||
const relationshipAdditions = await getRelationshipSchemaAdditions(schema)
|
||||
schema = {
|
||||
...schema,
|
||||
|
@ -71,20 +86,26 @@ export const fetchDatasourceSchema = async (
|
|||
* Fetches the definition of any kind of datasource.
|
||||
* @param datasource the datasource to fetch the schema for
|
||||
*/
|
||||
export const fetchDatasourceDefinition = async datasource => {
|
||||
export const fetchDatasourceDefinition = async <
|
||||
TDatasource extends { type: DataFetchType }
|
||||
>(
|
||||
datasource: TDatasource
|
||||
) => {
|
||||
const instance = getDatasourceFetchInstance(datasource)
|
||||
return await instance?.getDefinition(datasource)
|
||||
return await instance?.getDefinition()
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the schema of relationship fields for a SQL table schema
|
||||
* @param schema the schema to enrich
|
||||
*/
|
||||
export const getRelationshipSchemaAdditions = async schema => {
|
||||
export const getRelationshipSchemaAdditions = async (
|
||||
schema: Record<string, any>
|
||||
) => {
|
||||
if (!schema) {
|
||||
return null
|
||||
}
|
||||
let relationshipAdditions = {}
|
||||
let relationshipAdditions: Record<string, any> = {}
|
||||
for (let fieldKey of Object.keys(schema)) {
|
||||
const fieldSchema = schema[fieldKey]
|
||||
if (fieldSchema?.type === "link") {
|
||||
|
@ -92,7 +113,10 @@ export const getRelationshipSchemaAdditions = async schema => {
|
|||
type: "table",
|
||||
tableId: fieldSchema?.tableId,
|
||||
})
|
||||
Object.keys(linkSchema || {}).forEach(linkKey => {
|
||||
if (!linkSchema) {
|
||||
continue
|
||||
}
|
||||
Object.keys(linkSchema).forEach(linkKey => {
|
||||
relationshipAdditions[`${fieldKey}.${linkKey}`] = {
|
||||
type: linkSchema[linkKey].type,
|
||||
externalType: linkSchema[linkKey].externalType,
|
||||
|
|
|
@ -369,7 +369,7 @@ export default abstract class DataFetch<
|
|||
* @param schema the datasource schema
|
||||
* @return {object} the enriched datasource schema
|
||||
*/
|
||||
private enrichSchema(schema: TableSchema): TableSchema {
|
||||
enrichSchema(schema: TableSchema): TableSchema {
|
||||
// Check for any JSON fields so we can add any top level properties
|
||||
let jsonAdditions: Record<string, { type: string; nestedJSON: true }> = {}
|
||||
for (const fieldKey of Object.keys(schema)) {
|
||||
|
|
Loading…
Reference in New Issue