@@ -31,15 +31,16 @@ router.get('/', async (req, res) => {
3131
3232router . post ( '/' , async ( req , res ) => {
3333 try {
34- const { tableId, name, type } = req . body as {
35- tableId : number
36- name : string
37- type : string
38- }
34+ const tableId : number = req . body . tableId
35+ const name : string = req . body . name
3936 const getTableQuery = getTableSqlize ( tableId )
4037 const { name : table , schema } = ( await RunQuery ( req . headers . pg , getTableQuery ) ) . data [ 0 ]
4138
42- const query = addColumnSqlize ( { schema, table, name, type } )
39+ const addColumnArgs = req . body
40+ delete addColumnArgs . tableId
41+ addColumnArgs . table = table
42+ addColumnArgs . schema = schema
43+ const query = addColumnSqlize ( addColumnArgs )
4344 await RunQuery ( req . headers . pg , query )
4445
4546 const getColumnQuery = getColumnSqlize ( tableId , name )
@@ -59,12 +60,11 @@ router.patch('/:id', async (req, res) => {
5960 const column = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
6061 const { schema, table, name : oldName } = column
6162
62- const { name, type } = req . body as {
63- name ?: string
64- type ?: string
65- }
66-
67- const query = patchColumnSqlize ( { schema, table, oldName, name, type } )
63+ const alterColumnArgs = req . body
64+ alterColumnArgs . schema = schema
65+ alterColumnArgs . table = table
66+ alterColumnArgs . oldName = oldName
67+ const query = alterColumnSqlize ( alterColumnArgs )
6868 await RunQuery ( req . headers . pg , query )
6969
7070 const updated = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
@@ -100,13 +100,35 @@ const addColumnSqlize = ({
100100 table,
101101 name,
102102 type,
103+ defaultValue,
104+ isIdentity = false ,
105+ isNullable = true ,
106+ isPrimaryKey = false ,
107+ isUnique = false ,
103108} : {
104109 schema : string
105110 table : string
106111 name : string
107112 type : string
113+ defaultValue ?: any
114+ isIdentity ?: boolean
115+ isNullable ?: boolean
116+ isPrimaryKey ?: boolean
117+ isUnique ?: boolean
108118} ) => {
109- return `ALTER TABLE "${ schema } "."${ table } " ADD COLUMN "${ name } " "${ type } "`
119+ const defaultValueSql = defaultValue === undefined ? '' : `DEFAULT ${ defaultValue } `
120+ const isIdentitySql = isIdentity ? 'GENERATED BY DEFAULT AS IDENTITY' : ''
121+ const isNullableSql = isNullable ? 'NULL' : 'NOT NULL'
122+ const isPrimaryKeySql = isPrimaryKey ? 'PRIMARY KEY' : ''
123+ const isUniqueSql = isUnique ? 'UNIQUE' : ''
124+
125+ return `
126+ ALTER TABLE "${ schema } "."${ table } " ADD COLUMN "${ name } " "${ type } "
127+ ${ defaultValueSql }
128+ ${ isIdentitySql }
129+ ${ isNullableSql }
130+ ${ isPrimaryKeySql }
131+ ${ isUniqueSql } `
110132}
111133const getColumnSqlize = ( tableId : number , name : string ) => {
112134 return SQL `` . append ( columns ) . append ( SQL ` WHERE c.oid = ${ tableId } AND column_name = ${ name } ` )
@@ -116,18 +138,24 @@ const getColumnByPosSqlize = (tableId: number, ordinalPos: number) => {
116138 . append ( columns )
117139 . append ( SQL ` WHERE c.oid = ${ tableId } AND ordinal_position = ${ ordinalPos } ` )
118140}
119- const patchColumnSqlize = ( {
141+ const alterColumnSqlize = ( {
120142 schema,
121143 table,
122144 oldName,
123145 name,
124146 type,
147+ dropDefault = false ,
148+ defaultValue,
149+ isNullable,
125150} : {
126151 schema : string
127152 table : string
128153 oldName : string
129154 name ?: string
130155 type ?: string
156+ dropDefault ?: boolean
157+ defaultValue ?: any
158+ isNullable ?: boolean
131159} ) => {
132160 const nameSql =
133161 name === undefined
@@ -137,9 +165,24 @@ const patchColumnSqlize = ({
137165 type === undefined
138166 ? ''
139167 : `ALTER TABLE "${ schema } "."${ table } " ALTER COLUMN "${ oldName } " SET DATA TYPE "${ type } ";`
140- // Make sure typeSql comes first
168+ let defaultValueSql = ''
169+ if ( dropDefault ) {
170+ defaultValueSql = `ALTER TABLE "${ schema } "."${ table } " ALTER COLUMN "${ oldName } " DROP DEFAULT;`
171+ } else if ( defaultValue !== undefined ) {
172+ defaultValueSql = `ALTER TABLE "${ schema } "."${ table } " ALTER COLUMN "${ oldName } " SET DEFAULT ${ defaultValue } ;`
173+ }
174+ let isNullableSql = ''
175+ if ( isNullable !== undefined ) {
176+ isNullableSql = isNullable
177+ ? `ALTER TABLE "${ schema } "."${ table } " ALTER COLUMN "${ oldName } " DROP NOT NULL;`
178+ : `ALTER TABLE "${ schema } "."${ table } " ALTER COLUMN "${ oldName } " SET NOT NULL;`
179+ }
180+
181+ // nameSql must be last.
141182 return `
142183BEGIN;
184+ ${ isNullableSql }
185+ ${ defaultValueSql }
143186 ${ typeSql }
144187 ${ nameSql }
145188COMMIT;`
0 commit comments