From 2e5f26c622a802a5c89e3097872e660c59cc788a Mon Sep 17 00:00:00 2001 From: avallete Date: Mon, 19 May 2025 10:09:07 +0200 Subject: [PATCH 1/2] chore(wip): upgrade postgrest-js introduce services version options --- src/SupabaseClient.ts | 15 +++++++++++---- src/index.ts | 10 ++++++++-- test/client.test.ts | 9 +++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/SupabaseClient.ts b/src/SupabaseClient.ts index df7bb15f..0e8a8dfc 100644 --- a/src/SupabaseClient.ts +++ b/src/SupabaseClient.ts @@ -4,6 +4,7 @@ import { PostgrestClient, PostgrestFilterBuilder, PostgrestQueryBuilder, + ClientServerOptions as PostgrestClientServerOption, } from '@supabase/postgrest-js' import { RealtimeChannel, @@ -28,8 +29,12 @@ import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions * * An isomorphic Javascript client for interacting with Postgres. */ + +export type ServicesOptions = PostgrestClientServerOption & {} + export default class SupabaseClient< Database = any, + ClientOptions extends ServicesOptions = { postgrestVersion: 12 }, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, @@ -47,7 +52,7 @@ export default class SupabaseClient< protected authUrl: string protected storageUrl: string protected functionsUrl: string - protected rest: PostgrestClient + protected rest: PostgrestClient protected storageKey: string protected fetch?: Fetch protected changedAccessToken?: string @@ -154,16 +159,16 @@ export default class SupabaseClient< from< TableName extends string & keyof Schema['Tables'], Table extends Schema['Tables'][TableName] - >(relation: TableName): PostgrestQueryBuilder + >(relation: TableName): PostgrestQueryBuilder from( relation: ViewName - ): PostgrestQueryBuilder + ): PostgrestQueryBuilder /** * Perform a query on a table or a view. * * @param relation - The table or view name to query */ - from(relation: string): PostgrestQueryBuilder { + from(relation: string): PostgrestQueryBuilder { return this.rest.from(relation) } @@ -179,6 +184,7 @@ export default class SupabaseClient< schema: DynamicSchema ): PostgrestClient< Database, + ClientOptions, DynamicSchema, Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any > { @@ -218,6 +224,7 @@ export default class SupabaseClient< count?: 'exact' | 'planned' | 'estimated' } = {} ): PostgrestFilterBuilder< + ClientOptions, Schema, Fn['Returns'] extends any[] ? Fn['Returns'][number] extends Record diff --git a/src/index.ts b/src/index.ts index ff601926..c08d34cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import SupabaseClient from './SupabaseClient' import type { GenericSchema, SupabaseClientOptions } from './lib/types' +import type { ServicesOptions } from './SupabaseClient' export * from '@supabase/auth-js' export type { User as AuthUser, Session as AuthSession } from '@supabase/auth-js' @@ -26,6 +27,7 @@ export type { SupabaseClientOptions, QueryResult, QueryData, QueryError } from ' */ export const createClient = < Database = any, + ClientOptions extends ServicesOptions = { postgrestVersion: 12 }, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, @@ -36,6 +38,10 @@ export const createClient = < supabaseUrl: string, supabaseKey: string, options?: SupabaseClientOptions -): SupabaseClient => { - return new SupabaseClient(supabaseUrl, supabaseKey, options) +): SupabaseClient => { + return new SupabaseClient( + supabaseUrl, + supabaseKey, + options + ) } diff --git a/test/client.test.ts b/test/client.test.ts index 3bca2aaf..f13efefc 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -79,6 +79,15 @@ describe('Dynamic schema', () => { }) }) +describe('Postgrest 13 client', () => { + test('should be able to declare specific postgrestVersion ', async () => { + createClient('HTTP://localhost:3000', KEY) + createClient('HTTP://localhost:3000', KEY) + // @ts-expect-error should raise error if provinding invalid version + createClient('HTTP://localhost:3000', KEY) + }) +}) + // Socket should close when there are no open connections // https://github.com/supabase/supabase-js/issues/44 From 609dc51ff1d01d7d582fcf99e8a2cc654f889e72 Mon Sep 17 00:00:00 2001 From: avallete Date: Tue, 20 May 2025 09:30:25 +0200 Subject: [PATCH 2/2] chore: add comment for autocomplete --- test/client.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/client.test.ts b/test/client.test.ts index f13efefc..5d7e0614 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -81,6 +81,8 @@ describe('Dynamic schema', () => { describe('Postgrest 13 client', () => { test('should be able to declare specific postgrestVersion ', async () => { + // Note: The template argument properties (postgrestVersion) will not be autocompleted + // due to a Typescript bug tracked here: https://github.com/microsoft/TypeScript/issues/56299 createClient('HTTP://localhost:3000', KEY) createClient('HTTP://localhost:3000', KEY) // @ts-expect-error should raise error if provinding invalid version