Skip to content

Commit

Permalink
Fix stray COUNT field in list responses and add list counting support…
Browse files Browse the repository at this point in the history
… to the dart client.
  • Loading branch information
ignatz committed Jan 31, 2025
1 parent 5cec548 commit 933c555
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
12 changes: 10 additions & 2 deletions client/trailbase-dart/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,18 @@ class Pagination {
class ListResponse {
final String? cursor;
final List<Map<String, dynamic>> records;
final int? totalCount;

const ListResponse({this.cursor, required this.records});
const ListResponse({
this.cursor,
required this.records,
this.totalCount,
});

ListResponse.fromJson(Map<String, dynamic> json)
: cursor = json['cursor'],
records = (json['records'] as List).cast<Map<String, dynamic>>();
records = (json['records'] as List).cast<Map<String, dynamic>>(),
totalCount = json['total_count'];
}

abstract class RecordId {
Expand Down Expand Up @@ -293,6 +299,7 @@ class RecordApi {
Pagination? pagination,
List<String>? order,
List<String>? filters,
bool? count,
}) async {
final params = <String, dynamic>{};
if (pagination != null) {
Expand All @@ -304,6 +311,7 @@ class RecordApi {
}

if (order != null) params['order'] = order.join(',');
if (count ?? false) params['count'] = 'true';

if (filters != null) {
for (final filter in filters) {
Expand Down
13 changes: 13 additions & 0 deletions client/trailbase-dart/test/trailbase_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ Future<void> main() async {
orderedEquals(messages));
}

{
final response = (await api.list(
pagination: Pagination(limit: 1),
order: ['-text_not_null'],
filters: ['text_not_null[like]=%${now}'],
count: true,
));

expect(response.totalCount ?? -1, 2);
// Ensure there's no extra field, i.e the count doesn't get serialized.
expect(response.records[0].keys.length, 13);
}

final record = SimpleStrict.fromJson(await api.read(ids[0]));

expect(ids[0] == record.id, isTrue);
Expand Down
39 changes: 36 additions & 3 deletions trailbase-core/src/records/list_records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ pub async fn list_records_handler(
formatdoc!(
r#"
WITH total_count AS (
SELECT COUNT(*)
SELECT COUNT(*) AS _value_
FROM
'{table_name}' as _ROW_,
(SELECT :__user_id AS id) AS _USER_
WHERE
{clause}
)
SELECT _ROW_.*, _COUNT_.*
SELECT _ROW_.*, total_count._value_
FROM
total_count AS _COUNT_,
total_count,
'{table_name}' as _ROW_,
(SELECT :__user_id AS id) AS _USER_
WHERE
Expand Down Expand Up @@ -313,6 +313,15 @@ mod tests {
.records
.into_iter()
.map(|v| {
match v {
serde_json::Value::Object(ref obj) => {
let keys: Vec<&str> = obj.keys().map(|s| s.as_str()).collect();
assert_eq!(3, keys.len(), "Got: {:?}", keys);
assert_eq!(keys, vec!["id", "room", "data"])
}
_ => panic!("expected object, got {v:?}"),
};

let message = serde_json::from_value::<Message>(v).unwrap();
assert_eq!(None, message._owner);
message.data
Expand Down Expand Up @@ -340,6 +349,30 @@ mod tests {
assert_eq!(resp.records.len(), 2);
assert_eq!(resp.total_count, Some(2));

let messages: Vec<_> = resp
.records
.into_iter()
.map(|v| {
match v {
serde_json::Value::Object(ref obj) => {
let keys: Vec<&str> = obj.keys().map(|s| s.as_str()).collect();
assert_eq!(3, keys.len(), "Got: {:?}", keys);
assert_eq!(keys, vec!["id", "room", "data"])
}
_ => panic!("expected object, got {v:?}"),
};

let message = serde_json::from_value::<Message>(v).unwrap();
assert_eq!(None, message._owner);
message.data
})
.collect();

assert_eq!(
vec!["user_y to room0".to_string(), "user_x to room0".to_string()],
messages
);

// Let's paginate
let resp0 = list_records(
&state,
Expand Down

0 comments on commit 933c555

Please sign in to comment.