@@ -2,7 +2,7 @@ import { Router } from 'express'
2
2
3
3
import sql = require( '../lib/sql' )
4
4
const { columns, grants, primary_keys, relationships, tables } = sql
5
- import { coalesceRowsToArray } from '../lib/helpers'
5
+ import { coalesceRowsToArray , formatColumns } from '../lib/helpers'
6
6
import { RunQuery } from '../lib/connectionPool'
7
7
import { DEFAULT_SYSTEM_SCHEMAS } from '../lib/constants/schemas'
8
8
import { Tables } from '../lib/interfaces'
49
49
res . status ( 500 ) . json ( { error : 'Database error' , status : 500 } )
50
50
}
51
51
} )
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
+ } )
52
72
53
73
export = router
54
74
0 commit comments