Skip to content

Commit ef2953e

Browse files
committed
WIP: add functions to /get path
1 parent 7a38f41 commit ef2953e

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

sql/paths.sql

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ create or replace function oas_build_paths(schemas text[])
44
returns jsonb language sql stable as
55
$$
66
select oas_build_path_item_root() ||
7-
oas_build_path_items_from_tables(schemas);
7+
oas_build_path_items_from_tables(schemas) ||
8+
oas_build_path_items_from_functions(schemas);
89
$$;
910

1011
create or replace function oas_build_path_items_from_tables(schemas text[])
@@ -134,6 +135,72 @@ from (
134135
) x;
135136
$$;
136137

138+
create or replace function oas_build_path_items_from_functions(schemas text[])
139+
returns jsonb language sql stable as
140+
$$
141+
select jsonb_object_agg(x.path, x.oas_path_item)
142+
from (
143+
select '/rpc/' || proc_name as path,
144+
oas_path_item_object(
145+
get :=oas_operation_object(
146+
description := proc_description,
147+
tags := array[proc_name],
148+
parameters := jsonb_agg(
149+
oas_build_reference_to_parameters(format('rowFilter.%1$s.%2$s', proc_name, arg_name))
150+
-- TODO: Add table filters for when the function returns a table
151+
) ||
152+
case when rettype_is_table then
153+
jsonb_build_array(
154+
oas_build_reference_to_parameters('select'),
155+
oas_build_reference_to_parameters('order'),
156+
oas_build_reference_to_parameters('limit'),
157+
oas_build_reference_to_parameters('offset'),
158+
oas_build_reference_to_parameters('or'),
159+
oas_build_reference_to_parameters('and'),
160+
oas_build_reference_to_parameters('not.or'),
161+
oas_build_reference_to_parameters('not.and'),
162+
oas_build_reference_to_parameters('range'),
163+
oas_build_reference_to_parameters('preferGet')
164+
)
165+
else
166+
jsonb_build_array(
167+
oas_build_reference_to_parameters('preferGet')
168+
)
169+
end,
170+
responses :=
171+
case when rettype_is_table then
172+
jsonb_build_object(
173+
'200',
174+
oas_build_reference_to_responses('notEmpty.' || rettype_name, 'OK'),
175+
'206',
176+
oas_build_reference_to_responses('notEmpty.' || rettype_name, 'Partial Content'),
177+
)
178+
when rettype_is_composite then
179+
jsonb_build_object(
180+
'200',
181+
oas_build_reference_to_c,
182+
)
183+
else
184+
jsonb_build_object(
185+
'200',
186+
oas_build_reference_to_responses('notEmpty.' || name, 'OK'),
187+
)
188+
end ||
189+
jsonb_build_object(
190+
'default',
191+
oas_build_reference_to_responses('defaultError', 'Error')
192+
)
193+
)
194+
) as oas_path_item
195+
from (
196+
select proc_schema, proc_name, proc_description, rettype_schema, rettype_name, rettype_is_table, rettype_is_composite, unnest(all_args) as arg_name
197+
from postgrest_get_all_functions(schemas)
198+
) _
199+
where schema = any(schemas)
200+
group by proc_schema, proc_name, proc_description, rettype_is_table, rettype_is_composite, rettype_schema, rettype_name
201+
) x;
202+
$$;
203+
137204
create or replace function oas_build_path_item_root()
138205
returns jsonb language sql stable as
139206
$$

sql/utils.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ select case when type like any(array['character', 'character varying', 'text'])
88
when type like any(array['bigint', 'integer', 'smallint']) then 'integer'
99
when type like 'boolean' then 'boolean'
1010
when type like '%[]' then 'array'
11-
when type like 'json' then 'object'
12-
when type like 'jsonb' then 'object'
11+
when type like any(array['json', 'jsonb', 'record']) then 'object'
1312
else 'string' end;
1413
$$;
1514

0 commit comments

Comments
 (0)