Skip to content

chore(wip): upgrade postgrest-js introduce services version options #1416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/SupabaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PostgrestClient,
PostgrestFilterBuilder,
PostgrestQueryBuilder,
ClientServerOptions as PostgrestClientServerOption,
} from '@supabase/postgrest-js'
import {
RealtimeChannel,
Expand All @@ -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,
Expand All @@ -47,7 +52,7 @@ export default class SupabaseClient<
protected authUrl: string
protected storageUrl: string
protected functionsUrl: string
protected rest: PostgrestClient<Database, SchemaName, Schema>
protected rest: PostgrestClient<Database, ClientOptions, SchemaName, Schema>
protected storageKey: string
protected fetch?: Fetch
protected changedAccessToken?: string
Expand Down Expand Up @@ -154,16 +159,16 @@ export default class SupabaseClient<
from<
TableName extends string & keyof Schema['Tables'],
Table extends Schema['Tables'][TableName]
>(relation: TableName): PostgrestQueryBuilder<Schema, Table, TableName>
>(relation: TableName): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(
relation: ViewName
): PostgrestQueryBuilder<Schema, View, ViewName>
): PostgrestQueryBuilder<ClientOptions, Schema, View, ViewName>
/**
* Perform a query on a table or a view.
*
* @param relation - The table or view name to query
*/
from(relation: string): PostgrestQueryBuilder<Schema, any, any> {
from(relation: string): PostgrestQueryBuilder<ClientOptions, Schema, any, any> {
return this.rest.from(relation)
}

Expand All @@ -179,6 +184,7 @@ export default class SupabaseClient<
schema: DynamicSchema
): PostgrestClient<
Database,
ClientOptions,
DynamicSchema,
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any
> {
Expand Down Expand Up @@ -218,6 +224,7 @@ export default class SupabaseClient<
count?: 'exact' | 'planned' | 'estimated'
} = {}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Fn['Returns'] extends any[]
? Fn['Returns'][number] extends Record<string, unknown>
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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,
Expand All @@ -36,6 +38,10 @@ export const createClient = <
supabaseUrl: string,
supabaseKey: string,
options?: SupabaseClientOptions<SchemaName>
): SupabaseClient<Database, SchemaName, Schema> => {
return new SupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, options)
): SupabaseClient<Database, ClientOptions, SchemaName, Schema> => {
return new SupabaseClient<Database, ClientOptions, SchemaName, Schema>(
supabaseUrl,
supabaseKey,
options
)
}
11 changes: 11 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ 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<Database, { postgrestVersion: 13 }>('HTTP://localhost:3000', KEY)
createClient<Database, { postgrestVersion: 12 }>('HTTP://localhost:3000', KEY)
// @ts-expect-error should raise error if provinding invalid version
createClient<Database, { postgrestVersion: 42 }>('HTTP://localhost:3000', KEY)
})
})

// Socket should close when there are no open connections
// https://github.com/supabase/supabase-js/issues/44

Expand Down