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 10 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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@supabase/auth-js": "2.70.0",
"@supabase/functions-js": "2.4.5",
"@supabase/node-fetch": "2.6.15",
"@supabase/postgrest-js": "1.19.4",
"@supabase/postgrest-js": "1.21.0",
"@supabase/realtime-js": "2.11.15",
"@supabase/storage-js": "2.7.1"
},
Expand Down
20 changes: 10 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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: URL
protected storageUrl: URL
protected functionsUrl: URL
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 @@ -156,16 +163,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 @@ -177,10 +184,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 @@ -220,6 +228,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 @@ -228,7 +237,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 = GetGenericDatabaseWithOptions<Database>['options'],
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
)
}
8 changes: 4 additions & 4 deletions test/deno/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,59 @@ const supabase = createClient<Database>(URL, KEY)
}[]
>(channels)
}
// Test Postgrest13
// should be able to declare specific PostgrestVersion
{
// @ts-expect-error should raise error if provinding invalid version
createClient<Database, { PostgrestVersion: 42 }>('HTTP://localhost:3000', KEY)
}
// should be able to infer PostgrestVersion from Database __InternalSupabase
{
type DatabaseWithInternals = {
__InternalSupabase: {
PostgrestVersion: '13'
}
public: {
Tables: {
shops: {
Row: {
address: string | null
id: number
shop_geom: unknown | null
}
Insert: {
address?: string | null
id: number
shop_geom?: unknown | null
}
Update: {
address?: string | null
id?: number
shop_geom?: unknown | null
}
Relationships: []
}
}
Views: {
[_ in never]: never
}
Functions: {
[_ in never]: never
}
Enums: {
[_ in never]: never
}
CompositeTypes: {
[_ in never]: never
}
}
}
// Note: The template argument properties (PostgrestVersion) will not be autocompleted
// due to a Typescript bug tracked here: https://github.com/microsoft/TypeScript/issues/56299
const pg13Client = createClient<DatabaseWithInternals>('HTTP://localhost:3000', KEY)
const pg12Client = createClient<Database>('HTTP://localhost:3000', KEY)
const res13 = await pg13Client.from('shops').update({ id: 21 }).maxAffected(1)
const res12 = await pg12Client.from('shops').update({ id: 21 }).maxAffected(1)
expectType<typeof res13.data>(null)
expectType<typeof res12.Error>('maxAffected method only available on postgrest 13+')
}
18 changes: 10 additions & 8 deletions test/unit/SupabaseClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,20 @@ describe('SupabaseClient', () => {
test('should have custom header set', () => {
const customHeader = { 'X-Test-Header': 'value' }
const request = createClient(URL, KEY, { global: { headers: customHeader } }).rpc('')
// @ts-ignore
const getHeaders = request.headers
expect(getHeaders).toHaveProperty('X-Test-Header', 'value')
//@ts-expect-error headers is protected attribute
const requestHeader = request.headers.get('X-Test-Header')
expect(requestHeader).toBe(customHeader['X-Test-Header'])
})

test('should merge custom headers with default headers', () => {
const customHeader = { 'X-Test-Header': 'value' }
const client = createClient(URL, KEY, { global: { headers: customHeader } })
// @ts-ignore
expect(client.headers).toHaveProperty('X-Test-Header', 'value')
// @ts-ignore
expect(client.headers).toHaveProperty('X-Client-Info')
const request = createClient(URL, KEY, { global: { headers: customHeader } }).rpc('')

//@ts-expect-error headers is protected attribute
const requestHeader = request.headers.get('X-Test-Header')
expect(requestHeader).toBe(customHeader['X-Test-Header'])
//@ts-expect-error headers is protected attribute
expect(request.headers.get('X-Client-Info')).not.toBeNull()
})
})

Expand Down
Loading