Skip to content

Commit bf91a7e

Browse files
committed
feat(typegen): add postgrest_version parameter to typegen
1 parent 70c38c0 commit bf91a7e

File tree

3 files changed

+682
-0
lines changed

3 files changed

+682
-0
lines changed

src/server/routes/generators/typescript.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default async (fastify: FastifyInstance) => {
1111
excluded_schemas?: string
1212
included_schemas?: string
1313
detect_one_to_one_relationships?: string
14+
postgrest_version?: string
1415
}
1516
}>('/', async (request, reply) => {
1617
const config = createConnectionConfig(request)
@@ -19,6 +20,7 @@ export default async (fastify: FastifyInstance) => {
1920
const includedSchemas =
2021
request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? []
2122
const detectOneToOneRelationships = request.query.detect_one_to_one_relationships === 'true'
23+
const postgrestVersion = request.query.postgrest_version
2224

2325
const pgMeta: PostgresMeta = new PostgresMeta(config)
2426
const { data: generatorMeta, error: generatorMetaError } = await getGeneratorMetadata(pgMeta, {
@@ -34,6 +36,7 @@ export default async (fastify: FastifyInstance) => {
3436
return applyTypescriptTemplate({
3537
...generatorMeta,
3638
detectOneToOneRelationships,
39+
postgrestVersion,
3740
})
3841
})
3942
}

src/server/templates/typescript.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ export const apply = async ({
2121
functions,
2222
types,
2323
detectOneToOneRelationships,
24+
postgrestVersion,
2425
}: GeneratorMetadata & {
2526
detectOneToOneRelationships: boolean
27+
postgrestVersion?: string
2628
}): Promise<string> => {
2729
const columnsByTableId = Object.fromEntries<PostgresColumn[]>(
2830
[...tables, ...foreignTables, ...views, ...materializedViews].map((t) => [t.id, []])
@@ -32,6 +34,29 @@ export const apply = async ({
3234
.sort(({ name: a }, { name: b }) => a.localeCompare(b))
3335
.forEach((c) => columnsByTableId[c.table_id].push(c))
3436

37+
const internal_supabase_schema = postgrestVersion
38+
? `// Allows to automatically instanciate createClient with right options
39+
// instead of createClient<Database, { postgrestVersion: 'XX' }>(URL, KEY)
40+
__internal_supabase: {
41+
postgrestVersion: '${postgrestVersion}'
42+
Tables: {
43+
[_ in never]: never
44+
}
45+
Views: {
46+
[_ in never]: never
47+
}
48+
Functions: {
49+
[_ in never]: never
50+
}
51+
Enums: {
52+
[_ in never]: never
53+
}
54+
CompositeTypes: {
55+
[_ in never]: never
56+
}
57+
}`
58+
: ''
59+
3560
let output = `
3661
export type Json = string | number | boolean | null | { [key: string]: Json | undefined } | Json[]
3762
@@ -431,6 +456,7 @@ export type Database = {
431456
}
432457
}`
433458
})}
459+
${internal_supabase_schema}
434460
}
435461
436462
type DefaultSchema = Database[Extract<keyof Database, ${JSON.stringify(GENERATE_TYPES_DEFAULT_SCHEMA)}>]

0 commit comments

Comments
 (0)