Skip to content
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

Typescript: safer nullability in functions, domain support #573

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/lib/sql/types.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ select
t.typname as name,
n.nspname as schema,
format_type (t.oid, null) as format,
nullif(t.typbasetype, 0) as base_type_id,
not (t.typnotnull) as is_nullable,
coalesce(t_enums.enums, '[]') as enums,
coalesce(t_attributes.attributes, '[]') as attributes,
obj_description (t.oid, 'pg_type') as comment
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ export const postgresTypeSchema = Type.Object({
name: Type.String(),
schema: Type.String(),
format: Type.String(),
base_type_id: Type.Optional(Type.Integer()),
is_nullable: Type.Boolean(),
enums: Type.Array(Type.String()),
attributes: Type.Array(
Type.Object({
Expand Down
3 changes: 1 addition & 2 deletions src/server/routes/generators/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ export default async (fastify: FastifyInstance) => {
functions: functions.filter(
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
),
types: types.filter(({ name }) => name[0] !== '_'),
arrayTypes: types.filter(({ name }) => name[0] === '_'),
types,
})
})
}
3 changes: 1 addition & 2 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ if (EXPORT_DOCS) {
functions: functions.filter(
({ return_type }) => !['trigger', 'event_trigger'].includes(return_type)
),
types: types.filter(({ name }) => name[0] !== '_'),
arrayTypes: types.filter(({ name }) => name[0] === '_'),
types,
})
)
} else {
Expand Down
197 changes: 110 additions & 87 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ export const apply = ({
materializedViews,
functions,
types,
arrayTypes,
}: {
schemas: PostgresSchema[]
tables: (PostgresTable & { columns: unknown[] })[]
views: (PostgresView & { columns: unknown[] })[]
materializedViews: (PostgresMaterializedView & { columns: unknown[] })[]
functions: PostgresFunction[]
types: PostgresType[]
arrayTypes: PostgresType[]
}): string => {
let output = `
export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]
Expand Down Expand Up @@ -63,6 +61,15 @@ export interface Database {
const schemaEnums = types
.filter((type) => type.schema === schema.name && type.enums.length > 0)
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
const schemaDomainTypes = types
.flatMap((type) => {
const baseType =
type.schema === schema.name &&
type.base_type_id &&
types.find(({ id }) => id === type.base_type_id)
return baseType ? [{ type, baseType }] : []
})
.sort(({ type: { name: a } }, { type: { name: b } }) => a.localeCompare(b))
const schemaCompositeTypes = types
.filter((type) => type.schema === schema.name && type.attributes.length > 0)
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
Expand All @@ -82,8 +89,9 @@ export interface Database {
`${JSON.stringify(column.name)}: ${pgTypeToTsType(
column.format,
types,
schemas
)} ${column.is_nullable ? '| null' : ''}`
schemas,
{ nullable: column.is_nullable }
)}`
),
...schemaFunctions
.filter((fn) => fn.argument_types === table.name)
Expand All @@ -93,7 +101,7 @@ export interface Database {
fn.return_type,
types,
schemas
)} | null`
)}`
),
]}
}
Expand All @@ -117,11 +125,9 @@ export interface Database {
output += ':'
}

output += pgTypeToTsType(column.format, types, schemas)

if (column.is_nullable) {
output += '| null'
}
output += pgTypeToTsType(column.format, types, schemas, {
nullable: column.is_nullable,
})

return output
})}
Expand All @@ -136,11 +142,9 @@ export interface Database {
return `${output}?: never`
}

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

if (column.is_nullable) {
output += '| null'
}
output += `?: ${pgTypeToTsType(column.format, types, schemas, {
nullable: column.is_nullable,
})}`

