Skip to content

Prioritize current schema for pg type generation #939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 90 additions & 26 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export type Database = {
${[
...columnsByTableId[table.id].map(
(column) =>
`${JSON.stringify(column.name)}: ${pgTypeToTsType(column.format, {
`${JSON.stringify(column.name)}: ${pgTypeToTsType(schema, column.format, {
types,
schemas,
tables,
Expand All @@ -97,7 +97,12 @@ export type Database = {
const type = types.find(({ id }) => id === fn.return_type_id)
let tsType = 'unknown'
if (type) {
tsType = pgTypeToTsType(type.name, { types, schemas, tables, views })
tsType = pgTypeToTsType(schema, type.name, {
types,
schemas,
tables,
views,
})
}
return `${JSON.stringify(fn.name)}: ${tsType} | null`
}),
Expand All @@ -121,7 +126,12 @@ export type Database = {
output += ':'
}

output += pgTypeToTsType(column.format, { types, schemas, tables, views })
output += pgTypeToTsType(schema, column.format, {
types,
schemas,
tables,
views,
})

if (column.is_nullable) {
output += '| null'
Expand All @@ -138,7 +148,12 @@ export type Database = {
return `${output}?: never`
}

output += `?: ${pgTypeToTsType(column.format, { types, schemas, tables, views })}`
output += `?: ${pgTypeToTsType(schema, column.format, {
types,
schemas,
tables,
views,
})}`

if (column.is_nullable) {
output += '| null'
Expand Down Expand Up @@ -189,7 +204,7 @@ export type Database = {
Row: {
${columnsByTableId[view.id].map(
(column) =>
`${JSON.stringify(column.name)}: ${pgTypeToTsType(column.format, {
`${JSON.stringify(column.name)}: ${pgTypeToTsType(schema, column.format, {
types,
schemas,
tables,
Expand All @@ -207,7 +222,12 @@ export type Database = {
return `${output}?: never`
}

output += `?: ${pgTypeToTsType(column.format, { types, schemas, tables, views })} | null`
output += `?: ${pgTypeToTsType(schema, column.format, {
types,
schemas,
tables,
views,
})} | null`

return output
})}
Expand All @@ -220,7 +240,12 @@ export type Database = {
return `${output}?: never`
}

output += `?: ${pgTypeToTsType(column.format, { types, schemas, tables, views })} | null`
output += `?: ${pgTypeToTsType(schema, column.format, {
types,
schemas,
tables,
views,
})} | null`

return output
})}
Expand Down Expand Up @@ -290,7 +315,12 @@ export type Database = {
const type = types.find(({ id }) => id === type_id)
let tsType = 'unknown'
if (type) {
tsType = pgTypeToTsType(type.name, { types, schemas, tables, views })
tsType = pgTypeToTsType(schema, type.name, {
types,
schemas,
tables,
views,
})
}
return { name, type: tsType, has_default }
})
Expand All @@ -307,7 +337,12 @@ export type Database = {
const type = types.find(({ id }) => id === type_id)
let tsType = 'unknown'
if (type) {
tsType = pgTypeToTsType(type.name, { types, schemas, tables, views })
tsType = pgTypeToTsType(schema, type.name, {
types,
schemas,
tables,
views,
})
}
return { name, type: tsType }
})
Expand All @@ -327,20 +362,29 @@ export type Database = {
return `{
${columnsByTableId[relation.id].map(
(column) =>
`${JSON.stringify(column.name)}: ${pgTypeToTsType(column.format, {
types,
schemas,
tables,
views,
})} ${column.is_nullable ? '| null' : ''}`
`${JSON.stringify(column.name)}: ${pgTypeToTsType(
schema,
column.format,
{
types,
schemas,
tables,
views,
}
)} ${column.is_nullable ? '| null' : ''}`
)}
}`
}

// Case 3: returns base/array/composite/enum type.
const type = types.find(({ id }) => id === fns[0].return_type_id)
if (type) {
return pgTypeToTsType(type.name, { types, schemas, tables, views })
return pgTypeToTsType(schema, type.name, {
types,
schemas,
tables,
views,
})
}

return 'unknown'
Expand Down Expand Up @@ -372,7 +416,12 @@ export type Database = {
const type = types.find(({ id }) => id === type_id)
let tsType = 'unknown'
if (type) {
tsType = `${pgTypeToTsType(type.name, { types, schemas, tables, views })} | null`
tsType = `${pgTypeToTsType(schema, type.name, {
types,
schemas,
tables,
views,
})} | null`
}
return `${JSON.stringify(name)}: ${tsType}`
})}
Expand Down Expand Up @@ -519,6 +568,7 @@ export const Constants = {

// TODO: Make this more robust. Currently doesn't handle range types - returns them as unknown.
const pgTypeToTsType = (
schema: PostgresSchema,
pgType: string,
{
types,
Expand Down Expand Up @@ -560,10 +610,16 @@ const pgTypeToTsType = (
} else if (pgType === 'record') {
return 'Record<string, unknown>'
} else if (pgType.startsWith('_')) {
return `(${pgTypeToTsType(pgType.substring(1), { types, schemas, tables, views })})[]`
return `(${pgTypeToTsType(schema, pgType.substring(1), {
types,
schemas,
tables,
views,
})})[]`
} else {
const enumType = types.find((type) => type.name === pgType && type.enums.length > 0)
if (enumType) {
const enumTypes = types.filter((type) => type.name === pgType && type.enums.length > 0)
if (enumTypes.length > 0) {
const enumType = enumTypes.find((type) => type.schema === schema.name) || enumTypes[0]
if (schemas.some(({ name }) => name === enumType.schema)) {
return `Database[${JSON.stringify(enumType.schema)}]['Enums'][${JSON.stringify(
enumType.name
Expand All @@ -572,8 +628,12 @@ const pgTypeToTsType = (
return enumType.enums.map((variant) => JSON.stringify(variant)).join('|')
}

const compositeType = types.find((type) => type.name === pgType && type.attributes.length > 0)
if (compositeType) {
const compositeTypes = types.filter(
(type) => type.name === pgType && type.attributes.length > 0
)
if (compositeTypes.length > 0) {
const compositeType =
compositeTypes.find((type) => type.schema === schema.name) || compositeTypes[0]
if (schemas.some(({ name }) => name === compositeType.schema)) {
return `Database[${JSON.stringify(
compositeType.schema
Expand All @@ -582,8 +642,10 @@ const pgTypeToTsType = (
return 'unknown'
}

const tableRowType = tables.find((table) => table.name === pgType)
if (tableRowType) {
const tableRowTypes = tables.filter((table) => table.name === pgType)
if (tableRowTypes.length > 0) {
const tableRowType =
tableRowTypes.find((type) => type.schema === schema.name) || tableRowTypes[0]
if (schemas.some(({ name }) => name === tableRowType.schema)) {
return `Database[${JSON.stringify(tableRowType.schema)}]['Tables'][${JSON.stringify(
tableRowType.name
Expand All @@ -592,8 +654,10 @@ const pgTypeToTsType = (
return 'unknown'
}

const viewRowType = views.find((view) => view.name === pgType)
if (viewRowType) {
const viewRowTypes = views.filter((view) => view.name === pgType)
if (viewRowTypes.length > 0) {
const viewRowType =
viewRowTypes.find((type) => type.schema === schema.name) || viewRowTypes[0]
if (schemas.some(({ name }) => name === viewRowType.schema)) {
return `Database[${JSON.stringify(viewRowType.schema)}]['Views'][${JSON.stringify(
viewRowType.name
Expand Down