getByIds method

  1. @override
Future<ReadListResult<T>> getByIds(
  1. ReadByIdsOperation<T> operation
)
override

Loads all instances of T whose primary key is in the set at ReadByIdsOperation.itemIds.

This could in theory be a version of getItems with a specific IdsIn filter, but that would complicate the extra caching logic handled by this method. The source of that complication is the difference between a "where in" filter and a generic filter. The difference is that with a "where in" filter, you know when you are still missing objects and can forward that request onto the next source just to fill in the gaps. However, with a generic filter, you do not know whether or not you are missing any records and thus cannot be clever with the a request to the next source.

This method makes use of that extra knowledge afforded by a "where in" filter to load records efficiently; which is what would be lost, or at least greatly complicated, if this method was rolled into calling getItems with an equivalent filter.

Implementation

@override
Future<ReadListResult<T>> getByIds(ReadByIdsOperation<T> operation) async {
  if (getByIdsHandler == null) {
    throw UnimplementedError();
  }

  try {
    final objs = await getByIdsHandler!.call(operation);
    final loadedIds = objs.map<String>((obj) => bindings.getId(obj)!).toSet();
    return ReadListResult.fromList(
      objs,
      operation.details,
      operation.itemIds.difference(loadedIds),
      bindings.getId,
    );
  } on Exception catch (e) {
    _log.severe(e);
    return ReadListFailure<T>(
      FailureReason.serverError,
      'Failed to load $T by Ids',
    );
  }
}