Comments (5)
Hey @porfirioribeiro good to hear from you again. I’m typing on a tiny keyboard so please excuse the formatting.
I think what would be best here are using non-final fields:
static Future<List<T extends YourBaseModel>> fetchAndAssignFromParentId(String parentId) async {
final results = await repository.get<T>(
query: Query(where: [Where.exact('parentId', parentId)]),
);
return results.map((r) => r..parentId = parentId);
}
This way, you could declare parentId in the superclass that Entity would extend. This is how primaryKey is accessible and saved on every SqliteModel.
Alternatively, because I have a feeling this may not be exactly what you’re after, there’s an afterSave
hook for SqliteModels (OfflineFirstWithRestModel is a SqliteModel subclass) that may have access to query. I’m...not sure. I’ll get back with you when I’m not on this tiny keyboard.
from brick.
Thanks for your quick answer @tshedor
I found a way to make it work by passing seedOnly
, without it i would always get 0 results, and then store it again on sqlite after setting the parentId
.
Future<List<Entity>> getEntity(
MyRepository repository, String parentId) async {
final entities = await repository.get<Entity>(
alwaysHydrate: true,
seedOnly: true,
query: Query(where: [Where.exact('parentId', parentId)]),
);
await Future.wait(entities.map((e) => repository.sqliteProvider.upsert(e..parentId = parentId)));
return entities;
}
It works, but it's a bummer that i have to store it again on Sqlite right after getting it and saving it.
What i was trying to achieve was be able to do something between the data came from Rest and be stored in Sqlite.
For what i could see, afterSave
has no access to the query, and even if it did, it's called after saving to Sqlite, so would be the same.
Maybe would be possible to add a beforeSave
that would receive the query and be able to manipulate the instance before storing on Sqlite?
from brick.
@porfirioribeiro You're right, there aren't arguments for query on the Save
hooks. Sorry, I steered you in the wrong direction there.
I have some concerns about including query
from an architectural view:
- The query should only interact with the provider/repository and should be resolved before data is transmitted to a provider. For this reason, a model should only be aware of data and instances, not provider config.
- REST should not interact with SQLite, as these are two independent providers and need to be modular to allow mixing by the repository. The repository's purpose is to combine providers.
- The brick domain can get/is very complex, and to maintain sanity I try to keep concerns as separated as possible (i.e. provider-repository-model).
I'd love your input if you want to push back on either of those points as you've been using this for several months and have a use case that my team hasn't yet come across.
Now that I'm back on a big keyboard, I looked through the repository and remembered this less-documented method (it's only used by adapters). It might suit you:
class MyRepository extends OfflineFirstWithRestRepository {
@override
Future<List<_Model>> getAssociation<_Model extends _RepositoryModel>(Query query) async {
final results = await super.getAssociation<_Model>(query);
if (query?.providerArgs != null) {
final parentId = query.providerArgs['parent_id'];
if (parentId != null) {
return results.map((r) => r..parentId = parentId);
}
}
return results;
}
}
#getAssociation
is invoked before #hydrate
so if the record needs to be saved in your providers it will be later and you don't need to worry about upsert
ing here.
from brick.
I think #getAssociation
is only called if the model actually have any model association, which is not the case. And that function is not called. Solving this at the repository level would probably not be the best place, i don't know.
I understand the situation, managing to keep all this abstractions working together is not easy, and I'm very thankful for all the work you are putting on this. I've been able to workaround most quirks i found, and adapt this library for our app. Probably i should expose those to you, to be aware of.
We are starting to bring the App for production, and we have some customers already testing it.
It's not the first time i got this problem, but the last time i was able to fix it at the backend side by adding that field to the response, but this time was a bit more complicated.
Well, the workarounds are currently working, so we can keep it this way, and maybe later we can find a way to solve this problem in a better way!
from brick.
Closing because of workaround
from brick.
Related Issues (20)
- GraphQL support? HOT 2
- Thank you! HOT 1
- How to convert a model field to a small letter in a Query? HOT 2
- build_runner build error HOT 2
- Update version HOT 1
- Bug : broken "Full documentation" link in README.md HOT 1
- Analyzer version conflict with many libs HOT 1
- **Migrating to Brick 2 from Brick 1**
- Can't generate anything since update to v2 HOT 5
- Current state of this project? HOT 8
- Is @ConnectOfflineFirstWithRest() part of Brick 2 or Brick 1? HOT 2
- Question - Hive/Isar local storage support? HOT 1
- No Support for Realm - A More Fully-Featured DB for Resource Constrained Environments HOT 4
- Outdated documentation and examples HOT 4
- Get 'is not a Dart library. It may be a part file or a file without Dart source code.' error on run build runner HOT 12
- Need to run build_runner twice for correct schema.g.dart generation HOT 1
- Discussion: cache busting HOT 4
- **Migrating to Brick 3 from Brick 2**
- Casting issue with latest example code. HOT 2
- Migration not created for Sqlite Database when extending Model after upgrade to 3.0 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from brick.