1
1
import PostgrestBuilder from './PostgrestBuilder'
2
2
import { GetResult } from './select-query-parser'
3
- import {
4
- GenericSchema ,
5
- PostgrestMaybeSingleResponse ,
6
- PostgrestResponse ,
7
- PostgrestSingleResponse ,
8
- } from './types'
3
+ import { GenericSchema } from './types'
9
4
10
5
export default class PostgrestTransformBuilder <
11
6
Schema extends GenericSchema ,
@@ -21,9 +16,9 @@ export default class PostgrestTransformBuilder<
21
16
*
22
17
* @param columns - The columns to retrieve, separated by commas
23
18
*/
24
- select < Query extends string = '*' , NewResult = GetResult < Schema , Row , Query > > (
19
+ select < Query extends string = '*' , NewResultOne = GetResult < Schema , Row , Query > > (
25
20
columns ?: Query
26
- ) : PostgrestTransformBuilder < Schema , Row , NewResult > {
21
+ ) : PostgrestTransformBuilder < Schema , Row , NewResultOne [ ] > {
27
22
// Remove whitespaces except when quoted
28
23
let quoted = false
29
24
const cleanedColumns = ( columns ?? '*' )
@@ -43,7 +38,7 @@ export default class PostgrestTransformBuilder<
43
38
this . headers [ 'Prefer' ] += ','
44
39
}
45
40
this . headers [ 'Prefer' ] += 'return=representation'
46
- return this as unknown as PostgrestTransformBuilder < Schema , Row , NewResult >
41
+ return this as unknown as PostgrestTransformBuilder < Schema , Row , NewResultOne [ ] >
47
42
}
48
43
49
44
order < ColumnName extends string & keyof Row > (
@@ -138,9 +133,11 @@ export default class PostgrestTransformBuilder<
138
133
* Query result must be one row (e.g. using `.limit(1)`), otherwise this
139
134
* returns an error.
140
135
*/
141
- single ( ) : PromiseLike < PostgrestSingleResponse < Result > > {
136
+ single <
137
+ ResultOne = Result extends ( infer ResultOne ) [ ] ? ResultOne : never
138
+ > ( ) : PostgrestBuilder < ResultOne > {
142
139
this . headers [ 'Accept' ] = 'application/vnd.pgrst.object+json'
143
- return this as PromiseLike < PostgrestSingleResponse < Result > >
140
+ return this as PostgrestBuilder < ResultOne >
144
141
}
145
142
146
143
/**
@@ -149,26 +146,28 @@ export default class PostgrestTransformBuilder<
149
146
* Query result must be zero or one row (e.g. using `.limit(1)`), otherwise
150
147
* this returns an error.
151
148
*/
152
- maybeSingle ( ) : PromiseLike < PostgrestMaybeSingleResponse < Result > > {
149
+ maybeSingle <
150
+ ResultOne = Result extends ( infer ResultOne ) [ ] ? ResultOne : never
151
+ > ( ) : PostgrestBuilder < ResultOne | null > {
153
152
this . headers [ 'Accept' ] = 'application/vnd.pgrst.object+json'
154
153
this . allowEmpty = true
155
- return this as PromiseLike < PostgrestMaybeSingleResponse < Result > >
154
+ return this as PostgrestBuilder < ResultOne | null >
156
155
}
157
156
158
157
/**
159
158
* Return `data` as a string in CSV format.
160
159
*/
161
- csv ( ) : PromiseLike < PostgrestSingleResponse < string > > {
160
+ csv ( ) : PostgrestBuilder < string > {
162
161
this . headers [ 'Accept' ] = 'text/csv'
163
- return this as PromiseLike < PostgrestSingleResponse < string > >
162
+ return this as PostgrestBuilder < string >
164
163
}
165
164
166
165
/**
167
166
* Return `data` as an object in [GeoJSON](https://geojson.org) format.
168
167
*/
169
- geojson ( ) : PromiseLike < PostgrestSingleResponse < Record < string , unknown > > > {
168
+ geojson ( ) : PostgrestBuilder < Record < string , unknown > > {
170
169
this . headers [ 'Accept' ] = 'application/geo+json'
171
- return this as PromiseLike < PostgrestSingleResponse < Record < string , unknown > > >
170
+ return this as PostgrestBuilder < Record < string , unknown > >
172
171
}
173
172
174
173
/**
@@ -206,9 +205,7 @@ export default class PostgrestTransformBuilder<
206
205
buffers ?: boolean
207
206
wal ?: boolean
208
207
format ?: 'json' | 'text'
209
- } = { } ) :
210
- | PromiseLike < PostgrestResponse < Record < string , unknown > > >
211
- | PromiseLike < PostgrestSingleResponse < string > > {
208
+ } = { } ) : PostgrestBuilder < Record < string , unknown > [ ] > | PostgrestBuilder < string > {
212
209
const options = [
213
210
analyze ? 'analyze' : null ,
214
211
verbose ? 'verbose' : null ,
@@ -223,8 +220,8 @@ export default class PostgrestTransformBuilder<
223
220
this . headers [
224
221
'Accept'
225
222
] = `application/vnd.pgrst.plan+${ format } ; for="${ forMediatype } "; options=${ options } ;`
226
- if ( format === 'json' ) return this as PromiseLike < PostgrestResponse < Record < string , unknown > > >
227
- else return this as PromiseLike < PostgrestSingleResponse < string > >
223
+ if ( format === 'json' ) return this as PostgrestBuilder < Record < string , unknown > [ ] >
224
+ else return this as PostgrestBuilder < string >
228
225
}
229
226
230
227
/**
0 commit comments