query method

Future<List<T>> query(
  1. DatumQuery query, {
  2. required DataSource source,
  3. String? userId,
})

Executes a one-time query against the specified data source.

This provides a powerful way to fetch filtered and sorted data directly from either the local or remote adapter without relying on reactive streams.

Implementation

Future<List<T>> query(
  DatumQuery query, {
  required DataSource source,
  String? userId,
}) async {
  _ensureInitialized();

  // Create a cache key for this query
  final cacheKey = _createQueryCacheKey(query, source, userId);

  // Check cache first (only for local queries without related entities for simplicity)
  if (source == DataSource.local && query.withRelated.isEmpty) {
    final cached = _getCachedQuery(cacheKey);
    if (cached != null) {
      _logger.debug('Using cached query results for key: $cacheKey');
      return Future.wait(cached.map(_applyPostFetchTransforms));
    }
  }

  final adapter = (source == DataSource.local ? localAdapter : remoteAdapter) as dynamic;
  final entities = await adapter.query(query, userId: userId) as List<T>;

  if (query.withRelated.isNotEmpty && entities.isNotEmpty) {
    await _fetchAndStitchRelations(entities, query.withRelated, source, userId);
  }

  // Cache the results (only for local queries without related entities)
  if (source == DataSource.local && query.withRelated.isEmpty) {
    _cacheQuery(cacheKey, entities);
  }

  return Future.wait(entities.map(_applyPostFetchTransforms));
}