Skip to content

feat: add typings for functions returning table #469

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

Closed
Closed
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
12 changes: 11 additions & 1 deletion src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export interface Database {
([fnName, fns]) =>
`${JSON.stringify(fnName)}: ${fns
.map(
({ args, return_type }) => `{
({ args, return_type, complete_statement }) => `{
Args: ${(() => {
const inArgs = args.filter(({ mode }) => mode === 'in')

Expand Down Expand Up @@ -277,6 +277,16 @@ export interface Database {
)} }[]`
}

const setOfTable = schemaTables.find(
({ schema: _schema, name }) => _schema === schema.name && name === return_type
)

if (setOfTable) {
return `Database[${JSON.stringify(schema.name)}]['Tables'][${JSON.stringify(
return_type
)}]['Row']${complete_statement.includes('SETOF') ? '[]' : ''}`
}

return pgTypeToTsType(return_type, types, schemas)
})()}
}`
Expand Down
12 changes: 12 additions & 0 deletions test/db/00-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ create foreign table foreign_table (
name text,
status user_status
) server foreign_server options (schema_name 'public', table_name 'users');

create or replace function public.get_user_by_id(integer) returns public.users as $$
select * from public.users where id = $1;
$$ language sql stable;

create or replace function public.get_users() returns setof public.users as $$
select * from public.users;
$$ language sql stable;

create or replace function public.get_users_typed() returns table (id int, name text) as $$
select id, name from public.users;
$$ language sql stable;