Skip to content

Commit 5e2f3c0

Browse files
committed
fix(postgrest): use latest postgres-meta canary build
1 parent 02c28d9 commit 5e2f3c0

File tree

8 files changed

+295
-412
lines changed

8 files changed

+295
-412
lines changed

packages/core/postgrest-js/src/select-query-parser/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ export type IsStringUnion<T> = string extends T
635635
: false
636636

637637
// Functions matching utils
638-
export type IsMatchingArgs<
638+
type IsMatchingArgs<
639639
FnArgs extends GenericFunction['Args'],
640640
PassedArgs extends GenericFunction['Args'],
641641
> = [FnArgs] extends [Record<PropertyKey, never>]
@@ -648,19 +648,19 @@ export type IsMatchingArgs<
648648
: false
649649
: false
650650

651-
export type MatchingFunctionArgs<
651+
type MatchingFunctionArgs<
652652
Fn extends GenericFunction,
653653
Args extends GenericFunction['Args'],
654654
> = Fn extends { Args: infer A extends GenericFunction['Args'] }
655655
? IsMatchingArgs<A, Args> extends true
656656
? Fn
657657
: never
658-
: never
658+
: false
659659

660660
export type FindMatchingFunctionByArgs<
661661
FnUnion,
662662
Args extends GenericFunction['Args'],
663-
> = FnUnion extends infer Fn extends GenericFunction ? MatchingFunctionArgs<Fn, Args> : never
663+
> = FnUnion extends infer Fn extends GenericFunction ? MatchingFunctionArgs<Fn, Args> : false
664664

665665
type MatchingFunctionBySetofFrom<
666666
Fn extends GenericFunction,
@@ -669,7 +669,7 @@ type MatchingFunctionBySetofFrom<
669669
? TableName extends Fn['SetofOptions']['from']
670670
? Fn
671671
: never
672-
: never
672+
: false
673673

674674
type FindMatchingFunctionBySetofFrom<
675675
FnUnion,

packages/core/postgrest-js/src/types.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ type ExtractExactFunction<Fns, Args> = Fns extends infer F
1717

1818
type IsNever<T> = [T] extends [never] ? true : false
1919

20+
type RpcFunctionNotFound<FnName> = {
21+
Row: any
22+
Result: {
23+
error: true
24+
} & "Couldn't infer function definition matching provided arguments"
25+
RelationName: FnName
26+
Relationships: null
27+
}
28+
2029
export type GetRpcFunctionFilterBuilderByArgs<
2130
Schema extends GenericSchema,
2231
FnName extends string & keyof Schema['Functions'],
@@ -72,16 +81,9 @@ export type GetRpcFunctionFilterBuilderByArgs<
7281
}
7382
: // If we failed to find the function by argument, we still pass with any but also add an overridable
7483
Fn extends false
75-
? {
76-
Row: any
77-
Result: {
78-
error: true
79-
} & "Couldn't infer function definition matching provided arguments"
80-
RelationName: FnName
81-
Relationships: null
82-
}
83-
: never
84-
: never
84+
? RpcFunctionNotFound<FnName>
85+
: RpcFunctionNotFound<FnName>
86+
: RpcFunctionNotFound<FnName>
8587

