Skip to content

Commit 71cd6c5

Browse files
committed
fix(typegen): array types in fn args
1 parent 3438a1d commit 71cd6c5

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

src/server/app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ if (EXPORT_DOCS) {
6969
const { data: views, error: viewsError } = await pgMeta.views.list()
7070
const { data: functions, error: functionsError } = await pgMeta.functions.list()
7171
const { data: types, error: typesError } = await pgMeta.types.list({
72+
includeArrayTypes: true,
7273
includeSystemSchemas: true,
7374
})
7475
await pgMeta.end()
@@ -101,7 +102,8 @@ if (EXPORT_DOCS) {
101102
functions: functions.filter(
102103
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
103104
),
104-
types,
105+
types: types.filter(({ name }) => name[0] !== '_'),
106+
arrayTypes: types.filter(({ name }) => name[0] === '_'),
105107
})
106108
)
107109
})()

src/server/routes/generators/typescript.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default async (fastify: FastifyInstance) => {
2424
const { data: views, error: viewsError } = await pgMeta.views.list()
2525
const { data: functions, error: functionsError } = await pgMeta.functions.list()
2626
const { data: types, error: typesError } = await pgMeta.types.list({
27+
includeArrayTypes: true,
2728
includeSystemSchemas: true,
2829
})
2930
await pgMeta.end()
@@ -65,7 +66,8 @@ export default async (fastify: FastifyInstance) => {
6566
functions: functions.filter(
6667
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
6768
),
68-
types,
69+
types: types.filter(({ name }) => name[0] !== '_'),
70+
arrayTypes: types.filter(({ name }) => name[0] === '_'),
6971
})
7072
})
7173
}

src/server/templates/typescript.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ export const apply = ({
1313
views,
1414
functions,
1515
types,
16+
arrayTypes,
1617
}: {
1718
schemas: PostgresSchema[]
1819
tables: PostgresTable[]
1920
views: PostgresView[]
2021
functions: PostgresFunction[]
2122
types: PostgresType[]
23+
arrayTypes: PostgresType[]
2224
}): string => {
2325
let output = `
2426
export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]
@@ -214,11 +216,20 @@ export interface Database {
214216
}
215217
216218
const argsNameAndType = inArgs.map(({ name, type_id }) => {
217-
const type = types.find(({ id }) => id === type_id)
218-
if (!type) {
219-
return { name, type: 'unknown' }
219+
let type = arrayTypes.find(({ id }) => id === type_id)
220+
if (type) {
221+
// If it's an array type, the name looks like `_int8`.
222+
const elementTypeName = type.name.substring(1)
223+
return {
224+
name,
225+
type: `(${pgTypeToTsType(elementTypeName, types, schemas)})[]`,
226+
}
227+
}
228+
type = types.find(({ id }) => id === type_id)
229+
if (type) {
230+
return { name, type: pgTypeToTsType(type.name, types, schemas) }
220231
}
221-
return { name, type: pgTypeToTsType(type.name, types, schemas) }
232+
return { name, type: 'unknown' }
222233
})
223234
224235
return `{ ${argsNameAndType.map(
@@ -230,11 +241,20 @@ export interface Database {
230241
231242
if (tableArgs.length > 0) {
232243
const argsNameAndType = tableArgs.map(({ name, type_id }) => {
233-
const type = types.find(({ id }) => id === type_id)
234-
if (!type) {
235-
return { name, type: 'unknown' }
244+
let type = arrayTypes.find(({ id }) => id === type_id)
245+
if (type) {
246+
// If it's an array type, the name looks like `_int8`.
247+
const elementTypeName = type.name.substring(1)
248+
return {
249+
name,
250+
type: `(${pgTypeToTsType(elementTypeName, types, schemas)})[]`,
251+
}
236252
}
237-
return { name, type: pgTypeToTsType(type.name, types, schemas) }
253+
type = types.find(({ id }) => id === type_id)
254+
if (type) {
255+
return { name, type: pgTypeToTsType(type.name, types, schemas) }
256+
}
257+
return { name, type: 'unknown' }
238258
})
239259
240260
return `{ ${argsNameAndType.map(

0 commit comments

Comments
 (0)