Skip to content

Commit 8be8982

Browse files
bug-authorsoedirgo
andauthored
fix(typegen): dynamically set default schema alias in typescript typegen
* fix: use env var to set default schema alias in TypeScript types * refactor(constants): export DEFAULT_SCHEMA from constants * refactor(template): unify schema alias to use DefaultSchema - Remove separate aliases (PublicSchema, DefaultSchemaOrPublic) from TypeScript template - Add a single DefaultSchema alias - Update all helper types (Tables, TableInsert, etc) to use DefaultSchema * chore: update some namings --------- Co-authored-by: Bobbie Soedirgo <[email protected]>
1 parent 83c37d3 commit 8be8982

File tree

3 files changed

+216
-176
lines changed

3 files changed

+216
-176
lines changed

src/server/constants.ts

+3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ export const GENERATE_TYPES = process.env.PG_META_GENERATE_TYPES
4040
export const GENERATE_TYPES_INCLUDED_SCHEMAS = GENERATE_TYPES
4141
? (process.env.PG_META_GENERATE_TYPES_INCLUDED_SCHEMAS?.split(',') ?? [])
4242
: []
43+
export const GENERATE_TYPES_DEFAULT_SCHEMA =
44+
process.env.PG_META_GENERATE_TYPES_DEFAULT_SCHEMA || 'public'
4345
export const GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS =
4446
process.env.PG_META_GENERATE_TYPES_DETECT_ONE_TO_ONE_RELATIONSHIPS === 'true'
4547
export const GENERATE_TYPES_SWIFT_ACCESS_CONTROL = process.env
4648
.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL
4749
? (process.env.PG_META_GENERATE_TYPES_SWIFT_ACCESS_CONTROL as AccessControl)
4850
: 'internal'
51+
4952
export const DEFAULT_POOL_CONFIG: PoolConfig = {
5053
max: 1,
5154
connectionTimeoutMillis: PG_CONN_TIMEOUT_SECS * 1000,

src/server/templates/typescript.ts

+69-56
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
PostgresView,
99
} from '../../lib/index.js'
1010
import type { GeneratorMetadata } from '../../lib/generators.js'
11+
import { GENERATE_TYPES_DEFAULT_SCHEMA } from '../constants.js'
1112

