@@ -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,39 +192,56 @@ 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. This only applies when
198
+ * inserting new rows, not when merging with existing rows under
199
+ * `ignoreDuplicates: false`.
188
200
*/
189
201
upsert < Row extends Relation extends { Insert : unknown } ? Relation [ 'Insert' ] : never > (
190
202
values : Row | Row [ ] ,
191
203
{
192
204
onConflict,
193
205
ignoreDuplicates = false ,
194
206
count,
207
+ defaultToNull = true ,
195
208
} : {
196
209
onConflict ?: string
197
210
ignoreDuplicates ?: boolean
198
211
count ?: 'exact' | 'planned' | 'estimated'
212
+ defaultToNull ?: boolean
199
213
} = { }
200
214
) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null > {
201
215
const method = 'POST'
202
216
203
217
const prefersHeaders = [ `resolution=${ ignoreDuplicates ? 'ignore' : 'merge' } -duplicates` ]
204
218
205
219
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
+ }
207
223
if ( count ) {
208
224
prefersHeaders . push ( `count=${ count } ` )
209
225
}
210
- if ( this . headers [ 'Prefer' ] ) {
211
- prefersHeaders . unshift ( this . headers [ 'Prefer' ] )
226
+ if ( ! defaultToNull ) {
227
+ prefersHeaders . push ( 'missing=default' )
212
228
}
213
229
this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
214
230
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
+
215
239
return new PostgrestFilterBuilder ( {
216
240
method,
217
241
url : this . url ,
218
242
headers : this . headers ,
219
243
schema : this . schema ,
220
- body,
244
+ body : values ,
221
245
fetch : this . fetch ,
222
246
allowEmpty : false ,
223
247
} as unknown as PostgrestBuilder < null > )
@@ -254,21 +278,20 @@ export default class PostgrestQueryBuilder<
254
278
) : PostgrestFilterBuilder < Schema , Relation [ 'Row' ] , null > {
255
279
const method = 'PATCH'
256
280
const prefersHeaders = [ ]
257
- const body = values
281
+ if ( this . headers [ 'Prefer' ] ) {
282
+ prefersHeaders . push ( this . headers [ 'Prefer' ] )
283
+ }
258
284
if ( count ) {
259
285
prefersHeaders . push ( `count=${ count } ` )
260
286
}
261
- if ( this . headers [ 'Prefer' ] ) {
262
- prefersHeaders . unshift ( this . headers [ 'Prefer' ] )
263
- }
264
287
this . headers [ 'Prefer' ] = prefersHeaders . join ( ',' )
265
288
266
289
return new PostgrestFilterBuilder ( {
267
290
method,
268
291
url : this . url ,
269
292
headers : this . headers ,
270
293
schema : this . schema ,
271
- body,
294
+ body : values ,
272
295
fetch : this . fetch ,
273
296
allowEmpty : false ,
274
297
} as unknown as PostgrestBuilder < null > )
0 commit comments