Skip to content

Commit

Permalink
Merge pull request #117 from supabase/feat/filter-transform-insert
Browse files Browse the repository at this point in the history
Allow filters and transforms on insert
  • Loading branch information
kiwicopple committed Oct 12, 2020
2 parents a1ee43b + e9d72c3 commit c96ccfb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fetch from 'cross-fetch'

/**
* Error format
*
*
* {@link https://postgrest.org/en/stable/api.html?highlight=options#errors-and-http-status-codes}
*/
interface PostgrestError {
Expand All @@ -12,9 +12,9 @@ interface PostgrestError {
code: string
}

/**
/**
* Response format
*
*
* {@link https://github.com/supabase/supabase-js/issues/32}
*/
interface PostgrestResponse<T> {
Expand Down Expand Up @@ -130,14 +130,14 @@ export class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
insert(
values: Partial<T> | Partial<T>[],
{ upsert = false, onConflict }: { upsert?: boolean; onConflict?: string } = {}
): PostgrestBuilder<T> {
): PostgrestFilterBuilder<T> {
this.method = 'POST'
this.headers['Prefer'] = upsert
? 'return=representation,resolution=merge-duplicates'
: 'return=representation'
if (upsert && onConflict !== undefined) this.url.searchParams.set('on_conflict', onConflict)
this.body = values
return this
return new PostgrestFilterBuilder(this)
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/__snapshots__/transforms.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,25 @@ Object {
"statusText": "OK",
}
`;

exports[`single on insert 1`] = `
Object {
"body": Object {
"age_range": null,
"catchphrase": null,
"data": null,
"status": "ONLINE",
"username": "foo",
},
"data": Object {
"age_range": null,
"catchphrase": null,
"data": null,
"status": "ONLINE",
"username": "foo",
},
"error": null,
"status": 201,
"statusText": "Created",
}
`;
7 changes: 7 additions & 0 deletions test/transforms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ test('single', async () => {
const res = await postgrest.from('users').select().limit(1).single()
expect(res).toMatchSnapshot()
})

test('single on insert', async () => {
const res = await postgrest.from('users').insert({ username: 'foo' }).single()
expect(res).toMatchSnapshot()

await postgrest.from('users').delete().eq('username', 'foo')
})

0 comments on commit c96ccfb

Please sign in to comment.