Skip to content

Commit

Permalink
types: Improve supabase types to infer type from the column names in …
Browse files Browse the repository at this point in the history
…the select query
  • Loading branch information
jmeistrich committed Nov 2, 2024
1 parent e99eadf commit 3a86d00
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions src/sync-plugins/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ interface SyncedSupabaseProps<
supabase?: Client;
collection: Collection;
schema?: SchemaName;
select?: (
query: PostgrestQueryBuilder<
SupabaseSchemaOf<Client>,
SupabaseTableOf<Client, SchemaName>[Collection],
Collection
>,
) => PostgrestFilterBuilder<SupabaseSchemaOf<Client>, TRemote, TRemote[], Collection, []>;
select?: never;
filter?: (
select: PostgrestFilterBuilder<SupabaseSchemaOf<Client>, TRemote, TRemote[], Collection, []>,
params: SyncedGetParams<TRemote>,
Expand All @@ -115,6 +109,23 @@ interface SyncedSupabaseProps<
) => PromiseLike<PostgrestSingleResponse<TRemote>> | Promise<FunctionsResponse<NoInfer<TRemote>>>;
}

interface SyncedSupabasePropsWithSelect<
Client extends SupabaseClient<any, any>,
Collection extends SupabaseCollectionOf<Client, SchemaName>,
SchemaName extends SchemaNameOf<Client> = 'public',
TOption extends CrudAsOption = 'object',
TRemote extends SupabaseRowOf<Client, Collection, SchemaName> = SupabaseRowOf<Client, Collection, SchemaName>,
TLocal = TRemote,
> extends Omit<SyncedSupabaseProps<Client, Collection, SchemaName, TOption, TRemote, TLocal>, 'select'> {
select: (
query: PostgrestQueryBuilder<
SupabaseSchemaOf<Client>,
SupabaseTableOf<Client, SchemaName>[Collection],
Collection
>,
) => PostgrestFilterBuilder<SupabaseSchemaOf<Client>, TRemote, TRemote[], Collection, []>;
}

let channelNum = 1;
const supabaseConfig: SyncedSupabaseConfiguration = {};
const isEnabled$ = observable(true);
Expand Down Expand Up @@ -146,6 +157,16 @@ function wrapSupabaseFn(fn: (...args: any) => PromiseLike<any>, source: CrudErro
};
}

export function syncedSupabase<
Client extends SupabaseClient<any, any>,
Collection extends SupabaseCollectionOf<Client, SchemaName> & string,
SchemaName extends SchemaNameOf<Client> = 'public',
AsOption extends CrudAsOption = 'object',
TRemote = SupabaseRowOf<Client, Collection, SchemaName>,
TLocal = TRemote,
>(
props: SyncedSupabasePropsWithSelect<Client, Collection, SchemaName, AsOption, TRemote, TLocal>,
): SyncedCrudReturnType<TLocal, AsOption>;
export function syncedSupabase<
Client extends SupabaseClient<any, any>,
Collection extends SupabaseCollectionOf<Client, SchemaName> & string,
Expand All @@ -155,6 +176,18 @@ export function syncedSupabase<
TLocal = TRemote,
>(
props: SyncedSupabaseProps<Client, Collection, SchemaName, AsOption, TRemote, TLocal>,
): SyncedCrudReturnType<TLocal, AsOption>;
export function syncedSupabase<
Client extends SupabaseClient<any, any>,
Collection extends SupabaseCollectionOf<Client, SchemaName> & string,
SchemaName extends SchemaNameOf<Client> = 'public',
AsOption extends CrudAsOption = 'object',
TRemote extends SupabaseRowOf<Client, Collection, SchemaName> = SupabaseRowOf<Client, Collection, SchemaName>,
TLocal = TRemote,
>(
props:
| SyncedSupabaseProps<Client, Collection, SchemaName, AsOption, TRemote, TLocal>
| SyncedSupabasePropsWithSelect<Client, Collection, SchemaName, AsOption, TRemote, TLocal>,
): SyncedCrudReturnType<TLocal, AsOption> {
props = { ...supabaseConfig, ...props } as any;
const {
Expand Down

0 comments on commit 3a86d00

Please sign in to comment.