Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Here's the solution: An easy way to adapt the derived types and having fully typ
1. You generate the types at `supabase.ts`
1. You copy the file `supabase.extended.ts` to the same directory
1. You adapt `TableExtensions` to your likings
1. You adapt `FunctionExtensions` to your likings
1. You `import { Database } from './supabase.extended`
1. You instantiate a client with `createClient<Database>` from the previous import (of the extended file, not the `supabase.ts`)

Expand Down
34 changes: 31 additions & 3 deletions supabase.extended.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Database as PostgresSchema } from './supabase';

type PostgresTables = PostgresSchema['public']['Tables'];
type PostgresFunctions = PostgresSchema['public']['Functions'];


// THIS IS THE ONLY THING YOU EDIT
// <START>
Expand All @@ -18,6 +20,18 @@ type TableExtensions = {
// <END>
// ☝️ this is the only thing you edit

type FunctionsExtensions = {
/**
my_existing_function: {
my_json_argument: {
id: string;
name: string;
test: number[];
};
};
*/
};

type TakeDefinitionFromSecond<T extends object, P extends object> = Omit<
T,
keyof P
Expand Down Expand Up @@ -49,15 +63,29 @@ type NewTables = {
};
};

type NewFunctions = {
[k in keyof PostgresFunctions]: {
Args: k extends keyof FunctionsExtensions
? TakeDefinitionFromSecond<PostgresFunctions[k]['Args'], FunctionsExtensions[k]>
: PostgresFunctions[k]['Args'];
Returns: PostgresFunctions[k]['Returns'];
};
};

export type Database = {
public: Omit<PostgresSchema['public'], 'Tables'> & {
Tables: NewTables;
};
public: Omit<PostgresSchema['public'], 'Tables' | 'Functions'> & {
Tables: NewTables;
Functions: NewFunctions;
};
};

export type TableName = keyof Database['public']['Tables'];
export type TableRow<T extends TableName> =
Database['public']['Tables'][T]['Row'];

export type FunctionName = keyof Database['public']['Functions'];
export type FunctionArgs<T extends FunctionName> =
Database['public']['Functions'][T]['Args'];

export type TableView<View extends keyof Database['public']['Views']> =
Database['public']['Views'][View]['Row'];