Skip to content

feat: add typescript typegen endpoint #269

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

Merged
merged 2 commits into from
Jun 9, 2022
Merged
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
55 changes: 55 additions & 0 deletions src/server/routes/generators/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { FastifyInstance } from 'fastify'
import { PostgresMeta } from '../../../lib'
import { DEFAULT_POOL_CONFIG } from '../../constants'
import { extractRequestForLogging } from '../../utils'
import { apply as applyTypescriptTemplate } from '../../templates/typescript'

export default async (fastify: FastifyInstance) => {
fastify.get<{
Headers: { pg: string }
Querystring: {
excluded_schemas?: string
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is for auth, storage, graphql, realtime etc. - we should only generate for user's schemas

}
}>('/', async (request, reply) => {
const connectionString = request.headers.pg
const excludedSchemas =
request.query.excluded_schemas?.split(',').map((schema) => schema.trim()) ?? []

const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString })
const { data: schemas, error: schemasError } = await pgMeta.schemas.list()
const { data: tables, error: tablesError } = await pgMeta.tables.list()
const { data: functions, error: functionsError } = await pgMeta.functions.list()
const { data: types, error: typesError } = await pgMeta.types.list({
includeSystemSchemas: true,
})
await pgMeta.end()

if (schemasError) {
request.log.error({ error: schemasError, request: extractRequestForLogging(request) })
reply.code(500)
return { error: schemasError.message }
}
if (tablesError) {
request.log.error({ error: tablesError, request: extractRequestForLogging(request) })
reply.code(500)
return { error: tablesError.message }
}
if (functionsError) {
request.log.error({ error: functionsError, request: extractRequestForLogging(request) })
reply.code(500)
return { error: functionsError.message }
}
if (typesError) {
request.log.error({ error: typesError, request: extractRequestForLogging(request) })
reply.code(500)
return { error: typesError.message }
}

return applyTypescriptTemplate({
schemas: schemas.filter(({ name }) => !excludedSchemas.includes(name)),
tables,
functions,
types,
})
})
}
1 change: 1 addition & 0 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ export default async (fastify: FastifyInstance) => {
fastify.register(require('./triggers'), { prefix: '/triggers' })
fastify.register(require('./types'), { prefix: '/types' })
fastify.register(require('./views'), { prefix: '/views' })
fastify.register(require('./generators/typescript'), { prefix: '/generators/typescript' })
}
164 changes: 164 additions & 0 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import prettier from 'prettier'
import type { PostgresFunction, PostgresSchema, PostgresTable, PostgresType } from '../../lib'

export const apply = ({
schemas,
tables,
functions,
types,
}: {
schemas: PostgresSchema[]
tables: PostgresTable[]
functions: PostgresFunction[]
types: PostgresType[]
}): string => {
let output = `
export type Json = string | number | boolean | null | { [key: string]: Json } | Json[]

export interface Database {
${schemas.map(
(schema) =>
`${JSON.stringify(schema.name)}: {
Tables: {
${tables
.filter((table) => table.schema === schema.name)
.map(
(table) => `${JSON.stringify(table.name)}: {
Row: {
${table.columns.map(
(column) =>
`${JSON.stringify(column.name)}: ${pgTypeToTsType(column.format, types)} ${
column.is_nullable ? '| null' : ''
}`
)}
}
Insert: {
${table.columns.map((column) => {
let output = JSON.stringify(column.name)

if (column.identity_generation === 'ALWAYS') {
return `${output}?: never`
}

if (
column.is_nullable ||
column.is_identity ||
column.default_value !== null
) {
output += '?:'
} else {
output += ':'
}

output += pgTypeToTsType(column.format, types)

if (column.is_nullable) {
output += '| null'
}

return output
})}
}
Update: {
${table.columns.map((column) => {
let output = JSON.stringify(column.name)

if (column.identity_generation === 'ALWAYS') {
return `${output}?: never`
}

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

if (column.is_nullable) {
output += '| null'
}

return output
})}
}
}`
)}
}
Functions: {
${functions
.filter(
(function_) =>
function_.schema === schema.name && function_.return_type !== 'trigger'
)
.map(
(function_) => `${JSON.stringify(function_.name)}: {
Args: ${(() => {
if (function_.argument_types === '') {
return 'Record<PropertyKey, never>'
}

const splitArgs = function_.argument_types.split(',').map((arg) => arg.trim())
if (splitArgs.some((arg) => arg.includes('"') || !arg.includes(' '))) {
return 'Record<string, unknown>'
}

const argsNameAndType = splitArgs.map((arg) => {
const [name, ...rest] = arg.split(' ')
const type = types.find((_type) => _type.format === rest.join(' '))
if (!type) {
return { name, type: 'unknown' }
}
return { name, type: pgTypeToTsType(type.name, types) }
})

return `{ ${argsNameAndType.map(
({ name, type }) => `${JSON.stringify(name)}: ${type}`
)} }`
})()}
Returns: ${pgTypeToTsType(function_.return_type, types)}
}`
)}
}
}`
)}
}`

output = prettier.format(output, {
parser: 'typescript',
})
return output
}

// TODO: Make this more robust. Currently doesn't handle composite types - returns them as unknown.
const pgTypeToTsType = (pgType: string, types: PostgresType[]): 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',
'time',
'timetz',
'timestamp',
'timestamptz',
'uuid',
].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) + '[]'
} else {
const type = types.find((type) => type.name === pgType && type.enums.length > 0)
if (type) {
return type.enums.map((variant) => JSON.stringify(variant)).join('|')
}

return 'unknown'
}
}