@@ -36,15 +36,13 @@ router.post('/', async (req, res) => {
3636 name : string
3737 type : string
3838 }
39- const getTableQuery = SQL `` . append ( tables ) . append ( SQL ` AND c.oid = ${ tableId } ` )
39+ const getTableQuery = getTableSqlize ( tableId )
4040 const { name : table , schema } = ( await RunQuery ( req . headers . pg , getTableQuery ) ) . data [ 0 ]
4141
42- const query = `ALTER TABLE " ${ schema } "." ${ table } " ADD COLUMN " ${ name } " " ${ type } "`
42+ const query = addColumnSqlize ( { schema, table, name, type } )
4343 await RunQuery ( req . headers . pg , query )
4444
45- const getColumnQuery = SQL ``
46- . append ( columns )
47- . append ( SQL ` WHERE c.oid = ${ tableId } AND column_name = ${ name } ` )
45+ const getColumnQuery = getColumnSqlize ( tableId , name )
4846 const column = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
4947
5048 return res . status ( 200 ) . json ( column )
@@ -56,32 +54,17 @@ router.post('/', async (req, res) => {
5654
5755router . patch ( '/:id' , async ( req , res ) => {
5856 try {
59- const [ tableId , ordinalPos ] = req . params . id . split ( '.' )
60- const getColumnQuery = SQL ``
61- . append ( columns )
62- . append ( SQL ` WHERE c.oid = ${ tableId } AND ordinal_position = ${ ordinalPos } ` )
63- const { schema, table, name : oldName } = (
64- await RunQuery ( req . headers . pg , getColumnQuery )
65- ) . data [ 0 ]
57+ const [ tableId , ordinalPos ] = req . params . id . split ( '.' ) . map ( Number )
58+ const getColumnQuery = getColumnByPosSqlize ( tableId , ordinalPos )
59+ const column = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
60+ const { schema, table, name : oldName } = column
6661
6762 const { name, type } = req . body as {
6863 name ?: string
6964 type ?: string
7065 }
7166
72- const query = `
73- BEGIN;
74- ${
75- type === undefined
76- ? ''
77- : `ALTER TABLE "${ schema } "."${ table } " ALTER COLUMN "${ oldName } " SET DATA TYPE "${ type } ";`
78- }
79- ${
80- name === undefined
81- ? ''
82- : `ALTER TABLE "${ schema } "."${ table } " RENAME COLUMN "${ oldName } " TO "${ name } ";`
83- }
84- COMMIT;`
67+ const query = patchColumnSqlize ( { schema, table, oldName, name, type } )
8568 await RunQuery ( req . headers . pg , query )
8669
8770 const updated = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
@@ -94,15 +77,12 @@ COMMIT;`
9477
9578router . delete ( '/:id' , async ( req , res ) => {
9679 try {
97- const [ tableId , ordinalPos ] = req . params . id . split ( '.' )
98-
99- const getColumnQuery = SQL ``
100- . append ( columns )
101- . append ( SQL ` WHERE c.oid = ${ tableId } AND ordinal_position = ${ ordinalPos } ` )
80+ const [ tableId , ordinalPos ] = req . params . id . split ( '.' ) . map ( Number )
81+ const getColumnQuery = getColumnByPosSqlize ( tableId , ordinalPos )
10282 const column = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
10383 const { schema, table, name } = column
10484
105- const query = `ALTER TABLE " ${ schema } "." ${ table } " DROP COLUMN " ${ name } "`
85+ const query = dropColumnSqlize ( schema , table , name )
10686 await RunQuery ( req . headers . pg , query )
10787
10888 return res . status ( 200 ) . json ( column )
@@ -112,30 +92,63 @@ router.delete('/:id', async (req, res) => {
11292 }
11393} )
11494
115- const removeSystemSchemas = ( data : Tables . Column [ ] ) => {
116- return data . filter ( ( x ) => ! DEFAULT_SYSTEM_SCHEMAS . includes ( x . schema ) )
95+ const getTableSqlize = ( id : number ) => {
96+ return SQL `` . append ( tables ) . append ( SQL ` AND c.oid = ${ id } ` )
11797}
118- const newColumnSql = ( {
98+ const addColumnSqlize = ( {
99+ schema,
100+ table,
119101 name,
120- default_value,
121- is_identity = false ,
122- is_nullable = true ,
123- is_primary_key = false ,
124- data_type,
102+ type,
125103} : {
104+ schema : string
105+ table : string
126106 name : string
127- default_value ?: string
128- is_identity ?: boolean
129- is_nullable ?: boolean
130- is_primary_key ?: boolean
131- data_type : string
107+ type : string
108+ } ) => {
109+ return `ALTER TABLE "${ schema } "."${ table } " ADD COLUMN "${ name } " "${ type } "`
110+ }
111+ const getColumnSqlize = ( tableId : number , name : string ) => {
112+ return SQL `` . append ( columns ) . append ( SQL ` WHERE c.oid = ${ tableId } AND column_name = ${ name } ` )
113+ }
114+ const getColumnByPosSqlize = ( tableId : number , ordinalPos : number ) => {
115+ return SQL ``
116+ . append ( columns )
117+ . append ( SQL ` WHERE c.oid = ${ tableId } AND ordinal_position = ${ ordinalPos } ` )
118+ }
119+ const patchColumnSqlize = ( {
120+ schema,
121+ table,
122+ oldName,
123+ name,
124+ type,
125+ } : {
126+ schema : string
127+ table : string
128+ oldName : string
129+ name ?: string
130+ type ?: string
132131} ) => {
132+ const nameSql =
133+ name === undefined
134+ ? ''
135+ : `ALTER TABLE "${ schema } "."${ table } " RENAME COLUMN "${ oldName } " TO "${ name } ";`
136+ const typeSql =
137+ type === undefined
138+ ? ''
139+ : `ALTER TABLE "${ schema } "."${ table } " ALTER COLUMN "${ oldName } " SET DATA TYPE "${ type } ";`
140+ // Make sure typeSql comes first
133141 return `
134- ${ name } ${ data_type }
135- ${ default_value === undefined ? '' : `DEFAULT ${ default_value } ` }
136- ${ is_identity ? 'GENERATED BY DEFAULT AS IDENTITY' : '' }
137- ${ is_nullable ? '' : 'NOT NULL' }
138- ${ is_primary_key ? 'PRIMARY KEY' : '' } `
142+ BEGIN;
143+ ${ typeSql }
144+ ${ nameSql }
145+ COMMIT;`
146+ }
147+ const dropColumnSqlize = ( schema : string , table : string , name : string ) => {
148+ return `ALTER TABLE "${ schema } "."${ table } " DROP COLUMN "${ name } "`
149+ }
150+ const removeSystemSchemas = ( data : Tables . Column [ ] ) => {
151+ return data . filter ( ( x ) => ! DEFAULT_SYSTEM_SCHEMAS . includes ( x . schema ) )
139152}
140153
141154export = router
0 commit comments