Skip to content

Commit

Permalink
fix: insert w/ empty body can't have columns param
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed May 24, 2021
1 parent 791dc09 commit e14461c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/lib/PostgrestQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {

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

return new PostgrestFilterBuilder(this)
Expand Down
5 changes: 5 additions & 0 deletions test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,8 @@ test('insert includes columns param', async () => {
const client = postgrest.from('users').insert([{ foo: 1 }, { bar: 2 }])
expect((client as any).url.searchParams.get('columns')).toMatchInlineSnapshot(`"foo,bar"`)
})

test('insert w/ empty body has no columns param', async () => {
const client = postgrest.from('users').insert([{}, {}])
expect((client as any).url.searchParams.has('columns')).toBeFalsy()
})

0 comments on commit e14461c

Please sign in to comment.