@@ -8,7 +8,11 @@ import type {
88 PostgresView ,
99} from '../../lib/index.js'
1010import type { GeneratorMetadata } from '../../lib/generators.js'
11- import { GENERATE_TYPES_DEFAULT_SCHEMA } from '../constants.js'
11+ import {
12+ VALID_UNNAMED_FUNCTION_ARG_TYPES ,
13+ GENERATE_TYPES_DEFAULT_SCHEMA ,
14+ VALID_FUNCTION_ARGS_MODE ,
15+ } from '../constants.js'
1216
1317export const apply = async ( {
1418 schemas,
@@ -274,25 +278,56 @@ export type Database = {
274278 if ( schemaFunctions . length === 0 ) {
275279 return '[_ in never]: never'
276280 }
281+ const schemaFunctionsGroupedByName = schemaFunctions
282+ . filter ( ( func ) => {
283+ // Get all input args (in, inout, variadic modes)
284+
285+ const inArgs = func . args . filter ( ( { mode } ) => VALID_FUNCTION_ARGS_MODE . has ( mode ) )
286+ // Case 1: Function has no parameters
287+ if ( inArgs . length === 0 ) {
288+ return true
289+ }
277290
278- const schemaFunctionsGroupedByName = schemaFunctions . reduce (
279- ( acc , curr ) => {
280- acc [ curr . name ] ??= [ ]
281- acc [ curr . name ] . push ( curr )
282- return acc
283- } ,
284- { } as Record < string , PostgresFunction [ ] >
285- )
291+ // Case 2: All input args are named
292+ if ( ! inArgs . some ( ( { name } ) => name === '' ) ) {
293+ return true
294+ }
295+
296+ // Case 3: All unnamed args have default values
297+ if ( inArgs . every ( ( arg ) => ( arg . name === '' ? arg . has_default : true ) ) ) {
298+ return true
299+ }
300+
301+ // Case 4: Single unnamed parameter of valid type (json, jsonb, text)
302+ // Exclude all functions definitions that have only one single argument unnamed argument that isn't
303+ // a json/jsonb/text as it won't be considered by PostgREST
304+ if (
305+ inArgs . length === 1 &&
306+ inArgs [ 0 ] . name === '' &&
307+ VALID_UNNAMED_FUNCTION_ARG_TYPES . has ( inArgs [ 0 ] . type_id )
308+ ) {
309+ return true
310+ }
311+
312+ return false
313+ } )
314+ . reduce (
315+ ( acc , curr ) => {
316+ acc [ curr . name ] ??= [ ]
317+ acc [ curr . name ] . push ( curr )
318+ return acc
319+ } ,
320+ { } as Record < string , PostgresFunction [ ] >
321+ )
286322
287323 return Object . entries ( schemaFunctionsGroupedByName ) . map ( ( [ fnName , fns ] ) => {
288324 // Group functions by their argument names signature to detect conflicts
289325 const fnsByArgNames = new Map < string , PostgresFunction [ ] > ( )
326+ fns . sort ( ( fn1 , fn2 ) => fn1 . id - fn2 . id )
290327
291328 fns . forEach ( ( fn ) => {
292329 const namedInArgs = fn . args
293- . filter (
294- ( { mode, name } ) => [ 'in' , 'inout' , 'variadic' ] . includes ( mode ) && name !== ''
295- )
330+ . filter ( ( { mode, name } ) => VALID_FUNCTION_ARGS_MODE . has ( mode ) && name !== '' )
296331 . map ( ( arg ) => arg . name )
297332 . sort ( )
298333 . join ( ',' )
@@ -312,8 +347,7 @@ export type Database = {
312347 const firstFnArgTypes = new Map (
313348 firstFn . args
314349 . filter (
315- ( { mode, name } ) =>
316- [ 'in' , 'inout' , 'variadic' ] . includes ( mode ) && name !== ''
350+ ( { mode, name } ) => VALID_FUNCTION_ARGS_MODE . has ( mode ) && name !== ''
317351 )
318352 . map ( ( arg ) => [ arg . name , String ( arg . type_id ) ] )
319353 )
@@ -322,8 +356,7 @@ export type Database = {
322356 const fnArgTypes = new Map (
323357 fn . args
324358 . filter (
325- ( { mode, name } ) =>
326- [ 'in' , 'inout' , 'variadic' ] . includes ( mode ) && name !== ''
359+ ( { mode, name } ) => VALID_FUNCTION_ARGS_MODE . has ( mode ) && name !== ''
327360 )
328361 . map ( ( arg ) => [ arg . name , String ( arg . type_id ) ] )
329362 )
0 commit comments