Last active
May 18, 2025 01:07
-
-
Save maparent/6af5c87c6297893d0d353d5ae47e0018 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inspired by https://github.com/supabase/postgres-meta/issues/578#issuecomment-1955140767 | |
import { | |
type GenericSchema, | |
type GenericFunction, | |
} from "@supabase/supabase-js/src/lib/types"; | |
import { type Database } from "types.gen"; | |
type DatabaseShape = { | |
[schema_name: string]: GenericSchema; | |
}; | |
// Helper functions | |
type ChangeFields<T, R> = Omit<T, keyof R> & R; // Credits: https://stackoverflow.com/a/67884937 | |
type ExtractFnByName< | |
DBType extends DatabaseShape, | |
FnName extends string, | |
> = DBType["public"]["Functions"][FnName]; | |
type ChangePgFunctionType< | |
DBType extends DatabaseShape, | |
FnName extends string, | |
FnArg extends string, | |
> = ChangeFields< | |
DBType, | |
{ | |
public: ChangeFields< | |
DBType["public"], | |
{ | |
Functions: ChangeFields< | |
DBType["public"]["Functions"], | |
Record< | |
FnName, | |
ReplaceVectorArg<FnArg, ExtractFnByName<DBType, FnName>> | |
> | |
>; | |
} | |
>; | |
} | |
>; | |
type ReplaceVectorArg<ArgName extends string, T extends GenericFunction> = Omit< | |
T, | |
"Args" | |
> & { | |
Args: Omit<T["Args"], ArgName> & Record<ArgName, number[]>; | |
}; | |
// Fix database types | |
type TypeSafeDatabase0 = ChangePgFunctionType< | |
Database, | |
"function1", | |
"argname1" | |
>; | |
export type TypeSafeDatabase = ChangePgFunctionType< | |
TypeSafeDatabase0, | |
"function2", | |
"argname2" | |
>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment