How to add "implements" contracts to drift defined views? #2754
-
I have views defined in .drift files, and I want the data-classes (and the ViewInfo) to implement my custom interfaces (numerous). Here is the use-case. I have several tables, used to populate dropdown lookups. They are enhanced/supplemented using some (irrelevant) SQl logic - hence being views, rather than directly accessing the table. I want to define a custom abstract classes And I want the code generated by drift to apply the appropriate "implements WithSortOrder" to my view DataClass and ViewInfo. This will let me centralise the sorting in my repositories, such that I can always be sure my lists will be ordered correctly as defined in the database. The example below uses 2 custom contracts, for multiple sorting.
Summary: Is this possible? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
It's not possible to specify a base class for tables and views declared in drift files - neither for the data class nor for the table/view info class. It's not quite as reliable, but if no other table has columns with those names you could look them up via runtime schema inspection: final isDefault = t.columnsByName['is_default'];
final sortOrder = t.columnsByName['sort_order'];
query.orderBy([
if (isDefault != null)
(u) =>
OrderingTerm(expression: isDefault, mode: OrderingMode.desc),
if (sortOrder != null)
(u) =>
OrderingTerm(expression: sortOrder, mode: OrderingMode.asc)
]); |
Beta Was this translation helpful? Give feedback.
-
Thanks @simolus3 for that suggestion. Write a custom class which derives from the generated view - its sole purpose is to declare that the view implements certain contracts:
Then manually attach the view to the database during construction:
I guess the most significant difference to what you have suggested, would be this solution will apply compile-time checking. Thanks for the tip - now I have a couple of tools to achieve what I want. |
Beta Was this translation helpful? Give feedback.
Thanks @simolus3 for that suggestion.
I did actually find a way to achieve this:
Write a custom class which derives from the generated view - its sole purpose is to declare that the view implements certain contracts:
Then manually attach the view to the database during construction: