Merge pull request #662 from GetStream/feat/filters

feat(llc): add `Filter.contains` and `Filter.empty`
This commit is contained in:
Salvatore Giordano
2021-09-07 16:37:50 +02:00
committed by GitHub
4 changed files with 71 additions and 0 deletions
@@ -107,6 +107,48 @@ The 'exists' filter matches values that exist, or don't exist, based on the spec
Filter.exists('name', true)
```
#### Filter.contains
The 'contains' filter matches any list that contains the specified value.
```dart
Filter.contains('teams', 'red')
```
#### Filter.empty
The 'empty' filter constructor returns an empty filter. It's the equivalent of an empty map `{}`;
```dart
Filter.empty();
```
#### Filter.raw
The 'raw' filter constructor lets you specify a raw filter. We suggest using this only if you can't manage to build what you want using the other constructors.
```dart
Filter.raw(value: {
'members': [
..._selectedUsers.map((e) => e.id),
chatState.currentUser!.id,
],
'distinct': true,
});
```
#### Filter.custom
The 'custom' filter is used to create a custom filter in case it does not exists or it's not been added to the SDK yet.
Note that the filter must be supported by the Stream backend in order to work.
```dart
Filter.custom(
operator: '\$max',
value: 10,
)
```
### Group Queries
#### Filter.and
+4
View File
@@ -10,6 +10,10 @@
- `updatePinnedMessageReactions`
- `deletePinnedMessageReactionsByMessageId`
✅ Added
- Added `Filter.contains` and `Filter.empty`
## 2.2.1
🐞 Fixed
@@ -51,6 +51,9 @@ enum FilterOperator {
/// Matches none of the values specified in an array.
nor,
/// Matches any list that contains the specified value
contains,
}
/// Helper extension for [FilterOperator]
@@ -71,6 +74,7 @@ extension FilterOperatorX on FilterOperator {
FilterOperator.and: '\$and',
FilterOperator.or: '\$or',
FilterOperator.nor: '\$nor',
FilterOperator.contains: '\$contains',
}[this]!;
}
@@ -157,6 +161,10 @@ class Filter extends Equatable {
factory Filter.exists(String key, {bool exists = true}) =>
Filter._(operator: FilterOperator.exists, key: key, value: exists);
/// Matches any list that contains the specified values
factory Filter.contains(String key, Object value) =>
Filter._(operator: FilterOperator.contains, key: key, value: value);
/// Creates a custom [Filter] if there isn't one already available.
const factory Filter.custom({
required Object value,
@@ -164,6 +172,9 @@ class Filter extends Equatable {
String? key,
}) = Filter.__;
/// An empty filter
factory Filter.empty() => const Filter.raw(value: {});
/// Creates a custom [Filter] from a raw map value
///
/// ```dart
@@ -138,6 +138,20 @@ void main() {
expect(filter.value, value);
});
test('empty', () {
final filter = Filter.empty();
expect(filter.value, {});
});
test('contains', () {
const key = 'testKey';
const values = 'testValue';
final filter = Filter.contains(key, values);
expect(filter.key, key);
expect(filter.value, values);
expect(filter.operator, FilterOperator.contains.rawValue);
});
group('groupedOperator', () {
final filter1 = Filter.equal('testKey', 'testValue');
final filter2 = Filter.in_('testKey', const ['testValue']);