Skip to content

Commit b40aec2

Browse files
authored
Merge pull request #624 from gulfaraz/fix.fn-return-type
fix(typegen): use base/composite/enum type for fn columns
2 parents 07a1e4c + a10d206 commit b40aec2

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

src/server/templates/typescript.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ export interface Database {
101101
),
102102
...schemaFunctions
103103
.filter((fn) => fn.argument_types === table.name)
104-
.map(
105-
(fn) =>
106-
`${JSON.stringify(fn.name)}: ${pgTypeToTsType(
107-
fn.return_type,
108-
types,
109-
schemas
110-
)} | null`
111-
),
104+
.map((fn) => {
105+
const type = types.find(({ id }) => id === fn.return_type_id)
106+
let tsType = 'unknown'
107+
if (type) {
108+
tsType = pgTypeToTsType(type.name, types, schemas)
109+
}
110+
return `${JSON.stringify(fn.name)}: ${tsType} | null`
111+
}),
112112
]}
113113
}
114114
Insert: {
@@ -428,7 +428,7 @@ const pgTypeToTsType = (
428428
): string => {
429429
if (pgType === 'bool') {
430430
return 'boolean'
431-
} else if (['int2', 'int4', 'int8', 'float4', 'float8', 'numeric', 'integer'].includes(pgType)) {
431+
} else if (['int2', 'int4', 'int8', 'float4', 'float8', 'numeric'].includes(pgType)) {
432432
return 'number'
433433
} else if (
434434
[

test/db/00-init.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ $$
6161
select substring($1.details, 1, 3);
6262
$$ language sql stable;
6363

64+
create function public.blurb_varchar(public.todos) returns character varying as
65+
$$
66+
select substring($1.details, 1, 3);
67+
$$ language sql stable;
68+
69+
create function public.details_length(public.todos) returns integer as
70+
$$
71+
select length($1.details);
72+
$$ language sql stable;
73+
74+
create function public.details_is_long(public.todos) returns boolean as
75+
$$
76+
select $1.details_length > 20;
77+
$$ language sql stable;
78+
6479
create extension postgres_fdw;
6580
create server foreign_server foreign data wrapper postgres_fdw options (host 'localhost', port '5432', dbname 'postgres');
6681
create user mapping for postgres server foreign_server options (user 'postgres', password 'postgres');

test/server/typegen.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ test('typegen', async () => {
6969
id: number
7070
"user-id": number
7171
blurb: string | null
72+
blurb_varchar: string | null
73+
details_is_long: boolean | null
74+
details_length: number | null
7275
}
7376
Insert: {
7477
details?: string | null
@@ -278,6 +281,24 @@ test('typegen', async () => {
278281
}
279282
Returns: string
280283
}
284+
blurb_varchar: {
285+
Args: {
286+
"": unknown
287+
}
288+
Returns: string
289+
}
290+
details_is_long: {
291+
Args: {
292+
"": unknown
293+
}
294+
Returns: boolean
295+
}
296+
details_length: {
297+
Args: {
298+
"": unknown
299+
}
300+
Returns: number
301+
}
281302
function_returning_row: {
282303
Args: Record<PropertyKey, never>
283304
Returns: {
@@ -420,6 +441,9 @@ test('typegen w/ one-to-one relationships', async () => {
420441
id: number
421442
"user-id": number
422443
blurb: string | null
444+
blurb_varchar: string | null
445+
details_is_long: boolean | null
446+
details_length: number | null
423447
}
424448
Insert: {
425449
details?: string | null
@@ -641,6 +665,24 @@ test('typegen w/ one-to-one relationships', async () => {
641665
}
642666
Returns: string
643667
}
668+
blurb_varchar: {
669+
Args: {
670+
"": unknown
671+
}
672+
Returns: string
673+
}
674+
details_is_long: {
675+
Args: {
676+
"": unknown
677+
}
678+
Returns: boolean
679+
}
680+
details_length: {
681+
Args: {
682+
"": unknown
683+
}
684+
Returns: number
685+
}
644686
function_returning_row: {
645687
Args: Record<PropertyKey, never>
646688
Returns: {

0 commit comments

Comments
 (0)