@@ -40,6 +40,18 @@ export const apply = async ({
40
40
{ } as Record < number , ( typeof types ) [ number ] >
41
41
)
42
42
43
+ const getTsReturnType = ( fn : PostgresFunction , returnType : string ) => {
44
+ return `${ returnType } ${ fn . is_set_returning_function && fn . returns_multiple_rows ? '[]' : '' }
45
+ ${
46
+ fn . returns_set_of_table && fn . args . length === 1 && fn . args [ 0 ] . table_name
47
+ ? `SetofOptions: {
48
+ from: ${ JSON . stringify ( typesById [ fn . args [ 0 ] . type_id ] . format ) }
49
+ to: ${ JSON . stringify ( fn . return_table_name ) }
50
+ isOneToOne: ${ fn . returns_multiple_rows ? false : true }
51
+ }`
52
+ : ''
53
+ } `
54
+ }
43
55
const getReturnType = ( fn : PostgresFunction ) : string => {
44
56
// Case 1: `returns table`.
45
57
const tableArgs = fn . args . filter ( ( { mode } ) => mode === 'table' )
@@ -343,9 +355,14 @@ export type Database = {
343
355
// Exclude all functions definitions that have only one single argument unnamed argument that isn't
344
356
// a json/jsonb/text as it won't be considered by PostgREST
345
357
if (
346
- inArgs . length === 1 &&
347
- inArgs [ 0 ] . name === '' &&
348
- VALID_UNNAMED_FUNCTION_ARG_TYPES . has ( inArgs [ 0 ] . type_id )
358
+ ( inArgs . length === 1 &&
359
+ inArgs [ 0 ] . name === '' &&
360
+ VALID_UNNAMED_FUNCTION_ARG_TYPES . has ( inArgs [ 0 ] . type_id ) ) ||
361
+ // OR if the function have a single unnamed args which is another table (embeded function)
362
+ ( inArgs . length === 1 &&
363
+ inArgs [ 0 ] . name === '' &&
364
+ inArgs [ 0 ] . table_name &&
365
+ func . return_table_name )
349
366
) {
350
367
return true
351
368
}
@@ -434,7 +451,8 @@ export type Database = {
434
451
inArgs . length === 1 &&
435
452
inArgs [ 0 ] . name === '' &&
436
453
( VALID_UNNAMED_FUNCTION_ARG_TYPES . has ( inArgs [ 0 ] . type_id ) ||
437
- inArgs [ 0 ] . has_default )
454
+ inArgs [ 0 ] . has_default ) &&
455
+ ! fn . return_table_name
438
456
)
439
457
} )
440
458
@@ -463,7 +481,7 @@ export type Database = {
463
481
// No conflict - just add the no params signature
464
482
allSignatures . push ( `{
465
483
Args: Record<PropertyKey, never>
466
- Returns: ${ getReturnType ( noParamFn ) } ${ noParamFn . is_set_returning_function && noParamFn . returns_multiple_rows ? '[]' : '' }
484
+ Returns: ${ getTsReturnType ( noParamFn , getReturnType ( noParamFn ) ) }
467
485
}` )
468
486
}
469
487
}
@@ -485,10 +503,25 @@ export type Database = {
485
503
486
504
allSignatures . push ( `{
487
505
Args: { "": ${ tsType } }
488
- Returns: ${ getReturnType ( validUnnamedFn ) } ${ validUnnamedFn . is_set_returning_function && validUnnamedFn . returns_multiple_rows ? '[]' : '' }
506
+ Returns: ${ getTsReturnType ( validUnnamedFn , getReturnType ( validUnnamedFn ) ) }
489
507
}` )
490
508
}
491
509
}
510
+ const unnamedSetofFunctions = fns . filter ( ( fn ) => {
511
+ // Only include unnamed functions that:
512
+ // 1. Have a single unnamed parameter
513
+ // 2. The parameter is of a valid type (json, jsonb, text)
514
+ // 3. All parameters have default values
515
+ const inArgs = fn . args . filter ( ( { mode } ) => VALID_FUNCTION_ARGS_MODE . has ( mode ) )
516
+ return inArgs . length === 1 && inArgs [ 0 ] . name === '' && fn . return_table_name
517
+ } )
518
+ if ( unnamedSetofFunctions . length > 0 ) {
519
+ const unnamedEmbededFunctionsSignatures = unnamedSetofFunctions . map (
520
+ ( fn ) =>
521
+ `{ IsUnnamedEmbededTable: true, Args: Record<PropertyKey, never>, Returns: ${ getTsReturnType ( fn , getReturnType ( fn ) ) } }`
522
+ )
523
+ allSignatures . push ( ...unnamedEmbededFunctionsSignatures )
524
+ }
492
525
493
526
// For functions with named parameters, generate all signatures
494
527
const namedFns = fns . filter ( ( fn ) => ! fn . args . some ( ( { name } ) => name === '' ) )
@@ -545,20 +578,7 @@ export type Database = {
545
578
} )
546
579
. sort ( )
547
580
. join ( ', ' ) } }`}
548
- Returns: ${ returnType } ${ fn . is_set_returning_function && fn . returns_multiple_rows ? '[]' : '' }
549
- ${
550
- fn . returns_set_of_table
551
- ? `SetofOptions: {
552
- from: ${
553
- fn . args . length > 0 && fn . args [ 0 ] . table_name
554
- ? JSON . stringify ( typesById [ fn . args [ 0 ] . type_id ] . format )
555
- : '"*"'
556
- }
557
- to: ${ JSON . stringify ( fn . return_table_name ) }
558
- isOneToOne: ${ fn . returns_multiple_rows ? false : true }
559
- }`
560
- : ''
561
- }
581
+ Returns: ${ getTsReturnType ( fn , returnType ) }
562
582
}` )
563
583
}
564
584
} )
@@ -567,8 +587,13 @@ export type Database = {
567
587
return Array . from ( new Set ( allSignatures ) ) . sort ( )
568
588
} ) ( )
569
589
570
- // Remove duplicates, sort, and join with |
571
- return `${ JSON . stringify ( fnName ) } : ${ signatures . join ( '\n | ' ) } `
590
+ if ( signatures . length > 0 ) {
591
+ // Remove duplicates, sort, and join with |
592
+ return `${ JSON . stringify ( fnName ) } : ${ signatures . join ( '\n | ' ) } `
593
+ } else {
594
+ console . log ( 'fns' , fns )
595
+ return `${ JSON . stringify ( fnName ) } : ${ fns . map ( ( fn ) => `{ Args: unknown, Returns: ${ getTsReturnType ( fn , getReturnType ( fn ) ) } }` ) . join ( '\n |' ) } `
596
+ }
572
597
} )
573
598
} ) ( ) }
574
599
}
0 commit comments