fetchRelated<R extends DatumEntityInterface> method

Future<List<R>> fetchRelated<R extends DatumEntityInterface>(
  1. T parent,
  2. String relationName, {
  3. DataSource source = DataSource.local,
})

DEPRECATED: This method will be removed in a future version. Use the withRelated parameter in the query method for eager loading, or the fetch() method on the relation object for lazy loading. Fetches related entities for a given parent entity.

  • parent: The entity instance for which to fetch related data. This must be an instance of RelationalDatumEntity.
  • relationName: The name of the relation to fetch, as defined in the parent's belongsTo or manyToMany maps.
  • source: The DataSource to fetch from (defaults to local).

Returns a list of the related entities. Throws an ArgumentError if the parent is not a RelationalDatumEntity, or an Exception if the relation name is not defined on the parent.

Implementation

Future<List<R>> fetchRelated<R extends DatumEntityInterface>(
  T parent,
  String relationName, {
  DataSource source = DataSource.local,
}) async {
  _ensureInitialized();

  if (parent is RelationalDatumEntity) {
    final relation = parent.relations[relationName];
    if (relation == null) {
      throw ArgumentError(
        'Relation "$relationName" is not defined on entity type ${parent.runtimeType}.',
      );
    }
  } else {
    throw ArgumentError(
      'The parent entity must be a RelationalDatumEntity to fetch relations.',
    );
  }

  final relatedManager = Datum.manager<R>();

  switch (source) {
    case DataSource.local:
      return localAdapter.fetchRelated(
        parent,
        relationName,
        relatedManager.localAdapter,
      );
    case DataSource.remote:
      return remoteAdapter.fetchRelated(
        parent,
        relationName,
        relatedManager.remoteAdapter,
      );
  }
}