Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit c96ccfb

Browse files
authored
Merge pull request #117 from supabase/feat/filter-transform-insert
Allow filters and transforms on insert
2 parents a1ee43b + e9d72c3 commit c96ccfb

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/builder.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fetch from 'cross-fetch'
22

33
/**
44
* Error format
5-
*
5+
*
66
* {@link https://postgrest.org/en/stable/api.html?highlight=options#errors-and-http-status-codes}
77
*/
88
interface PostgrestError {
@@ -12,9 +12,9 @@ interface PostgrestError {
1212
code: string
1313
}
1414

15-
/**
15+
/**
1616
* Response format
17-
*
17+
*
1818
* {@link https://github.com/supabase/supabase-js/issues/32}
1919
*/
2020
interface PostgrestResponse<T> {
@@ -130,14 +130,14 @@ export class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
130130
insert(
131131
values: Partial<T> | Partial<T>[],
132132
{ upsert = false, onConflict }: { upsert?: boolean; onConflict?: string } = {}
133-
): PostgrestBuilder<T> {
133+
): PostgrestFilterBuilder<T> {
134134
this.method = 'POST'
135135
this.headers['Prefer'] = upsert
136136
? 'return=representation,resolution=merge-duplicates'
137137
: 'return=representation'
138138
if (upsert && onConflict !== undefined) this.url.searchParams.set('on_conflict', onConflict)
139139
this.body = values
140-
return this
140+
return new PostgrestFilterBuilder(this)
141141
}
142142

143143
/**

test/__snapshots__/transforms.test.ts.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,25 @@ Object {
169169
"statusText": "OK",
170170
}
171171
`;
172+
173+
exports[`single on insert 1`] = `
174+
Object {
175+
"body": Object {
176+
"age_range": null,
177+
"catchphrase": null,
178+
"data": null,
179+
"status": "ONLINE",
180+
"username": "foo",
181+
},
182+
"data": Object {
183+
"age_range": null,
184+
"catchphrase": null,
185+
"data": null,
186+
"status": "ONLINE",
187+
"username": "foo",
188+
},
189+
"error": null,
190+
"status": 201,
191+
"statusText": "Created",
192+
}
193+
`;

test/transforms.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ test('single', async () => {
2121
const res = await postgrest.from('users').select().limit(1).single()
2222
expect(res).toMatchSnapshot()
2323
})
24+
25+
test('single on insert', async () => {
26+
const res = await postgrest.from('users').insert({ username: 'foo' }).single()
27+
expect(res).toMatchSnapshot()
28+
29+
await postgrest.from('users').delete().eq('username', 'foo')
30+
})

0 commit comments

Comments
 (0)