@@ -58,10 +58,28 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
5858 * Performs an INSERT into the table.
5959 *
6060 * @param values The values to insert.
61- * @param upsert If `true`, performs an UPSERT.
62- * @param onConflict By specifying the `on_conflict` query parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint.
6361 * @param returning By default the new record is returned. Set this to 'minimal' if you don't need this value.
62+ * @param count Count algorithm to use to count rows in a table.
6463 */
64+ insert (
65+ values : Partial < T > | Partial < T > [ ] ,
66+ options ?: {
67+ returning ?: 'minimal' | 'representation'
68+ count ?: null | 'exact' | 'planned' | 'estimated'
69+ }
70+ ) : PostgrestFilterBuilder < T >
71+ /**
72+ * @deprecated Use `upsert()` instead.
73+ */
74+ insert (
75+ values : Partial < T > | Partial < T > [ ] ,
76+ options ?: {
77+ upsert ?: boolean
78+ onConflict ?: string
79+ returning ?: 'minimal' | 'representation'
80+ count ?: null | 'exact' | 'planned' | 'estimated'
81+ }
82+ ) : PostgrestFilterBuilder < T >
6583 insert (
6684 values : Partial < T > | Partial < T > [ ] ,
6785 {
@@ -78,18 +96,52 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
7896 ) : PostgrestFilterBuilder < T > {
7997 this . method = 'POST'
8098
81- let prefersHeaders = [ ]
82- prefersHeaders . push ( `return=${ returning } ` )
99+ const prefersHeaders = [ `return=${ returning } ` ]
83100 if ( upsert ) prefersHeaders . push ( 'resolution=merge-duplicates' )
84101
85102 if ( upsert && onConflict !== undefined ) this . url . searchParams . set ( 'on_conflict' , onConflict )
86103 this . body = values
87104 if ( count ) {
88105 prefersHeaders . push ( `count=${ count } ` )
89106 }
90-
107+
91108 this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
92-
109+
110+ return new PostgrestFilterBuilder ( this )
111+ }
112+
113+ /**
114+ * Performs an UPSERT into the table.
115+ *
116+ * @param values The values to insert.
117+ * @param onConflict By specifying the `on_conflict` query parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint.
118+ * @param returning By default the new record is returned. Set this to 'minimal' if you don't need this value.
119+ * @param count Count algorithm to use to count rows in a table.
120+ */
121+ upsert (
122+ values : Partial < T > | Partial < T > [ ] ,
123+ {
124+ onConflict,
125+ returning = 'representation' ,
126+ count = null ,
127+ } : {
128+ onConflict ?: string
129+ returning ?: 'minimal' | 'representation'
130+ count ?: null | 'exact' | 'planned' | 'estimated'
131+ } = { }
132+ ) : PostgrestFilterBuilder < T > {
133+ this . method = 'POST'
134+
135+ const prefersHeaders = [ 'resolution=merge-duplicates' , `return=${ returning } ` ]
136+
137+ if ( onConflict !== undefined ) this . url . searchParams . set ( 'on_conflict' , onConflict )
138+ this . body = values
139+ if ( count ) {
140+ prefersHeaders . push ( `count=${ count } ` )
141+ }
142+
143+ this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
144+
93145 return new PostgrestFilterBuilder ( this )
94146 }
95147
@@ -98,6 +150,7 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
98150 *
99151 * @param values The values to update.
100152 * @param returning By default the updated record is returned. Set this to 'minimal' if you don't need this value.
153+ * @param count Count algorithm to use to count rows in a table.
101154 */
102155 update (
103156 values : Partial < T > ,
@@ -110,8 +163,7 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
110163 } = { }
111164 ) : PostgrestFilterBuilder < T > {
112165 this . method = 'PATCH'
113- let prefersHeaders = [ ]
114- prefersHeaders . push ( `return=${ returning } ` )
166+ const prefersHeaders = [ `return=${ returning } ` ]
115167 this . body = values
116168 if ( count ) {
117169 prefersHeaders . push ( `count=${ count } ` )
@@ -124,6 +176,7 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
124176 * Performs a DELETE on the table.
125177 *
126178 * @param returning If `true`, return the deleted row(s) in the response.
179+ * @param count Count algorithm to use to count rows in a table.
127180 */
128181 delete ( {
129182 returning = 'representation' ,
@@ -133,8 +186,7 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
133186 count ?: null | 'exact' | 'planned' | 'estimated'
134187 } = { } ) : PostgrestFilterBuilder < T > {
135188 this . method = 'DELETE'
136- let prefersHeaders = [ ]
137- prefersHeaders . push ( `return=${ returning } ` )
189+ const prefersHeaders = [ `return=${ returning } ` ]
138190 if ( count ) {
139191 prefersHeaders . push ( `count=${ count } ` )
140192 }
0 commit comments