@@ -27,7 +27,26 @@ export interface Database {
27
27
${ schemas . map ( ( schema ) => {
28
28
const schemaTables = tables . filter ( ( table ) => table . schema === schema . name )
29
29
const schemaViews = views . filter ( ( view ) => view . schema === schema . name )
30
- const schemaFunctions = functions . filter ( ( func ) => func . schema === schema . name )
30
+ const schemaFunctions = functions . filter ( ( func ) => {
31
+ if ( func . schema !== schema . name ) {
32
+ return false
33
+ }
34
+
35
+ // Either:
36
+ // 1. All input args are be named, or
37
+ // 2. There is only one input arg which is unnamed
38
+ const inArgs = func . args . filter ( ( { mode } ) => [ 'in' , 'inout' , 'variadic' ] . includes ( mode ) )
39
+
40
+ if ( ! inArgs . some ( ( { name } ) => name === '' ) ) {
41
+ return true
42
+ }
43
+
44
+ if ( inArgs . length === 1 ) {
45
+ return true
46
+ }
47
+
48
+ return false
49
+ } )
31
50
const schemaEnums = types . filter ( ( type ) => type . schema === schema . name && type . enums . length > 0 )
32
51
return `${ JSON . stringify ( schema . name ) } : {
33
52
Tables: {
@@ -176,20 +195,16 @@ export interface Database {
176
195
( [ fnName , fns ] ) =>
177
196
`${ JSON . stringify ( fnName ) } : ${ fns
178
197
. map (
179
- ( fn ) => `{
198
+ ( { args , return_type } ) => `{
180
199
Args: ${ ( ( ) => {
181
- if ( fn . argument_types === '' ) {
182
- return 'Record<PropertyKey, never>'
183
- }
200
+ const inArgs = args . filter ( ( { mode } ) => mode === 'in' )
184
201
185
- const splitArgs = fn . argument_types . split ( ',' ) . map ( ( arg ) => arg . trim ( ) )
186
- if ( splitArgs . some ( ( arg ) => arg . includes ( '"' ) || ! arg . includes ( ' ' ) ) ) {
187
- return 'Record<string, unknown>'
202
+ if ( inArgs . length === 0 ) {
203
+ return 'Record<PropertyKey, never>'
188
204
}
189
205
190
- const argsNameAndType = splitArgs . map ( ( arg ) => {
191
- const [ name , ...rest ] = arg . split ( ' ' )
192
- const type = types . find ( ( _type ) => _type . format === rest . join ( ' ' ) )
206
+ const argsNameAndType = inArgs . map ( ( { name, type_id } ) => {
207
+ const type = types . find ( ( { id } ) => id === type_id )
193
208
if ( ! type ) {
194
209
return { name, type : 'unknown' }
195
210
}
@@ -200,7 +215,7 @@ export interface Database {
200
215
( { name, type } ) => `${ JSON . stringify ( name ) } : ${ type } `
201
216
) } }`
202
217
} ) ( ) }
203
- Returns: ${ pgTypeToTsType ( fn . return_type , types , schemas ) }
218
+ Returns: ${ pgTypeToTsType ( return_type , types , schemas ) }
204
219
}`
205
220
)
206
221
. join ( '|' ) } `
@@ -225,6 +240,7 @@ export interface Database {
225
240
226
241
output = prettier . format ( output , {
227
242
parser : 'typescript' ,
243
+ semi : false ,
228
244
} )
229
245
return output
230
246
}
0 commit comments