$_getPrefetchedData<$CurrentDataclass, $CurrentTable extends Table, $ReferencedDataclass> function
Future<List<MultiTypedResultEntry<$ReferencedDataclass> > >
$_getPrefetchedData<$CurrentDataclass, $CurrentTable extends Table, $ReferencedDataclass>({
- required ProcessedTableManager<
dynamic, dynamic, $ReferencedDataclass, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, $ReferencedDataclass, dynamic> managerFromTypedResult(), - required MultiTypedResultKey<
Table, dynamic> referencedTable, - required List<
TypedResult> typedResults, - required TableInfo<
$CurrentTable, $CurrentDataclass> currentTable, - required Iterable<
$ReferencedDataclass> referencedItemsForCurrentItem(- $CurrentDataclass item,
- List<
$ReferencedDataclass> referencedItems
This function is used to fetch referenced data for a List<TypedResults>.
Here is an example. Let's say we wanted to get all the groups, with their users. We would need to:
- Then run a 2nd query to get all the users who are in the groups. (Users who arent any groups will be ignored)
- Split the users into groups
- Return these users as a
List<List<User>>(The first list is the groups, the 2nd list is the users in the group), along with the referenced table
Manually this would look like:
final groups = await groups.get();
final groupIds = groups.map((group)=> group.id);
final users = await users.filter((f)=> f.group.id.isIn(groupIds)).get()
final groupsWithUsers = groups.map((group)=>(group,users.where((user)=> user.group == group.id)));
This function does the same thing, but for any table and any referenced table.
Arguments:
typedResultsis the raw result of the query, together withcurrentTablewe can read it's resultsmanagerFromTypedResultis the equivalent of:
What we do, is collect all the filters from all of thesefinal groups = await groups.withReferences().get(); for (final (group, refs) in groups){ /// Manager with a filter to only get the users of this group. refs.users; }refs.usersand combine them with an OR operator to create a query which gets all the usersmanagerFromTypedResultis the function which turns a singlegroupintorefs.usersreferencedTableis aMultiTypedResultKeywhich is to write the results to theTypedResultobject, This same class will be used in theBaseReferencesclass to read from theTypedResult.referencedItemsForCurrentItemis the callback which does the mapping. It is the equivalent ofusers.where((user)=> user.group == group.id).
Results are returned as a list of MultiTypedResultEntry objects which contain the key and the list of references for each row.
This function is used by the generated code and should not be used directly.
Implementation
// ignore: non_constant_identifier_names
Future<List<MultiTypedResultEntry<$ReferencedDataclass>>> $_getPrefetchedData<
$CurrentDataclass, $CurrentTable extends Table, $ReferencedDataclass>(
{required ProcessedTableManager<
dynamic,
dynamic,
$ReferencedDataclass,
dynamic,
dynamic,
dynamic,
dynamic,
dynamic,
dynamic,
$ReferencedDataclass,
dynamic>
Function(TypedResult)
managerFromTypedResult,
required MultiTypedResultKey referencedTable,
required List<TypedResult> typedResults,
required TableInfo<$CurrentTable, $CurrentDataclass> currentTable,
required Iterable<$ReferencedDataclass> Function(
$CurrentDataclass item, List<$ReferencedDataclass> referencedItems)
referencedItemsForCurrentItem}) async {
if (typedResults.isEmpty) {
return [];
} else {
final managers = typedResults.map(managerFromTypedResult);
// Combine all the referenced managers into 1 large query which will return all the
// referenced items in one go.
final manager = managers.reduce((value, element) {
if (element.$state.filter != null) {
return value._filter(
(_) => element.$state.filter!, _BooleanOperator.or);
} else {
return value;
}
});
return manager.get(distinct: true).then(
(value) {
return typedResults.map((e) {
final item = e.readTable(currentTable);
final refs = referencedItemsForCurrentItem(item, value).toList();
return MultiTypedResultEntry(key: referencedTable, value: refs);
}).toList();
},
);
}
}