Skip to content

Commit b69e594

Browse files
committed
wip
1 parent f2d5cc9 commit b69e594

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@
7272
"ts-node": "^10.9.1",
7373
"typescript": "^5.6.3",
7474
"vitest": "^3.0.5"
75-
}
75+
},
76+
"packageManager": "[email protected]+sha512.845196026aab1cc3f098a0474b64dfbab2afe7a1b4e91dd86895d8e4aa32a7a6d03049e2d0ad770bbe4de023a7122fb68c1a1d6e0d033c7076085f9d5d4800d4"
7677
}

src/server/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ export const GENERATE_TYPES_SWIFT_ACCESS_CONTROL = process.env
4949
.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL
5050
? (process.env.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL as AccessControl)
5151
: 'internal'
52+
// json/jsonb/text types
53+
export const VALID_UNNAMED_FUNCTION_ARG_TYPES = new Set([114, 3802, 25])
54+
export const VALID_FUNCTION_ARGS_MODE = new Set(['in', 'inout', 'variadic'])
5255

5356
export const PG_META_MAX_RESULT_SIZE = process.env.PG_META_MAX_RESULT_SIZE_MB
5457
? // Node-postgres get a maximum size in bytes make the conversion from the env variable

src/server/templates/typescript.ts

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import type {
88
PostgresView,
99
} from '../../lib/index.js'
1010
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'
1216

1317
export 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

Comments
 (0)