Skip to content

feat(typescript): add functions setof type introspection #915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .sentryclirc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[auth]
token=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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
"ts-node": "^10.9.1",
"typescript": "^5.6.3",
"vitest": "^3.0.5"
}
},
"packageManager": "[email protected]+sha512.845196026aab1cc3f098a0474b64dfbab2afe7a1b4e91dd86895d8e4aa32a7a6d03049e2d0ad770bbe4de023a7122fb68c1a1d6e0d033c7076085f9d5d4800d4"
}
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class PostgresMetaTypes {
t.typrelid = 0
or (
select
c.relkind ${includeTableTypes ? `in ('c', 'r')` : `= 'c'`}
c.relkind ${includeTableTypes ? `in ('c', 'r', 'v')` : `= 'c'`}
from
pg_class c
where
Expand Down
75 changes: 55 additions & 20 deletions src/lib/sql/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ select
pg_get_function_result(f.oid) as return_type,
nullif(rt.typrelid::int8, 0) as return_type_relation_id,
f.proretset as is_set_returning_function,
case
when f.proretset and rt.typrelid != 0 and exists (
select 1 from pg_class c
where c.oid = rt.typrelid
-- exclude custom types relation from what is considered a set of table
and c.relkind in ('r', 'p', 'v', 'm', 'f')
) then true
else false
end as returns_set_of_table,
case
when f.proretset and rt.typrelid != 0 then
(select relname from pg_class where oid = rt.typrelid)
else null
end as return_table_name,
case
when f.proretset then
coalesce(f.prorows, 0) > 1
else false
end as returns_multiple_rows,
case
when f.provolatile = 'i' then 'IMMUTABLE'
when f.provolatile = 's' then 'STABLE'
Expand Down Expand Up @@ -76,32 +95,48 @@ from
select
oid,
jsonb_agg(jsonb_build_object(
'mode', t2.mode,
'mode', mode,
'name', name,
'type_id', type_id,
'has_default', has_default
'has_default', has_default,
'table_name', table_name
)) as args
from
(
select
oid,
unnest(arg_modes) as mode,
unnest(arg_names) as name,
unnest(arg_types)::int8 as type_id,
unnest(arg_has_defaults) as has_default
t1.oid,
t2.mode,
t1.name,
t1.type_id,
t1.has_default,
case
when pt.typrelid != 0 then pc.relname
else null
end as table_name
from
functions
) as t1,
lateral (
select
case
when t1.mode = 'i' then 'in'
when t1.mode = 'o' then 'out'
when t1.mode = 'b' then 'inout'
when t1.mode = 'v' then 'variadic'
else 'table'
end as mode
) as t2
(
select
oid,
unnest(arg_modes) as mode,
unnest(arg_names) as name,
unnest(arg_types)::int8 as type_id,
unnest(arg_has_defaults) as has_default
from
functions
) as t1
cross join lateral (
select
case
when t1.mode = 'i' then 'in'
when t1.mode = 'o' then 'out'
when t1.mode = 'b' then 'inout'
when t1.mode = 'v' then 'variadic'
else 'table'
end as mode
) as t2
left join pg_type pt on pt.oid = t1.type_id
left join pg_class pc on pc.oid = pt.typrelid
) sub
group by
t1.oid
oid
) f_args on f_args.oid = f.oid
4 changes: 4 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ const postgresFunctionSchema = Type.Object({
name: Type.String(),
type_id: Type.Number(),
has_default: Type.Boolean(),
table_name: Type.Union([Type.String(), Type.Null()]),
})
),
argument_types: Type.String(),
Expand All @@ -156,6 +157,9 @@ const postgresFunctionSchema = Type.Object({
return_type: Type.String(),
return_type_relation_id: Type.Union([Type.Integer(), Type.Null()]),
is_set_returning_function: Type.Boolean(),
returns_set_of_table: Type.Boolean(),
return_table_name: Type.Union([Type.String(), Type.Null()]),
returns_multiple_rows: Type.Boolean(),
behavior: Type.Union([
Type.Literal('IMMUTABLE'),
Type.Literal('STABLE'),
Expand Down
3 changes: 3 additions & 0 deletions src/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export const GENERATE_TYPES_SWIFT_ACCESS_CONTROL = process.env
.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL
? (process.env.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL as AccessControl)
: 'internal'
// json/jsonb/text types
export const VALID_UNNAMED_FUNCTION_ARG_TYPES = new Set([114, 3802, 25])
export const VALID_FUNCTION_ARGS_MODE = new Set(['in', 'inout', 'variadic'])

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