@@ -2,12 +2,20 @@ import PostgrestQueryBuilder from './PostgrestQueryBuilder'
2
2
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
3
3
import PostgrestBuilder from './PostgrestBuilder'
4
4
import { DEFAULT_HEADERS } from './constants'
5
- import { Fetch } from './types'
5
+ import { Fetch , GenericSchema } from './types'
6
6
7
- export default class PostgrestClient {
7
+ export default class PostgrestClient <
8
+ Database = any ,
9
+ SchemaName extends string & keyof Database = 'public' extends keyof Database
10
+ ? 'public'
11
+ : string & keyof Database ,
12
+ Schema extends GenericSchema = Database [ SchemaName ] extends GenericSchema
13
+ ? Database [ SchemaName ]
14
+ : any
15
+ > {
8
16
url : string
9
17
headers : Record < string , string >
10
- schema ?: string
18
+ schema ?: SchemaName
11
19
fetch ?: Fetch
12
20
shouldThrowOnError : boolean
13
21
@@ -27,7 +35,7 @@ export default class PostgrestClient {
27
35
throwOnError = false ,
28
36
} : {
29
37
headers ?: Record < string , string >
30
- schema ?: string
38
+ schema ?: SchemaName
31
39
fetch ?: Fetch
32
40
throwOnError ?: boolean
33
41
} = { }
@@ -56,9 +64,12 @@ export default class PostgrestClient {
56
64
*
57
65
* @param table The table name to operate on.
58
66
*/
59
- from < T = any > ( table : string ) : PostgrestQueryBuilder < T > {
60
- const url = `${ this . url } /${ table } `
61
- return new PostgrestQueryBuilder < T > ( url , {
67
+ from <
68
+ TableName extends string & keyof Schema [ 'Tables' ] ,
69
+ Table extends Schema [ 'Tables' ] [ TableName ]
70
+ > ( table : TableName ) : PostgrestQueryBuilder < Table > {
71
+ const url = new URL ( `${ this . url } /${ table } ` )
72
+ return new PostgrestQueryBuilder < Table > ( url , {
62
73
headers : { ...this . headers } ,
63
74
schema : this . schema ,
64
75
fetch : this . fetch ,
@@ -70,33 +81,43 @@ export default class PostgrestClient {
70
81
* Perform a function call.
71
82
*
72
83
* @param fn The function name to call.
73
- * @param params The parameters to pass to the function call.
84
+ * @param args The parameters to pass to the function call.
74
85
* @param options Named parameters.
75
86
* @param options.head When set to true, no data will be returned.
76
87
* @param options.count Count algorithm to use to count rows in a table.
77
88
*/
78
- rpc < T = any > (
79
- fn : string ,
80
- params : Record < string , unknown > = { } ,
89
+ rpc <
90
+ FunctionName extends string & keyof Schema [ 'Functions' ] ,
91
+ Function_ extends Schema [ 'Functions' ] [ FunctionName ]
92
+ > (
93
+ fn : FunctionName ,
94
+ args : Function_ [ 'Args' ] = { } ,
81
95
{
82
96
head = false ,
83
97
count,
84
98
} : {
85
99
head ?: boolean
86
100
count ?: 'exact' | 'planned' | 'estimated'
87
101
} = { }
88
- ) : PostgrestFilterBuilder < T > {
102
+ ) : PostgrestFilterBuilder <
103
+ Function_ [ 'Returns' ] extends any [ ]
104
+ ? Function_ [ 'Returns' ] [ number ] extends Record < string , unknown >
105
+ ? Function_ [ 'Returns' ] [ number ]
106
+ : never
107
+ : never ,
108
+ Function_ [ 'Returns' ]
109
+ > {
89
110
let method : 'HEAD' | 'POST'
90
111
const url = new URL ( `${ this . url } /rpc/${ fn } ` )
91
112
let body : unknown | undefined
92
113
if ( head ) {
93
114
method = 'HEAD'
94
- Object . entries ( params ) . forEach ( ( [ name , value ] ) => {
115
+ Object . entries ( args ) . forEach ( ( [ name , value ] ) => {
95
116
url . searchParams . append ( name , `${ value } ` )
96
117
} )
97
118
} else {
98
119
method = 'POST'
99
- body = params
120
+ body = args
100
121
}
101
122
102
123
const headers = { ...this . headers }
@@ -113,6 +134,6 @@ export default class PostgrestClient {
113
134
fetch : this . fetch ,
114
135
shouldThrowOnError : this . shouldThrowOnError ,
115
136
allowEmpty : false ,
116
- } as unknown as PostgrestBuilder < T > )
137
+ } as unknown as PostgrestBuilder < Function_ [ 'Returns' ] > )
117
138
}
118
139
}
0 commit comments