Skip to content
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

fix(template): dynamically set default schema alias in typescript type generation #904

Merged
merged 4 commits into from
Mar 7, 2025

Conversation

bug-author
Copy link
Contributor

@bug-author bug-author commented Mar 3, 2025

What kind of change does this PR introduce?

This PR fixes the incorrect default alias generation for TypeScript type schemas when using a single non‑public schema with supabase gen types.

What is the current behavior?

Currently, the TypeScript template always hardcodes the default schema alias as:

export type PublicSchema = Database[Extract<keyof Database, "public">]

This means that when a user generates types using a non‑public schema (for example, --schema robo), the generated helper types (for example. Tables<"test_table">) incorrectly reference the public schema, even if no tables exist there.

Relevant issues:

What is the new behavior?

This PR updates the TypeScript template in pg‑meta so that when exactly one non‑public schema is provided, the template automatically uses the environment variable PG_META_GENERATE_TYPES_DEFAULT_SCHEMA to generate a default alias. For example, if the schema provided is "robo", the template will now output:

export type DefaultSchema = Database[Extract<keyof Database, "robo">]
export type DefaultSchemaOrPublic = DefaultSchema

The helper types (Tables, TablesInsert, etc.) reference DefaultSchemaOrPublic instead of the hardcoded "public". In multi‑schema scenarios, the environment variable is not set, and the default behavior (using "public") remains, the users can explicitly specify the schema for non-public tables.

This change ensures:

  • In single‑schema mode with a non‑public schema, shorthand type references (like Tables<"test_table">) correctly resolve to Database["robo"]["Tables"]["test_table"].
  • In multi‑schema mode, users must explicitly specify which schema to use (for example, Tables<{ schema: "robo" }, "test_table">).

Additional context

Usage example

CREATE SCHEMA robo;
CREATE TABLE robo.test_table (
  id SERIAL PRIMARY KEY,
  name TEXT
);


CREATE TABLE public.example_table (
  id SERIAL PRIMARY KEY,
  description TEXT
);
// Scenario 1: No schema flag (defaults to "public")
import * as PublicTypes from "./dbTypes-public";

type PublicExampleRow = PublicTypes.Tables<"example_table">;

// Scenario 2: Single non‑public schema (e.g. "robo")
import * as RoboTypes from "./dbTypesRobo";

type RoboExampleRow = RoboTypes.Tables<"test_table">;

// Scenario 3: Multiple schemas specified (e.g. "public" and "robo")
import * as MultiTypes from "./dbTypes-multi";

type MultiPublicRow = MultiTypes.Tables<"example_table">;
type MultiRoboRow = MultiTypes.Tables<{ schema: "robo" }, "test_table">;

// examples
const publicRow: PublicExampleRow = {
  id: 1,
  description: "A public record",
};


const roboRow: RoboExampleRow = {
  id: 10,
  name: "Robo record",
};

const multiPublicRow: MultiPublicRow = {
  id: 2,
  description: "Multi public record",
};

const multiRoboRow: MultiRoboRow = {
  id: 20,
  name: null,
};

Corresponding cli PR

@soedirgo
Copy link
Member

soedirgo commented Mar 5, 2025

Thanks @bug-author! Left some comments

@bug-author
Copy link
Contributor Author

@soedirgo Thank you for the review. I have made the suggested changes:

  • Moved the default schema constant to src/server/constants.ts
  • Refactored schema aliases to use DefaultSchema only

Thanks for helping me refine the implementation.

- Remove separate aliases (PublicSchema, DefaultSchemaOrPublic) from TypeScript template
- Add a single DefaultSchema alias
- Update all helper types (Tables, TableInsert, etc) to use DefaultSchema
@soedirgo soedirgo force-pushed the fix/ts-template-default-schema branch from 607da9d to 7cf3c78 Compare March 7, 2025 07:13
@coveralls
Copy link

Pull Request Test Coverage Report for Build 13716318077

Details

  • 4 of 4 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.01%) to 75.334%

Totals Coverage Status
Change from base Build 13671731927: 0.01%
Covered Lines: 4912
Relevant Lines: 6434

💛 - Coveralls

Copy link
Member

@soedirgo soedirgo left a comment

Choose a reason for hiding this comment

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

Thanks again @bug-author!

@soedirgo soedirgo merged commit 8be8982 into supabase:master Mar 7, 2025
5 checks passed
@bug-author
Copy link
Contributor Author

Hi @soedirgo

I appreciate you taking the time to refine the code, I’ll make sure to pay even closer attention to code quality in future contributions. 🫡

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants