@@ -36,15 +36,13 @@ router.post('/', async (req, res) => {
36
36
name : string
37
37
type : string
38
38
}
39
- const getTableQuery = SQL `` . append ( tables ) . append ( SQL ` AND c.oid = ${ tableId } ` )
39
+ const getTableQuery = getTableSqlize ( tableId )
40
40
const { name : table , schema } = ( await RunQuery ( req . headers . pg , getTableQuery ) ) . data [ 0 ]
41
41
42
- const query = `ALTER TABLE " ${ schema } "." ${ table } " ADD COLUMN " ${ name } " " ${ type } "`
42
+ const query = addColumnSqlize ( { schema, table, name, type } )
43
43
await RunQuery ( req . headers . pg , query )
44
44
45
- const getColumnQuery = SQL ``
46
- . append ( columns )
47
- . append ( SQL ` WHERE c.oid = ${ tableId } AND column_name = ${ name } ` )
45
+ const getColumnQuery = getColumnSqlize ( tableId , name )
48
46
const column = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
49
47
50
48
return res . status ( 200 ) . json ( column )
@@ -56,32 +54,17 @@ router.post('/', async (req, res) => {
56
54
57
55
router . patch ( '/:id' , async ( req , res ) => {
58
56
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
66
61
67
62
const { name, type } = req . body as {
68
63
name ?: string
69
64
type ?: string
70
65
}
71
66
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 } )
85
68
await RunQuery ( req . headers . pg , query )
86
69
87
70
const updated = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
@@ -94,15 +77,12 @@ COMMIT;`
94
77
95
78
router . delete ( '/:id' , async ( req , res ) => {
96
79
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 )
102
82
const column = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
103
83
const { schema, table, name } = column
104
84
105
- const query = `ALTER TABLE " ${ schema } "." ${ table } " DROP COLUMN " ${ name } "`
85
+ const query = dropColumnSqlize ( schema , table , name )
106
86
await RunQuery ( req . headers . pg , query )
107
87
108
88
return res . status ( 200 ) . json ( column )
@@ -112,30 +92,63 @@ router.delete('/:id', async (req, res) => {
112
92
}
113
93
} )
114
94
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 } ` )
117
97
}
118
- const newColumnSql = ( {
98
+ const addColumnSqlize = ( {
99
+ schema,
100
+ table,
119
101
name,
120
- default_value,
121
- is_identity = false ,
122
- is_nullable = true ,
123
- is_primary_key = false ,
124
- data_type,
102
+ type,
125
103
} : {
104
+ schema : string
105
+ table : string
126
106
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
132
131
} ) => {
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
133
141
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 ) )
139
152
}
140
153
141
154
export = router
0 commit comments