@@ -8,7 +8,11 @@ import type {
8
8
PostgresView ,
9
9
} from '../../lib/index.js'
10
10
import 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'
12
16
13
17
export const apply = async ( {
14
18
schemas,
@@ -274,25 +278,56 @@ export type Database = {
274
278
if ( schemaFunctions . length === 0 ) {
275
279
return '[_ in never]: never'
276
280
}
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
+ }
277
290
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
+ )
286
322
287
323
return Object . entries ( schemaFunctionsGroupedByName ) . map ( ( [ fnName , fns ] ) => {
288
324
// Group functions by their argument names signature to detect conflicts
289
325
const fnsByArgNames = new Map < string , PostgresFunction [ ] > ( )
326
+ fns . sort ( ( fn1 , fn2 ) => fn1 . id - fn2 . id )
290
327
291
328
fns . forEach ( ( fn ) => {
292
329
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 !== '' )
296
331
. map ( ( arg ) => arg . name )
297
332
. sort ( )
298
333
. join ( ',' )
@@ -312,8 +347,7 @@ export type Database = {
312
347
const firstFnArgTypes = new Map (
313
348
firstFn . args
314
349
. filter (
315
- ( { mode, name } ) =>
316
- [ 'in' , 'inout' , 'variadic' ] . includes ( mode ) && name !== ''
350
+ ( { mode, name } ) => VALID_FUNCTION_ARGS_MODE . has ( mode ) && name !== ''
317
351
)
318
352
. map ( ( arg ) => [ arg . name , String ( arg . type_id ) ] )
319
353
)
@@ -322,8 +356,7 @@ export type Database = {
322
356
const fnArgTypes = new Map (
323
357
fn . args
324
358
. filter (
325
- ( { mode, name } ) =>
326
- [ 'in' , 'inout' , 'variadic' ] . includes ( mode ) && name !== ''
359
+ ( { mode, name } ) => VALID_FUNCTION_ARGS_MODE . has ( mode ) && name !== ''
327
360
)
328
361
. map ( ( arg ) => [ arg . name , String ( arg . type_id ) ] )
329
362
)
0 commit comments