Skip to content

Commit a9443a1

Browse files
committed
chore: add tests
1 parent 03dd288 commit a9443a1

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type { SupabaseClientOptions, QueryResult, QueryData, QueryError } from '
2828
*/
2929
export const createClient = <
3030
Database = any,
31-
ClientOptions extends ServicesOptions = { PostgrestVersion: '12' },
31+
ClientOptions extends ServicesOptions = GetGenericDatabaseWithOptions<Database>['options'],
3232
SchemaName extends string &
3333
keyof GetGenericDatabaseWithOptions<Database>['db'] = 'public' extends keyof GetGenericDatabaseWithOptions<Database>['db']
3434
? 'public'

test/client.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,7 @@ describe('Dynamic schema', () => {
7575
test('should swap schemas', async () => {
7676
const client = createClient<Database>('HTTP://localhost:3000', KEY)
7777
expect(client.schema('personal')).toBeInstanceOf(PostgrestClient)
78-
expect(client.schema('personal').from('users').schema).toBe('personal')
79-
})
80-
})
81-
82-
describe('Postgrest 13 client', () => {
83-
test('should be able to declare specific PostgrestVersion ', async () => {
84-
// Note: The template argument properties (PostgrestVersion) will not be autocompleted
85-
// due to a Typescript bug tracked here: https://github.com/microsoft/TypeScript/issues/56299
86-
createClient<Database, { PostgrestVersion: '13' }>('HTTP://localhost:3000', KEY)
87-
createClient<Database, { PostgrestVersion: '12' }>('HTTP://localhost:3000', KEY)
88-
// @ts-expect-error should raise error if provinding invalid version
89-
createClient<Database, { PostgrestVersion: 42 }>('HTTP://localhost:3000', KEY)
78+
expect(client.schema('personal').from('users').insert([]).maxAffected(21)).toBe('personal')
9079
})
9180
})
9281

test/index.test-d.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,59 @@ const supabase = createClient<Database>(URL, KEY)
136136
}[]
137137
>(channels)
138138
}
139+
// Test Postgrest13
140+
// should be able to declare specific PostgrestVersion
141+
{
142+
// @ts-expect-error should raise error if provinding invalid version
143+
createClient<Database, { PostgrestVersion: 42 }>('HTTP://localhost:3000', KEY)
144+
}
145+
// should be able to infer PostgrestVersion from Database __InternalSupabase
146+
{
147+
type DatabaseWithInternals = {
148+
__InternalSupabase: {
149+
PostgrestVersion: '13'
150+
}
151+
public: {
152+
Tables: {
153+
shops: {
154+
Row: {
155+
address: string | null
156+
id: number
157+
shop_geom: unknown | null
158+
}
159+
Insert: {
160+
address?: string | null
161+
id: number
162+
shop_geom?: unknown | null
163+
}
164+
Update: {
165+
address?: string | null
166+
id?: number
167+
shop_geom?: unknown | null
168+
}
169+
Relationships: []
170+
}
171+
}
172+
Views: {
173+
[_ in never]: never
174+
}
175+
Functions: {
176+
[_ in never]: never
177+
}
178+
Enums: {
179+
[_ in never]: never
180+
}
181+
CompositeTypes: {
182+
[_ in never]: never
183+
}
184+
}
185+
}
186+
// Note: The template argument properties (PostgrestVersion) will not be autocompleted
187+
// due to a Typescript bug tracked here: https://github.com/microsoft/TypeScript/issues/56299
188+
const pg13Client = createClient<DatabaseWithInternals>('HTTP://localhost:3000', KEY)
189+
const pg12Client = createClient<Database>('HTTP://localhost:3000', KEY)
190+
const res13 = await pg13Client.from('shops').update({ id: 21 }).maxAffected(1)
191+
const res12 = await pg12Client.from('shops').update({ id: 21 }).maxAffected(1)
192+
expectType<typeof res13.data>(null)
193+
expectType<typeof res12.Error>('maxAffected method only available on postgrest 13+')
194+
}

0 commit comments

Comments
 (0)