Skip to content

Additional query methods #611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 29, 2021
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
9 changes: 9 additions & 0 deletions packages/dart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ if (dietPlan.success) {
}
```

### Alternative query methods

The standard query method `query()` returns a `ParseResponse` that contains the result or the error. As an alternative, you can also use `Future<List<T>> find()` for receiving options.
This method returns an `Future` that either resolves in an error (equivalent of the error in the `ParseResponse`) or an `List` containing the queried objects. One difference, you should be aware of, is the fact, that `Future<List<T>> find()` will return an empty list instead of the 'No results' error you receive in case no object matches you query.

Choosing between `query()` and `find()` comes down to personal preference. Both methods can be used for querying a `ParseQuery`, just the output method differs.

Similar to `find()` the `QueryBuilder` also has a function called `Future<T>? first()`. Just like `find()` `first()` is just a convenience method that makes querying the first object satisfying the query simpler. `first()` returns an `Future`, that resoles in an error or the first object matching the query. In case no object satisfies the query, the result will be `null`.

## Complex queries
You can create complex queries to really put your database to the test:

Expand Down
21 changes: 21 additions & 0 deletions packages/dart/lib/src/network/parse_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,25 @@ class QueryBuilder<T extends ParseObject> {
});
return result;
}

/// Find the first object that satisfies the query.
/// Returns null, if no object is found.
Future<T>? first() async {
ParseResponse parseResponse =
await (QueryBuilder.copy(this)..setLimit(1)).query();
if (parseResponse.success) {
return parseResponse.results?.first;
}
throw parseResponse.error ?? ParseError();
}

/// Find the objects that satisfy the query.
/// Returns an empty list if no objects are found.
Future<List<T>> find() async {
ParseResponse parseResponse = await query();
if (parseResponse.success) {
return parseResponse.results?.map((e) => e as T).toList() ?? <T>[];
}
throw parseResponse.error ?? ParseError();
}
}
9 changes: 9 additions & 0 deletions packages/flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ if (response.success) {
}
```

### Alternative query methods

The standard query method `query()` returns a `ParseResponse` that contains the result or the error. As an alternative, you can also use `Future<List<T>> find()` for receiving options.
This method returns an `Future` that either resolves in an error (equivalent of the error in the `ParseResponse`) or an `List` containing the queried objects. One difference, you should be aware of, is the fact, that `Future<List<T>> find()` will return an empty list instead of the 'No results' error you receive in case no object matches you query.

Choosing between `query()` and `find()` comes down to personal preference. Both methods can be used for querying a `ParseQuery`, just the output method differs.

Similar to `find()` the `QueryBuilder` also has a function called `Future<T>? first()`. Just like `find()` `first()` is just a convenience method that makes querying the first object satisfying the query simpler. `first()` returns an `Future`, that resoles in an error or the first object matching the query. In case no object satisfies the query, the result will be `null`.

## Complex queries
You can create complex queries to really put your database to the test:

Expand Down