File tree Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -94,6 +94,45 @@ export default class PostgresMetaFunctions {
94
94
return await this . retrieve ( { name, schema } )
95
95
}
96
96
97
+ async update (
98
+ id : number ,
99
+ {
100
+ name,
101
+ schema,
102
+ params,
103
+ extension,
104
+ } : {
105
+ name : string
106
+ schema ?: string
107
+ params ?: string [ ] //optional params for overloaded functions
108
+ extension ?: string //e.g. sqrt DEPENDS ON EXTENSION mathlib
109
+ }
110
+ ) : Promise < PostgresMetaResult < PostgresFunction > > {
111
+ const { data : old , error : retrieveError } = await this . retrieve ( { id } )
112
+ if ( retrieveError ) {
113
+ return { data : null , error : retrieveError }
114
+ }
115
+
116
+ const alter = `ALTER FUNCTION ${ ident ( old ! . schema ) } .${ ident ( old ! . name ) } ${
117
+ params && params . length ? `(${ params . join ( ',' ) } )` : ''
118
+ } `
119
+
120
+ const schemaSql =
121
+ schema === undefined || name == old ! . schema ? '' : `${ alter } SET SCHEMA ${ ident ( schema ) } ;`
122
+ const extSql = extension === undefined ? '' : `${ alter } DEPENDS ON EXTENSION ${ extension } ;`
123
+ const nameSql =
124
+ name === undefined || name == old ! . name ? '' : `${ alter } RENAME TO ${ ident ( name ) } ;`
125
+ //Note: leaving out search_path and owner - should these be alterable from this api?
126
+
127
+ const sql = `BEGIN; ${ schemaSql } ${ extSql } ${ nameSql } COMMIT;`
128
+
129
+ const { error } = await this . query ( sql )
130
+ if ( error ) {
131
+ return { data : null , error }
132
+ }
133
+ return await this . retrieve ( { id } )
134
+ }
135
+
97
136
async remove (
98
137
id : number ,
99
138
{ cascade = false } = { }
Original file line number Diff line number Diff line change @@ -61,6 +61,28 @@ export default async (fastify: FastifyInstance) => {
61
61
return data
62
62
} )
63
63
64
+ fastify . patch < {
65
+ Headers : { pg : string }
66
+ Params : {
67
+ id : string
68
+ }
69
+ Body : any
70
+ } > ( '/:id(\\d+)' , async ( request , reply ) => {
71
+ const connectionString = request . headers . pg
72
+ const id = Number ( request . params . id )
73
+
74
+ const pgMeta = new PostgresMeta ( { connectionString, max : 1 } )
75
+ const { data, error } = await pgMeta . functions . update ( id , request . body )
76
+ await pgMeta . end ( )
77
+ if ( error ) {
78
+ request . log . error ( JSON . stringify ( { error, req : request . body } ) )
79
+ reply . code ( 400 )
80
+ if ( error . message . startsWith ( 'Cannot find' ) ) reply . code ( 404 )
81
+ return { error : error . message }
82
+ }
83
+ return data
84
+ } )
85
+
64
86
fastify . delete < {
65
87
Headers : { pg : string }
66
88
Params : {
Original file line number Diff line number Diff line change @@ -206,6 +206,19 @@ describe('/functions', () => {
206
206
assert . equal ( newFunc . return_type , 'int4' )
207
207
func . id = newFunc . id
208
208
} )
209
+ it ( 'PATCH' , async ( ) => {
210
+ const updates = {
211
+ name : 'test_func_renamed' ,
212
+ params : [ 'integer' , 'integer' ] ,
213
+ // schema: 'test_schema' // TODO: test patching function schema
214
+ // extension: 'mathlib', // TODO: test patching function extension
215
+ }
216
+
217
+ let { data : updated } = await axios . patch ( `${ URL } /functions/${ func . id } ` , updates )
218
+ assert . equal ( updated . id , func . id )
219
+ assert . equal ( updated . name , 'test_func_renamed' )
220
+ //assert.equal(updated.schema, 'test_schema')
221
+ } )
209
222
it ( 'DELETE' , async ( ) => {
210
223
await axios . delete ( `${ URL } /functions/${ func . id } ` )
211
224
const { data : functions } = await axios . get ( `${ URL } /functions` )
You can’t perform that action at this time.
0 commit comments