feat: Get references from manager
Is your feature request related to a problem? Please describe. Make it easy to get referenced objects.
Describe the solution you'd like This is the syntax that I've gotten to work. I'm shocked that this is possible using Dart's very limited type system:
Syntax
await db.managers.todoItems.withReferences().withCategory().get()
// [(TodoItem(id: 31, title: Math, content: Do homework, category: 37), (category: Category(id: 37, description: School)))]
await db.managers.todoItems.withReferences().get()
// [(TodoItem(id: 31, title: Math, content: Do homework, category: 37), (category: null))]
Type Hinting
The cool part is that when a with___ is not specified, the type hint is void:
And when added it's a nullable reference
I think this syntax is both developer friendly, while still being pretty simple in the source code.
@simolus3 What are your thoughts before I proceed?
The API looks convincing, but I think I need some more details that aren't clear to me (but probably important to get right/clarified first):
- How (if at all) does this chain multiple reference links (e.g. if categories had outgoing references as well)? How do we deal with circular references (or self-references in a table)
- How does this play along with the rest of the manager API, in particular the methods to write filters? Will they still only use the primary table or is there a different state for joins as well?
- I haven't experimented with how nesting would be done. It's isn't supported in my current implementation. Even so there wouldn't be any circular recursion issues.
- Once
withReferencesis called, it uses whatever filters and ordering were applied to the manager and wraps it in a new class. It will then run queries to get the referenced objects and attach them to a record. So filters and ordering can't be applied to what references get returned. Serverpod allows filtering what references are returned, but it's not type safe. I think it's worth it to have all-or-nothing and be type-safe
I'll have a PR in a couple of minutes which will allow you to experiment with it. Although modular will prob not work (temporarily)
@simolus3 Check this out https://github.com/simolus3/drift/pull/2984