1213
export const apply = async ({
1314
schemas,
@@ -390,98 +391,110 @@ export type Database = {
390391
})}
391392
}
392393
393-
type PublicSchema = Database[Extract<keyof Database, "public">]
394+
type DefaultSchema = Database[Extract<keyof Database, ${JSON.stringify(GENERATE_TYPES_DEFAULT_SCHEMA)}>]
394395
395396
export type Tables<
396-
PublicTableNameOrOptions extends
397-
| keyof (PublicSchema["Tables"] & PublicSchema["Views"])
397+
DefaultSchemaTableNameOrOptions extends
398+
| keyof (DefaultSchema["Tables"] & DefaultSchema["Views"])
398399
| { schema: keyof Database },
399-
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
400-
? keyof (Database[PublicTableNameOrOptions["schema"]]["Tables"] &
401-
Database[PublicTableNameOrOptions["schema"]]["Views"])
402-
: never = never
403-
> = PublicTableNameOrOptions extends { schema: keyof Database }
404-
? (Database[PublicTableNameOrOptions["schema"]]["Tables"] &
405-
Database[PublicTableNameOrOptions["schema"]]["Views"])[TableName] extends {
400+
TableName extends DefaultSchemaTableNameOrOptions extends {
401+
schema: keyof Database
402+
}
403+
? keyof (Database[DefaultSchemaTableNameOrOptions["schema"]]["Tables"] &
404+
Database[DefaultSchemaTableNameOrOptions["schema"]]["Views"])
405+
: never = never,
406+
> = DefaultSchemaTableNameOrOptions extends { schema: keyof Database }
407+
? (Database[DefaultSchemaTableNameOrOptions["schema"]]["Tables"] &
408+
Database[DefaultSchemaTableNameOrOptions["schema"]]["Views"])[TableName] extends {
406409
Row: infer R
407410
}
408411
? R
409412
: never
410-
: PublicTableNameOrOptions extends keyof (PublicSchema["Tables"] & PublicSchema["Views"])
411-
? (PublicSchema["Tables"] & PublicSchema["Views"])[PublicTableNameOrOptions] extends {
413+
: DefaultSchemaTableNameOrOptions extends keyof (DefaultSchema["Tables"] &
414+
DefaultSchema["Views"])
415+
? (DefaultSchema["Tables"] &
416+
DefaultSchema["Views"])[DefaultSchemaTableNameOrOptions] extends {
412417
Row: infer R
413418
}
414419
? R
415420
: never
416421
: never
417422
418423
export type TablesInsert<
419-
PublicTableNameOrOptions extends
420-
| keyof PublicSchema["Tables"]
424+
DefaultSchemaTableNameOrOptions extends
425+
| keyof DefaultSchema["Tables"]
421426
| { schema: keyof Database },
422-
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
423-
? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
424-
: never = never
425-
> = PublicTableNameOrOptions extends { schema: keyof Database }
426-
? Database[PublicTableNameOrOptions["schema"]]["Tables"][TableName] extends {
427+
TableName extends DefaultSchemaTableNameOrOptions extends {
428+
schema: keyof Database
429+
}
430+
? keyof Database[DefaultSchemaTableNameOrOptions["schema"]]["Tables"]
431+
: never = never,
432+
> = DefaultSchemaTableNameOrOptions extends { schema: keyof Database }
433+
? Database[DefaultSchemaTableNameOrOptions["schema"]]["Tables"][TableName] extends {
427434
Insert: infer I
428435
}
429436
? I
430437
: never
431-
: PublicTableNameOrOptions extends keyof PublicSchema["Tables"]
432-
? PublicSchema["Tables"][PublicTableNameOrOptions] extends {
433-
Insert: infer I
434-
}
435-
? I
438+
: DefaultSchemaTableNameOrOptions extends keyof DefaultSchema["Tables"]
439+
? DefaultSchema["Tables"][DefaultSchemaTableNameOrOptions] extends {
440+
Insert: infer I
441+
}
442+
? I
443+
: never
436444
: never
437-
: never
438445
439446
export type TablesUpdate<
440-
PublicTableNameOrOptions extends
441-
| keyof PublicSchema["Tables"]
447+
DefaultSchemaTableNameOrOptions extends
448+
| keyof DefaultSchema["Tables"]
442449
| { schema: keyof Database },
443-
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
444-
? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
445-
: never = never
446-
> = PublicTableNameOrOptions extends { schema: keyof Database }
447-
? Database[PublicTableNameOrOptions["schema"]]["Tables"][TableName] extends {
450+
TableName extends DefaultSchemaTableNameOrOptions extends {
451+
schema: keyof Database
452+
}
453+
? keyof Database[DefaultSchemaTableNameOrOptions["schema"]]["Tables"]
454+
: never = never,
455+
> = DefaultSchemaTableNameOrOptions extends { schema: keyof Database }
456+
? Database[DefaultSchemaTableNameOrOptions["schema"]]["Tables"][TableName] extends {
448457
Update: infer U
449458
}
450459
? U
451460
: never
452-
: PublicTableNameOrOptions extends keyof PublicSchema["Tables"]
453-
? PublicSchema["Tables"][PublicTableNameOrOptions] extends {
454-
Update: infer U
455-
}
456-
? U
461+
: DefaultSchemaTableNameOrOptions extends keyof DefaultSchema["Tables"]
462+
? DefaultSchema["Tables"][DefaultSchemaTableNameOrOptions] extends {
463+
Update: infer U
464+
}
465+
? U
466+
: never
457467
: never
458-
: never
459468
460469
export type Enums<
461-
PublicEnumNameOrOptions extends
462-
| keyof PublicSchema["Enums"]
470+
DefaultSchemaEnumNameOrOptions extends
471+
| keyof DefaultSchema["Enums"]
463472
| { schema: keyof Database },
464-
EnumName extends PublicEnumNameOrOptions extends { schema: keyof Database }
465-
? keyof Database[PublicEnumNameOrOptions["schema"]]["Enums"]
466-
: never = never
467-
> = PublicEnumNameOrOptions extends { schema: keyof Database }
468-
? Database[PublicEnumNameOrOptions["schema"]]["Enums"][EnumName]
469-
: PublicEnumNameOrOptions extends keyof PublicSchema["Enums"]
470-
? PublicSchema["Enums"][PublicEnumNameOrOptions]
471-
: never
473+
EnumName extends DefaultSchemaEnumNameOrOptions extends {
474+
schema: keyof Database
475+
}
476+
? keyof Database[DefaultSchemaEnumNameOrOptions["schema"]]["Enums"]
477+
: never = never,
478+
> = DefaultSchemaEnumNameOrOptions extends { schema: keyof Database }
479+
? Database[DefaultSchemaEnumNameOrOptions["schema"]]["Enums"][EnumName]
480+
: DefaultSchemaEnumNameOrOptions extends keyof DefaultSchema["Enums"]
481+
? DefaultSchema["Enums"][DefaultSchemaEnumNameOrOptions]
482+
: never
472483
473484
export type CompositeTypes<
474485
PublicCompositeTypeNameOrOptions extends
475-
| keyof PublicSchema['CompositeTypes']
486+
| keyof DefaultSchema["CompositeTypes"]
476487
| { schema: keyof Database },
477-
CompositeTypeName extends PublicCompositeTypeNameOrOptions extends { schema: keyof Database }
478-
? keyof Database[PublicCompositeTypeNameOrOptions['schema']]['CompositeTypes']
479-
: never = never
488+
CompositeTypeName extends PublicCompositeTypeNameOrOptions extends {
489+
schema: keyof Database
490+
}
491+
? keyof Database[PublicCompositeTypeNameOrOptions["schema"]]["CompositeTypes"]
492+
: never = never,
480493
> = PublicCompositeTypeNameOrOptions extends { schema: keyof Database }
481-
? Database[PublicCompositeTypeNameOrOptions['schema']]['CompositeTypes'][CompositeTypeName]
482-
: PublicCompositeTypeNameOrOptions extends keyof PublicSchema['CompositeTypes']
483-
? PublicSchema['CompositeTypes'][PublicCompositeTypeNameOrOptions]
484-
: never;
494+
? Database[PublicCompositeTypeNameOrOptions["schema"]]["CompositeTypes"][CompositeTypeName]
495+
: PublicCompositeTypeNameOrOptions extends keyof DefaultSchema["CompositeTypes"]
496+
? DefaultSchema["CompositeTypes"][PublicCompositeTypeNameOrOptions]
497+
: never
485498
`
486499

487500
output = await prettier.format(output, {

0 commit comments

Comments
 (0)