diff --git a/README.md b/README.md index c8f9c52..979cfe0 100644 --- a/README.md +++ b/README.md @@ -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` from the previous import (of the extended file, not the `supabase.ts`) diff --git a/supabase.extended.ts b/supabase.extended.ts index 0c886f2..47bcd09 100644 --- a/supabase.extended.ts +++ b/supabase.extended.ts @@ -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 // @@ -18,6 +20,18 @@ type TableExtensions = { // // ☝️ this is the only thing you edit +type FunctionsExtensions = { + /** + my_existing_function: { + my_json_argument: { + id: string; + name: string; + test: number[]; + }; + }; + */ +}; + type TakeDefinitionFromSecond = Omit< T, keyof P @@ -49,15 +63,29 @@ type NewTables = { }; }; +type NewFunctions = { + [k in keyof PostgresFunctions]: { + Args: k extends keyof FunctionsExtensions + ? TakeDefinitionFromSecond + : PostgresFunctions[k]['Args']; + Returns: PostgresFunctions[k]['Returns']; + }; +}; + export type Database = { - public: Omit & { - Tables: NewTables; - }; + public: Omit & { + Tables: NewTables; + Functions: NewFunctions; + }; }; export type TableName = keyof Database['public']['Tables']; export type TableRow = Database['public']['Tables'][T]['Row']; +export type FunctionName = keyof Database['public']['Functions']; +export type FunctionArgs = +Database['public']['Functions'][T]['Args']; + export type TableView = Database['public']['Views'][View]['Row'];