1
1
import fetch from 'cross-fetch'
2
2
3
- // https://postgrest.org/en/stable/api.html?highlight=options#errors-and-http-status-codes
3
+ /**
4
+ * Error format
5
+ * {@link https://postgrest.org/en/stable/api.html?highlight=options#errors-and-http-status-codes}
6
+ */
4
7
interface PostgrestError {
5
8
message : string
6
9
details : string
7
10
hint : string
8
11
code : string
9
12
}
10
13
11
- // Response format: https://github.com/supabase/supabase-js/issues/32
14
+ /**
15
+ * Response format
16
+ * {@link https://github.com/supabase/supabase-js/issues/32}
17
+ */
12
18
interface PostgrestResponse < T > {
13
19
error : PostgrestError | null
14
20
data : T | T [ ] | null
@@ -21,7 +27,6 @@ interface PostgrestResponse<T> {
21
27
/**
22
28
* Base builder
23
29
*/
24
-
25
30
export abstract class PostgrestBuilder < T > implements PromiseLike < any > {
26
31
method ! : 'GET' | 'HEAD' | 'POST' | 'PATCH' | 'DELETE'
27
32
url ! : URL
@@ -90,7 +95,7 @@ export class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
90
95
/**
91
96
* Performs horizontal filtering with SELECT.
92
97
*
93
- * @param columns The columns to retrieve, separated by commas.
98
+ * @param { string } columns The columns to retrieve, separated by commas.
94
99
*/
95
100
select ( columns = '*' ) : PostgrestFilterBuilder < T > {
96
101
this . method = 'GET'
@@ -115,8 +120,8 @@ export class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
115
120
/**
116
121
* Performs an INSERT into the table.
117
122
*
118
- * @param values The values to insert.
119
- * @param upsert If `true`, performs an UPSERT.
123
+ * @param { Partial<T>|Partial<T>[] } values The values to insert.
124
+ * @param { boolean } object.upsert If `true`, performs an UPSERT.
120
125
*/
121
126
insert ( values : Partial < T > | Partial < T > [ ] , { upsert = false } = { } ) : PostgrestBuilder < T > {
122
127
this . method = 'POST'
@@ -130,7 +135,7 @@ export class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
130
135
/**
131
136
* Performs an UPDATE on the table.
132
137
*
133
- * @param values The values to update.
138
+ * @param { Partial<T> } values The values to update.
134
139
*/
135
140
update ( values : Partial < T > ) : PostgrestFilterBuilder < T > {
136
141
this . method = 'PATCH'
@@ -164,10 +169,10 @@ class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
164
169
/**
165
170
* Orders the result with the specified `column`.
166
171
*
167
- * @param column The column to order on.
168
- * @param ascending If `true`, the result will be in ascending order.
169
- * @param nullsFirst If `true`, `null`s appear first.
170
- * @param foreignTable The foreign table to use (if `column` is a foreign column).
172
+ * @param { T } column The column to order on.
173
+ * @param { boolean? } ascending If `true`, the result will be in ascending order.
174
+ * @param { boolean? } nullsFirst If `true`, `null`s appear first.
175
+ * @param { string? } foreignTable The foreign table to use (if `column` is a foreign column).
171
176
*/
172
177
order (
173
178
column : keyof T ,
@@ -188,8 +193,8 @@ class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
188
193
/**
189
194
* Limits the result with the specified `count`.
190
195
*
191
- * @param count The maximum no. of rows to limit to.
192
- * @param foreignTable The foreign table to use (for foreign columns).
196
+ * @param { number } count The maximum no. of rows to limit to.
197
+ * @param { string? } object.foreignTable The foreign table to use (for foreign columns).
193
198
*/
194
199
limit (
195
200
count : number ,
@@ -203,9 +208,9 @@ class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
203
208
/**
204
209
* Limits the result to rows within the specified range, inclusive.
205
210
*
206
- * @param from The starting index from which to limit the result, inclusive.
207
- * @param to The last index to which to limit the result, inclusive.
208
- * @param foreignTable The foreign table to use (for foreign columns).
211
+ * @param { number } from The starting index from which to limit the result, inclusive.
212
+ * @param { number } to The last index to which to limit the result, inclusive.
213
+ * @param { string? } foreignTable The foreign table to use (for foreign columns).
209
214
*/
210
215
range (
211
216
from : number ,
@@ -264,9 +269,9 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
264
269
/**
265
270
* Finds all rows which doesn't satisfy the filter.
266
271
*
267
- * @param column The column to filter on.
268
- * @param operator The operator to filter with.
269
- * @param value The value to filter with.
272
+ * @param { T } column The column to filter on.
273
+ * @param { FilterOperator } operator The operator to filter with.
274
+ * @param { any } value The value to filter with.
270
275
*/
271
276
not ( column : keyof T , operator : FilterOperator , value : any ) : this {
272
277
this . url . searchParams . append ( `${ column } ` , `not.${ operator } .${ value } ` )
@@ -276,7 +281,7 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
276
281
/**
277
282
* Finds all rows satisfying at least one of the filters.
278
283
*
279
- * @param filters The filters to use, separated by commas.
284
+ * @param { string } filters The filters to use, separated by commas.
280
285
*/
281
286
or ( filters : string ) : this {
282
287
this . url . searchParams . append ( 'or' , `(${ filters } )` )
@@ -287,8 +292,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
287
292
* Finds all rows whose value on the stated `column` exactly matches the
288
293
* specified `value`.
289
294
*
290
- * @param column The column to filter on.
291
- * @param value The value to filter with.
295
+ * @param { T } column The column to filter on.
296
+ * @param { T[] } value The value to filter with.
292
297
*/
293
298
eq ( column : keyof T , value : T [ keyof T ] ) : this {
294
299
this . url . searchParams . append ( `${ column } ` , `eq.${ value } ` )
@@ -299,8 +304,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
299
304
* Finds all rows whose value on the stated `column` doesn't match the
300
305
* specified `value`.
301
306
*
302
- * @param column The column to filter on.
303
- * @param value The value to filter with.
307
+ * @param { T } column The column to filter on.
308
+ * @param { T[] } value The value to filter with.
304
309
*/
305
310
neq ( column : keyof T , value : T [ keyof T ] ) : this {
306
311
this . url . searchParams . append ( `${ column } ` , `neq.${ value } ` )
@@ -311,8 +316,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
311
316
* Finds all rows whose value on the stated `column` is greater than the
312
317
* specified `value`.
313
318
*
314
- * @param column The column to filter on.
315
- * @param value The value to filter with.
319
+ * @param { T } column The column to filter on.
320
+ * @param { T[] } value The value to filter with.
316
321
*/
317
322
gt ( column : keyof T , value : T [ keyof T ] ) : this {
318
323
this . url . searchParams . append ( `${ column } ` , `gt.${ value } ` )
@@ -323,8 +328,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
323
328
* Finds all rows whose value on the stated `column` is greater than or
324
329
* equal to the specified `value`.
325
330
*
326
- * @param column The column to filter on.
327
- * @param value The value to filter with.
331
+ * @param { T } column The column to filter on.
332
+ * @param { T[] } value The value to filter with.
328
333
*/
329
334
gte ( column : keyof T , value : T [ keyof T ] ) : this {
330
335
this . url . searchParams . append ( `${ column } ` , `gte.${ value } ` )
@@ -335,8 +340,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
335
340
* Finds all rows whose value on the stated `column` is less than the
336
341
* specified `value`.
337
342
*
338
- * @param column The column to filter on.
339
- * @param value The value to filter with.
343
+ * @param { T } column The column to filter on.
344
+ * @param { T[] } value The value to filter with.
340
345
*/
341
346
lt ( column : keyof T , value : T [ keyof T ] ) : this {
342
347
this . url . searchParams . append ( `${ column } ` , `lt.${ value } ` )
@@ -347,8 +352,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
347
352
* Finds all rows whose value on the stated `column` is less than or equal
348
353
* to the specified `value`.
349
354
*
350
- * @param column The column to filter on.
351
- * @param value The value to filter with.
355
+ * @param { T } column The column to filter on.
356
+ * @param { T[] } value The value to filter with.
352
357
*/
353
358
lte ( column : keyof T , value : T [ keyof T ] ) : this {
354
359
this . url . searchParams . append ( `${ column } ` , `lte.${ value } ` )
@@ -359,8 +364,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
359
364
* Finds all rows whose value in the stated `column` matches the supplied
360
365
* `pattern` (case sensitive).
361
366
*
362
- * @param column The column to filter on.
363
- * @param pattern The pattern to filter with.
367
+ * @param { T } column The column to filter on.
368
+ * @param { string } pattern The pattern to filter with.
364
369
*/
365
370
like ( column : keyof T , pattern : string ) : this {
366
371
this . url . searchParams . append ( `${ column } ` , `like.${ pattern } ` )
@@ -371,8 +376,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
371
376
* Finds all rows whose value in the stated `column` matches the supplied
372
377
* `pattern` (case insensitive).
373
378
*
374
- * @param column The column to filter on.
375
- * @param pattern The pattern to filter with.
379
+ * @param { T } column The column to filter on.
380
+ * @param { string } pattern The pattern to filter with.
376
381
*/
377
382
ilike ( column : keyof T , pattern : string ) : this {
378
383
this . url . searchParams . append ( `${ column } ` , `ilike.${ pattern } ` )
@@ -383,8 +388,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
383
388
* A check for exact equality (null, true, false), finds all rows whose
384
389
* value on the stated `column` exactly match the specified `value`.
385
390
*
386
- * @param column The column to filter on.
387
- * @param value The value to filter with.
391
+ * @param { T } column The column to filter on.
392
+ * @param { boolean|null } value The value to filter with.
388
393
*/
389
394
is ( column : keyof T , value : boolean | null ) : this {
390
395
this . url . searchParams . append ( `${ column } ` , `is.${ value } ` )
@@ -395,8 +400,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
395
400
* Finds all rows whose value on the stated `column` is found on the
396
401
* specified `values`.
397
402
*
398
- * @param column The column to filter on.
399
- * @param values The values to filter with.
403
+ * @param { T } column The column to filter on.
404
+ * @param { T[] } values The values to filter with.
400
405
*/
401
406
in ( column : keyof T , values : T [ keyof T ] [ ] ) : this {
402
407
this . url . searchParams . append ( `${ column } ` , `in.(${ cleanFilterArray ( values ) } )` )
@@ -407,8 +412,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
407
412
* Finds all rows whose json, array, or range value on the stated `column`
408
413
* contains the values specified in `value`.
409
414
*
410
- * @param column The column to filter on.
411
- * @param value The value to filter with.
415
+ * @param { T } column The column to filter on.
416
+ * @param { string|T[]|object } value The value to filter with.
412
417
*/
413
418
cs ( column : keyof T , value : string | T [ keyof T ] [ ] | object ) : this {
414
419
if ( typeof value === 'string' ) {
@@ -429,8 +434,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
429
434
* Finds all rows whose json, array, or range value on the stated `column` is
430
435
* contained by the specified `value`.
431
436
*
432
- * @param column The column to filter on.
433
- * @param value The value to filter with.
437
+ * @param { T } column The column to filter on.
438
+ * @param { string|T[]|object } value The value to filter with.
434
439
*/
435
440
cd ( column : keyof T , value : string | T [ keyof T ] [ ] | object ) : this {
436
441
if ( typeof value === 'string' ) {
@@ -450,8 +455,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
450
455
* Finds all rows whose range value on the stated `column` is strictly to the
451
456
* left of the specified `range`.
452
457
*
453
- * @param column The column to filter on.
454
- * @param range The range to filter with.
458
+ * @param { T } column The column to filter on.
459
+ * @param { string } range The range to filter with.
455
460
*/
456
461
sl ( column : keyof T , range : string ) : this {
457
462
this . url . searchParams . append ( `${ column } ` , `sl.${ range } ` )
@@ -462,8 +467,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
462
467
* Finds all rows whose range value on the stated `column` is strictly to
463
468
* the right of the specified `range`.
464
469
*
465
- * @param column The column to filter on.
466
- * @param range The range to filter with.
470
+ * @param { T } column The column to filter on.
471
+ * @param { string } range The range to filter with.
467
472
*/
468
473
sr ( column : keyof T , range : string ) : this {
469
474
this . url . searchParams . append ( `${ column } ` , `sr.${ range } ` )
@@ -474,8 +479,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
474
479
* Finds all rows whose range value on the stated `column` does not extend
475
480
* to the left of the specified `range`.
476
481
*
477
- * @param column The column to filter on.
478
- * @param range The range to filter with.
482
+ * @param { T } column The column to filter on.
483
+ * @param { string } range The range to filter with.
479
484
*/
480
485
nxl ( column : keyof T , range : string ) : this {
481
486
this . url . searchParams . append ( `${ column } ` , `nxl.${ range } ` )
@@ -486,8 +491,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
486
491
* Finds all rows whose range value on the stated `column` does not extend
487
492
* to the right of the specified `range`.
488
493
*
489
- * @param column The column to filter on.
490
- * @param range The range to filter with.
494
+ * @param { T } column The column to filter on.
495
+ * @param { string } range The range to filter with.
491
496
*/
492
497
nxr ( column : keyof T , range : string ) : this {
493
498
this . url . searchParams . append ( `${ column } ` , `nxr.${ range } ` )
@@ -498,8 +503,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
498
503
* Finds all rows whose range value on the stated `column` is adjacent to
499
504
* the specified `range`.
500
505
*
501
- * @param column The column to filter on.
502
- * @param range The range to filter with.
506
+ * @param { T } column The column to filter on.
507
+ * @param { string } range The range to filter with.
503
508
*/
504
509
adj ( column : keyof T , range : string ) : this {
505
510
this . url . searchParams . append ( `${ column } ` , `adj.${ range } ` )
@@ -510,8 +515,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
510
515
* Finds all rows whose array or range value on the stated `column` is
511
516
* contained by the specified `value`.
512
517
*
513
- * @param column The column to filter on.
514
- * @param value The value to filter with.
518
+ * @param { T } column The column to filter on.
519
+ * @param { string|T[] } value The value to filter with.
515
520
*/
516
521
ov ( column : keyof T , value : string | T [ keyof T ] [ ] ) : this {
517
522
if ( typeof value === 'string' ) {
@@ -528,9 +533,9 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
528
533
* Finds all rows whose tsvector value on the stated `column` matches
529
534
* to_tsquery(`query`).
530
535
*
531
- * @param column The column to filter on.
532
- * @param query The Postgres tsquery string to filter with.
533
- * @param config The text search configuration to use.
536
+ * @param { T } column The column to filter on.
537
+ * @param { string } query The Postgres tsquery string to filter with.
538
+ * @param { string? } config The text search configuration to use.
534
539
*/
535
540
fts ( column : keyof T , query : string , { config } : { config ?: string } = { } ) : this {
536
541
const configPart = typeof config === 'undefined' ? '' : `(${ config } )`
@@ -542,8 +547,8 @@ class PostgrestFilterBuilder<T> extends PostgrestTransformBuilder<T> {
542
547
* Finds all rows whose tsvector value on the stated `column` matches
543
548
* plainto_tsquery(`query`).
544
549
*
545
- * @param column The column to filter on.
546
- * @param query The Postgres tsquery string to filter with.
550
+ * @param { * } column The column to filter on.
551
+ * @param { } query The Postgres tsquery string to filter with.
547
552
* @param config The text search configuration to use.
548
553
*/
549
554
plfts ( column : keyof T , query : string , { config } : { config ?: string } = { } ) : this {
0 commit comments