toStream()

Creates a stream that tracks changes to documents in a Set.

Signature

toStream(): Stream<A>

Description

The toStream() method creates a stream that tracks changes to documents in a Set.

You can only call toStream() on a Set from a supported source. The exact behavior of the method depends on this source. The calling Set isn’t changed.

Source Sets for streams support a limited number of transformations and filters.

A query that creates a stream returns a Stream.

Parameters

None

Return value

Type Description

Stream

Stream token used to subscribe to the stream.

Examples

Collection streams

Calling toStream() directly on all() tracks any change to any document in the collection.

Product.all().toStream()

The query returns a stream token.

"g9WD1YPG..."

Index streams

Streams on indexes only send events for changes to the index’s terms or values fields.

For example, the following Product collection’s byCategory index has:

  • A term field of category

  • Value fields of name and price

collection Product {
  index byCategory {
    terms [.category]
    values [.name, .price]
  }
  ...
}

The following query only tracks changes to the category, name, or price fields for Product documents with a category of sports.

Product.byCategory('sports').toStream()

Document streams

You can use streams to track changes to a Set containing a single document. These streams are only sent events when the document changes.

let product = Product.byId(392174614626697728)!
Set.single(product).toStream()

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!