Skip to content

Commit

Permalink
fix: use ?columns= on upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Apr 17, 2023
1 parent 5ad7562 commit a9c3629
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/PostgrestQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ export default class PostgrestQueryBuilder<
* numbers.
*
* @param options.defaultToNull - Make missing fields default to `null`.
* Otherwise, use the default value for the column.
* Otherwise, use the default value for the column. This only applies when
* inserting new rows, not when merging with existing rows under
* `ignoreDuplicates: false`.
*/
upsert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
values: Row | Row[],
Expand Down Expand Up @@ -226,6 +228,14 @@ export default class PostgrestQueryBuilder<
}
this.headers['Prefer'] = prefersHeaders.join(',')

if (Array.isArray(values)) {
const columns = values.reduce((acc, x) => acc.concat(Object.keys(x)), [] as string[])
if (columns.length > 0) {
const uniqueColumns = [...new Set(columns)].map((column) => `"${column}"`)
this.url.searchParams.set('columns', uniqueColumns.join(','))
}
}

return new PostgrestFilterBuilder({
method,
url: this.url,
Expand Down

0 comments on commit a9c3629

Please sign in to comment.