Skip to content

feat(typegen): add postgrest_version parameter to typegen #948

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 2 commits 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
1 change: 1 addition & 0 deletions src/server/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const GENERATE_TYPES_DEFAULT_SCHEMA =
process.env.PG_META_GENERATE_TYPES_DEFAULT_SCHEMA || 'public'
export const GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS =
process.env.PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS === 'true'
export const POSTGREST_VERSION = process.env.PG_META_POSTGREST_VERSION
export const GENERATE_TYPES_SWIFT_ACCESS_CONTROL = process.env
.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL
? (process.env.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL as AccessControl)
Expand Down
3 changes: 3 additions & 0 deletions src/server/routes/generators/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default async (fastify: FastifyInstance) => {
excluded_schemas?: string
included_schemas?: string
detect_one_to_one_relationships?: string
postgrest_version?: string
}
}>('/', async (request, reply) => {
const config = createConnectionConfig(request)
Expand All @@ -19,6 +20,7 @@ export default async (fastify: FastifyInstance) => {
const includedSchemas =
request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? []
const detectOneToOneRelationships = request.query.detect_one_to_one_relationships === 'true'
const postgrestVersion = request.query.postgrest_version

const pgMeta: PostgresMeta = new PostgresMeta(config)
const { data: generatorMeta, error: generatorMetaError } = await getGeneratorMetadata(pgMeta, {
Expand All @@ -34,6 +36,7 @@ export default async (fastify: FastifyInstance) => {
return applyTypescriptTemplate({
...generatorMeta,
detectOneToOneRelationships,
postgrestVersion,
})
})
}
2 changes: 2 additions & 0 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
PG_CONNECTION,
PG_META_HOST,
PG_META_PORT,
POSTGREST_VERSION,
} from './constants.js'
import { apply as applyTypescriptTemplate } from './templates/typescript.js'
import { apply as applyGoTemplate } from './templates/go.js'
Expand Down Expand Up @@ -129,6 +130,7 @@ async function getTypeOutput(): Promise<string | null> {
),
types: types!,
detectOneToOneRelationships: GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS,
postgresVersion: POSTGREST_VERSION,
}

switch (GENERATE_TYPES?.toLowerCase()) {
Expand Down
26 changes: 26 additions & 0 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ export const apply = async ({
functions,
types,
detectOneToOneRelationships,
postgrestVersion,
}: GeneratorMetadata & {
detectOneToOneRelationships: boolean
postgrestVersion?: string
}): Promise<string> => {
const columnsByTableId = Object.fromEntries<PostgresColumn[]>(
[...tables, ...foreignTables, ...views, ...materializedViews].map((t) => [t.id, []])
Expand All @@ -32,6 +34,29 @@ export const apply = async ({
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
.forEach((c) => columnsByTableId[c.table_id].push(c))

const internal_supabase_schema = postgrestVersion
? `// Allows to automatically instanciate createClient with right options
// instead of createClient<Database, { postgrestVersion: 'XX' }>(URL, KEY)
__internal_supabase: {
postgrestVersion: '${postgrestVersion}'
Tables: {
[_ in never]: never
}
Views: {
[_ in never]: never
}
Functions: {
[_ in never]: never
}
Enums: {
[_ in never]: never
}
CompositeTypes: {
[_ in never]: never
}
}`
: ''

let output = `
export type Json = string | number | boolean | null | { [key: string]: Json | undefined } | Json[]

Expand Down Expand Up @@ -431,6 +456,7 @@ export type Database = {
}
}`
})}
${internal_supabase_schema}
}

type DefaultSchema = Database[Extract<keyof Database, ${JSON.stringify(GENERATE_TYPES_DEFAULT_SCHEMA)}>]
Expand Down
Loading