tekartik / sembast.dart

Simple io database
BSD 2-Clause "Simplified" License
780 stars 64 forks source link

[feature] Watch the records count in a StoreRef as a Stream #319

Closed navaronbracke closed 2 years ago

navaronbracke commented 2 years ago

Currently we can get the count of a StoreRef using StoreRef.count(database). However this is a Future and there is no way to get a stream of changes for this value.

I have tried using return _storeRef.query().onSnapshots(_database).map((e) => e.length); but that is a little cumbersome, and it gives extra updates where it is not needed (when existing records are updated we get events too).

Ideally we should be able to do final Stream<int> countStream = _storeRef.countStream();

alextekartik commented 2 years ago

Good idea!

I have added StoreRef.onCount() method that should do what you ask for.. Dev version 3.3.0-dev.1 published.

It requires doing

dependencies:
  sembast: >=3.3.0-dev.1`

in pubspec.yaml

navaronbracke commented 2 years ago

That was quick, thank you! I'll bump my pubspec then :)