From 30a578299e186cf26db37c529e2efd7b88fd47eb Mon Sep 17 00:00:00 2001 From: Maximilian Fischer Date: Sat, 24 Apr 2021 21:43:02 +0200 Subject: [PATCH 1/3] add first and find to the QueryBuilder --- .../dart/lib/src/network/parse_query.dart | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/dart/lib/src/network/parse_query.dart b/packages/dart/lib/src/network/parse_query.dart index 4165adac0..5fea70b61 100644 --- a/packages/dart/lib/src/network/parse_query.dart +++ b/packages/dart/lib/src/network/parse_query.dart @@ -513,4 +513,25 @@ class QueryBuilder { }); return result; } + + /// Find the first object that satisfies the query. + /// Returns null, if no object is found. + Future? 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> find() async { + ParseResponse parseResponse = await query(); + if (parseResponse.success) { + return (parseResponse.results ?? []) as List; + } + throw parseResponse.error ?? ParseError(); + } } From 8ecdc21b576a643dec1929eb3932e45d7305f0a2 Mon Sep 17 00:00:00 2001 From: Maximilian Fischer Date: Sat, 24 Apr 2021 21:48:21 +0200 Subject: [PATCH 2/3] fix QueryBuilder.find() --- packages/dart/lib/src/network/parse_query.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dart/lib/src/network/parse_query.dart b/packages/dart/lib/src/network/parse_query.dart index 5fea70b61..627759a35 100644 --- a/packages/dart/lib/src/network/parse_query.dart +++ b/packages/dart/lib/src/network/parse_query.dart @@ -530,7 +530,7 @@ class QueryBuilder { Future> find() async { ParseResponse parseResponse = await query(); if (parseResponse.success) { - return (parseResponse.results ?? []) as List; + return parseResponse.results?.map((e) => e as T).toList() ?? []; } throw parseResponse.error ?? ParseError(); } From ca169f202adc2390da6fb1ef435079b6aa7efd56 Mon Sep 17 00:00:00 2001 From: Maximilian Fischer Date: Thu, 29 Apr 2021 16:17:15 +0200 Subject: [PATCH 3/3] add documentation for first() and find() --- packages/dart/README.md | 9 +++++++++ packages/flutter/README.md | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/packages/dart/README.md b/packages/dart/README.md index 8d75418dd..8b72b1daf 100644 --- a/packages/dart/README.md +++ b/packages/dart/README.md @@ -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> 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> 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? 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: diff --git a/packages/flutter/README.md b/packages/flutter/README.md index 3b2032a06..9d91da202 100644 --- a/packages/flutter/README.md +++ b/packages/flutter/README.md @@ -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> 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> 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? 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: