set.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 token.

Parameters

None

Return value

Type Description

Stream

Stream token used to subscribe to the stream.

Examples

Collection streams

Calling toStream() directly on collection.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 byName() index has:

  • A term field of name

  • Value fields of name and price

collection Product {
  *: Any

  index byName {
    terms [.name]
    values [.name, .price]
  }
  ...
}

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

Product.byName("limes").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.

// Uses the `Product` collection's `byName()` index and
// the `first()` method to get a single document.
let product = Product.byName("cups").first()
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!