-
Notifications
You must be signed in to change notification settings - Fork 733
Expand file tree
/
Copy pathPostgrestQueryBuilder.ts
More file actions
561 lines (535 loc) · 14.8 KB
/
Copy pathPostgrestQueryBuilder.ts
File metadata and controls
561 lines (535 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import PostgrestFilterBuilder from './PostgrestFilterBuilder'
import { GetResult } from './select-query-parser/result'
import { SelectItem, serializeSelectSpec } from './select-query-parser/select-builder'
import {
ClientServerOptions,
Fetch,
GenericSchema,
GenericTable,
GenericView,
} from './types/common/common'
export default class PostgrestQueryBuilder<
ClientOptions extends ClientServerOptions,
Schema extends GenericSchema,
Relation extends GenericTable | GenericView,
RelationName = unknown,
Relationships = Relation extends { Relationships: infer R } ? R : unknown,
> {
url: URL
headers: Headers
schema?: string
signal?: AbortSignal
fetch?: Fetch
urlLengthLimit: number
/**
* Creates a query builder scoped to a Postgres table or view.
*
* @example
* ```ts
* import PostgrestQueryBuilder from '@supabase/postgrest-js'
*
* const query = new PostgrestQueryBuilder(
* new URL('https://xyzcompany.supabase.co/rest/v1/users'),
* { headers: { apikey: 'public-anon-key' } }
* )
* ```
*/
constructor(
url: URL,
{
headers = {},
schema,
fetch,
urlLengthLimit = 8000,
}: {
headers?: HeadersInit
schema?: string
fetch?: Fetch
urlLengthLimit?: number
}
) {
this.url = url
this.headers = new Headers(headers)
this.schema = schema
this.fetch = fetch
this.urlLengthLimit = urlLengthLimit
}
/**
* Clone URL and headers to prevent shared state between operations.
*/
private cloneRequestState(): { url: URL; headers: Headers } {
return {
url: new URL(this.url.toString()),
headers: new Headers(this.headers),
}
}
/**
* Perform a SELECT query on the table or view.
*
* @param columns - The columns to retrieve. Can be:
* - A string with comma-separated column names (e.g., `'id, name, email'`)
* - An array of column names and/or field specs (e.g., `['id', { column: 'name', as: 'display_name' }]`)
*
* @param options - Named parameters
*
* @param options.head - When set to `true`, `data` will not be returned.
* Useful if you only need the count.
*
* @param options.count - Count algorithm to use to count rows in the table or view.
*
* `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the
* hood.
*
* `"planned"`: Approximated but fast count algorithm. Uses the Postgres
* statistics under the hood.
*
* `"estimated"`: Uses exact count for low numbers and planned count for high
* numbers.
*
* @remarks
* When using `count` with `.range()` or `.limit()`, the returned `count` is the total number of rows
* that match your filters, not the number of rows in the current page. Use this to build pagination UI.
*
* @example String-based select
* ```ts
* .select('id, name, email')
* .select('posts(id, title)')
* ```
*
* @example Array-based select (provides IDE autocomplete)
* ```ts
* .select(['id', 'name', 'email'])
* .select(['id', { relation: 'posts', select: ['id', 'title'] }])
* .select([{ column: 'username', as: 'display_name' }])
* ```
*/
select<
Query extends string = '*',
ResultOne = GetResult<
Schema,
Relation['Row'],
RelationName,
Relationships,
Query,
ClientOptions
>,
>(
columns?: Query | SelectItem[],
options?: {
head?: boolean
count?: 'exact' | 'planned' | 'estimated'
}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Relation['Row'],
ResultOne[],
RelationName,
Relationships,
'GET'
> {
const { head = false, count } = options ?? {}
const method = head ? 'HEAD' : 'GET'
// Serialize array-based select specs to string
let columnsString: string
if (columns === undefined) {
columnsString = '*'
} else if (Array.isArray(columns)) {
columnsString = serializeSelectSpec(columns)
} else {
columnsString = columns
}
// Remove whitespaces except when quoted
let quoted = false
const cleanedColumns = columnsString
.split('')
.map((c) => {
if (/\s/.test(c) && !quoted) {
return ''
}
if (c === '"') {
quoted = !quoted
}
return c
})
.join('')
const { url, headers } = this.cloneRequestState()
url.searchParams.set('select', cleanedColumns)
if (count) {
headers.append('Prefer', `count=${count}`)
}
return new PostgrestFilterBuilder({
method,
url,
headers,
schema: this.schema,
fetch: this.fetch,
urlLengthLimit: this.urlLengthLimit,
})
}
// TODO(v3): Make `defaultToNull` consistent for both single & bulk inserts.
insert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
values: Row,
options?: {
count?: 'exact' | 'planned' | 'estimated'
}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Relation['Row'],
null,
RelationName,
Relationships,
'POST'
>
insert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
values: Row[],
options?: {
count?: 'exact' | 'planned' | 'estimated'
defaultToNull?: boolean
}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Relation['Row'],
null,
RelationName,
Relationships,
'POST'
>
/**
* Perform an INSERT into the table or view.
*
* By default, inserted rows are not returned. To return it, chain the call
* with `.select()`.
*
* @param values - The values to insert. Pass an object to insert a single row
* or an array to insert multiple rows.
*
* @param options - Named parameters
*
* @param options.count - Count algorithm to use to count inserted rows.
*
* `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the
* hood.
*
* `"planned"`: Approximated but fast count algorithm. Uses the Postgres
* statistics under the hood.
*
* `"estimated"`: Uses exact count for low numbers and planned count for high
* numbers.
*
* @param options.defaultToNull - Make missing fields default to `null`.
* Otherwise, use the default value for the column. Only applies for bulk
* inserts.
*/
insert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
values: Row | Row[],
{
count,
defaultToNull = true,
}: {
count?: 'exact' | 'planned' | 'estimated'
defaultToNull?: boolean
} = {}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Relation['Row'],
null,
RelationName,
Relationships,
'POST'
> {
const method = 'POST'
const { url, headers } = this.cloneRequestState()
if (count) {
headers.append('Prefer', `count=${count}`)
}
if (!defaultToNull) {
headers.append('Prefer', `missing=default`)
}
if (Array.isArray(values)) {
const columns = values.reduce((acc, x) => acc.concat(Object.keys(x)), [] as string[])
if (columns.length > 0) {
const uniqueColumns = [...new Set(columns)].map((column) => `"${column}"`)
url.searchParams.set('columns', uniqueColumns.join(','))
}
}
return new PostgrestFilterBuilder({
method,
url,
headers,
schema: this.schema,
body: values,
fetch: this.fetch ?? fetch,
urlLengthLimit: this.urlLengthLimit,
})
}
// TODO(v3): Make `defaultToNull` consistent for both single & bulk upserts.
upsert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
values: Row,
options?: {
onConflict?: string
ignoreDuplicates?: boolean
count?: 'exact' | 'planned' | 'estimated'
}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Relation['Row'],
null,
RelationName,
Relationships,
'POST'
>
upsert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
values: Row[],
options?: {
onConflict?: string
ignoreDuplicates?: boolean
count?: 'exact' | 'planned' | 'estimated'
defaultToNull?: boolean
}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Relation['Row'],
null,
RelationName,
Relationships,
'POST'
>
/**
* Perform an UPSERT on the table or view. Depending on the column(s) passed
* to `onConflict`, `.upsert()` allows you to perform the equivalent of
* `.insert()` if a row with the corresponding `onConflict` columns doesn't
* exist, or if it does exist, perform an alternative action depending on
* `ignoreDuplicates`.
*
* By default, upserted rows are not returned. To return it, chain the call
* with `.select()`.
*
* @param values - The values to upsert with. Pass an object to upsert a
* single row or an array to upsert multiple rows.
*
* @param options - Named parameters
*
* @param options.onConflict - Comma-separated UNIQUE column(s) to specify how
* duplicate rows are determined. Two rows are duplicates if all the
* `onConflict` columns are equal.
*
* @param options.ignoreDuplicates - If `true`, duplicate rows are ignored. If
* `false`, duplicate rows are merged with existing rows.
*
* @param options.count - Count algorithm to use to count upserted rows.
*
* `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the
* hood.
*
* `"planned"`: Approximated but fast count algorithm. Uses the Postgres
* statistics under the hood.
*
* `"estimated"`: Uses exact count for low numbers and planned count for high
* numbers.
*
* @param options.defaultToNull - Make missing fields default to `null`.
* Otherwise, use the default value for the column. This only applies when
* inserting new rows, not when merging with existing rows under
* `ignoreDuplicates: false`. This also only applies when doing bulk upserts.
*
* @example Upsert a single row using a unique key
* ```ts
* // Upserting a single row, overwriting based on the 'username' unique column
* const { data, error } = await supabase
* .from('users')
* .upsert({ username: 'supabot' }, { onConflict: 'username' })
*
* // Example response:
* // {
* // data: [
* // { id: 4, message: 'bar', username: 'supabot' }
* // ],
* // error: null
* // }
* ```
*
* @example Upsert with conflict resolution and exact row counting
* ```ts
* // Upserting and returning exact count
* const { data, error, count } = await supabase
* .from('users')
* .upsert(
* {
* id: 3,
* message: 'foo',
* username: 'supabot'
* },
* {
* onConflict: 'username',
* count: 'exact'
* }
* )
*
* // Example response:
* // {
* // data: [
* // {
* // id: 42,
* // handle: "saoirse",
* // display_name: "Saoirse"
* // }
* // ],
* // count: 1,
* // error: null
* // }
* ```
*/
upsert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
values: Row | Row[],
{
onConflict,
ignoreDuplicates = false,
count,
defaultToNull = true,
}: {
onConflict?: string
ignoreDuplicates?: boolean
count?: 'exact' | 'planned' | 'estimated'
defaultToNull?: boolean
} = {}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Relation['Row'],
null,
RelationName,
Relationships,
'POST'
> {
const method = 'POST'
const { url, headers } = this.cloneRequestState()
headers.append('Prefer', `resolution=${ignoreDuplicates ? 'ignore' : 'merge'}-duplicates`)
if (onConflict !== undefined) url.searchParams.set('on_conflict', onConflict)
if (count) {
headers.append('Prefer', `count=${count}`)
}
if (!defaultToNull) {
headers.append('Prefer', 'missing=default')
}
if (Array.isArray(values)) {
const columns = values.reduce((acc, x) => acc.concat(Object.keys(x)), [] as string[])
if (columns.length > 0) {
const uniqueColumns = [...new Set(columns)].map((column) => `"${column}"`)
url.searchParams.set('columns', uniqueColumns.join(','))
}
}
return new PostgrestFilterBuilder({
method,
url,
headers,
schema: this.schema,
body: values,
fetch: this.fetch ?? fetch,
urlLengthLimit: this.urlLengthLimit,
})
}
/**
* Perform an UPDATE on the table or view.
*
* By default, updated rows are not returned. To return it, chain the call
* with `.select()` after filters.
*
* @param values - The values to update with
*
* @param options - Named parameters
*
* @param options.count - Count algorithm to use to count updated rows.
*
* `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the
* hood.
*
* `"planned"`: Approximated but fast count algorithm. Uses the Postgres
* statistics under the hood.
*
* `"estimated"`: Uses exact count for low numbers and planned count for high
* numbers.
*/
update<Row extends Relation extends { Update: unknown } ? Relation['Update'] : never>(
values: Row,
{
count,
}: {
count?: 'exact' | 'planned' | 'estimated'
} = {}
): PostgrestFilterBuilder<
ClientOptions,
Schema,
Relation['Row'],
null,
RelationName,
Relationships,
'PATCH'
> {
const method = 'PATCH'
const { url, headers } = this.cloneRequestState()
if (count) {
headers.append('Prefer', `count=${count}`)
}
return new PostgrestFilterBuilder({
method,
url,
headers,
schema: this.schema,
body: values,
fetch: this.fetch ?? fetch,
urlLengthLimit: this.urlLengthLimit,
})
}
/**
* Perform a DELETE on the table or view.
*
* By default, deleted rows are not returned. To return it, chain the call
* with `.select()` after filters.
*
* @param options - Named parameters
*
* @param options.count - Count algorithm to use to count deleted rows.
*
* `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the
* hood.
*
* `"planned"`: Approximated but fast count algorithm. Uses the Postgres
* statistics under the hood.
*
* `"estimated"`: Uses exact count for low numbers and planned count for high
* numbers.
*/
delete({
count,
}: {
count?: 'exact' | 'planned' | 'estimated'
} = {}): PostgrestFilterBuilder<
ClientOptions,
Schema,
Relation['Row'],
null,
RelationName,
Relationships,
'DELETE'
> {
const method = 'DELETE'
const { url, headers } = this.cloneRequestState()
if (count) {
headers.append('Prefer', `count=${count}`)
}
return new PostgrestFilterBuilder({
method,
url,
headers,
schema: this.schema,
fetch: this.fetch ?? fetch,
urlLengthLimit: this.urlLengthLimit,
})
}
}