stream method

Stream<T> stream({
  1. int chunkSize = 100,
})

Returns an async stream of query results.

This method efficiently streams large result sets without loading everything into memory at once.

Example:

await for (final user in context.query<User>().stream()) {
  print(user.email);
}

Implementation

Stream<T> stream({int chunkSize = 100}) async* {
  int offset = 0;
  bool hasMore = true;

  while (hasMore) {
    final chunk = await limit(chunkSize).offset(offset).get();

    if (chunk.isEmpty) {
      hasMore = false;
      break;
    }

    for (final model in chunk) {
      yield model;
    }

    offset += chunkSize;

    if (chunk.length < chunkSize) {
      hasMore = false;
    }
  }
}