Skip to content

Commit acf1224

Browse files
authored
Additional query methods (#611)
* add first and find to the QueryBuilder * add documentation for first() and find()
1 parent cb55e11 commit acf1224

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

packages/dart/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ if (dietPlan.success) {
248248
}
249249
```
250250

251+
### Alternative query methods
252+
253+
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.
254+
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.
255+
256+
Choosing between `query()` and `find()` comes down to personal preference. Both methods can be used for querying a `ParseQuery`, just the output method differs.
257+
258+
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`.
259+
251260
## Complex queries
252261
You can create complex queries to really put your database to the test:
253262

packages/dart/lib/src/network/parse_query.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,25 @@ class QueryBuilder<T extends ParseObject> {
513513
});
514514
return result;
515515
}
516+
517+
/// Find the first object that satisfies the query.
518+
/// Returns null, if no object is found.
519+
Future<T>? first() async {
520+
ParseResponse parseResponse =
521+
await (QueryBuilder.copy(this)..setLimit(1)).query();
522+
if (parseResponse.success) {
523+
return parseResponse.results?.first;
524+
}
525+
throw parseResponse.error ?? ParseError();
526+
}
527+
528+
/// Find the objects that satisfy the query.
529+
/// Returns an empty list if no objects are found.
530+
Future<List<T>> find() async {
531+
ParseResponse parseResponse = await query();
532+
if (parseResponse.success) {
533+
return parseResponse.results?.map((e) => e as T).toList() ?? <T>[];
534+
}
535+
throw parseResponse.error ?? ParseError();
536+
}
516537
}

packages/flutter/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,15 @@ if (response.success) {
265265
}
266266
```
267267

268+
### Alternative query methods
269+
270+
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.
271+
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.
272+
273+
Choosing between `query()` and `find()` comes down to personal preference. Both methods can be used for querying a `ParseQuery`, just the output method differs.
274+
275+
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`.
276+
268277
## Complex queries
269278
You can create complex queries to really put your database to the test:
270279

0 commit comments

Comments
 (0)