Skip to content

Commit d521264

Browse files
authored
feat(typegen): add functions setof type introspection (#971)
* feat(typegen): add setof function type introspection - Introspect the setof function fields for functions - Restore functions as unions of args + returns * chore: update snapshots * chore: unify sort and dedup loops * chore: remove duplicate sort * chore: include view in type * fix: isOneToOne * fix: tests * chore: dedup typescript typegen logic * chore: re-use generateColumn * fix: retrieve prorows only * chore: refactor typegen for prorows only * fix: only get reltype in types * chore: reuse relationTypeByIds * chore: reduce functions changes to minimum * chore: only single loop for types * chore: single sort for relationships * chore: reduce loops * fix: relationtype setof functions generation * chore: fix prettier * chore: update snapshots * chore: fix types test * fix: test types * fix: include materializedView types * test: add search_todos_by_details function * fix: add setof from * for all relation functions * fix(typescript): union unknown null (#995) * fix(typescript): unknown is already nullable Fixes: supabase/cli#4234 supabase/cli#577 * fix: also exclude any from null union
1 parent 1d0dad1 commit d521264

File tree

10 files changed

+3308
-178
lines changed

10 files changed

+3308
-178
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:20 as build
1+
FROM node:20 AS build
22
WORKDIR /usr/src/app
33
# Do `npm ci` separately so we can cache `node_modules`
44
# https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"gen:types:go": "PG_META_GENERATE_TYPES=go node --loader ts-node/esm src/server/server.ts",
2727
"gen:types:swift": "PG_META_GENERATE_TYPES=swift node --loader ts-node/esm src/server/server.ts",
2828
"start": "node dist/server/server.js",
29-
"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",
29+
"dev": "trap 'npm run db:clean' INT && run-s db:clean db:run && run-s dev:code",
30+
"dev:code": "nodemon --exec node --loader ts-node/esm src/server/server.ts | pino-pretty --colorize",
3031
"test": "run-s db:clean db:run test:run db:clean",
3132
"db:clean": "cd test/db && docker compose down",
3233
"db:run": "cd test/db && docker compose up --detach --wait",

src/lib/sql/functions.sql.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ select
8585
pg_get_function_result(f.oid) as return_type,
8686
nullif(rt.typrelid::int8, 0) as return_type_relation_id,
8787
f.proretset as is_set_returning_function,
88+
case
89+
when f.proretset then nullif(f.prorows, 0)
90+
else null
91+
end as prorows,
8892
case
8993
when f.provolatile = 'i' then 'IMMUTABLE'
9094
when f.provolatile = 's' then 'STABLE'

src/lib/sql/types.sql.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ select
1313
format_type (t.oid, null) as format,
1414
coalesce(t_enums.enums, '[]') as enums,
1515
coalesce(t_attributes.attributes, '[]') as attributes,
16-
obj_description (t.oid, 'pg_type') as comment
16+
obj_description (t.oid, 'pg_type') as comment,
17+
nullif(t.typrelid::int8, 0) as type_relation_id
1718
from
1819
pg_type t
1920
left join pg_namespace n on n.oid = t.typnamespace
@@ -46,7 +47,7 @@ from
4647
t.typrelid = 0
4748
or (
4849
select
49-
c.relkind ${props.includeTableTypes ? `in ('c', 'r')` : `= 'c'`}
50+
c.relkind ${props.includeTableTypes ? `in ('c', 'r', 'v', 'm')` : `= 'c'`}
5051
from
5152
pg_class c
5253
where

src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ const postgresFunctionSchema = Type.Object({
156156
return_type: Type.String(),
157157
return_type_relation_id: Type.Union([Type.Integer(), Type.Null()]),
158158
is_set_returning_function: Type.Boolean(),
159+
prorows: Type.Union([Type.Number(), Type.Null()]),
159160
behavior: Type.Union([
160161
Type.Literal('IMMUTABLE'),
161162
Type.Literal('STABLE'),
@@ -442,6 +443,7 @@ export const postgresTypeSchema = Type.Object({
442443
enums: Type.Array(Type.String()),
443444
attributes: Type.Array(Type.Object({ name: Type.String(), type_id: Type.Integer() })),
444445
comment: Type.Union([Type.String(), Type.Null()]),
446+
type_relation_id: Type.Union([Type.Integer(), Type.Null()]),
445447
})
446448
export type PostgresType = Static<typeof postgresTypeSchema>
447449

0 commit comments

Comments
 (0)