read method

Future<T?> read(
  1. String id, {
  2. String? userId,
})

Reads a single entity by its ID from the primary local adapter.

Implementation

Future<T?> read(String id, {String? userId}) async {
  _ensureInitialized();

  // Create cache key for entity existence
  final cacheKey = '${T.toString()}:$id:${userId ?? ''}';

  // Check entity existence cache first
  final cachedExists = _entityExistenceCache[cacheKey];
  if (cachedExists != null) {
    if (!cachedExists) {
      _logger.debug('Using cached entity existence (does not exist) for key: $cacheKey');
      return null;
    }
    // If cache says it exists, we still need to fetch it
  }

  final entity = await localAdapter.read(id, userId: userId);

  // Cache the existence result
  _entityExistenceCache[cacheKey] = entity != null;
  _logger.debug('Cached entity existence for key: $cacheKey (exists: ${entity != null})');

  if (entity == null) return null;
  return _applyPostFetchTransforms(entity);
}