pull method

  1. @override
Future<List<SyncRecord>> pull({
  1. required String collection,
  2. DateTime? since,
  3. int? limit,
  4. int? offset,
  5. SyncFilter? filter,
})
override

Pull data from backend with optional pagination and filtering

Parameters:

  • collection: Collection name to pull from
  • since: Only pull records modified after this timestamp
  • limit: Maximum number of records to pull
  • offset: Number of records to skip (for pagination)
  • filter: Optional sync filter for selective synchronization

Implementation

@override
Future<List<SyncRecord>> pull({
  required String collection,
  DateTime? since,
  int? limit,
  int? offset,
  SyncFilter? filter,
}) async {
  Query query = firestore.collection(collection);

  // Use filter's since if provided, otherwise use since parameter
  final effectiveSince = filter?.since ?? since;
  if (effectiveSince != null) {
    query = query.where('updatedAt',
        isGreaterThan: Timestamp.fromDate(effectiveSince));
  }

  // Apply filter where conditions
  if (filter?.where != null) {
    for (final entry in filter!.where!.entries) {
      query = query.where('data.${entry.key}', isEqualTo: entry.value);
    }
  }

  // Apply pagination - use filter's limit if provided
  final effectiveLimit = filter?.limit ?? limit;
  if (effectiveLimit != null) {
    query = query.limit(effectiveLimit);
  }

  // Note: Firestore doesn't have native offset, so we skip documents
  // For better performance, consider using cursor-based pagination in production
  if (offset != null && offset > 0) {
    final skipSnapshot = await query.limit(offset).get();
    if (skipSnapshot.docs.isNotEmpty) {
      query = query.startAfterDocument(skipSnapshot.docs.last);
    }
  }

  final snapshot = await query.get();

  return snapshot.docs.map((doc) {
    final data = doc.data() as Map<String, dynamic>;
    var recordData = data['data'] as Map<String, dynamic>;

    // Apply field filtering if specified
    if (filter != null) {
      recordData = filter.applyFieldFilter(recordData);
    }

    return SyncRecord(
      recordId: doc.id,
      data: recordData,
      updatedAt: (data['updatedAt'] as Timestamp).toDate(),
      version: data['version'] as int? ?? 1,
    );
  }).toList();
}