Budibase is an open-source low-code platform for creating internal apps in minutes. Supports PostgreSQL, MySQL, MSSQL, MongoDB, Rest API, Docker, K8s 🚀
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

39 lines
984 B

export const convertJSONSchemaToTableSchema = (
jsonSchema,
squashObjects = false
) => {
if (!jsonSchema) {
return null
}
if (jsonSchema.schema) {
jsonSchema = jsonSchema.schema
}
const keys = extractJSONSchemaKeys(jsonSchema, squashObjects)
let schema = {}
keys.forEach(({ key, type }) => {
schema[key] = { type, name: key }
})
return schema
}
const extractJSONSchemaKeys = (jsonSchema, squashObjects = false) => {
if (!jsonSchema || !Object.keys(jsonSchema).length) {
return []
}
let keys = []
Object.keys(jsonSchema).forEach(key => {
const type = jsonSchema[key].type
if (type === "json" && squashObjects) {
const childKeys = extractJSONSchemaKeys(jsonSchema[key].schema)
keys = keys.concat(
childKeys.map(childKey => ({
key: `${key}.${childKey.key}`,
type: childKey.type,
}))
)
} else if (type !== "array") {
keys.push({ key, type })
}
})
return keys
}