Skip to content
Open
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20 as build
FROM node:20 AS build
WORKDIR /usr/src/app
# Do `npm ci` separately so we can cache `node_modules`
# https://nodejs.org/en/docs/guides/nodejs-docker-webapp/
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"gen:types:go": "PG_META_GENERATE_TYPES=go node --loader ts-node/esm src/server/server.ts",
"gen:types:swift": "PG_META_GENERATE_TYPES=swift node --loader ts-node/esm src/server/server.ts",
"start": "node dist/server/server.js",
"dev": "trap 'npm run db:clean' INT && run-s db:clean db:run && nodemon --exec node --loader ts-node/esm src/server/server.ts | pino-pretty --colorize",
"dev": "trap 'npm run db:clean' INT && run-s db:clean db:run && run-s dev:code",
"dev:code": "nodemon --exec node --loader ts-node/esm src/server/server.ts | pino-pretty --colorize",
"test": "run-s db:clean db:run test:run db:clean",
"db:clean": "cd test/db && docker compose down",
"db:run": "cd test/db && docker compose up --detach --wait",
Expand Down
76 changes: 56 additions & 20 deletions src/lib/sql/functions.sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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 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,
Comment on lines +88 to +106
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not do select from pg_class within result column expressions - this might cause performance issues.

returns_set_of_table should be redundant with is_set_returning_function and return_type_relation_id (is_set_returning_function && return_type_relation_id !== null)

And return_table_name should be redundant with return_type_relation_id since we can get the table name from the /tables output using the relation id

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking instead of returns_multiple_rows we can just return prorows as is (setting it as null if 0)

It's easy to confuse it with is_set_returning_function since they mean the same thing; rather what we're doing is adhering to PostgREST's idiosyncrasies

case
when f.provolatile = 'i' then 'IMMUTABLE'
when f.provolatile = 's' then 'STABLE'
Expand Down Expand Up @@ -117,34 +136,51 @@ 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
from
functions
) as t1,
lateral (
select
t1.oid,
t2.mode,
t1.name,
t1.type_id,
t1.has_default,
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
when pt.typrelid != 0 then pc.relname
else null
end as table_name
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
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
order by t1.name asc
) sub
group by
t1.oid
oid
) f_args on f_args.oid = f.oid
${props.limit ? `limit ${props.limit}` : ''}
${props.offset ? `offset ${props.offset}` : ''}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/sql/types.sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ from
t.typrelid = 0
or (
select
c.relkind ${props.includeTableTypes ? `in ('c', 'r')` : `= 'c'`}
c.relkind ${props.includeTableTypes ? `in ('c', 'r', 'v')` : `= 'c'`}
from
pg_class c
where
Expand Down
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
4 changes: 4 additions & 0 deletions src/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export const GENERATE_TYPES_SWIFT_ACCESS_CONTROL = process.env
? (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
// from MB to Bytes
Expand Down
Loading