return output
})}
Expand All @@ -163,8 +167,9 @@ export interface Database {
`${JSON.stringify(column.name)}: ${pgTypeToTsType(
column.format,
types,
schemas
)} ${column.is_nullable ? '| null' : ''}`
schemas,
{ nullable: column.is_nullable }
)}`
)}
}
${
Expand All @@ -179,7 +184,9 @@ export interface Database {
return `${output}?: never`
}

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

return output
})}
Expand All @@ -198,7 +205,9 @@ export interface Database {
return `${output}?: never`
}

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

return output
})}
Expand Down Expand Up @@ -239,17 +248,7 @@ export interface Database {
}

const argsNameAndType = inArgs.map(({ name, type_id, has_default }) => {
let type = arrayTypes.find(({ id }) => id === type_id)
if (type) {
// If it's an array type, the name looks like `_int8`.
const elementTypeName = type.name.substring(1)
return {
name,
type: `(${pgTypeToTsType(elementTypeName, types, schemas)})[]`,
has_default,
}
}
type = types.find(({ id }) => id === type_id)
const type = types.find(({ id }) => id === type_id)
if (type) {
return {
name,
Expand All @@ -272,19 +271,13 @@ export interface Database {
const tableArgs = args.filter(({ mode }) => mode === 'table')
if (tableArgs.length > 0) {
const argsNameAndType = tableArgs.map(({ name, type_id }) => {
let type = arrayTypes.find(({ id }) => id === type_id)
const type = types.find(({ id }) => id === type_id)
if (type) {
// If it's an array type, the name looks like `_int8`.
const elementTypeName = type.name.substring(1)
return {
name,
type: `(${pgTypeToTsType(elementTypeName, types, schemas)})[]`,
type: pgTypeToTsType(type.name, types, schemas),
}
}
type = types.find(({ id }) => id === type_id)
if (type) {
return { name, type: pgTypeToTsType(type.name, types, schemas) }
}
return { name, type: 'unknown' }
})

Expand All @@ -308,8 +301,9 @@ export interface Database {
`${JSON.stringify(column.name)}: ${pgTypeToTsType(
column.format,
types,
schemas
)} ${column.is_nullable ? '| null' : ''}`
schemas,
{ nullable: column.is_nullable }
)}`
)}
}`
}
Expand Down Expand Up @@ -340,6 +334,21 @@ export interface Database {
)
}
}
DomainTypes: {
${
schemaDomainTypes.length === 0
? '[_ in never]: never'
: schemaDomainTypes.map(
({ type: domain_, baseType }) =>
`${JSON.stringify(domain_.name)}: ${pgTypeToTsType(
baseType.name,
types,
schemas,
{ nullable: domain_.is_nullable }
)}`
)
}
}
CompositeTypes: {
${
schemaCompositeTypes.length === 0
Expand Down Expand Up @@ -377,58 +386,72 @@ export interface Database {
const pgTypeToTsType = (
pgType: string,
types: PostgresType[],
schemas: PostgresSchema[]
schemas: PostgresSchema[],
opts: { nullable?: boolean } = {}
): string => {
if (pgType === 'bool') {
return 'boolean'
} else if (['int2', 'int4', 'int8', 'float4', 'float8', 'numeric'].includes(pgType)) {
return 'number'
} else if (
[
'bytea',
'bpchar',
'varchar',
'date',
'text',
'citext',
'time',
'timetz',
'timestamp',
'timestamptz',
'uuid',
'vector',
].includes(pgType)
) {
return 'string'
} else if (['json', 'jsonb'].includes(pgType)) {
return 'Json'
} else if (pgType === 'void') {
return 'undefined'
} else if (pgType === 'record') {
return 'Record<string, unknown>'
} else if (pgType.startsWith('_')) {
return `(${pgTypeToTsType(pgType.substring(1), types, schemas)})[]`
} else {
const enumType = types.find((type) => type.name === pgType && type.enums.length > 0)
if (enumType) {
if (schemas.some(({ name }) => name === enumType.schema)) {
return `Database[${JSON.stringify(enumType.schema)}]['Enums'][${JSON.stringify(
enumType.name
)}]`
const type = types.find((type) => type.name === pgType)
const strictTsType = pgTypeToStrictTsType()
return strictTsType
? `${strictTsType}${opts.nullable ?? type?.is_nullable ? ' | null' : ''}`
: 'unknown'

function pgTypeToStrictTsType() {
if (pgType === 'bool') {
return 'boolean'
} else if (['int2', 'int4', 'int8', 'float4', 'float8', 'numeric'].includes(pgType)) {
return 'number'
} else if (
[
'bytea',
'bpchar',
'varchar',
'date',
'text',
'citext',
'time',
'timetz',
'timestamp',
'timestamptz',
'uuid',
'vector',
].includes(pgType)
) {
return 'string'
} else if (['json', 'jsonb'].includes(pgType)) {
return 'Json'
} else if (pgType === 'void') {
return 'undefined'
} else if (pgType === 'record') {
return 'Record<string, unknown>'
} else if (pgType.startsWith('_')) {
return `(${pgTypeToTsType(pgType.substring(1), types, schemas)})[]`
} else if (type != null) {
if (type.base_type_id != null) {
if (schemas.some(({ name }) => name === type.schema)) {
return `Database[${JSON.stringify(type.schema)}]['DomainTypes'][${JSON.stringify(
type.name
)}]`
}
return undefined
}

if (type.enums.length > 0) {
if (schemas.some(({ name }) => name === type.schema)) {
return `Database[${JSON.stringify(type.schema)}]['Enums'][${JSON.stringify(type.name)}]`
}
return type.enums.map((variant) => JSON.stringify(variant)).join('|')
}
return enumType.enums.map((variant) => JSON.stringify(variant)).join('|')
}

const compositeType = types.find((type) => type.name === pgType && type.attributes.length > 0)
if (compositeType) {
if (schemas.some(({ name }) => name === compositeType.schema)) {
return `Database[${JSON.stringify(
compositeType.schema
)}]['CompositeTypes'][${JSON.stringify(compositeType.name)}]`
if (type.attributes.length > 0) {
if (schemas.some(({ name }) => name === type.schema)) {
return `Database[${JSON.stringify(type.schema)}]['CompositeTypes'][${JSON.stringify(
type.name
)}]`
}
return undefined
}
return 'unknown'
}

return 'unknown'
return undefined
}
}
Loading