postgres-meta icon indicating copy to clipboard operation
postgres-meta copied to clipboard

Better type definitions

Open trulysinclair opened this issue 2 years ago • 3 comments

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 avatar Aug 25 '22 02:08 trulysinclair

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.

ZetiMente avatar Aug 30 '22 15:08 ZetiMente

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>;
  
  ...
}

trulysinclair avatar Aug 31 '22 04:08 trulysinclair

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'];

davepar avatar Jan 09 '23 02:01 davepar