@@ -113,24 +113,31 @@ export default class PostgrestQueryBuilder<
113113 *
114114 * `"estimated"`: Uses exact count for low numbers and planned count for high
115115 * numbers.
116+ *
117+ * @param options.defaultToNull - Make missing fields default to `null`.
118+ * Otherwise, use the default value for the column.
116119 */
117120 insert < Row extends Relation extends { Insert : unknown } ? Relation [ 'Insert' ] : never > (
118121 values : Row | Row [ ] ,
119122 {
120123 count,
124+ defaultToNull = true ,
121125 } : {
122126 count ?: 'exact' | 'planned' | 'estimated'
127+ defaultToNull ?: boolean
123128 } = { }
124129 ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null > {
125130 const method = 'POST'
126131
127132 const prefersHeaders = [ ]
128- const body = values
133+ if ( this . headers [ 'Prefer' ] ) {
134+ prefersHeaders . push ( this . headers [ 'Prefer' ] )
135+ }
129136 if ( count ) {
130137 prefersHeaders . push ( `count=${ count } ` )
131138 }
132- if ( this . headers [ 'Prefer' ] ) {
133- prefersHeaders . unshift ( this . headers [ 'Prefer' ] )
139+ if ( ! defaultToNull ) {
140+ prefersHeaders . push ( 'missing=default' )
134141 }
135142 this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
136143
@@ -147,7 +154,7 @@ export default class PostgrestQueryBuilder<
147154 url : this . url ,
148155 headers : this . headers ,
149156 schema : this . schema ,
150- body,
157+ body : values ,
151158 fetch : this . fetch ,
152159 allowEmpty : false ,
153160 } as unknown as PostgrestBuilder < null > )
@@ -185,39 +192,56 @@ export default class PostgrestQueryBuilder<
185192 *
186193 * `"estimated"`: Uses exact count for low numbers and planned count for high
187194 * numbers.
195+ *
196+ * @param options.defaultToNull - Make missing fields default to `null`.
197+ * Otherwise, use the default value for the column. This only applies when
198+ * inserting new rows, not when merging with existing rows under
199+ * `ignoreDuplicates: false`.
188200 */
189201 upsert < Row extends Relation extends { Insert : unknown } ? Relation [ 'Insert' ] : never > (
190202 values : Row | Row [ ] ,
191203 {
192204 onConflict,
193205 ignoreDuplicates = false ,
194206 count,
207+ defaultToNull = true ,
195208 } : {
196209 onConflict ?: string
197210 ignoreDuplicates ?: boolean
198211 count ?: 'exact' | 'planned' | 'estimated'
212+ defaultToNull ?: boolean
199213 } = { }
200214 ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null > {
201215 const method = 'POST'
202216
203217 const prefersHeaders = [ `resolution=${ ignoreDuplicates ? 'ignore' : 'merge' } -duplicates` ]
204218
205219 if ( onConflict !== undefined ) this . url . searchParams . set ( 'on_conflict' , onConflict )
206- const body = values
220+ if ( this . headers [ 'Prefer' ] ) {
221+ prefersHeaders . push ( this . headers [ 'Prefer' ] )
222+ }
207223 if ( count ) {
208224 prefersHeaders . push ( `count=${ count } ` )
209225 }
210- if ( this . headers [ 'Prefer' ] ) {
211- prefersHeaders . unshift ( this . headers [ 'Prefer' ] )
226+ if ( ! defaultToNull ) {
227+ prefersHeaders . push ( 'missing=default' )
212228 }
213229 this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
214230
231+ if ( Array . isArray ( values ) ) {
232+ const columns = values . reduce ( ( acc , x ) => acc . concat ( Object . keys ( x ) ) , [ ] as string [ ] )
233+ if ( columns . length > 0 ) {
234+ const uniqueColumns = [ ...new Set ( columns ) ] . map ( ( column ) => `"${ column } "` )
235+ this . url . searchParams . set ( 'columns' , uniqueColumns . join ( ',' ) )
236+ }
237+ }
238+
215239 return new PostgrestFilterBuilder ( {
216240 method,
217241 url : this . url ,
218242 headers : this . headers ,
219243 schema : this . schema ,
220- body,
244+ body : values ,
221245 fetch : this . fetch ,
222246 allowEmpty : false ,
223247 } as unknown as PostgrestBuilder < null > )
@@ -254,21 +278,20 @@ export default class PostgrestQueryBuilder<
254278 ) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null > {
255279 const method = 'PATCH'
256280 const prefersHeaders = [ ]
257- const body = values
281+ if ( this . headers [ 'Prefer' ] ) {
282+ prefersHeaders . push ( this . headers [ 'Prefer' ] )
283+ }
258284 if ( count ) {
259285 prefersHeaders . push ( `count=${ count } ` )
260286 }
261- if ( this . headers [ 'Prefer' ] ) {
262- prefersHeaders . unshift ( this . headers [ 'Prefer' ] )
263- }
264287 this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
265288
266289 return new PostgrestFilterBuilder ( {
267290 method,
268291 url : this . url ,
269292 headers : this . headers ,
270293 schema : this . schema ,
271- body,
294+ body : values ,
272295 fetch : this . fetch ,
273296 allowEmpty : false ,
274297 } as unknown as PostgrestBuilder < null > )
0 commit comments