From 3956f42eefde026aa0268269ce2ef0e7b319659e Mon Sep 17 00:00:00 2001 From: dshukertjr <18113850+dshukertjr@users.noreply.github.com> Date: Wed, 5 May 2021 14:39:39 +0900 Subject: [PATCH 1/3] Added upsert method to postgrest_query_builder.dart --- lib/src/postgrest_query_builder.dart | 34 +++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/src/postgrest_query_builder.dart b/lib/src/postgrest_query_builder.dart index 1afa6ba..5ef37a6 100644 --- a/lib/src/postgrest_query_builder.dart +++ b/lib/src/postgrest_query_builder.dart @@ -49,20 +49,38 @@ class PostgrestQueryBuilder extends PostgrestBuilder { /// Performs an INSERT into the table. /// - /// When [options] has `upsert` is true, performs an UPSERT. /// ```dart - /// postgrest.from('messages').insert({ message: 'foo', username: 'supabot', channel_id: 1 }) - /// postgrest.from('messages').insert({ id: 3, message: 'foo', username: 'supabot', channel_id: 2 }, { upsert: true }) + /// postgrest.from('messages').insert({ 'message': 'foo', 'username': 'supabot', 'channel_id': 1 }) /// ``` PostgrestBuilder insert( dynamic values, { - bool upsert = false, + @Deprecated('Use `upsert()` method instead') bool upsert = false, + @Deprecated('Use `upsert()` method instead') String? onConflict, + }) { + method = 'POST'; + headers['Prefer'] = + upsert ? 'return=representation,resolution=merge-duplicates' : 'return=representation'; + if (onConflict != null) { + url.queryParameters.addAll({'on_conflict': onConflict}); + } + body = values; + return this; + } + + /// Performs an UPSERT into the table. + /// + /// ```dart + /// postgrest.from('messages').upsert({ 'id': 3, message: 'foo', 'username': 'supabot', 'channel_id': 2 }, { upsert: true }) + /// ``` + PostgrestBuilder upsert( + dynamic values, { String? onConflict, }) { method = 'POST'; - headers['Prefer'] = upsert - ? 'return=representation,resolution=merge-duplicates' - : 'return=representation'; + headers['Prefer'] = 'return=representation,resolution=merge-duplicates'; + if (onConflict != null) { + url.queryParameters.addAll({'on_conflict': onConflict}); + } body = values; return this; } @@ -70,7 +88,7 @@ class PostgrestQueryBuilder extends PostgrestBuilder { /// Performs an UPDATE on the table. /// /// ```dart - /// postgrest.from('messages').update({ channel_id: 2 }).eq('message', 'foo') + /// postgrest.from('messages').update({ 'channel_id': 2 }).eq('message', 'foo') /// ``` PostgrestFilterBuilder update(Map values) { method = 'PATCH'; From 178550f2eab4042c574f27ec5a377a0bdfe024c6 Mon Sep 17 00:00:00 2001 From: dshukertjr <18113850+dshukertjr@users.noreply.github.com> Date: Wed, 5 May 2021 14:43:40 +0900 Subject: [PATCH 2/3] Updated tests to use upsert method for upsertinng --- test/basic_test.dart | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/basic_test.dart b/test/basic_test.dart index 07ab1a4..52db4f4 100644 --- a/test/basic_test.dart +++ b/test/basic_test.dart @@ -49,17 +49,17 @@ void main() { expect(res.data.length, 5); }); - test('on_conflict insert', () async { - final res = await postgrest.from('users').insert({'username': 'dragarcia', 'status': 'OFFLINE'}, - upsert: true, onConflict: 'username').execute(); + test('on_conflict upsert', () async { + final res = await postgrest + .from('users') + .upsert({'username': 'dragarcia', 'status': 'OFFLINE'}, onConflict: 'username').execute(); expect(res.data[0]['status'], 'OFFLINE'); }); test('upsert', () async { - final res = await postgrest.from('messages').insert( - {'id': 3, 'message': 'foo', 'username': 'supabot', 'channel_id': 2}, - upsert: true).execute(); - //{id: 3, message: foo, username: supabot, channel_id: 2} + final res = await postgrest + .from('messages') + .upsert({'id': 3, 'message': 'foo', 'username': 'supabot', 'channel_id': 2}).execute(); expect(res.data[0]['id'], 3); final resMsg = await postgrest.from('messages').select().execute(); @@ -137,9 +137,9 @@ void main() { }); test('insert with count: exact', () async { - final res = await postgrest.from('users').insert( + final res = await postgrest.from('users').upsert( {'username': 'countexact', 'status': 'OFFLINE'}, - upsert: true, onConflict: 'username').execute(count: CountOption.exact); + onConflict: 'username').execute(count: CountOption.exact); expect(res.count, 1); }); From 064f1be5db0eaa9845fe2d82c3944d9bc14c62ed Mon Sep 17 00:00:00 2001 From: dshukertjr <18113850+dshukertjr@users.noreply.github.com> Date: Wed, 5 May 2021 15:01:34 +0900 Subject: [PATCH 3/3] Updating url to add on_conflict parameter --- lib/src/postgrest_query_builder.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/postgrest_query_builder.dart b/lib/src/postgrest_query_builder.dart index 5ef37a6..396abcc 100644 --- a/lib/src/postgrest_query_builder.dart +++ b/lib/src/postgrest_query_builder.dart @@ -61,7 +61,7 @@ class PostgrestQueryBuilder extends PostgrestBuilder { headers['Prefer'] = upsert ? 'return=representation,resolution=merge-duplicates' : 'return=representation'; if (onConflict != null) { - url.queryParameters.addAll({'on_conflict': onConflict}); + url = url.replace(queryParameters: {'on_conflict': onConflict, ...url.queryParameters}); } body = values; return this; @@ -79,7 +79,7 @@ class PostgrestQueryBuilder extends PostgrestBuilder { method = 'POST'; headers['Prefer'] = 'return=representation,resolution=merge-duplicates'; if (onConflict != null) { - url.queryParameters.addAll({'on_conflict': onConflict}); + url = url.replace(queryParameters: {'on_conflict': onConflict, ...url.queryParameters}); } body = values; return this;