Skip to content

Commit c5ee251

Browse files
committed
feat: reland postgrest 13 support
1 parent 1137035 commit c5ee251

File tree

5 files changed

+88
-27
lines changed

5 files changed

+88
-27
lines changed

package-lock.json

Lines changed: 30 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@supabase/auth-js": "2.71.1",
5454
"@supabase/functions-js": "2.4.5",
5555
"@supabase/node-fetch": "2.6.15",
56-
"@supabase/postgrest-js": "1.19.4",
56+
"@supabase/postgrest-js": "file:../postgrest-js",
5757
"@supabase/realtime-js": "2.15.1",
5858
"@supabase/storage-js": "^2.10.4"
5959
},

src/SupabaseClient.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,32 @@ import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions
3030
*/
3131
export default class SupabaseClient<
3232
Database = any,
33-
SchemaName extends string & keyof Database = 'public' extends keyof Database
33+
// The second type parameter is also used for specifying db_schema, so we
34+
// support both cases.
35+
// TODO: Allow setting db_schema from ClientOptions.
36+
SchemaNameOrClientOptions extends
37+
| (string & keyof Database)
38+
| { PostgrestVersion: string } = 'public' extends keyof Database
3439
? 'public'
3540
: string & keyof Database,
36-
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
37-
? Database[SchemaName]
38-
: any
41+
SchemaName extends string &
42+
keyof Omit<Database, '__InternalSupabase'> = SchemaNameOrClientOptions extends string &
43+
keyof Omit<Database, '__InternalSupabase'>
44+
? SchemaNameOrClientOptions
45+
: 'public' extends keyof Omit<Database, '__InternalSupabase'>
46+
? 'public'
47+
: string & keyof Omit<Omit<Database, '__InternalSupabase'>, '__InternalSupabase'>,
48+
Schema extends Omit<Database, '__InternalSupabase'>[SchemaName] extends GenericSchema
49+
? Omit<Database, '__InternalSupabase'>[SchemaName]
50+
: never = Omit<Database, '__InternalSupabase'>[SchemaName] extends GenericSchema
51+
? Omit<Database, '__InternalSupabase'>[SchemaName]
52+
: never,
53+
ClientOptions extends { PostgrestVersion: string } = SchemaNameOrClientOptions extends string &
54+
keyof Omit<Database, '__InternalSupabase'>
55+
? { PostgrestVersion: '12' }
56+
: SchemaNameOrClientOptions extends { PostgrestVersion: string }
57+
? SchemaNameOrClientOptions
58+
: never
3959
> {
4060
/**
4161
* Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies.
@@ -51,7 +71,7 @@ export default class SupabaseClient<
5171
protected authUrl: URL
5272
protected storageUrl: URL
5373
protected functionsUrl: URL
54-
protected rest: PostgrestClient<Database, SchemaName, Schema>
74+
protected rest: PostgrestClient<Database, ClientOptions, SchemaName>
5575
protected storageKey: string
5676
protected fetch?: Fetch
5777
protected changedAccessToken?: string
@@ -161,16 +181,16 @@ export default class SupabaseClient<
161181
from<
162182
TableName extends string & keyof Schema['Tables'],
163183
Table extends Schema['Tables'][TableName]
164-
>(relation: TableName): PostgrestQueryBuilder<Schema, Table, TableName>
184+
>(relation: TableName): PostgrestQueryBuilder<ClientOptions, Schema, Table, TableName>
165185
from<ViewName extends string & keyof Schema['Views'], View extends Schema['Views'][ViewName]>(
166186
relation: ViewName
167-
): PostgrestQueryBuilder<Schema, View, ViewName>
187+
): PostgrestQueryBuilder<ClientOptions, Schema, View, ViewName>
168188
/**
169189
* Perform a query on a table or a view.
170190
*
171191
* @param relation - The table or view name to query
172192
*/
173-
from(relation: string): PostgrestQueryBuilder<Schema, any, any> {
193+
from(relation: string): PostgrestQueryBuilder<ClientOptions, Schema, any> {
174194
return this.rest.from(relation)
175195
}
176196

@@ -182,10 +202,11 @@ export default class SupabaseClient<
182202
*
183203
* @param schema - The schema to query
184204
*/
185-
schema<DynamicSchema extends string & keyof Database>(
205+
schema<DynamicSchema extends string & keyof Omit<Database, '__InternalSupabase'>>(
186206
schema: DynamicSchema
187207
): PostgrestClient<
188208
Database,
209+
ClientOptions,
189210
DynamicSchema,
190211
Database[DynamicSchema] extends GenericSchema ? Database[DynamicSchema] : any
191212
> {
@@ -225,6 +246,7 @@ export default class SupabaseClient<
225246
count?: 'exact' | 'planned' | 'estimated'
226247
} = {}
227248
): PostgrestFilterBuilder<
249+
ClientOptions,
228250
Schema,
229251
Fn['Returns'] extends any[]
230252
? Fn['Returns'][number] extends Record<string, unknown>
@@ -233,7 +255,8 @@ export default class SupabaseClient<
233255
: never,
234256
Fn['Returns'],
235257
FnName,
236-
null
258+
null,
259+
'RPC'
237260
> {
238261
return this.rest.rpc(fn, args, options)
239262
}

src/index.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import SupabaseClient from './SupabaseClient'
2-
import type { GenericSchema, SupabaseClientOptions } from './lib/types'
2+
import type { SupabaseClientOptions } from './lib/types'
33

44
export * from '@supabase/auth-js'
55
export type { User as AuthUser, Session as AuthSession } from '@supabase/auth-js'
@@ -26,18 +26,28 @@ export type { SupabaseClientOptions, QueryResult, QueryData, QueryError } from '
2626
*/
2727
export const createClient = <
2828
Database = any,
29-
SchemaName extends string & keyof Database = 'public' extends keyof Database
29+
SchemaNameOrClientOptions extends
30+
| (string & keyof Database)
31+
| { PostgrestVersion: string } = 'public' extends keyof Database
3032
? 'public'
3133
: string & keyof Database,
32-
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
33-
? Database[SchemaName]
34-
: any
34+
SchemaName extends string &
35+
keyof Omit<Database, '__InternalSupabase'> = SchemaNameOrClientOptions extends string &
36+
keyof Omit<Database, '__InternalSupabase'>
37+
? SchemaNameOrClientOptions
38+
: 'public' extends keyof Omit<Database, '__InternalSupabase'>
39+
? 'public'
40+
: string & keyof Omit<Omit<Database, '__InternalSupabase'>, '__InternalSupabase'>
3541
>(
3642
supabaseUrl: string,
3743
supabaseKey: string,
3844
options?: SupabaseClientOptions<SchemaName>
39-
): SupabaseClient<Database, SchemaName, Schema> => {
40-
return new SupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, options)
45+
): SupabaseClient<Database, SchemaNameOrClientOptions, SchemaName> => {
46+
return new SupabaseClient<Database, SchemaNameOrClientOptions, SchemaName>(
47+
supabaseUrl,
48+
supabaseKey,
49+
options
50+
)
4151
}
4252

4353
// Check for Node.js <= 18 deprecation

test/types/index.test-d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { expectError, expectType } from 'tsd'
2-
import { PostgrestSingleResponse, createClient } from '../../src/index'
2+
import { PostgrestSingleResponse, createClient, SupabaseClient } from '../../src/index'
33
import { Database, Json } from '../types'
44

55
const URL = 'http://localhost:3000'
66
const KEY = 'some.fake.key'
77
const supabase = createClient<Database>(URL, KEY)
88

9+
// createClient with custom schema
10+
{
11+
createClient<Database, 'personal'>(URL, KEY, { db: { schema: 'personal' } })
12+
new SupabaseClient<Database, 'personal'>(URL, KEY, { db: { schema: 'personal' } })
13+
}
14+
915
// table invalid type
1016
{
1117
expectError(supabase.from(42))

0 commit comments

Comments
 (0)