1
- import { postgrest , selectParams , RPC_NAME } from '../rpc'
1
+ import { postgrest , selectParams , RPC_NAME_SCALAR } from '../rpc'
2
2
import { Database } from '../types'
3
3
import { expectType } from 'tsd'
4
4
import { TypeEqual } from 'ts-expect'
5
5
6
6
// RPC call with no params
7
7
{
8
8
const { data } = await postgrest
9
- . rpc ( RPC_NAME , { name_param : 'supabot' } )
9
+ . rpc ( RPC_NAME_SCALAR , { name_param : 'supabot' } )
10
10
. select ( selectParams . noParams )
11
11
let result : Exclude < typeof data , null >
12
- let expected : Database [ 'public' ] [ 'Functions' ] [ typeof RPC_NAME ] [ 'Returns' ]
12
+ let expected : Database [ 'public' ] [ 'Functions' ] [ typeof RPC_NAME_SCALAR ] [ 'Returns' ]
13
13
expectType < TypeEqual < typeof result , typeof expected > > ( true )
14
14
}
15
15
16
16
// RPC call with star select
17
17
{
18
18
const { data } = await postgrest
19
- . rpc ( RPC_NAME , { name_param : 'supabot' } )
19
+ . rpc ( RPC_NAME_SCALAR , { name_param : 'supabot' } )
20
20
. select ( selectParams . starSelect )
21
21
let result : Exclude < typeof data , null >
22
- let expected : Database [ 'public' ] [ 'Functions' ] [ typeof RPC_NAME ] [ 'Returns' ]
22
+ let expected : Database [ 'public' ] [ 'Functions' ] [ typeof RPC_NAME_SCALAR ] [ 'Returns' ]
23
23
expectType < TypeEqual < typeof result , typeof expected > > ( true )
24
24
}
25
25
26
26
// RPC call with single field select
27
27
{
28
28
const { data } = await postgrest
29
- . rpc ( RPC_NAME , { name_param : 'supabot' } )
29
+ . rpc ( RPC_NAME_SCALAR , { name_param : 'supabot' } )
30
30
. select ( selectParams . fieldSelect )
31
31
let result : Exclude < typeof data , null >
32
32
let expected : { username : string } [ ]
@@ -36,17 +36,17 @@ import { TypeEqual } from 'ts-expect'
36
36
// RPC call with multiple fields select
37
37
{
38
38
const { data } = await postgrest
39
- . rpc ( RPC_NAME , { name_param : 'supabot' } )
39
+ . rpc ( RPC_NAME_SCALAR , { name_param : 'supabot' } )
40
40
. select ( selectParams . fieldsSelect )
41
41
let result : Exclude < typeof data , null >
42
- let expected : Database [ 'public' ] [ 'Functions' ] [ typeof RPC_NAME ] [ 'Returns' ]
42
+ let expected : Database [ 'public' ] [ 'Functions' ] [ typeof RPC_NAME_SCALAR ] [ 'Returns' ]
43
43
expectType < TypeEqual < typeof result , typeof expected > > ( true )
44
44
}
45
45
46
46
// RPC call with field aliasing
47
47
{
48
48
const { data } = await postgrest
49
- . rpc ( RPC_NAME , { name_param : 'supabot' } )
49
+ . rpc ( RPC_NAME_SCALAR , { name_param : 'supabot' } )
50
50
. select ( selectParams . fieldAliasing )
51
51
let result : Exclude < typeof data , null >
52
52
let expected : { name : string } [ ]
@@ -56,7 +56,7 @@ import { TypeEqual } from 'ts-expect'
56
56
// RPC call with field casting
57
57
{
58
58
const { data } = await postgrest
59
- . rpc ( RPC_NAME , { name_param : 'supabot' } )
59
+ . rpc ( RPC_NAME_SCALAR , { name_param : 'supabot' } )
60
60
. select ( selectParams . fieldCasting )
61
61
let result : Exclude < typeof data , null >
62
62
let expected : { status : string } [ ]
@@ -66,9 +66,69 @@ import { TypeEqual } from 'ts-expect'
66
66
// RPC call with field aggregate
67
67
{
68
68
const { data } = await postgrest
69
- . rpc ( RPC_NAME , { name_param : 'supabot' } )
69
+ . rpc ( RPC_NAME_SCALAR , { name_param : 'supabot' } )
70
70
. select ( selectParams . fieldAggregate )
71
71
let result : Exclude < typeof data , null >
72
72
let expected : { count : number ; status : 'ONLINE' | 'OFFLINE' } [ ]
73
73
expectType < TypeEqual < typeof result , typeof expected > > ( true )
74
74
}
75
+
76
+ // RPC call with select field and embed relation
77
+ // TODO: Implement support for RPC functions that return a set of rows
78
+ //
79
+ // Current introspection for functions returning setof:
80
+ // get_all_users: {
81
+ // Args: Record<PropertyKey, never>
82
+ // Returns: {
83
+ // age_range: unknown | null
84
+ // catchphrase: unknown | null
85
+ // data: Json | null
86
+ // status: Database["public"]["Enums"]["user_status"] | null
87
+ // username: string
88
+ // }[]
89
+ // }
90
+ //
91
+ // Proposed introspection change:
92
+ // get_all_users: {
93
+ // setOf?: { refName: '<table-id-in-schema>' }
94
+ // Args: Record<PropertyKey, never>
95
+ // }
96
+ //
97
+ // This would allow for proper typing of RPC calls that return sets,
98
+ // enabling them to use the same filtering and selection capabilities
99
+ // as table queries.
100
+ //
101
+ // On the PostgrestClient side, the rpc method should be updated to
102
+ // handle the 'setOf' property, branching the return type based on its presence:
103
+ //
104
+ // rpc<FnName extends string & keyof Schema['Functions'], Fn extends Schema['Functions'][FnName]>(
105
+ // ...
106
+ // ):
107
+ // Fn['setOf'] extends { refName: string extends keyof Schema['Tables'] }
108
+ // ? PostgrestFilterBuilder<Schema, GetTable<Fn['setOf']>['Row'], Fn['setOf']['refName'], GetTable<Fn['setOf']>['Relationships']>
109
+ // : PostgrestFilterBuilder<
110
+ // Schema,
111
+ // Fn['Returns'] extends any[]
112
+ // ? Fn['Returns'][number] extends Record<string, unknown>
113
+ // ? Fn['Returns'][number]
114
+ // : never
115
+ // : never,
116
+ // Fn['Returns'],
117
+ // FnName,
118
+ // null
119
+ // >
120
+ //
121
+ // Implementation can be done in a follow-up PR.
122
+
123
+ // {
124
+ // const { data } = await postgrest
125
+ // .rpc(RPC_SETOF_NAME, {})
126
+ // .select(selectParams.selectFieldAndEmbedRelation)
127
+ // let result: Exclude<typeof data, null>
128
+ // let expected: {
129
+ // username: string
130
+ // status: 'ONLINE' | 'OFFLINE'
131
+ // profile: { id: number }[]
132
+ // }[]
133
+ // expectType<TypeEqual<typeof result, typeof expected>>(true)
134
+ // }
0 commit comments