query method
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);
}
// Apply post-fetch transforms with error handling
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;
}