8000 fix: mark optional function parameters for typescript gen · joeally/postgres-meta@f85ff89 · GitHub
[go: up one dir, main page]

Skip to content

Commit f85ff89

Browse files
fix: mark optional function parameters for typescript gen
Co-authored-by: Han Qiao <qiao@supabase.io>
1 parent e4251fa commit f85ff89

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

src/lib/sql/functions.sql

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ with functions as (
1515
array_fill(''::text, array[cardinality(coalesce(p.proallargtypes, p.proargtypes))])
1616
) as arg_names,
1717
-- proallargtypes is null when all arg modes are IN
18-
coalesce(p.proallargtypes, p.proargtypes) as arg_types
18+
coalesce(p.proallargtypes, p.proargtypes) as arg_types,
19+
array_cat(
20+
array_fill(false, array[pronargs - pronargdefaults]),
21+
array_fill(true, array[pronargdefaults])) as arg_has_defaults
1922
from
2023
pg_proc as p
2124
where
@@ -69,14 +72,20 @@ from
6972
left join (
7073
select
7174
oid,
72-
jsonb_agg(jsonb_build_object('mode', t2.mode, 'name', name, 'type_id', type_id)) as args
75+
jsonb_agg(jsonb_build_object(
76+
'mode', t2.mode,
77+
'name', name,
78+
'type_id', type_id,
79+
'has_default', has_default
80+
)) as args
7381
from
7482
(
7583
select
7684
oid,
7785
unnest(arg_modes) as mode,
7886
unnest(arg_names) as name,
79-
unnest(arg_types)::int8 as type_id
87+
unnest(arg_types)::int8 as type_id,
88+
unnest(arg_has_defaults) as has_default
8089
from
8190
functions
8291
) as t1,

src/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const postgresFunctionSchema = Type.Object({
102102
]),
103103
name: Type.String(),
104104
type_id: Type.Number(),
105+
has_default: Type.Boolean(),
105106
})
106107
),
107108
argument_types: Type.String(),

src/server/templates/typescript.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,25 +230,31 @@ export interface Database {
230230
return 'Record<PropertyKey, never>'
231231
}
232232
233-
const argsNameAndType = inArgs.map(({ name, type_id }) => {
233+
const argsNameAndType = inArgs.map(({ name, type_id, has_default }) => {
234234
let type = arrayTypes.find(({ id }) => id === type_id)
235235
if (type) {
236236
// If it's an array type, the name looks like `_int8`.
237237
const elementTypeName = type.name.substring(1)
238238
return {
239239
name,
240240
type: `(${pgTypeToTsType(elementTypeName, types, schemas)})[]`,
241+
has_default,
241242
}
242243
}
243244
type = types.find(({ id }) => id === type_id)
244245
if (type) {
245-
return { name, type: pgTypeToTsType(type.name, types, schemas) }
246+
return {
247+
name,
248+
type: pgTypeToTsType(type.name, types, schemas),
249+
has_default,
250+
}
246251
}
247-
return { name, type: 'unknown' }
252+
return { name, type: 'unknown', has_default }
248253
})
249254
250255
return `{ ${argsNameAndType.map(
251-
({ name, type }) => `${JSON.stringify(name)}: ${type}`
256+
({ name, type, has_default }) =>
257+
`${JSON.stringify(name)}${has_default ? '?' : ''}: ${type}`
252258
)} }`
253259
})()}
254260
Returns: ${(() => {

test/lib/functions.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ test('list', async () => {
88
{
99
"args": [
1010
{
11+
"has_default": false,
1112
9E7A "mode": "in",
1213
"name": "",
1314
"type_id": 23,
1415
},
1516
{
17+
"has_default": false,
1618
"mode": "in",
1719
"name": "",
1820
"type_id": 23,
@@ -98,11 +100,13 @@ test('retrieve, create, update, delete', async () => {
98100
"data": {
99101
"args": [
100102
{
103+
"has_default": false,
101104
"mode": "in",
102105
"name": "a",
103106
"type_id": 21,
104107
},
105108
{
109+
"has_default": false,
106110
"mode": "in",
107111
"name": "b",
108112
"type_id": 21,
@@ -143,11 +147,13 @@ test('retrieve, create, update, delete', async () => {
143147
"data": {
144148
"args": [
145149
{
150+
"has_default": false,
146151
"mode": "in",
147152
"name": "a",
148153
"type_id": 21,
149154
},
150155
{
156+
"has_default": false,
151157
"mode": "in",
152158
"name": "b",
153159
"type_id": 21,
@@ -192,11 +198,13 @@ test('retrieve, create, update, delete', async () => {
192198
"data": {
193199
"args": [
194200
{
201+
"has_default": false,
195202
"mode": "in",
196203
"name": "a",
197204
"type_id": 21,
198205
},
199206
{
207+
"has_default": false,
200208
"mode": "in",
201209
"name": "b",
202210
"type_id": 21,
@@ -237,11 +245,13 @@ test('retrieve, create, update, delete', async () => {
237245
"data": {
238246
"args": [
239247
{
248+
"has_default": false,
240249
"mode": "in",
241250
"name": "a",
242251
"type_id": 21,
243252
},
244253
{
254+
"has_default": false,
245255
"mode": "in",
246256
"name": "b",
247257
"type_id": 21,

0 commit comments

Comments
 (0)
0