set.toStream()

set.toStream() is deprecated. Use set.eventSource() instead.

Creates an event source that tracks changes to documents in a supported Set.

Signature

toStream() => EventSource<A>

Description

The toStream() method creates an event source token that tracks changes to documents in a supported Set. The token has the EventSource type:

"g9WD1YPG..."

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

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

Parameters

None

Return value

Type Description

EventSource

A string-encoded token that represents the event source. Use the token to consume events as an Event Feed or Event Stream.

Examples

Collection event sources

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

Product.all().toStream()

The query returns an event source:

"g9WD1YPG..."

Index event sources

Event sources for 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 event sources

You can use event sources to track changes to a Set containing a single document. These event sources 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!