Skip to content

Commit 76b1a2f

Browse files
committed
fix: use base/composite/enum type for fn columns
1 parent dd45793 commit 76b1a2f

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/server/templates/typescript.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ export interface Database {
9999
),
100100
...schemaFunctions
101101
.filter((fn) => fn.argument_types === table.name)
102-
.map(
103-
(fn) =>
104-
`${JSON.stringify(fn.name)}: ${pgTypeToTsType(
105-
fn.return_type,
106-
types,
107-
schemas
108-
)} | null`
109-
),
102+
.map((fn) => {
103+
const type = types.find(({ id }) => id === fn.return_type_id)
104+
let tsType = 'unknown'
105+
if (type) {
106+
tsType = pgTypeToTsType(type.name, types, schemas)
107+
}
108+
return `${JSON.stringify(fn.name)}: ${tsType} | null`
109+
}),
110110
]}
111111
}
112112
Insert: {

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ test('typegen', async () => {
7272
id: number
7373
"user-id": number
7474
blurb: string | null
75+
blurb_varchar: string | null
76+
details_is_long: boolean | null
77+
details_length: number | null
7578
}
7679
Insert: {
7780
details?: string | null
@@ -217,6 +220,24 @@ test('typegen', async () => {
217220
}
218221
Returns: string
219222
}
223+
blurb_varchar: {
224+
Args: {
225+
"": unknown
226+
}
227+
Returns: string
228+
}
229+
details_is_long: {
230+
Args: {
231+
"": unknown
232+
}
233+
Returns: boolean
234+
}
235+
details_length: {
236+
Args: {
237+
"": unknown
238+
}
239+
Returns: number
240+
}
220241
function_returning_row: {
221242
Args: Record<PropertyKey, never>
222243
Returns: {

0 commit comments

Comments
 (0)