Skip to content

Commit 2963d88

Browse files
committed
feat: Prefer: return=minimal by default
BREAKING CHANGE: set `Prefer: return=minimal` by default everywhere We previously set `returning = 'representation'` by default so that inserted/updated/deleted rows are returned. We now change the default so that inserted/updated/deleted rows are NOT returned. To return inserted/updated/deleted rows, call `.select()` at the end of the call chain.
1 parent 7a83c8c commit 2963d88

File tree

4 files changed

+61
-43
lines changed

4 files changed

+61
-43
lines changed

packages/core/postgrest-js/src/PostgrestQueryBuilder.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,19 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
7474
* Performs an INSERT into the table.
7575
*
7676
* @param values The values to insert.
77-
* @param returning By default the new record is returned. Set this to 'minimal' if you don't need this value.
7877
* @param count Count algorithm to use to count rows in a table.
7978
*/
8079
insert(
8180
values: Partial<T> | Partial<T>[],
8281
{
83-
returning = 'representation',
8482
count = null,
8583
}: {
86-
returning?: 'minimal' | 'representation'
8784
count?: null | 'exact' | 'planned' | 'estimated'
8885
} = {}
8986
): PostgrestFilterBuilder<T> {
9087
this.method = 'POST'
9188

92-
const prefersHeaders = [`return=${returning}`]
89+
const prefersHeaders = []
9390
this.body = values
9491
if (count) {
9592
prefersHeaders.push(`count=${count}`)
@@ -114,31 +111,26 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
114111
* Performs an UPSERT into the table.
115112
*
116113
* @param values The values to insert.
117-
* @param onConflict By specifying the `on_conflict` query parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint.
118-
* @param returning By default the new record is returned. Set this to 'minimal' if you don't need this value.
119114
* @param count Count algorithm to use to count rows in a table.
120-
* @param ignoreDuplicates Specifies if duplicate rows should be ignored and not inserted.
115+
* @param options Named parameters.
116+
* @param options.onConflict By specifying the `on_conflict` query parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint.
117+
* @param options.ignoreDuplicates Specifies if duplicate rows should be ignored and not inserted.
121118
*/
122119
upsert(
123120
values: Partial<T> | Partial<T>[],
124121
{
125122
onConflict,
126-
returning = 'representation',
127123
count = null,
128124
ignoreDuplicates = false,
129125
}: {
130126
onConflict?: string
131-
returning?: 'minimal' | 'representation'
132127
count?: null | 'exact' | 'planned' | 'estimated'
133128
ignoreDuplicates?: boolean
134129
} = {}
135130
): PostgrestFilterBuilder<T> {
136131
this.method = 'POST'
137132

138-
const prefersHeaders = [
139-
`resolution=${ignoreDuplicates ? 'ignore' : 'merge'}-duplicates`,
140-
`return=${returning}`,
141-
]
133+
const prefersHeaders = [`resolution=${ignoreDuplicates ? 'ignore' : 'merge'}-duplicates`]
142134

143135
if (onConflict !== undefined) this.url.searchParams.set('on_conflict', onConflict)
144136
this.body = values
@@ -157,21 +149,18 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
157149
* Performs an UPDATE on the table.
158150
*
159151
* @param values The values to update.
160-
* @param returning By default the updated record is returned. Set this to 'minimal' if you don't need this value.
161152
* @param count Count algorithm to use to count rows in a table.
162153
*/
163154
update(
164155
values: Partial<T>,
165156
{
166-
returning = 'representation',
167157
count = null,
168158
}: {
169-
returning?: 'minimal' | 'representation'
170159
count?: null | 'exact' | 'planned' | 'estimated'
171160
} = {}
172161
): PostgrestFilterBuilder<T> {
173162
this.method = 'PATCH'
174-
const prefersHeaders = [`return=${returning}`]
163+
const prefersHeaders = []
175164
this.body = values
176165
if (count) {
177166
prefersHeaders.push(`count=${count}`)
@@ -186,18 +175,15 @@ export default class PostgrestQueryBuilder<T> extends PostgrestBuilder<T> {
186175
/**
187176
* Performs a DELETE on the table.
188177
*
189-
* @param returning If `true`, return the deleted row(s) in the response.
190178
* @param count Count algorithm to use to count rows in a table.
191179
*/
192180
delete({
193-
returning = 'representation',
194181
count = null,
195182
}: {
196-
returning?: 'minimal' | 'representation'
197183
count?: null | 'exact' | 'planned' | 'estimated'
198184
} = {}): PostgrestFilterBuilder<T> {
199185
this.method = 'DELETE'
200-
const prefersHeaders = [`return=${returning}`]
186+
const prefersHeaders = []
201187
if (count) {
202188
prefersHeaders.push(`count=${count}`)
203189
}

packages/core/postgrest-js/src/PostgrestTransformBuilder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export default class PostgrestTransformBuilder<T> extends PostgrestBuilder<T> {
2727
})
2828
.join('')
2929
this.url.searchParams.set('select', cleanedColumns)
30+
if (this.headers['Prefer']) {
31+
this.headers['Prefer'] += ','
32+
}
33+
this.headers['Prefer'] += 'return=representation'
3034
return this
3135
}
3236

packages/core/postgrest-js/test/basic.ts

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,34 @@ test('custom headers', async () => {
2626
describe('custom prefer headers with ', () => {
2727
test('insert', async () => {
2828
const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } })
29-
const postgrestFilterBuilder = postgrest.from('users').insert({ username: 'dragarcia' }) as any
29+
const postgrestFilterBuilder = postgrest
30+
.from('users')
31+
.insert({ username: 'dragarcia' })
32+
.select() as any
3033
expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback')
3134
expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=')
3235
})
3336
test('update', async () => {
3437
const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } })
35-
const postgrestFilterBuilder = postgrest.from('users').update({ username: 'dragarcia' }) as any
38+
const postgrestFilterBuilder = postgrest
39+
.from('users')
40+
.update({ username: 'dragarcia' })
41+
.select() as any
3642
expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback')
3743
expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=')
3844
})
3945
test('upsert', async () => {
4046
const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } })
41-
const postgrestFilterBuilder = postgrest.from('users').upsert({ username: 'dragarcia' }) as any
47+
const postgrestFilterBuilder = postgrest
48+
.from('users')
49+
.upsert({ username: 'dragarcia' })
50+
.select() as any
4251
expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback')
4352
expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=')
4453
})
4554
test('delete', async () => {
4655
const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } })
47-
const postgrestFilterBuilder = postgrest.from('users').delete() as any
56+
const postgrestFilterBuilder = postgrest.from('users').delete().select() as any
4857
expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback')
4958
expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=')
5059
})
@@ -65,13 +74,15 @@ test('on_conflict insert', async () => {
6574
const res = await postgrest
6675
.from('users')
6776
.upsert({ username: 'dragarcia' }, { onConflict: 'username' })
77+
.select()
6878
expect(res).toMatchSnapshot()
6979
})
7080

