Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a5b3af1

Browse files
committedMay 13, 2025·
wip
1 parent 29c52c4 commit a5b3af1

File tree

6 files changed

+1165
-435
lines changed

6 files changed

+1165
-435
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": "pnpm@9.15.5+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: 245 additions & 207 deletions
Large diffs are not rendered by default.

‎test/db/00-init.sql

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ as $$
127127
select id, name from public.users;
128128
$$;
129129

130+
create or replace function public.function_returning_table_with_args(user_id int)
131+
returns table (id int, name text)
132+
language sql
133+
stable
134+
as $$
135+
select id, name from public.users WHERE id = user_id;
136+
$$;
137+
138+
130139
create or replace function public.polymorphic_function(text) returns void language sql as '';
131140
create or replace function public.polymorphic_function(bool) returns void language sql as '';
132141

@@ -295,11 +304,84 @@ create function postgrest_resolvable_with_override_function(user_row users) retu
295304
SELECT * FROM todos WHERE "user-id" = user_row.id;
296305
$$;
297306

298-
create or replace function public.polymorphic_function_with_different_return(text) returns void language sql as '';
299307
create or replace function public.polymorphic_function_with_different_return(bool) returns int language sql as 'SELECT 1';
308+
create or replace function public.polymorphic_function_with_different_return(int) returns int language sql as 'SELECT 2';
309+
create or replace function public.polymorphic_function_with_different_return(text) returns text language sql as $$ SELECT 'foo' $$;
300310

311+
create or replace function public.polymorphic_function_with_no_params_or_unnamed() returns int language sql as 'SELECT 1';
312+
create or replace function public.polymorphic_function_with_no_params_or_unnamed(bool) returns int language sql as 'SELECT 2';
313+
create or replace function public.polymorphic_function_with_no_params_or_unnamed(text) returns text language sql as $$ SELECT 'foo' $$;
301314
-- Function with a single unnamed params that isn't a json/jsonb/text should never appears in the type gen as it won't be in postgrest schema
302315
create or replace function public.polymorphic_function_with_unnamed_integer(int) returns int language sql as 'SELECT 1';
303316
create or replace function public.polymorphic_function_with_unnamed_json(json) returns int language sql as 'SELECT 1';
304317
create or replace function public.polymorphic_function_with_unnamed_jsonb(jsonb) returns int language sql as 'SELECT 1';
305-
create or replace function public.polymorphic_function_with_unnamed_text(text) returns int language sql as 'SELECT 1';
318+
create or replace function public.polymorphic_function_with_unnamed_text(text) returns int language sql as 'SELECT 1';
319+
320+
-- Functions with unnamed parameters that have default values
321+
create or replace function public.polymorphic_function_with_unnamed_default() returns int language sql as 'SELECT 1';
322+
create or replace function public.polymorphic_function_with_unnamed_default(int default 42) returns int language sql as 'SELECT 2';
323+
create or replace function public.polymorphic_function_with_unnamed_default(text default 'default') returns text language sql as $$ SELECT 'foo' $$;
324+
325+
-- Functions with unnamed parameters that have default values and multiple overloads
326+
create or replace function public.polymorphic_function_with_unnamed_default_overload() returns int language sql as 'SELECT 1';
327+
create or replace function public.polymorphic_function_with_unnamed_default_overload(int default 42) returns int language sql as 'SELECT 2';
328+
create or replace function public.polymorphic_function_with_unnamed_default_overload(text default 'default') returns text language sql as $$ SELECT 'foo' $$;
329+
create or replace function public.polymorphic_function_with_unnamed_default_overload(bool default true) returns int language sql as 'SELECT 3';
330+
331+
-- Test function with unnamed row parameter returning setof
332+
CREATE OR REPLACE FUNCTION public.test_unnamed_row_setof(todos)
333+
RETURNS SETOF todos
334+
LANGUAGE SQL STABLE
335+
AS $$
336+
SELECT * FROM public.todos WHERE "user-id" = $1."user-id";
337+
$$;
338+
339+
CREATE OR REPLACE FUNCTION public.test_unnamed_row_setof(users)
340+
RETURNS SETOF todos
341+
LANGUAGE SQL STABLE
342+
AS $$
343+
SELECT * FROM public.todos WHERE "user-id" = $1."id";
344+
$$;
345+
346+
347+
CREATE OR REPLACE FUNCTION public.test_unnamed_row_setof(user_id bigint)
348+
RETURNS SETOF todos
349+
LANGUAGE SQL STABLE
350+
AS $$
351+
SELECT * FROM public.todos WHERE "user-id" = user_id;
352+
$$;
353+
354+
-- Test function with unnamed row parameter returning scalar
355+
CREATE OR REPLACE FUNCTION public.test_unnamed_row_scalar(todos)
356+
RETURNS integer
357+
LANGUAGE SQL STABLE
358+
AS $$
359+
SELECT COUNT(*) FROM public.todos WHERE "user-id" = $1."user-id";
360+
$$;
361+
362+
-- Test function with unnamed view row parameter
363+
CREATE OR REPLACE FUNCTION public.test_unnamed_view_row(todos_view)
364+
RETURNS SETOF todos
365+
LANGUAGE SQL STABLE
366+
AS $$
367+
SELECT * FROM public.todos WHERE "user-id" = $1."user-id";
368+
$$;
369+
370+
-- Test function with multiple unnamed row parameters
371+
CREATE OR REPLACE FUNCTION public.test_unnamed_multiple_rows(users, todos)
372+
RETURNS SETOF todos
373+
LANGUAGE SQL STABLE
374+
AS $$
375+
SELECT * FROM public.todos
376+
WHERE "user-id" = $1.id
377+
AND id = $2.id;
378+
$$;
379+
380+
-- Test function with unnamed row parameter returning composite
381+
CREATE OR REPLACE FUNCTION public.test_unnamed_row_composite(users)
382+
RETURNS composite_type_with_array_attribute
383+
LANGUAGE SQL STABLE
384+
AS $$
385+
SELECT ROW(ARRAY[$1.name])::composite_type_with_array_attribute;
386+
$$;
387+

‎test/lib/functions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ test('list set-returning function with single object limit', async () => {
8080
"definition": "
8181
SELECT * FROM public.users_audit WHERE user_id = user_row.id;
8282
",
83-
"id": 16502,
83+
"id": 16503,
8484
"identity_argument_types": "user_row users",
8585
"is_set_returning_function": true,
8686
"language": "sql",
@@ -126,7 +126,7 @@ test('list set-returning function with multiples definitions', async () => {
126126
"definition": "
127127
SELECT * FROM public.todos WHERE "user-id" = user_row.id;
128128
",
129-
"id": 16503,
129+
"id": 16504,
130130
"identity_argument_types": "user_row users",
131131
"is_set_returning_function": true,
132132
"language": "sql",
@@ -164,7 +164,7 @@ test('list set-returning function with multiples definitions', async () => {
164164
"definition": "
165165
SELECT * FROM public.todos WHERE "user-id" = todo_row."user-id";
166166
",
167-
"id": 16504,
167+
"id": 16505,
168168
"identity_argument_types": "todo_row todos",
169169
"is_set_returning_function": true,
170170
"language": "sql",

‎test/server/typegen.ts

Lines changed: 828 additions & 222 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.