@ -9,6 +9,7 @@ const {
BudibaseInternalDB ,
} = require ( "../../../db/utils" )
const { getTable } = require ( "./utils" )
const { FieldTypes } = require ( "../../../constants" )
function pickApi ( { tableId , table } ) {
if ( table && ! tableId ) {
@ -81,6 +82,38 @@ exports.destroy = async function (ctx) {
ctx . body = { message : ` Table ${ tableId } deleted. ` }
}
exports . schemaGenerate = function ( ctx ) {
const { json } = ctx . request . body
function recurse ( schemaLevel , objectLevel ) {
for ( let [ key , value ] of Object . entries ( objectLevel ) ) {
const type = typeof value
// check array first, since arrays are objects
if ( Array . isArray ( value ) ) {
schemaLevel [ key ] = {
type : FieldTypes . ARRAY ,
}
} else if ( type === "object" ) {
schemaLevel [ key ] = recurse ( schemaLevel [ key ] , objectLevel )
} else if ( type === "string" ) {
schemaLevel [ key ] = {
type : FieldTypes . STRING ,
}
} else if ( type === "boolean" ) {
schemaLevel [ key ] = {
type : FieldTypes . BOOLEAN ,
}
} else if ( type === "number" ) {
schemaLevel [ key ] = {
type : FieldTypes . NUMBER ,
}
}
}
return schemaLevel
}
ctx . body = recurse ( { } , json ) || { }
}
exports . bulkImport = async function ( ctx ) {
const tableId = ctx . params . tableId
await pickApi ( { tableId } ) . bulkImport ( ctx )