-
Notifications
You must be signed in to change notification settings - Fork 26
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
Query Argument for Get Method #116
Comments
@SubutaDan no, If you're trying to fetch the latest data of something, you can do a query based on Brick's internal primary key: import 'package:brick_sqlite_abstract/db.dart';
final repositoryInstances = Repository().get<MyModelType>(query: Query.where(InsertTable.PRIMARY_KEY_FIELD, myModelInstance.primaryKey, limit1: true);
final hydratedInstance = (repositoryInstances?.isNotEmpty ?? false) ? repositoryInstances.first : null; |
Thanks, @tshedor , but my question is slightly different. Sorry I wasn't more clear. Is there a way to pass a URL query string argument to a get method: https://myapiserver.com/cats?name=${i_want-to_pass_in_this_value} If the method is a PUT or a POST I can use an instance field in the query string but I don''t know how to pass a value for a GET as there is no instance. -Dan |
Ah, I got you. Yes. @ConnectOfflineFirstWithRest(
restConfig: RestSerializable(
endpoint: r'''{
if (query?.action == QueryAction.get && query?.where != null) {
final name = Where.firstByField('name', query.where)?.value;
if (name != null) return "/cats?name=${name}";
}
return "/cats";
}''';
)
)
class Cat extends OfflineFirstWithRestModel {} And then you'd invoke this: await Repository().get<Cat>(query: Query.where('name').isExactly('Old Deuteronomy')); Of course, it's up to you on how to handle the query operator, so |
Thanks, Tim. This makes sense and I will try it.
…-Dan
On 4 January 2021 00:57:03 GMT, Tim Shedor ***@***.***> wrote:
@SubutaDan
Ah, I got you. Yes. `endpoint:` uses an [`instance` and `query` as its
parameters](https://github.com/greenbits/brick/blob/master/packages/brick_rest/README.md#with-querywhere).
```dart
@ConnectOfflineFirstWithRest(
restConfig: RestSerializable(
endpoint: r'''{
if (query?.action == QueryAction.get && query?.where != null) {
final name = Where.firstByField('name', query.where)?.value;
if (name != null) return "/cats?name=${name}";
}
return "/cats";
}''';
)
)
class Cat extends OfflineFirstWithRestModel {}
```
And then you'd invoke this:
```dart
await Repository().get<Cat>(query: Query.where('name').isExactly('Old
Deuteronomy'));
```
Of course, it's up to you on how to handle the query operator, so
`isExactly`, `contains`, `notEqual` will all be the same with the above
implementation of `endpoint:`.
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#116 (comment)
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
Hi @SubutaDan could you please give this mixin a try? |
I'll try :)
…On 18 January 2021 23:10:46 GMT, Tim Shedor ***@***.***> wrote:
Hi @SubutaDan could you please [give this mixin a
try](https://github.com/greenbits/brick/pull/121/files#diff-846d1cac334f035bf435dacd34232da6f0e1522ed2c097684a5a39fd0c761a06)?
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#116 (comment)
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
Closing this issue; confirmed working |
Hello,
I want to pass an entity to a Repository.get method so I can use a field from the entity as a query parameter. From the example program on Flutter by Example it seems that I should be able to do that, but my get method doesn't accept an instance variable:
//I can't do this:
Repository().get<MyModelType>(myModelInstance);
because I get:
Too many positional arguments: 0 expected, but 1 found.
Is passing instance variables to Repository.get methods supported? How can I pass a value to be used as a query parameter for a get?
Thanks.
-Dan
The text was updated successfully, but these errors were encountered: