fetchSince method

  1. @override
Future<Delta<T, Id>> fetchSince(
  1. SyncScope scope,
  2. DateTime? since
)
override

Implementation

@override
Future<Delta<T, Id>> fetchSince(SyncScope scope, DateTime? since) async {
  // Build queries via helper for testability
  final queries = buildFetchQueries(scope, since);

  // Fetch paginated
  final upserts = <T>[];
  final deletes = <Id>[];
  String? cursor;
  const limit = 100;
  while (true) {
    final res = await config.databases.listDocuments(
      databaseId: config.databaseId,
      collectionId: config.collectionId,
      queries: [
        ...queries,
        if (cursor != null) aw.Query.cursorAfter(cursor),
        aw.Query.limit(limit),
        aw.Query.orderAsc(config.updatedAtField),
      ],
    );
    for (final d in res.documents) {
      final data = Map<String, dynamic>.from(d.data);
      final isDeleted =
          config.deletedAtField != null &&
          data[config.deletedAtField!] != null;
      if (isDeleted) {
        deletes.add(config.idFromString(d.$id));
      } else {
        upserts.add(config.fromJson(data));
      }
    }
    if (res.total <= (res.documents.length + (cursor == null ? 0 : limit))) {
      // Heuristic: stop when we've likely reached the end. Appwrite doesn't return a next cursor; we can break when page shorter than limit.
      if (res.documents.length < limit) {
        break;
      }
    }
    if (res.documents.isEmpty) break;
    cursor = res.documents.last.$id;
  }

  final serverTs = await getServerTime();
  return Delta<T, Id>(
    upserts: upserts,
    deletes: deletes,
    serverTimestamp: serverTs,
  );
}