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