60 lines
1.9 KiB
TypeScript
Executable File
60 lines
1.9 KiB
TypeScript
Executable File
import { HttpContext } from '@adonisjs/core/http'
|
|
import { getDatabaseTables, getColumnsForTable } from '#services/database_inspector'
|
|
import { mapDBTypeToTs } from '#services/module_generator/db_types'
|
|
import { generateModule } from '#services/module_generator/generate_module'
|
|
import {
|
|
ModuleAsset,
|
|
ModuleLayoutConfig,
|
|
ModuleTableConfig,
|
|
} from '#services/module_generator/types'
|
|
|
|
export default class MakeModuleController {
|
|
async tables({ response }: HttpContext) {
|
|
const tables = await getDatabaseTables()
|
|
return response.ok(tables)
|
|
}
|
|
|
|
async columns({ request, response }: HttpContext) {
|
|
const { table } = request.params()
|
|
const columns = await getColumnsForTable(table)
|
|
|
|
return response.ok({
|
|
columns: columns.map((col) => ({
|
|
name: col.column_name,
|
|
type: mapDBTypeToTs(col.data_type) + (col.is_nullable === 'YES' ? ' | null' : ''),
|
|
isDateTime: ['created_at', 'updated_at'].includes(col.column_name),
|
|
})),
|
|
})
|
|
}
|
|
|
|
async generate({ request, response }: HttpContext) {
|
|
const { name, table, generate, form, datatable } = request.only([
|
|
'name',
|
|
'table',
|
|
'generate',
|
|
'form',
|
|
'datatable',
|
|
]) as {
|
|
name: string
|
|
table: string
|
|
generate: ModuleAsset[]
|
|
form: ModuleLayoutConfig | null
|
|
datatable: ModuleTableConfig | null
|
|
}
|
|
|
|
if (typeof name !== 'string' || !/^[a-z][a-zA-Z0-9]*$/.test(name)) {
|
|
return response.unprocessableEntity({ message: 'Invalid module name' })
|
|
}
|
|
if (typeof table !== 'string' || !/^[a-z_][a-z0-9_]*$/i.test(table)) {
|
|
return response.unprocessableEntity({ message: 'Invalid table name' })
|
|
}
|
|
if (!Array.isArray(generate)) {
|
|
return response.unprocessableEntity({ message: 'Invalid generate list' })
|
|
}
|
|
|
|
const moduleName = await generateModule({ name, table, generate, form, datatable })
|
|
|
|
return response.ok({ message: `Module "${moduleName}" created based on table "${table}"` })
|
|
}
|
|
}
|