7181
test('ignoreDuplicates upsert', async () => {
7282
const res = await postgrest
7383
.from('users')
7484
.upsert({ username: 'dragarcia' }, { onConflict: 'username', ignoreDuplicates: true })
85+
.select()
7586
expect(res).toMatchSnapshot()
7687
})
7788

@@ -80,6 +91,7 @@ describe('basic insert, update, delete', () => {
8091
let res = await postgrest
8192
.from('messages')
8293
.insert({ message: 'foo', username: 'supabot', channel_id: 1 })
94+
.select()
8395
expect(res).toMatchSnapshot()
8496

8597
res = await postgrest.from('messages').select()
@@ -90,33 +102,41 @@ describe('basic insert, update, delete', () => {
90102
let res = await postgrest
91103
.from('messages')
92104
.upsert({ id: 3, message: 'foo', username: 'supabot', channel_id: 2 })
105+
.select()
93106
expect(res).toMatchSnapshot()
94107

95108
res = await postgrest.from('messages').select()
96109
expect(res).toMatchSnapshot()
97110
})
98111

99112
test('bulk insert', async () => {
100-
let res = await postgrest.from('messages').insert([
101-
{ message: 'foo', username: 'supabot', channel_id: 1 },
102-
{ message: 'foo', username: 'supabot', channel_id: 1 },
103-
])
113+
let res = await postgrest
114+
.from('messages')
115+
.insert([
116+
{ message: 'foo', username: 'supabot', channel_id: 1 },
117+
{ message: 'foo', username: 'supabot', channel_id: 1 },
118+
])
119+
.select()
104120
expect(res).toMatchSnapshot()
105121

106122
res = await postgrest.from('messages').select()
107123
expect(res).toMatchSnapshot()
108124
})
109125

110126
test('basic update', async () => {
111-
let res = await postgrest.from('messages').update({ channel_id: 2 }).eq('message', 'foo')
127+
let res = await postgrest
128+
.from('messages')
129+
.update({ channel_id: 2 })
130+
.eq('message', 'foo')
131+
.select()
112132
expect(res).toMatchSnapshot()
113133

114134
res = await postgrest.from('messages').select()
115135
expect(res).toMatchSnapshot()
116136
})
117137

118138
test('basic delete', async () => {
119-
let res = await postgrest.from('messages').delete().eq('message', 'foo')
139+
let res = await postgrest.from('messages').delete().eq('message', 'foo').select()
120140
expect(res).toMatchSnapshot()
121141

122142
res = await postgrest.from('messages').select()
@@ -249,9 +269,7 @@ test('allow ordering on JSON column', async () => {
249269
})
250270

251271
test('Prefer: return=minimal', async () => {
252-
const { data } = await postgrest
253-
.from('users')
254-
.insert({ username: 'bar' }, { returning: 'minimal' })
272+
const { data } = await postgrest.from('users').insert({ username: 'bar' })
255273
expect(data).toMatchSnapshot()
256274

257275
await postgrest.from('users').delete().eq('username', 'bar')
@@ -305,6 +323,7 @@ describe("insert, update, delete with count: 'exact'", () => {
305323
let res = await postgrest
306324
.from('messages')
307325
.insert({ message: 'foo', username: 'supabot', channel_id: 1 }, { count: 'exact' })
326+
.select()
308327
expect(res).toMatchSnapshot()
309328

310329
res = await postgrest.from('messages').select()
@@ -315,20 +334,24 @@ describe("insert, update, delete with count: 'exact'", () => {
315334
let res = await postgrest
316335
.from('messages')
317336
.upsert({ id: 3, message: 'foo', username: 'supabot', channel_id: 2 }, { count: 'exact' })
337+
.select()
318338
expect(res).toMatchSnapshot()
319339

320340
res = await postgrest.from('messages').select()
321341
expect(res).toMatchSnapshot()
322342
})
323343

324344
test("bulk insert with count: 'exact'", async () => {
325-
let res = await postgrest.from('messages').insert(
326-
[
327-
{ message: 'foo', username: 'supabot', channel_id: 1 },
328-
{ message: 'foo', username: 'supabot', channel_id: 1 },
329-
],
330-
{ count: 'exact' }
331-
)
345+
let res = await postgrest
346+
.from('messages')
347+
.insert(
348+
[
349+
{ message: 'foo', username: 'supabot', channel_id: 1 },
350+
{ message: 'foo', username: 'supabot', channel_id: 1 },
351+
],
352+
{ count: 'exact' }
353+
)
354+
.select()
332355
expect(res).toMatchSnapshot()
333356

334357
res = await postgrest.from('messages').select()
@@ -340,14 +363,19 @@ describe("insert, update, delete with count: 'exact'", () => {
340363
.from('messages')
341364
.update({ channel_id: 2 }, { count: 'exact' })
342365
.eq('message', 'foo')
366+
.select()
343367
expect(res).toMatchSnapshot()
344368

345369
res = await postgrest.from('messages').select()
346370
expect(res).toMatchSnapshot()
347371
})
348372

349373
test("basic delete count: 'exact'", async () => {
350-
let res = await postgrest.from('messages').delete({ count: 'exact' }).eq('message', 'foo')
374+
let res = await postgrest
375+
.from('messages')
376+
.delete({ count: 'exact' })
377+
.eq('message', 'foo')
378+
.select()
351379
expect(res).toMatchSnapshot()
352380

353381
res = await postgrest.from('messages').select()

packages/core/postgrest-js/test/transforms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test('single', async () => {
3434
})
3535

3636
test('single on insert', async () => {
37-
const res = await postgrest.from('users').insert({ username: 'foo' }).single()
37+
const res = await postgrest.from('users').insert({ username: 'foo' }).select().single()
3838
expect(res).toMatchSnapshot()
3939

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

0 commit comments

Comments
 (0)