8688
/**
8789
* Response format

packages/core/postgrest-js/test/db/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ services:
4141
POSTGRES_HOST: /var/run/postgresql
4242
POSTGRES_PORT: 5432
4343
pgmeta:
44-
image: supabase/postgres-meta:v0.91.5
44+
image: supabase/postgres-meta:canary-pr-971-12217b2b59b6eeddbb65949ddb257e17fec56393
4545
ports:
4646
- '8080:8080'
4747
environment:

packages/core/postgrest-js/test/embeded_functions_join.test.ts

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ describe('embeded functions select', () => {
7272
})
7373
)
7474
let expected: RequiredDeep<z.infer<typeof ExpectedSchema>>
75+
// Assert over the keys of the expected and result objects to ensure consistency between versions of types
76+
// should always fallback to a SelectQueryError if the relation cannot be found
77+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
7578
expectType<TypeEqual<typeof result, typeof expected>>(true)
7679
ExpectedSchema.parse(res.data)
7780
})
@@ -130,6 +133,7 @@ describe('embeded functions select', () => {
130133
})
131134
)
132135
let expected: z.infer<typeof ExpectedSchema>
136+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
133137
expectType<TypeEqual<typeof result, typeof expected>>(true)
134138
ExpectedSchema.parse(res.data)
135139
})
@@ -204,6 +208,7 @@ describe('embeded functions select', () => {
204208
})
205209
)
206210
let expected: RequiredDeep<z.infer<typeof ExpectedSchema>>
211+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
207212
expectType<TypeEqual<typeof result, typeof expected>>(true)
208213
ExpectedSchema.parse(res.data)
209214
})
@@ -268,6 +273,7 @@ describe('embeded functions select', () => {
268273
})
269274
)
270275
let expected: z.infer<typeof ExpectedSchema>
276+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
271277
expectType<TypeEqual<typeof result, typeof expected>>(true)
272278
ExpectedSchema.parse(res.data)
273279
})
@@ -336,6 +342,7 @@ describe('embeded functions select', () => {
336342
})
337343
)
338344
let expected: z.infer<typeof ExpectedSchema>
345+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
339346
expectType<TypeEqual<typeof result, typeof expected>>(true)
340347
ExpectedSchema.parse(res.data)
341348
})
@@ -390,6 +397,7 @@ describe('embeded functions select', () => {
390397
})
391398
)
392399
let expected: z.infer<typeof ExpectedSchema>
400+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
393401
expectType<TypeEqual<typeof result, typeof expected>>(true)
394402
ExpectedSchema.parse(res.data)
395403
})
@@ -454,14 +462,17 @@ describe('embeded functions select', () => {
454462
})
455463
)
456464
let expected: z.infer<typeof ExpectedSchema>
465+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
457466
expectType<TypeEqual<typeof result, typeof expected>>(true)
458467
ExpectedSchema.parse(res.data)
459468
})
460469

461470
test('embeded_setof_row_one_function_not_nullable - function returning a single row embeded table not nullable', async () => {
462471
const res = await postgrest
463472
.from('users')
464-
.select('username, user_called_profile_not_null:get_user_profile_non_nullable(*)')
473+
// Inner join to ensure the join result is not nullable can also be set at relation level
474+
// by setting isNotNullable for the function SetofOptions definition to true
475+
.select('username, user_called_profile_not_null:get_user_profile_non_nullable!inner(*)')
465476
expect(res).toMatchInlineSnapshot(`
466477
Object {
467478
"count": null,
@@ -473,22 +484,6 @@ describe('embeded functions select', () => {
473484
},
474485
"username": "supabot",
475486
},
476-
Object {
477-
"user_called_profile_not_null": null,
478-
"username": "kiwicopple",
479-
},
480-
Object {
481-
"user_called_profile_not_null": null,
482-
"username": "awailas",
483-
},
484-
Object {
485-
"user_called_profile_not_null": null,
486-
"username": "jsonuser",
487-
},
488-
Object {
489-
"user_called_profile_not_null": null,
490-
"username": "dragarcia",
491-
},
492487
],
493488
"error": null,
494489
"status": 200,
@@ -509,22 +504,10 @@ describe('embeded functions select', () => {
509504
})
510505
)
511506
let expected: z.infer<typeof ExpectedSchema>
507+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
512508
expectType<TypeEqual<typeof result, typeof expected>>(true)
513-
// Parsing with the non-nullable schema should throw, because there are nulls in the data.
514-
expect(() => ExpectedSchema.parse(res.data)).toThrowError()
515-
// However, parsing with a nullable schema should succeed.
516-
const ExpectedNullable = z.array(
517-
z.object({
518-
username: z.string(),
519-
user_called_profile_not_null: z
520-
.object({
521-
id: z.number(),
522-
username: z.string().nullable(),
523-
})
524-
.nullable(),
525-
})
526-
)
527-
ExpectedNullable.parse(res.data)
509+
// Can parse the data because the !inner ensure the join result from function is not nullable
510+
ExpectedSchema.parse(res.data)
528511
})
529512

530513
test('embeded_setof_row_one_function_with_fields_selection - function returning a single row embeded table with fields selection', async () => {
@@ -581,6 +564,7 @@ describe('embeded functions select', () => {
581564
})
582565
)
583566
let expected: z.infer<typeof ExpectedSchema>
567+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
584568
expectType<TypeEqual<typeof result, typeof expected>>(true)
585569
ExpectedSchema.parse(res.data)
586570
})
@@ -655,6 +639,7 @@ describe('embeded functions select', () => {
655639
})
656640
)
657641
let expected: z.infer<typeof ExpectedSchema>
642+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
658643
expectType<TypeEqual<typeof result, typeof expected>>(true)
659644
ExpectedSchema.parse(res.data)
660645
})
@@ -729,6 +714,7 @@ describe('embeded functions select', () => {
729714
})
730715
)
731716
let expected: RequiredDeep<z.infer<typeof ExpectedSchema>>
717+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
732718
expectType<TypeEqual<typeof result, typeof expected>>(true)
733719
ExpectedSchema.parse(res.data)
734720
})
@@ -801,6 +787,7 @@ describe('embeded functions select', () => {
801787
})
802788
)
803789
let expected: RequiredDeep<z.infer<typeof ExpectedSchema>>
790+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
804791
expectType<TypeEqual<typeof result, typeof expected>>(true)
805792
ExpectedSchema.parse(res.data)
806793
})
@@ -877,6 +864,7 @@ describe('embeded functions select', () => {
877864
})
878865
)
879866
let expected: RequiredDeep<z.infer<typeof ExpectedSchema>>
867+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
880868
expectType<TypeEqual<typeof result, typeof expected>>(true)
881869
ExpectedSchema.parse(res.data)
882870
})
@@ -949,6 +937,7 @@ describe('embeded functions select', () => {
949937
})
950938
)
951939
let expected: RequiredDeep<z.infer<typeof ExpectedSchema>>
940+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
952941
expectType<TypeEqual<typeof result, typeof expected>>(true)
953942
ExpectedSchema.parse(res.data)
954943
})
@@ -1017,6 +1006,7 @@ describe('embeded functions select', () => {
10171006
})
10181007
)
10191008
let expected: z.infer<typeof ExpectedSchema>
1009+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
10201010
expectType<TypeEqual<typeof result, typeof expected>>(true)
10211011
ExpectedSchema.parse(res.data)
10221012
})
@@ -1088,6 +1078,7 @@ describe('embeded functions select', () => {
10881078
})
10891079
)
10901080
let expected: z.infer<typeof ExpectedSchema>
1081+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
10911082
expectType<TypeEqual<typeof result, typeof expected>>(true)
10921083
ExpectedSchema.parse(res.data)
10931084
})
@@ -1175,6 +1166,7 @@ describe('embeded functions select', () => {
11751166
})
11761167
)
11771168
let expected: z.infer<typeof ExpectedSchema>
1169+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
11781170
expectType<TypeEqual<typeof result, typeof expected>>(true)
11791171
ExpectedSchema.parse(res.data)
11801172
})
@@ -1229,6 +1221,7 @@ describe('embeded functions select', () => {
12291221
})
12301222
)
12311223
let expected: z.infer<typeof ExpectedSchema>
1224+
expectType<TypeEqual<keyof (typeof expected)[number], keyof (typeof result)[number]>>(true)
12321225
expectType<TypeEqual<typeof result, typeof expected>>(true)
12331226
ExpectedSchema.parse(res.data)
12341227
})

packages/core/postgrest-js/test/rpc.test.ts

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { expectType, TypeEqual } from './types'
44
import { z } from 'zod'
55

66
const REST_URL = 'http://localhost:3000'
7-
const postgrest = new PostgrestClient<Database>(REST_URL)
7+
export const postgrest = new PostgrestClient<Database>(REST_URL)
88

9-
const RPC_NAME = 'get_username_and_status'
9+
export const RPC_NAME = 'get_username_and_status'
1010

1111
test('RPC call with no params', async () => {
1212
const res = await postgrest.rpc(RPC_NAME, { name_param: 'supabot' }).select()
@@ -206,43 +206,3 @@ test('RPC call with field aggregate', async () => {
206206
expectType<TypeEqual<typeof result, typeof expected>>(true)
207207
ExpectedSchema.parse(res.data)
208208
})
209-
210-
test('RPC get_status with no params', async () => {
211-
const res = await postgrest.rpc('get_status')
212-
expect(res).toMatchInlineSnapshot(`
213-
Object {
214-
"count": null,
215-
"data": null,
216-
"error": Object {
217-
"code": "PGRST202",
218-
"details": "Searched for the function public.get_status without parameters or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.",
219-
"hint": null,
220-
"message": "Could not find the function public.get_status without parameters in the schema cache",
221-
},
222-
"status": 404,
223-
"statusText": "Not Found",
224-
}
225-
`)
226-
let result: Exclude<typeof res.data, null>
227-
// get_status without name param doesn't exist in the schema so we expect never
228-
let expected: never
229-
expectType<TypeEqual<typeof result, typeof expected>>(true)
230-
})
231-
232-
test('RPC get_status with name params', async () => {
233-
const res = await postgrest.rpc('get_status', { name_param: 'supabot' })
234-
expect(res).toMatchInlineSnapshot(`
235-
Object {
236-
"count": null,
237-
"data": "ONLINE",
238-
"error": null,
239-
"status": 200,
240-
"statusText": "OK",
241-
}
242-
`)
243-
let result: Exclude<typeof res.data, null>
244-
const ExpectedSchema = z.enum(['ONLINE', 'OFFLINE'] as const)
245-
let expected: z.infer<typeof ExpectedSchema>
246-
expectType<TypeEqual<typeof result, typeof expected>>(true)
247-
ExpectedSchema.parse(res.data)
248-
})

0 commit comments

Comments
 (0)