@@ -31,15 +31,16 @@ router.get('/', async (req, res) => {
31
31
32
32
router . post ( '/' , async ( req , res ) => {
33
33
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
39
36
const getTableQuery = getTableSqlize ( tableId )
40
37
const { name : table , schema } = ( await RunQuery ( req . headers . pg , getTableQuery ) ) . data [ 0 ]
41
38
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 )
43
44
await RunQuery ( req . headers . pg , query )
44
45
45
46
const getColumnQuery = getColumnSqlize ( tableId , name )
@@ -59,12 +60,11 @@ router.patch('/:id', async (req, res) => {
59
60
const column = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
60
61
const { schema, table, name : oldName } = column
61
62
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 )
68
68
await RunQuery ( req . headers . pg , query )
69
69
70
70
const updated = ( await RunQuery ( req . headers . pg , getColumnQuery ) ) . data [ 0 ]
@@ -100,13 +100,35 @@ const addColumnSqlize = ({
100
100
table,
101
101
name,
102
102
type,
103
+ defaultValue,
104
+ isIdentity = false ,
105
+ isNullable = true ,
106
+ isPrimaryKey = false ,
107
+ isUnique = false ,
103
108
} : {
104
109
schema : string
105
110
table : string
106
111
name : string
107
112
type : string
113
+ defaultValue ?: any
114
+ isIdentity ?: boolean
115
+ isNullable ?: boolean
116
+ isPrimaryKey ?: boolean
117
+ isUnique ?: boolean
108
118
} ) => {
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 } `
110
132
}
111
133
const getColumnSqlize = ( tableId : number , name : string ) => {
112
134
return SQL `` . append ( columns ) . append ( SQL ` WHERE c.oid = ${ tableId } AND column_name = ${ name } ` )
@@ -116,18 +138,24 @@ const getColumnByPosSqlize = (tableId: number, ordinalPos: number) => {
116
138
. append ( columns )
117
139
. append ( SQL ` WHERE c.oid = ${ tableId } AND ordinal_position = ${ ordinalPos } ` )
118
140
}
119
- const patchColumnSqlize = ( {
141
+ const alterColumnSqlize = ( {
120
142
schema,
121
143
table,
122
144
oldName,
123
145
name,
124
146
type,
147
+ dropDefault = false ,
148
+ defaultValue,
149
+ isNullable,
125
150
} : {
126
151
schema : string
127
152
table : string
128
153
oldName : string
129
154
name ?: string
130
155
type ?: string
156
+ dropDefault ?: boolean
157
+ defaultValue ?: any
158
+ isNullable ?: boolean
131
159
} ) => {
132
160
const nameSql =
133
161
name === undefined
@@ -137,9 +165,24 @@ const patchColumnSqlize = ({
137
165
type === undefined
138
166
? ''
139
167
: `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.
141
182
return `
142
183
BEGIN;
184
+ ${ isNullableSql }
185
+ ${ defaultValueSql }
143
186
${ typeSql }
144
187
${ nameSql }
145
188
COMMIT;`
0 commit comments