Skip to content

Commit e643201

Browse files
committed
wip
1 parent 4a3ace4 commit e643201

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

src/server/templates/typescript.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,23 @@ export type Database = {
276276
: schemaViews.map(
277277
(view) => `${JSON.stringify(view.name)}: {
278278
Row: {
279-
${columnsByTableId[view.id].map(
280-
(column) =>
281-
`${JSON.stringify(column.name)}: ${pgTypeToTsType(schema, column.format, {
282-
types,
283-
schemas,
284-
tables,
285-
views,
286-
})} ${column.is_nullable ? '| null' : ''}`
287-
)}
279+
${[
280+
...columnsByTableId[view.id].map(
281+
(column) =>
282+
`${JSON.stringify(column.name)}: ${pgTypeToTsType(schema, column.format, {
283+
types,
284+
schemas,
285+
tables,
286+
views,
287+
})} ${column.is_nullable ? '| null' : ''}`
288+
),
289+
...schemaFunctions
290+
.filter((fn) => fn.argument_types === view.name)
291+
.map(
292+
(fn) =>
293+
`${JSON.stringify(fn.name)}: ${getFunctionReturnType(schema, fn)} | null`
294+
),
295+
]}
288296
}
289297
${
290298
'is_updatable' in view && view.is_updatable
@@ -409,8 +417,11 @@ export type Database = {
409417
},
410418
{} as Record<string, PostgresFunction[]>
411419
)
420+
const sortedGroupdSchemaFunctions = Object.entries(
421+
schemaFunctionsGroupedByName
422+
).toSorted((a, b) => a[0].localeCompare(b[0]))
412423
413-
return Object.entries(schemaFunctionsGroupedByName).map(([fnName, fns]) => {
424+
return sortedGroupdSchemaFunctions.map(([fnName, fns]) => {
414425
// Group functions by their argument names signature to detect conflicts
415426
const fnsByArgNames = new Map<string, PostgresFunction[]>()
416427
fns.sort((fn1, fn2) => fn1.id - fn2.id)
@@ -553,10 +564,18 @@ export type Database = {
553564
return inArgs.length === 1 && inArgs[0].name === '' && fn.return_table_name
554565
})
555566
if (unnamedSetofFunctions.length > 0) {
556-
const unnamedEmbededFunctionsSignatures = unnamedSetofFunctions.map(
557-
(fn) =>
558-
`{ IsUnnamedEmbededTable: true, Args: Record<PropertyKey, never>, Returns: ${getFunctionTsReturnType(fn, getFunctionReturnType(schema, fn))} }`
559-
)
567+
const unnamedEmbededFunctionsSignatures = unnamedSetofFunctions.map((fn) => {
568+
const firstArgType = typesById[fn.args[0].type_id]
569+
const tsType = firstArgType
570+
? pgTypeToTsType(schema, firstArgType.name, {
571+
types,
572+
schemas,
573+
tables,
574+
views,
575+
})
576+
: 'Record<PropertyKey, never>'
577+
return `{ Args: ${tsType}, Returns: ${getFunctionTsReturnType(fn, getFunctionReturnType(schema, fn))} }`
578+
})
560579
allSignatures.push(...unnamedEmbededFunctionsSignatures)
561580
}
562581

0 commit comments

Comments
 (0)