Skip to content

fix(typegen): use base/composite/enum type for fn columns #624

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

Merged
merged 7 commits into from
Nov 9, 2023
Merged
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
18 changes: 9 additions & 9 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ export interface Database {
),
...schemaFunctions
.filter((fn) => fn.argument_types === table.name)
.map(
(fn) =>
`${JSON.stringify(fn.name)}: ${pgTypeToTsType(
fn.return_type,
types,
schemas
)} | null`
),
.map((fn) => {
const type = types.find(({ id }) => id === fn.return_type_id)
let tsType = 'unknown'
if (type) {
tsType = pgTypeToTsType(type.name, types, schemas)
}
return `${JSON.stringify(fn.name)}: ${tsType} | null`
}),
]}
}
Insert: {
Expand Down Expand Up @@ -428,7 +428,7 @@ const pgTypeToTsType = (
): string => {
if (pgType === 'bool') {
return 'boolean'
} else if (['int2', 'int4', 'int8', 'float4', 'float8', 'numeric', 'integer'].includes(pgType)) {
} else if (['int2', 'int4', 'int8', 'float4', 'float8', 'numeric'].includes(pgType)) {
return 'number'
} else if (
[
Expand Down
15 changes: 15 additions & 0 deletions test/db/00-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ $$
select substring($1.details, 1, 3);
$$ language sql stable;

create function public.blurb_varchar(public.todos) returns character varying as
$$
select substring($1.details, 1, 3);
$$ language sql stable;

create function public.details_length(public.todos) returns integer as
$$
select length($1.details);
$$ language sql stable;

create function public.details_is_long(public.todos) returns boolean as
$$
select $1.details_length > 20;
$$ language sql stable;

create extension postgres_fdw;
create server foreign_server foreign data wrapper postgres_fdw options (host 'localhost', port '5432', dbname 'postgres');
create user mapping for postgres server foreign_server options (user 'postgres', password 'postgres');
Expand Down
42 changes: 42 additions & 0 deletions test/server/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ test('typegen', async () => {
id: number
"user-id": number
blurb: string | null
blurb_varchar: string | null
details_is_long: boolean | null
details_length: number | null
}
Insert: {
details?: string | null
Expand Down Expand Up @@ -278,6 +281,24 @@ test('typegen', async () => {
}
Returns: string
}
blurb_varchar: {
Args: {
"": unknown
}
Returns: string
}
details_is_long: {
Args: {
"": unknown
}
Returns: boolean
}
details_length: {
Args: {
"": unknown
}
Returns: number
}
function_returning_row: {
Args: Record<PropertyKey, never>
Returns: {
Expand Down Expand Up @@ -420,6 +441,9 @@ test('typegen w/ one-to-one relationships', async () => {
id: number
"user-id": number
blurb: string | null
blurb_varchar: string | null
details_is_long: boolean | null
details_length: number | null
}
Insert: {
details?: string | null
Expand Down Expand Up @@ -641,6 +665,24 @@ test('typegen w/ one-to-one relationships', async () => {
}
Returns: string
}
blurb_varchar: {
Args: {
"": unknown
}
Returns: string
}
details_is_long: {
Args: {
"": unknown
}
Returns: boolean
}
details_length: {
Args: {
"": unknown
}
Returns: number
}
function_returning_row: {
Args: Record<PropertyKey, never>
Returns: {
Expand Down