Skip to content

Commit 38bc3a1

Browse files
fix(dataStore): update in memory sorts and filters for IDs (#1597)
* fix(dataStore): update in memory sorts and filters for IDs * chore: refactor field name logic
1 parent 3d75358 commit 38bc3a1

File tree

6 files changed

+51
-3
lines changed

6 files changed

+51
-3
lines changed

packages/amplify_core/lib/src/types/query/query_field.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
library query_field;
1717

1818
import 'package:amplify_core/amplify_core.dart';
19+
import 'package:amplify_core/src/types/query/query_utils.dart';
1920
import 'package:flutter/foundation.dart';
2021

2122
part 'query_field_operators.dart';

packages/amplify_core/lib/src/types/query/query_predicate.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ class QueryPredicateOperation extends QueryPredicate {
6969

7070
@override
7171
bool evaluate(Model model) {
72+
String fieldName = getFieldName(field);
7273
//ignore:implicit_dynamic_variable
73-
var value = model.toJson()[field];
74+
var value = model.toJson()[fieldName];
7475
return queryFieldOperator.evaluate(value);
7576
}
7677

packages/amplify_core/lib/src/types/query/query_sort.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ class QuerySortBy {
3232
const QuerySortBy({required this.order, required this.field});
3333

3434
int compare<T extends Model>(T a, T b) {
35-
dynamic valueA = a.toJson()[field];
36-
dynamic valueB = b.toJson()[field];
35+
String fieldName = getFieldName(field);
36+
dynamic valueA = a.toJson()[fieldName];
37+
dynamic valueB = b.toJson()[fieldName];
3738
int orderMultiplier = order == QuerySortOrder.ascending ? 1 : -1;
3839
if (valueA == null || valueB == null) {
3940
return orderMultiplier * _compareNull(valueA, valueB);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
/// Removes the model name that is pre-pended to id fields.
17+
///
18+
/// ID fields are named `<Model_Name>.<Field_Name>`, for example "blog.id".
19+
/// This util will remove the model name and return just the field name ("id").
20+
String getFieldName(String fieldName) {
21+
return fieldName.endsWith('.id') ? 'id' : fieldName;
22+
}

packages/amplify_datastore/test/query_predicate_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ void main() {
161161
expect(testPredicate.evaluate(post2), isFalse);
162162
expect(testPredicate.evaluate(post4), isFalse);
163163
});
164+
165+
test('equals (ID)', () {
166+
QueryPredicate testPredicate = Post.ID.eq(post2.id);
167+
expect(testPredicate.evaluate(post1), isFalse);
168+
expect(testPredicate.evaluate(post2), isTrue);
169+
expect(testPredicate.evaluate(post4), isFalse);
170+
});
171+
164172
test('not equals', () {
165173
QueryPredicate testPredicate = Post.LIKECOUNT.ne(1);
166174
expect(testPredicate.evaluate(post1), isFalse);

packages/amplify_datastore/test/query_sort_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ void main() {
5353

5454
group('compare', () {
5555
Post post1 = Post(
56+
id: '123e4567-e89b-12d3-a456-426614174000',
5657
title: 'post1',
5758
rating: 1,
5859
created: TemporalDateTime(DateTime(2020, 01, 01, 10, 30)),
5960
);
6061

6162
Post post2 = Post(
63+
id: '123e4567-e89b-12d3-a456-426614174001',
6264
title: 'post2',
6365
rating: 2,
6466
created: TemporalDateTime(DateTime(2020, 01, 01, 12, 30)),
@@ -72,6 +74,19 @@ void main() {
7274

7375
Post post4Copy = post4.copyWith();
7476

77+
test('should compare ID fields', () {
78+
QuerySortBy sortByAsc = Post.ID.ascending();
79+
QuerySortBy sortByDesc = Post.ID.descending();
80+
81+
expect(sortByAsc.compare(post1, post2), -1);
82+
expect(sortByAsc.compare(post2, post1), 1);
83+
expect(sortByAsc.compare(post2, post2Copy), 0);
84+
85+
expect(sortByDesc.compare(post1, post2), 1);
86+
expect(sortByDesc.compare(post2, post1), -1);
87+
expect(sortByDesc.compare(post2, post2Copy), 0);
88+
});
89+
7590
test('should compare int fields', () {
7691
QuerySortBy sortByAsc = Post.RATING.ascending();
7792
QuerySortBy sortByDesc = Post.RATING.descending();

0 commit comments

Comments
 (0)