@@ -6,30 +6,35 @@ import { Fetch, GenericSchema, GenericTable, GenericView } from './types'
6
6
export default class PostgrestQueryBuilder <
7
7
Schema extends GenericSchema ,
8
8
Relation extends GenericTable | GenericView ,
9
+ ThrowOnError extends boolean ,
9
10
Relationships = Relation extends { Relationships : infer R } ? R : unknown
10
11
> {
11
12
url : URL
12
13
headers : Record < string , string >
13
14
schema ?: string
14
15
signal ?: AbortSignal
15
16
fetch ?: Fetch
17
+ shouldThrowOnError : ThrowOnError
16
18
17
19
constructor (
18
20
url : URL ,
19
21
{
20
22
headers = { } ,
21
23
schema,
22
24
fetch,
25
+ shouldThrowOnError,
23
26
} : {
24
27
headers ?: Record < string , string >
25
28
schema ?: string
26
29
fetch ?: Fetch
30
+ shouldThrowOnError ?: ThrowOnError
27
31
}
28
32
) {
29
33
this . url = url
30
34
this . headers = headers
31
35
this . schema = schema
32
36
this . fetch = fetch
37
+ this . shouldThrowOnError = Boolean ( shouldThrowOnError ) as ThrowOnError
33
38
}
34
39
35
40
/**
@@ -65,7 +70,7 @@ export default class PostgrestQueryBuilder<
65
70
head ?: boolean
66
71
count ?: 'exact' | 'planned' | 'estimated'
67
72
} = { }
68
- ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , ResultOne [ ] , Relationships > {
73
+ ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , ResultOne [ ] , Relationships , ThrowOnError > {
69
74
const method = head ? 'HEAD' : 'GET'
70
75
// Remove whitespaces except when quoted
71
76
let quoted = false
@@ -93,7 +98,8 @@ export default class PostgrestQueryBuilder<
93
98
schema : this . schema ,
94
99
fetch : this . fetch ,
95
100
allowEmpty : false ,
96
- } as unknown as PostgrestBuilder < ResultOne [ ] > )
101
+ shouldThrowOnError : this . shouldThrowOnError ,
102
+ } as unknown as PostgrestBuilder < ResultOne [ ] , ThrowOnError > )
97
103
}
98
104
99
105
// TODO(v3): Make `defaultToNull` consistent for both single & bulk inserts.
@@ -102,14 +108,14 @@ export default class PostgrestQueryBuilder<
102
108
options ?: {
103
109
count ?: 'exact' | 'planned' | 'estimated'
104
110
}
105
- ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships >
111
+ ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships , ThrowOnError >
106
112
insert < Row extends Relation extends { Insert : unknown } ? Relation [ 'Insert' ] : never > (
107
113
values : Row [ ] ,
108
114
options ?: {
109
115
count ?: 'exact' | 'planned' | 'estimated'
110
116
defaultToNull ?: boolean
111
117
}
112
- ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships >
118
+ ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships , ThrowOnError >
113
119
/**
114
120
* Perform an INSERT into the table or view.
115
121
*
@@ -145,7 +151,7 @@ export default class PostgrestQueryBuilder<
145
151
count ?: 'exact' | 'planned' | 'estimated'
146
152
defaultToNull ?: boolean
147
153
} = { }
148
- ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships > {
154
+ ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships , ThrowOnError > {
149
155
const method = 'POST'
150
156
151
157
const prefersHeaders = [ ]
@@ -176,7 +182,8 @@ export default class PostgrestQueryBuilder<
176
182
body : values ,
177
183
fetch : this . fetch ,
178
184
allowEmpty : false ,
179
- } as unknown as PostgrestBuilder < null > )
185
+ shouldThrowOnError : this . shouldThrowOnError ,
186
+ } as unknown as PostgrestBuilder < null , ThrowOnError > )
180
187
}
181
188
182
189
// TODO(v3): Make `defaultToNull` consistent for both single & bulk upserts.
@@ -187,7 +194,7 @@ export default class PostgrestQueryBuilder<
187
194
ignoreDuplicates ?: boolean
188
195
count ?: 'exact' | 'planned' | 'estimated'
189
196
}
190
- ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships >
197
+ ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships , ThrowOnError >
191
198
upsert < Row extends Relation extends { Insert : unknown } ? Relation [ 'Insert' ] : never > (
192
199
values : Row [ ] ,
193
200
options ?: {
@@ -196,7 +203,7 @@ export default class PostgrestQueryBuilder<
196
203
count ?: 'exact' | 'planned' | 'estimated'
197
204
defaultToNull ?: boolean
198
205
}
199
- ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships >
206
+ ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships , ThrowOnError >
200
207
/**
201
208
* Perform an UPSERT on the table or view. Depending on the column(s) passed
202
209
* to `onConflict`, `.upsert()` allows you to perform the equivalent of
@@ -248,7 +255,7 @@ export default class PostgrestQueryBuilder<
248
255
count ?: 'exact' | 'planned' | 'estimated'
249
256
defaultToNull ?: boolean
250
257
} = { }
251
- ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships > {
258
+ ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships , ThrowOnError > {
252
259
const method = 'POST'
253
260
254
261
const prefersHeaders = [ `resolution=${ ignoreDuplicates ? 'ignore' : 'merge' } -duplicates` ]
@@ -281,7 +288,8 @@ export default class PostgrestQueryBuilder<
281
288
body : values ,
282
289
fetch : this . fetch ,
283
290
allowEmpty : false ,
284
- } as unknown as PostgrestBuilder < null > )
291
+ shouldThrowOnError : this . shouldThrowOnError ,
292
+ } as unknown as PostgrestBuilder < null , ThrowOnError > )
285
293
}
286
294
287
295
/**
@@ -312,7 +320,7 @@ export default class PostgrestQueryBuilder<
312
320
} : {
313
321
count ?: 'exact' | 'planned' | 'estimated'
314
322
} = { }
315
- ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships > {
323
+ ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships , ThrowOnError > {
316
324
const method = 'PATCH'
317
325
const prefersHeaders = [ ]
318
326
if ( this . headers [ 'Prefer' ] ) {
@@ -331,7 +339,8 @@ export default class PostgrestQueryBuilder<
331
339
body : values ,
332
340
fetch : this . fetch ,
333
341
allowEmpty : false ,
334
- } as unknown as PostgrestBuilder < null > )
342
+ shouldThrowOnError : this . shouldThrowOnError ,
343
+ } as unknown as PostgrestBuilder < null , ThrowOnError > )
335
344
}
336
345
337
346
/**
@@ -357,7 +366,7 @@ export default class PostgrestQueryBuilder<
357
366
count,
358
367
} : {
359
368
count ?: 'exact' | 'planned' | 'estimated'
360
- } = { } ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships > {
369
+ } = { } ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null , Relationships , ThrowOnError > {
361
370
const method = 'DELETE'
362
371
const prefersHeaders = [ ]
363
372
if ( count ) {
@@ -375,6 +384,7 @@ export default class PostgrestQueryBuilder<
375
384
schema : this . schema ,
376
385
fetch : this . fetch ,
377
386
allowEmpty : false ,
378
- } as unknown as PostgrestBuilder < null > )
387
+ shouldThrowOnError : this . shouldThrowOnError ,
388
+ } as unknown as PostgrestBuilder < null , ThrowOnError > )
379
389
}
380
390
}
0 commit comments