2022-01-04 15:34:09 +01:00
|
|
|
import {
|
|
|
|
convertJSONSchemaToTableSchema,
|
|
|
|
getJSONArrayDatasourceSchema,
|
|
|
|
} from "builder/src/builderStore/jsonUtils"
|
|
|
|
import { fetchTableDefinition } from "api"
|
2021-12-17 09:22:04 +01:00
|
|
|
import TableFetch from "./fetch/TableFetch.js"
|
|
|
|
import ViewFetch from "./fetch/ViewFetch.js"
|
|
|
|
import QueryFetch from "./fetch/QueryFetch.js"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the schema of any kind of datasource.
|
|
|
|
*/
|
|
|
|
export const fetchDatasourceSchema = async datasource => {
|
|
|
|
const type = datasource?.type
|
2022-01-04 15:34:09 +01:00
|
|
|
let schema
|
2021-12-17 09:22:04 +01:00
|
|
|
|
|
|
|
// Nested providers should already have exposed their own schema
|
|
|
|
if (type === "provider") {
|
2022-01-04 15:34:09 +01:00
|
|
|
schema = datasource.value?.schema
|
2021-12-17 09:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Field sources have their schema statically defined
|
|
|
|
if (type === "field") {
|
|
|
|
if (datasource.fieldType === "attachment") {
|
2022-01-04 15:34:09 +01:00
|
|
|
schema = {
|
2021-12-17 09:22:04 +01:00
|
|
|
url: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
name: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else if (datasource.fieldType === "array") {
|
2022-01-04 15:34:09 +01:00
|
|
|
schema = {
|
2021-12-17 09:22:04 +01:00
|
|
|
value: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 15:34:09 +01:00
|
|
|
// JSON arrays need their table definitions fetched.
|
|
|
|
// We can then extract their schema as a subset of the table schema.
|
|
|
|
if (type === "jsonarray") {
|
|
|
|
const table = await fetchTableDefinition(datasource.tableId)
|
|
|
|
schema = getJSONArrayDatasourceSchema(table?.schema, datasource)
|
|
|
|
}
|
|
|
|
|
|
|
|
// All normal datasource schema can use their corresponding implementations
|
2021-12-17 09:22:04 +01:00
|
|
|
// in the data fetch classes
|
2021-12-17 19:39:48 +01:00
|
|
|
const handler = {
|
|
|
|
table: TableFetch,
|
|
|
|
link: TableFetch,
|
|
|
|
view: ViewFetch,
|
|
|
|
query: QueryFetch,
|
|
|
|
}[type]
|
|
|
|
if (handler) {
|
|
|
|
const definition = await handler.getDefinition(datasource)
|
2022-01-04 15:34:09 +01:00
|
|
|
schema = handler.getSchema(datasource, definition)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for any JSON fields so we can add any top level properties
|
|
|
|
if (schema) {
|
|
|
|
let jsonAdditions = {}
|
|
|
|
Object.keys(schema).forEach(fieldKey => {
|
|
|
|
const fieldSchema = schema[fieldKey]
|
|
|
|
if (fieldSchema?.type === "json") {
|
|
|
|
const jsonSchema = convertJSONSchemaToTableSchema(fieldSchema, {
|
|
|
|
squashObjects: true,
|
|
|
|
})
|
|
|
|
Object.keys(jsonSchema).forEach(jsonKey => {
|
|
|
|
jsonAdditions[`${fieldKey}.${jsonKey}`] = {
|
|
|
|
type: jsonSchema[jsonKey].type,
|
|
|
|
nestedJSON: true,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return { ...schema, ...jsonAdditions }
|
2021-12-17 09:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|