Skip to content

Commit b256480

Browse files
authored
Merge pull request #334 from supabase/fix/typegen/change-empty-object-type
fix(typegen): change empty object type
2 parents e2fe8d5 + 935f528 commit b256480

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

src/lib/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ export {
88
PostgresConfig,
99
PostgresExtension,
1010
PostgresFunction,
11+
PostgresFunctionCreate,
1112
PostgresPolicy,
1213
PostgresPrimaryKey,
1314
PostgresPublication,
1415
PostgresRelationship,
1516
PostgresRole,
1617
PostgresSchema,
18+
PostgresSchemaCreate,
19+
PostgresSchemaUpdate,
1720
PostgresTable,
1821
PostgresTrigger,
1922
PostgresType,
2023
PostgresVersion,
24+
PostgresView,
2125
} from './types'

src/server/app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ if (EXPORT_DOCS) {
9292
GENERATE_TYPES_INCLUDED_SCHEMAS.includes(name)
9393
),
9494
tables,
95-
functions,
95+
functions: functions.filter(
96+
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
97+
),
9698
types,
9799
})
98100
)

src/server/routes/generators/typescript.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ export default async (fastify: FastifyInstance) => {
5555
(includedSchemas.length === 0 || includedSchemas.includes(name))
5656
),
5757
tables,
58-
functions,
58+
functions: functions.filter(
59+
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
60+
),
5961
types,
6062
})
6163
})

src/server/templates/typescript.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ export const apply = ({
1616
export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]
1717
1818
export interface Database {
19-
${schemas.map(
20-
(schema) =>
21-
`${JSON.stringify(schema.name)}: {
19+
${schemas.map((schema) => {
20+
const schemaTables = tables.filter((table) => table.schema === schema.name)
21+
const schemaFunctions = functions.filter((func) => func.schema === schema.name)
22+
return `${JSON.stringify(schema.name)}: {
2223
Tables: {
23-
${tables
24-
.filter((table) => table.schema === schema.name)
25-
.map(
26-
(table) => `${JSON.stringify(table.name)}: {
24+
${
25+
schemaTables.length === 0
26+
? '[_ in never]: never'
27+
: schemaTables.map(
28+
(table) => `${JSON.stringify(table.name)}: {
2729
Row: {
2830
${table.columns.map(
2931
(column) =>
@@ -77,22 +79,21 @@ export interface Database {
7779
})}
7880
}
7981
}`
80-
)}
82+
)
83+
}
8184
}
8285
Functions: {
83-
${functions
84-
.filter(
85-
(function_) =>
86-
function_.schema === schema.name && function_.return_type !== 'trigger'
87-
)
88-
.map(
89-
(function_) => `${JSON.stringify(function_.name)}: {
86+
${
87+
schemaFunctions.length === 0
88+
? '[_ in never]: never'
89+
: schemaFunctions.map(
90+
(func) => `${JSON.stringify(func.name)}: {
9091
Args: ${(() => {
91-
if (function_.argument_types === '') {
92+
if (func.argument_types === '') {
9293
return 'Record<PropertyKey, never>'
9394
}
9495
95-
const splitArgs = function_.argument_types.split(',').map((arg) => arg.trim())
96+
const splitArgs = func.argument_types.split(',').map((arg) => arg.trim())
9697
if (splitArgs.some((arg) => arg.includes('"') || !arg.includes(' '))) {
9798
return 'Record<string, unknown>'
9899
}
@@ -110,12 +111,13 @@ export interface Database {
110111
({ name, type }) => `${JSON.stringify(name)}: ${type}`
111112
)} }`
112113
})()}
113-
Returns: ${pgTypeToTsType(function_.return_type, types)}
114+
Returns: ${pgTypeToTsType(func.return_type, types)}
114115
}`
115-
)}
116+
)
117+
}
116118
}
117119
}`
118-
)}
120+
})}
119121
}`
120122

121123
output = prettier.format(output, {

0 commit comments

Comments
 (0)