Skip to content

Commit

Permalink
Merge pull request #624 from gulfaraz/fix.fn-return-type
Browse files Browse the repository at this point in the history
fix(typegen): use base/composite/enum type for fn columns
  • Loading branch information
soedirgo authored Nov 9, 2023
2 parents 07a1e4c + a10d206 commit b40aec2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
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

0 comments on commit b40aec2

Please sign in to comment.