@@ -2,7 +2,7 @@ import { Router } from 'express'
22
33import sql = require( '../lib/sql' )
44const { columns, grants, primary_keys, relationships, tables } = sql
5- import { coalesceRowsToArray } from '../lib/helpers'
5+ import { coalesceRowsToArray , formatColumns } from '../lib/helpers'
66import { RunQuery } from '../lib/connectionPool'
77import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants/schemas'
88import { Tables } from '../lib/interfaces'
4949 res . status ( 500 ) . json ( { error : 'Database error' , status : 500 } )
5050 }
5151} )
52+ router . post ( '/' , async ( req , res ) => {
53+ try {
54+ const { schema = 'public' , name, columns, primary_keys = [ ] } = req . body as {
55+ schema ?: string
56+ name : string
57+ columns : Tables . Column [ ]
58+ primary_keys ?: Tables . PrimaryKey [ ]
59+ }
60+ const sql = `
61+ CREATE TABLE ${ schema } .${ name } (
62+ ${ formatColumns ( { columns, primary_keys } ) }
63+ )`
64+ const { data } = await RunQuery ( req . headers . pg , sql )
65+ return res . status ( 200 ) . json ( data )
66+ } catch ( error ) {
67+ // For this one, we always want to give back the error to the customer
68+ console . log ( 'Soft error!' , error )
69+ res . status ( 200 ) . json ( [ { error : error . toString ( ) } ] )
70+ }
71+ } )
5272
5373export = router
5474
0 commit comments