@@ -113,24 +113,31 @@ export default class PostgrestQueryBuilder<
113
113
*
114
114
* `"estimated"`: Uses exact count for low numbers and planned count for high
115
115
* numbers.
116
+ *
117
+ * @param options.defaultToNull - Make missing fields default to `null`.
118
+ * Otherwise, use the default value for the column.
116
119
*/
117
120
insert < Row extends Relation extends { Insert : unknown } ? Relation [ 'Insert' ] : never > (
118
121
values : Row | Row [ ] ,
119
122
{
120
123
count,
124
+ defaultToNull = true ,
121
125
} : {
122
126
count ?: 'exact' | 'planned' | 'estimated'
127
+ defaultToNull ?: boolean
123
128
} = { }
124
129
) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null > {
125
130
const method = 'POST'
126
131
127
132
const prefersHeaders = [ ]
128
- const body = values
133
+ if ( this . headers [ 'Prefer' ] ) {
134
+ prefersHeaders . push ( this . headers [ 'Prefer' ] )
135
+ }
129
136
if ( count ) {
130
137
prefersHeaders . push ( `count=${ count } ` )
131
138
}
132
- if ( this . headers [ 'Prefer' ] ) {
133
- prefersHeaders . unshift ( this . headers [ 'Prefer' ] )
139
+ if ( ! defaultToNull ) {
140
+ prefersHeaders . push ( 'missing=default' )
134
141
}
135
142
this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
136
143
@@ -147,7 +154,7 @@ export default class PostgrestQueryBuilder<
147
154
url : this . url ,
148
155
headers : this . headers ,
149
156
schema : this . schema ,
150
- body,
157
+ body : values ,
151
158
fetch : this . fetch ,
152
159
allowEmpty : false ,
153
160
} as unknown as PostgrestBuilder < null > )
@@ -185,30 +192,37 @@ export default class PostgrestQueryBuilder<
185
192
*
186
193
* `"estimated"`: Uses exact count for low numbers and planned count for high
187
194
* numbers.
195
+ *
196
+ * @param options.defaultToNull - Make missing fields default to `null`.
197
+ * Otherwise, use the default value for the column.
188
198
*/
189
199
upsert < Row extends Relation extends { Insert : unknown } ? Relation [ 'Insert' ] : never > (
190
200
values : Row | Row [ ] ,
191
201
{
192
202
onConflict,
193
203
ignoreDuplicates = false ,
194
204
count,
205
+ defaultToNull = true ,
195
206
} : {
196
207
onConflict ?: string
197
208
ignoreDuplicates ?: boolean
198
209
count ?: 'exact' | 'planned' | 'estimated'
210
+ defaultToNull ?: boolean
199
211
} = { }
200
212
) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null > {
201
213
const method = 'POST'
202
214
203
215
const prefersHeaders = [ `resolution=${ ignoreDuplicates ? 'ignore' : 'merge' } -duplicates` ]
204
216
205
217
if ( onConflict !== undefined ) this . url . searchParams . set ( 'on_conflict' , onConflict )
206
- const body = values
218
+ if ( this . headers [ 'Prefer' ] ) {
219
+ prefersHeaders . push ( this . headers [ 'Prefer' ] )
220
+ }
207
221
if ( count ) {
208
222
prefersHeaders . push ( `count=${ count } ` )
209
223
}
210
- if ( this . headers [ 'Prefer' ] ) {
211
- prefersHeaders . unshift ( this . headers [ 'Prefer' ] )
224
+ if ( ! defaultToNull ) {
225
+ prefersHeaders . push ( 'missing=default' )
212
226
}
213
227
this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
214
228
@@ -217,7 +231,7 @@ export default class PostgrestQueryBuilder<
217
231
url : this . url ,
218
232
headers : this . headers ,
219
233
schema : this . schema ,
220
- body,
234
+ body : values ,
221
235
fetch : this . fetch ,
222
236
allowEmpty : false ,
223
237
} as unknown as PostgrestBuilder < null > )
@@ -254,21 +268,20 @@ export default class PostgrestQueryBuilder<
254
268
) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null > {
255
269
const method = 'PATCH'
256
270
const prefersHeaders = [ ]
257
- const body = values
271
+ if ( this . headers [ 'Prefer' ] ) {
272
+ prefersHeaders . push ( this . headers [ 'Prefer' ] )
273
+ }
258
274
if ( count ) {
259
275
prefersHeaders . push ( `count=${ count } ` )
260
276
}
261
- if ( this . headers [ 'Prefer' ] ) {
262
- prefersHeaders . unshift ( this . headers [ 'Prefer' ] )
263
- }
264
277
this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
265
278
266
279
return new PostgrestFilterBuilder ( {
267
280
method,
268
281
url : this . url ,
269
282
headers : this . headers ,
270
283
schema : this . schema ,
271
- body,
284
+ body : values ,
272
285
fetch : this . fetch ,
273
286
allowEmpty : false ,
274
287
} as unknown as PostgrestBuilder < null > )
0 commit comments