Skip to content
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
5 changes: 4 additions & 1 deletion packages/postgrest/lib/src/postgrest_query_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,13 @@ class PostgrestQueryBuilder {
json = null;
}
}
if (json is Map<String, dynamic>) {
return json;
}
if (json is! Map || json.keys.any((key) => key is! String)) {
throw ArgumentError.value(row, 'values', 'Every row must be an object');
}
return json.cast();
return Map.from(json);
}

/// Only performs a count query on the table or view.
Expand Down
35 changes: 35 additions & 0 deletions packages/postgrest/test/insert_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,39 @@ void main() {
expect(request.queryParameters['limit'], '1');
expect(request.headers['Prefer'], 'return=representation');
});

for (final (name, write) in [
(
'insert()',
(PostgrestQueryBuilder table, List<Object?> rows) => table.insert(rows),
),
(
'upsert()',
(PostgrestQueryBuilder table, List<Object?> rows) => table.upsert(rows),
),
]) {
test('$name sends rows given as Map<dynamic, dynamic> and '
'Map<String, Object?>', () async {
await write(postgrest.from('users'), [
<dynamic, dynamic>{'username': 'foo'},
<String, Object?>{'username': 'bar', 'status': 'ONLINE'},
]);

final request = httpClient.requests.last;
expect(
request.body,
'[{"username":"foo"},{"username":"bar","status":"ONLINE"}]',
);
expect(request.queryParameters['columns'], '"username","status"');
});

test('$name of a row with a non-String key throws', () {
expect(
() => write(postgrest.from('users'), [
<dynamic, dynamic>{1: 'foo'},
]),
throwsArgumentError,
);
});
}
}
Loading