Skip to content
This repository was archived by the owner on May 13, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions lib/src/postgrest_query_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,46 @@ 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 = url.replace(queryParameters: {'on_conflict': onConflict, ...url.queryParameters});
}
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 = url.replace(queryParameters: {'on_conflict': onConflict, ...url.queryParameters});
}
body = values;
return this;
}

/// 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';
Expand Down
18 changes: 9 additions & 9 deletions test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});

Expand Down