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

Better type definitions #511

Open
trulysinclair opened this issue Aug 25, 2022 · 3 comments
Open

Better type definitions #511

trulysinclair opened this issue Aug 25, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@trulysinclair
Copy link

trulysinclair commented Aug 25, 2022

Feature request

Is your feature request related to a problem? Please describe.

The types generated by the CLI should have multiple interfaces/types instead of nested types to improve usability.

Currently we have to use createClient<Database>(...) and for explicitly typing return types we have to use Database['public']['Tables']['...']['Row']

Describe the solution you'd like

By giving us multiple defined types/interfaces we could just use TodoTableRow or TodoTableInsert for example.

type PartialRequired<T, K extends keyof T> = Partial<T> & Pick<T, K>;

export type Json =
  | string
  | number
  | boolean
  | null
  | { [key: string]: Json }
  | Json[];

export interface Database {
  public: {
    Tables: Tables;
    Functions: {};
  }
}

export interface ProfilesTable {
  id: string;
  created_at: string | null;
  updated_at: string | null;
  installation_id: number | null;
  name: string | null;
}

export interface Tables {
  profiles: {
    Row: ProfilesTable;
    Insert: PartialRequired<ProfilesTable, 'id'>;
    Update: Partial<ProfilesTable>;
  };
}
@trulysinclair trulysinclair added the enhancement New feature or request label Aug 25, 2022
@ZetiMente
Copy link

Agree heavily. Supabase should add as many Prisma features as possible. If I could manage my Supabase database with a schema file (pulls and pushes like Prisma) and have type definition tied into that. It would be incredible.

@trulysinclair
Copy link
Author

It's also greatly useful for NestJS. Creating a provider works fine using the generated types, but attempting to use them for typing the DTOs is impossible as you're not able to use Database['public']['Tables']... with implements nor should you have to.

@Injectable({ scope: Scope.REQUEST })
export class Supabase {
  private readonly logger = new Logger(Supabase.name);
  private clientInstance: SupabaseClient<Database>;
  
  ...
}

@davepar
Copy link
Contributor

davepar commented Jan 9, 2023

Can't you define whatever name you want with a type alias?
https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-aliases

type ToDoTableRow = Database['public']['Tables']['...']['Row'];

@kiwicopple kiwicopple transferred this issue from supabase/supabase Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants