$_getPrefetchedDataByColumns<$CurrentDataclass, $CurrentTable extends Table, $ReferencedDataclass, $ReferencedTable extends Table> function

Future<List<MultiTypedResultEntry<$ReferencedDataclass>>> $_getPrefetchedDataByColumns<$CurrentDataclass, $CurrentTable extends Table, $ReferencedDataclass, $ReferencedTable extends Table>({
  1. required ProcessedTableManager<dynamic, $ReferencedTable, $ReferencedDataclass, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, $ReferencedDataclass, dynamic> managerFromTypedResult(
    1. TypedResult
    ),
  2. required MultiTypedResultKey<Table, dynamic> referencedTable,
  3. required List<TypedResult> typedResults,
  4. required TableInfo<$CurrentTable, $CurrentDataclass> currentTable,
  5. required String currentColumn,
  6. required String referencedColumn,
})

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:

  1. Then run a 2nd query to get all the users who are in the groups. (Users who arent any groups will be ignored)
  2. Split the users into groups
  3. 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:

  • typedResults is the raw result of the query, together with currentTable we can read it's results
  • managerFromTypedResult is the equivalent of:
    final 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;
    }
    
    What we do, is collect all the filters from all of these refs.users and combine them with an OR operator to create a query which gets all the users managerFromTypedResult is the function which turns a single group into refs.users
  • referencedTable is a MultiTypedResultKey which is to write the results to the TypedResult object, This same class will be used in the BaseReferences class to read from the TypedResult.
  • currentColumn and referencedColumn define the columns doing the grouping / mapping.

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

Future<List<MultiTypedResultEntry<$ReferencedDataclass>>>
// ignore: non_constant_identifier_names
$_getPrefetchedDataByColumns<
  $CurrentDataclass,
  $CurrentTable extends Table,
  $ReferencedDataclass,
  $ReferencedTable extends Table
>({
  required ProcessedTableManager<
    dynamic,
    $ReferencedTable,
    $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 String currentColumn,
  required String referencedColumn,
}) async {
  if (typedResults.isEmpty) {
    return [];
  }

  // Combine all the referenced managers into 1 large query which will return
  // all the referenced items in one go.
  final manager = typedResults.map(managerFromTypedResult).reduce((
    value,
    element,
  ) {
    if (element.$state.filter != null) {
      return value._filter((_) => element.$state.filter!, _BooleanOperator.or);
    } else {
      return value;
    }
  });
  final state = manager.$state.copyWith(distinct: true);
  final table = state._tableAsTableInfo;
  final referencedKey = table.columnsByName[referencedColumn]!;
  final currentKey = currentTable.columnsByName[currentColumn]!;

  final rows = await state.buildSelectStatement().get();
  final referencedItemsByKey = <Object?, List<$ReferencedDataclass>>{};
  for (final row in rows) {
    referencedItemsByKey
        .putIfAbsent(row.read(referencedKey), () => [])
        .add(row.readTable(table));
  }

  return [
    for (final row in typedResults)
      MultiTypedResultEntry(
        key: referencedTable,
        value: [...?referencedItemsByKey[row.read(currentKey)]],
      ),
  ];
}