Skip to content

Commit 607da9d

Browse files
committed
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
1 parent 55bd777 commit 607da9d

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/server/templates/typescript.ts

+17-24
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import type {
88
PostgresView,
99
} from '../../lib/index.js'
1010
import type { GeneratorMetadata } from '../../lib/generators.js'
11-
12-
const defaultSchema = process.env.PG_META_GENERATE_TYPES_DEFAULT_SCHEMA
11+
import { DEFAULT_SCHEMA } from '../constants.js'
1312

1413
export const apply = async ({
1514
schemas,
@@ -392,18 +391,12 @@ export type Database = {
392391
})}
393392
}
394393
395-
${
396-
defaultSchema
397-
? `export type DefaultSchema = Database[Extract<keyof Database, ${JSON.stringify(defaultSchema)}>]
398-
export type DefaultSchemaOrPublic = DefaultSchema`
399-
: `export type PublicSchema = Database[Extract<keyof Database, "public">]
400-
export type DefaultSchemaOrPublic = PublicSchema`
401-
}
394+
type DefaultSchema = Database[Extract<keyof Database, ${JSON.stringify(DEFAULT_SCHEMA) || '"public"'}>]
402395
403396
404397
export type Tables<
405398
PublicTableNameOrOptions extends
406-
| keyof (DefaultSchemaOrPublic["Tables"] & DefaultSchemaOrPublic["Views"])
399+
| keyof (DefaultSchema["Tables"] & DefaultSchema["Views"])
407400
| { schema: keyof Database },
408401
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
409402
? keyof (Database[PublicTableNameOrOptions["schema"]]["Tables"] &
@@ -416,8 +409,8 @@ export type Tables<
416409
}
417410
? R
418411
: never
419-
: PublicTableNameOrOptions extends keyof (DefaultSchemaOrPublic["Tables"] & DefaultSchemaOrPublic["Views"])
420-
? (DefaultSchemaOrPublic["Tables"] & DefaultSchemaOrPublic["Views"])[PublicTableNameOrOptions] extends {
412+
: PublicTableNameOrOptions extends keyof (DefaultSchema["Tables"] & DefaultSchema["Views"])
413+
? (DefaultSchema["Tables"] & DefaultSchema["Views"])[PublicTableNameOrOptions] extends {
421414
Row: infer R
422415
}
423416
? R
@@ -426,7 +419,7 @@ export type Tables<
426419
427420
export type TablesInsert<
428421
PublicTableNameOrOptions extends
429-
| keyof DefaultSchemaOrPublic["Tables"]
422+
| keyof DefaultSchema["Tables"]
430423
| { schema: keyof Database },
431424
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
432425
? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
@@ -437,8 +430,8 @@ export type TablesInsert<
437430
}
438431
? I
439432
: never
440-
: PublicTableNameOrOptions extends keyof DefaultSchemaOrPublic["Tables"]
441-
? DefaultSchemaOrPublic["Tables"][PublicTableNameOrOptions] extends {
433+
: PublicTableNameOrOptions extends keyof DefaultSchema["Tables"]
434+
? DefaultSchema["Tables"][PublicTableNameOrOptions] extends {
442435
Insert: infer I
443436
}
444437
? I
@@ -447,7 +440,7 @@ export type TablesInsert<
447440
448441
export type TablesUpdate<
449442
PublicTableNameOrOptions extends
450-
| keyof DefaultSchemaOrPublic["Tables"]
443+
| keyof DefaultSchema["Tables"]
451444
| { schema: keyof Database },
452445
TableName extends PublicTableNameOrOptions extends { schema: keyof Database }
453446
? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"]
@@ -458,8 +451,8 @@ export type TablesUpdate<
458451
}
459452
? U
460453
: never
461-
: PublicTableNameOrOptions extends keyof DefaultSchemaOrPublic["Tables"]
462-
? DefaultSchemaOrPublic["Tables"][PublicTableNameOrOptions] extends {
454+
: PublicTableNameOrOptions extends keyof DefaultSchema["Tables"]
455+
? DefaultSchema["Tables"][PublicTableNameOrOptions] extends {
463456
Update: infer U
464457
}
465458
? U
@@ -468,28 +461,28 @@ export type TablesUpdate<
468461
469462
export type Enums<
470463
PublicEnumNameOrOptions extends
471-
| keyof DefaultSchemaOrPublic["Enums"]
464+
| keyof DefaultSchema["Enums"]
472465
| { schema: keyof Database },
473466
EnumName extends PublicEnumNameOrOptions extends { schema: keyof Database }
474467
? keyof Database[PublicEnumNameOrOptions["schema"]]["Enums"]
475468
: never = never
476469
> = PublicEnumNameOrOptions extends { schema: keyof Database }
477470
? Database[PublicEnumNameOrOptions["schema"]]["Enums"][EnumName]
478-
: PublicEnumNameOrOptions extends keyof DefaultSchemaOrPublic["Enums"]
479-
? DefaultSchemaOrPublic["Enums"][PublicEnumNameOrOptions]
471+
: PublicEnumNameOrOptions extends keyof DefaultSchema["Enums"]
472+
? DefaultSchema["Enums"][PublicEnumNameOrOptions]
480473
: never
481474
482475
export type CompositeTypes<
483476
PublicCompositeTypeNameOrOptions extends
484-
| keyof DefaultSchemaOrPublic['CompositeTypes']
477+
| keyof DefaultSchema['CompositeTypes']
485478
| { schema: keyof Database },
486479
CompositeTypeName extends PublicCompositeTypeNameOrOptions extends { schema: keyof Database }
487480
? keyof Database[PublicCompositeTypeNameOrOptions['schema']]['CompositeTypes']
488481
: never = never
489482
> = PublicCompositeTypeNameOrOptions extends { schema: keyof Database }
490483
? Database[PublicCompositeTypeNameOrOptions['schema']]['CompositeTypes'][CompositeTypeName]
491-
: PublicCompositeTypeNameOrOptions extends keyof DefaultSchemaOrPublic['CompositeTypes']
492-
? DefaultSchemaOrPublic['CompositeTypes'][PublicCompositeTypeNameOrOptions]
484+
: PublicCompositeTypeNameOrOptions extends keyof DefaultSchema['CompositeTypes']
485+
? DefaultSchema['CompositeTypes'][PublicCompositeTypeNameOrOptions]
493486
: never;
494487
`
495488

0 commit comments

Comments
 (0)