diff --git a/src/PostgrestQueryBuilder.ts b/src/PostgrestQueryBuilder.ts index c91db90f..0d299d5f 100644 --- a/src/PostgrestQueryBuilder.ts +++ b/src/PostgrestQueryBuilder.ts @@ -74,22 +74,19 @@ export default class PostgrestQueryBuilder extends PostgrestBuilder { * Performs an INSERT into the table. * * @param values The values to insert. - * @param returning By default the new record is returned. Set this to 'minimal' if you don't need this value. * @param count Count algorithm to use to count rows in a table. */ insert( values: Partial | Partial[], { - returning = 'representation', count = null, }: { - returning?: 'minimal' | 'representation' count?: null | 'exact' | 'planned' | 'estimated' } = {} ): PostgrestFilterBuilder { this.method = 'POST' - const prefersHeaders = [`return=${returning}`] + const prefersHeaders = [] this.body = values if (count) { prefersHeaders.push(`count=${count}`) @@ -114,31 +111,26 @@ export default class PostgrestQueryBuilder extends PostgrestBuilder { * Performs an UPSERT into the table. * * @param values The values to insert. - * @param onConflict By specifying the `on_conflict` query parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint. - * @param returning By default the new record is returned. Set this to 'minimal' if you don't need this value. * @param count Count algorithm to use to count rows in a table. - * @param ignoreDuplicates Specifies if duplicate rows should be ignored and not inserted. + * @param options Named parameters. + * @param options.onConflict By specifying the `on_conflict` query parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint. + * @param options.ignoreDuplicates Specifies if duplicate rows should be ignored and not inserted. */ upsert( values: Partial | Partial[], { onConflict, - returning = 'representation', count = null, ignoreDuplicates = false, }: { onConflict?: string - returning?: 'minimal' | 'representation' count?: null | 'exact' | 'planned' | 'estimated' ignoreDuplicates?: boolean } = {} ): PostgrestFilterBuilder { this.method = 'POST' - const prefersHeaders = [ - `resolution=${ignoreDuplicates ? 'ignore' : 'merge'}-duplicates`, - `return=${returning}`, - ] + const prefersHeaders = [`resolution=${ignoreDuplicates ? 'ignore' : 'merge'}-duplicates`] if (onConflict !== undefined) this.url.searchParams.set('on_conflict', onConflict) this.body = values @@ -157,21 +149,18 @@ export default class PostgrestQueryBuilder extends PostgrestBuilder { * Performs an UPDATE on the table. * * @param values The values to update. - * @param returning By default the updated record is returned. Set this to 'minimal' if you don't need this value. * @param count Count algorithm to use to count rows in a table. */ update( values: Partial, { - returning = 'representation', count = null, }: { - returning?: 'minimal' | 'representation' count?: null | 'exact' | 'planned' | 'estimated' } = {} ): PostgrestFilterBuilder { this.method = 'PATCH' - const prefersHeaders = [`return=${returning}`] + const prefersHeaders = [] this.body = values if (count) { prefersHeaders.push(`count=${count}`) @@ -186,18 +175,15 @@ export default class PostgrestQueryBuilder extends PostgrestBuilder { /** * Performs a DELETE on the table. * - * @param returning If `true`, return the deleted row(s) in the response. * @param count Count algorithm to use to count rows in a table. */ delete({ - returning = 'representation', count = null, }: { - returning?: 'minimal' | 'representation' count?: null | 'exact' | 'planned' | 'estimated' } = {}): PostgrestFilterBuilder { this.method = 'DELETE' - const prefersHeaders = [`return=${returning}`] + const prefersHeaders = [] if (count) { prefersHeaders.push(`count=${count}`) } diff --git a/src/PostgrestTransformBuilder.ts b/src/PostgrestTransformBuilder.ts index e07edda7..cbe529f2 100644 --- a/src/PostgrestTransformBuilder.ts +++ b/src/PostgrestTransformBuilder.ts @@ -27,6 +27,10 @@ export default class PostgrestTransformBuilder extends PostgrestBuilder { }) .join('') this.url.searchParams.set('select', cleanedColumns) + if (this.headers['Prefer']) { + this.headers['Prefer'] += ',' + } + this.headers['Prefer'] += 'return=representation' return this } diff --git a/test/basic.ts b/test/basic.ts index 50dd634c..f95429e0 100644 --- a/test/basic.ts +++ b/test/basic.ts @@ -26,25 +26,34 @@ test('custom headers', async () => { describe('custom prefer headers with ', () => { test('insert', async () => { const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } }) - const postgrestFilterBuilder = postgrest.from('users').insert({ username: 'dragarcia' }) as any + const postgrestFilterBuilder = postgrest + .from('users') + .insert({ username: 'dragarcia' }) + .select() as any expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback') expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=') }) test('update', async () => { const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } }) - const postgrestFilterBuilder = postgrest.from('users').update({ username: 'dragarcia' }) as any + const postgrestFilterBuilder = postgrest + .from('users') + .update({ username: 'dragarcia' }) + .select() as any expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback') expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=') }) test('upsert', async () => { const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } }) - const postgrestFilterBuilder = postgrest.from('users').upsert({ username: 'dragarcia' }) as any + const postgrestFilterBuilder = postgrest + .from('users') + .upsert({ username: 'dragarcia' }) + .select() as any expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback') expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=') }) test('delete', async () => { const postgrest = new PostgrestClient(REST_URL, { headers: { Prefer: 'tx=rollback' } }) - const postgrestFilterBuilder = postgrest.from('users').delete() as any + const postgrestFilterBuilder = postgrest.from('users').delete().select() as any expect(postgrestFilterBuilder.headers['Prefer']).toContain('tx=rollback') expect(postgrestFilterBuilder.headers['Prefer']).toContain('return=') }) @@ -65,6 +74,7 @@ test('on_conflict insert', async () => { const res = await postgrest .from('users') .upsert({ username: 'dragarcia' }, { onConflict: 'username' }) + .select() expect(res).toMatchSnapshot() }) @@ -72,6 +82,7 @@ test('ignoreDuplicates upsert', async () => { const res = await postgrest .from('users') .upsert({ username: 'dragarcia' }, { onConflict: 'username', ignoreDuplicates: true }) + .select() expect(res).toMatchSnapshot() }) @@ -80,6 +91,7 @@ describe('basic insert, update, delete', () => { let res = await postgrest .from('messages') .insert({ message: 'foo', username: 'supabot', channel_id: 1 }) + .select() expect(res).toMatchSnapshot() res = await postgrest.from('messages').select() @@ -90,6 +102,7 @@ describe('basic insert, update, delete', () => { let res = await postgrest .from('messages') .upsert({ id: 3, message: 'foo', username: 'supabot', channel_id: 2 }) + .select() expect(res).toMatchSnapshot() res = await postgrest.from('messages').select() @@ -97,10 +110,13 @@ describe('basic insert, update, delete', () => { }) test('bulk insert', async () => { - let res = await postgrest.from('messages').insert([ - { message: 'foo', username: 'supabot', channel_id: 1 }, - { message: 'foo', username: 'supabot', channel_id: 1 }, - ]) + let res = await postgrest + .from('messages') + .insert([ + { message: 'foo', username: 'supabot', channel_id: 1 }, + { message: 'foo', username: 'supabot', channel_id: 1 }, + ]) + .select() expect(res).toMatchSnapshot() res = await postgrest.from('messages').select() @@ -108,7 +124,11 @@ describe('basic insert, update, delete', () => { }) test('basic update', async () => { - let res = await postgrest.from('messages').update({ channel_id: 2 }).eq('message', 'foo') + let res = await postgrest + .from('messages') + .update({ channel_id: 2 }) + .eq('message', 'foo') + .select() expect(res).toMatchSnapshot() res = await postgrest.from('messages').select() @@ -116,7 +136,7 @@ describe('basic insert, update, delete', () => { }) test('basic delete', async () => { - let res = await postgrest.from('messages').delete().eq('message', 'foo') + let res = await postgrest.from('messages').delete().eq('message', 'foo').select() expect(res).toMatchSnapshot() res = await postgrest.from('messages').select() @@ -249,9 +269,7 @@ test('allow ordering on JSON column', async () => { }) test('Prefer: return=minimal', async () => { - const { data } = await postgrest - .from('users') - .insert({ username: 'bar' }, { returning: 'minimal' }) + const { data } = await postgrest.from('users').insert({ username: 'bar' }) expect(data).toMatchSnapshot() await postgrest.from('users').delete().eq('username', 'bar') @@ -305,6 +323,7 @@ describe("insert, update, delete with count: 'exact'", () => { let res = await postgrest .from('messages') .insert({ message: 'foo', username: 'supabot', channel_id: 1 }, { count: 'exact' }) + .select() expect(res).toMatchSnapshot() res = await postgrest.from('messages').select() @@ -315,6 +334,7 @@ describe("insert, update, delete with count: 'exact'", () => { let res = await postgrest .from('messages') .upsert({ id: 3, message: 'foo', username: 'supabot', channel_id: 2 }, { count: 'exact' }) + .select() expect(res).toMatchSnapshot() res = await postgrest.from('messages').select() @@ -322,13 +342,16 @@ describe("insert, update, delete with count: 'exact'", () => { }) test("bulk insert with count: 'exact'", async () => { - let res = await postgrest.from('messages').insert( - [ - { message: 'foo', username: 'supabot', channel_id: 1 }, - { message: 'foo', username: 'supabot', channel_id: 1 }, - ], - { count: 'exact' } - ) + let res = await postgrest + .from('messages') + .insert( + [ + { message: 'foo', username: 'supabot', channel_id: 1 }, + { message: 'foo', username: 'supabot', channel_id: 1 }, + ], + { count: 'exact' } + ) + .select() expect(res).toMatchSnapshot() res = await postgrest.from('messages').select() @@ -340,6 +363,7 @@ describe("insert, update, delete with count: 'exact'", () => { .from('messages') .update({ channel_id: 2 }, { count: 'exact' }) .eq('message', 'foo') + .select() expect(res).toMatchSnapshot() res = await postgrest.from('messages').select() @@ -347,7 +371,11 @@ describe("insert, update, delete with count: 'exact'", () => { }) test("basic delete count: 'exact'", async () => { - let res = await postgrest.from('messages').delete({ count: 'exact' }).eq('message', 'foo') + let res = await postgrest + .from('messages') + .delete({ count: 'exact' }) + .eq('message', 'foo') + .select() expect(res).toMatchSnapshot() res = await postgrest.from('messages').select() diff --git a/test/transforms.ts b/test/transforms.ts index edcd3037..8cf6117d 100644 --- a/test/transforms.ts +++ b/test/transforms.ts @@ -34,7 +34,7 @@ test('single', async () => { }) test('single on insert', async () => { - const res = await postgrest.from('users').insert({ username: 'foo' }).single() + const res = await postgrest.from('users').insert({ username: 'foo' }).select().single() expect(res).toMatchSnapshot() await postgrest.from('users').delete().eq('username', 'foo')