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 3 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
30 changes: 20 additions & 10 deletions src/SupabaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
PostgrestClient,
PostgrestFilterBuilder,
PostgrestQueryBuilder,
ClientServerOptions as PostgrestClientServerOption,
GetGenericDatabaseWithOptions,
} from '@supabase/postgrest-js'
import {
RealtimeChannel,
Expand All @@ -28,13 +30,18 @@ import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions
*
* An isomorphic Javascript client for interacting with Postgres.
*/

export type ServicesOptions = PostgrestClientServerOption & {}

export default class SupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
ClientOptions extends ServicesOptions = { PostgrestVersion: '12' },
SchemaName extends string &
keyof GetGenericDatabaseWithOptions<Database>['db'] = 'public' extends keyof GetGenericDatabaseWithOptions<Database>['db']
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: string & keyof GetGenericDatabaseWithOptions<Database>['db'],
Schema extends GenericSchema = GetGenericDatabaseWithOptions<Database>['db'][SchemaName] extends GenericSchema
? GetGenericDatabaseWithOptions<Database>['db'][SchemaName]
: any
> {
/**
Expand All @@ -47,7 +54,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 +161,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 @@ -175,10 +182,11 @@ export default class SupabaseClient<
*
* @param schema - The schema to query
*/
schema<DynamicSchema extends string & keyof Database>(
schema<DynamicSchema extends string & keyof GetGenericDatabaseWithOptions<Database>['db']>(
schema: DynamicSchema
): PostgrestClient<
Database,
ClientOptions,
DynamicSchema,
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any
> {
Expand Down Expand Up @@ -218,6 +226,7 @@ export default class SupabaseClient<
count?: 'exact' | 'planned' | 'estimated'
} = {}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Fn['Returns'] extends any[]
? Fn['Returns'][number] extends Record<string, unknown>
Expand All @@ -226,7 +235,8 @@ export default class SupabaseClient<
: never,
Fn['Returns'],
FnName,
null
null,
'RPC'
> {
return this.rest.rpc(fn, args, options)
}
Expand Down
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import SupabaseClient from './SupabaseClient'
import type { GenericSchema, SupabaseClientOptions } from './lib/types'
import type { ServicesOptions } from './SupabaseClient'
import type { GetGenericDatabaseWithOptions } from '@supabase/postgrest-js'

export * from '@supabase/auth-js'
export type { User as AuthUser, Session as AuthSession } from '@supabase/auth-js'
Expand All @@ -26,16 +28,22 @@ export type { SupabaseClientOptions, QueryResult, QueryData, QueryError } from '
*/
export const createClient = <
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
ClientOptions extends ServicesOptions = { PostgrestVersion: '12' },
SchemaName extends string &
keyof GetGenericDatabaseWithOptions<Database>['db'] = 'public' extends keyof GetGenericDatabaseWithOptions<Database>['db']
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: string & keyof GetGenericDatabaseWithOptions<Database>['db'],
Schema extends GenericSchema = GetGenericDatabaseWithOptions<Database>['db'][SchemaName] extends GenericSchema
? GetGenericDatabaseWithOptions<Database>['db'][SchemaName]
: any
>(
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
)
}
17 changes: 14 additions & 3 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ describe('Custom Headers', () => {

const request = createClient(URL, KEY, { global: { headers: customHeader } }).rpc('')

// @ts-ignore
const getHeaders = request.headers
//@ts-expect-error headers is protected attribute
const requestHeader = request.headers.get('X-Test-Header')

expect(getHeaders).toHaveProperty('X-Test-Header', 'value')
expect(requestHeader).toBe(customHeader['X-Test-Header'])
})
})

Expand Down 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