readAll method

Future<List<T>> readAll({
  1. String? userId,
  2. List<String> withRelated = const [],
  3. bool includeDeleted = false,
})

Reads all entities from the primary local adapter.

Soft-delete tombstones are excluded unless includeDeleted is true.

The withRelated parameter allows eager loading of related entities.

Implementation

Future<List<T>> readAll({String? userId, List<String> withRelated = const [], bool includeDeleted = false}) async {
  _ensureInitialized();
  var entities = await localAdapter.readAll(userId: userId);
  if (!includeDeleted) {
    entities = entities.where((e) => !e.isDeleted).toList();
  }

  if (withRelated.isNotEmpty && entities.isNotEmpty) {
    await _fetchAndStitchRelations(entities, withRelated, DataSource.local, userId);
  }

  final transformedEntities = <T>[];
  for (final entity in entities) {
    try {
      final transformed = await _applyPostFetchTransforms(entity);
      transformedEntities.add(transformed);
    } catch (e, stack) {
      _logger.error('Failed to apply post-fetch transforms to entity ${entity.id}: $e', stack);
      // Continue with other entities instead of failing the entire operation
      transformedEntities.add(entity); // Use original entity if transform fails
    }
  }
  return transformedEntities;
}