From 3a86d000f0106b9c9787670d4f06c52c61291e56 Mon Sep 17 00:00:00 2001 From: Jay Meistrich Date: Sat, 2 Nov 2024 10:43:07 +0000 Subject: [PATCH] types: Improve supabase types to infer type from the column names in the select query --- src/sync-plugins/supabase.ts | 47 ++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/sync-plugins/supabase.ts b/src/sync-plugins/supabase.ts index 235da33b..377b93f0 100644 --- a/src/sync-plugins/supabase.ts +++ b/src/sync-plugins/supabase.ts @@ -87,13 +87,7 @@ interface SyncedSupabaseProps< supabase?: Client; collection: Collection; schema?: SchemaName; - select?: ( - query: PostgrestQueryBuilder< - SupabaseSchemaOf, - SupabaseTableOf[Collection], - Collection - >, - ) => PostgrestFilterBuilder, TRemote, TRemote[], Collection, []>; + select?: never; filter?: ( select: PostgrestFilterBuilder, TRemote, TRemote[], Collection, []>, params: SyncedGetParams, @@ -115,6 +109,23 @@ interface SyncedSupabaseProps< ) => PromiseLike> | Promise>>; } +interface SyncedSupabasePropsWithSelect< + Client extends SupabaseClient, + Collection extends SupabaseCollectionOf, + SchemaName extends SchemaNameOf = 'public', + TOption extends CrudAsOption = 'object', + TRemote extends SupabaseRowOf = SupabaseRowOf, + TLocal = TRemote, +> extends Omit, 'select'> { + select: ( + query: PostgrestQueryBuilder< + SupabaseSchemaOf, + SupabaseTableOf[Collection], + Collection + >, + ) => PostgrestFilterBuilder, TRemote, TRemote[], Collection, []>; +} + let channelNum = 1; const supabaseConfig: SyncedSupabaseConfiguration = {}; const isEnabled$ = observable(true); @@ -146,6 +157,16 @@ function wrapSupabaseFn(fn: (...args: any) => PromiseLike, source: CrudErro }; } +export function syncedSupabase< + Client extends SupabaseClient, + Collection extends SupabaseCollectionOf & string, + SchemaName extends SchemaNameOf = 'public', + AsOption extends CrudAsOption = 'object', + TRemote = SupabaseRowOf, + TLocal = TRemote, +>( + props: SyncedSupabasePropsWithSelect, +): SyncedCrudReturnType; export function syncedSupabase< Client extends SupabaseClient, Collection extends SupabaseCollectionOf & string, @@ -155,6 +176,18 @@ export function syncedSupabase< TLocal = TRemote, >( props: SyncedSupabaseProps, +): SyncedCrudReturnType; +export function syncedSupabase< + Client extends SupabaseClient, + Collection extends SupabaseCollectionOf & string, + SchemaName extends SchemaNameOf = 'public', + AsOption extends CrudAsOption = 'object', + TRemote extends SupabaseRowOf = SupabaseRowOf, + TLocal = TRemote, +>( + props: + | SyncedSupabaseProps + | SyncedSupabasePropsWithSelect, ): SyncedCrudReturnType { props = { ...supabaseConfig, ...props } as any; const {