Skip to content

Commit 1bb9f82

Browse files
authored
docs(postgrest): add upsert tsdoc examples and fix response/options (#1882)
1 parent 0afbf00 commit 1bb9f82

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

packages/core/postgrest-js/src/PostgrestQueryBuilder.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export default class PostgrestQueryBuilder<
265265
Relationships,
266266
'POST'
267267
>
268-
/**
268+
/**
269269
* Perform an UPSERT on the table or view. Depending on the column(s) passed
270270
* to `onConflict`, `.upsert()` allows you to perform the equivalent of
271271
* `.insert()` if a row with the corresponding `onConflict` columns doesn't
@@ -302,7 +302,56 @@ export default class PostgrestQueryBuilder<
302302
* Otherwise, use the default value for the column. This only applies when
303303
* inserting new rows, not when merging with existing rows under
304304
* `ignoreDuplicates: false`. This also only applies when doing bulk upserts.
305+
*
306+
* @example Upsert a single row using a unique key
307+
* ```ts
308+
* // Upserting a single row, overwriting based on the 'username' unique column
309+
* const { data, error } = await supabase
310+
* .from('users')
311+
* .upsert({ username: 'supabot' }, { onConflict: 'username' })
312+
*
313+
* // Example response:
314+
* // {
315+
* // data: [
316+
* // { id: 4, message: 'bar', username: 'supabot' }
317+
* // ],
318+
* // error: null
319+
* // }
320+
* ```
321+
*
322+
* @example Upsert with conflict resolution and exact row counting
323+
* ```ts
324+
* // Upserting and returning exact count
325+
* const { data, error, count } = await supabase
326+
* .from('users')
327+
* .upsert(
328+
* {
329+
* id: 3,
330+
* message: 'foo',
331+
* username: 'supabot'
332+
* },
333+
* {
334+
* onConflict: 'username',
335+
* count: 'exact'
336+
* }
337+
* )
338+
*
339+
* // Example response:
340+
* // {
341+
* // data: [
342+
* // {
343+
* // id: 42,
344+
* // handle: "saoirse",
345+
* // display_name: "Saoirse"
346+
* // }
347+
* // ],
348+
* // count: 1,
349+
* // error: null
350+
* // }
351+
* ```
305352
*/
353+
354+
306355
upsert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
307356
values: Row | Row[],
308357
{

0 commit comments

Comments
 (0)