How to map custom select results to data class? #2976
-
I have the following data class: @JsonSerializable()
class CardData extends BaseModel {
final int id;
final String name;
CardData(this.id, this.name);
} I have defined a table as such: @UseRowClass(CardData)
class Card extends Table {
IntColumn get id => integer()();
TextColumn get name => text()();
@override
Set<Column> get primaryKey => {id};
} I am building a custom select statement based off user filters. I would like to write a method that returns a list of // buildQuery = "SELECT * FROM card WHERE name = 'test'"
await dbClient.customSelect(buildQuery(filters), readsFrom: {dbClient.card})... // This gives me a `Selectable<QueryRow>` but how do I map it from here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Drift offers no builtin conversion mechanisms for custom queries (unless they're written in a drift file), but that's something else. If your query is selecting all columns from the table, you can use the dbClient.customSelect(buildQuery(filters), readsFrom: {dbClient.card}).map((row) => dbClient.card.map(row.data)) |
Beta Was this translation helpful? Give feedback.
Drift offers no builtin conversion mechanisms for custom queries (unless they're written in a drift file), but that's something else. If your query is selecting all columns from the table, you can use the
map
method on the generated table: