Projection

zio.blocks.projection.Projection
See theProjection companion object
final class Projection[A]

A declarative description of a read model A built from events: which sources to listen to, how to route their events, and what each kind of event does to A.

A Projection is immutable data and does nothing by itself. Every builder call returns a new Projection, and an engine (such as ProjectionEngine) later reads it to process events. Handlers only describe changes as ProjectionActions, so they cannot perform effects.

Create one with Projection[A](name) (one row per entity) or Projection.global[A](name) (shared totals), then chain from, a routing call, and on:

val greetings = Projection[Greeting]("greetings")
 .from("people")
 .routeToSelf
 .on[PersonGreeted]
 .insert((e, ctx) => Greeting(ctx.entityId, s"Hello, ${e.name}!"))

Further from(...) calls add more sources, and further on[...] calls add more handlers to the latest source. An on[...] with no from(...) before it creates a default source named "_default" that routes to the entity itself.

Call validate before starting an engine to catch an empty projection.

Value parameters

bindings

the sources with their routing and handlers, in the order they were added

entityPath

where the store lives and which field of A is its ID. It is None for global projections

isGlobal

whether this is a global projection

name

the projection's name, which also names its store file

schema

the schema of the projected type A

Attributes

Companion
object
Source
Projection.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def allHandlers: List[Handler[_, A]]

All handlers of all sources, in binding order.

All handlers of all sources, in binding order.

Attributes

Source
Projection.scala
def dispatch(event: Any, ctx: ProjectionContext, source: Option[String] = ...): Option[ProjectionAction[A]]

Same as handle. Kept for binary compatibility.

Same as handle. Kept for binary compatibility.

Attributes

Source
Projection.scala
def from(sourceName: String): FromBuilder[A]

Starts a new source binding. Follow it with routeToSelf, routeToAll, routedBy or routedByField to choose how the source's events are routed.

Starts a new source binding. Follow it with routeToSelf, routeToAll, routedBy or routedByField to choose how the source's events are routed.

Value parameters

sourceName

the name of the event source

Attributes

Source
Projection.scala
def handle(event: Any, ctx: ProjectionContext, source: Option[String] = ...): Option[ProjectionAction[A]]

Finds the handler for event and runs it, returning the action to apply, or None if no handler matches the event's class.

Finds the handler for event and runs it, returning the action to apply, or None if no handler matches the event's class.

When source is given, only the handlers of the first binding with that name are considered. Otherwise all handlers are searched in binding order, and the first match wins, even across sources. Pass source when the projection has several sources, so that an event is not handled by another source's handler.

Value parameters

ctx

the metadata of the event

event

the event to handle

source

restricts the search to this source

Attributes

Source
Projection.scala
def on[E](using evidence$1: Schema[E], ct: ClassTag[E]): HandlerBuilder[E, A]

Starts a handler for events of type E and attaches it to the most recently added source. Follow it with insert, delete, aggregate or another HandlerBuilder method to say what the event does.

Starts a handler for events of type E and attaches it to the most recently added source. Follow it with insert, delete, aggregate or another HandlerBuilder method to say what the event does.

If no source has been added yet, a source named "_default" that routes to the entity itself is created first.

Attributes

Source
Projection.scala
def routingKey(event: Any, ctx: ProjectionContext, sourceName: String): Option[String]

The routing key of event for the named source: ctx.entityId for routeToSelf, the extracted key for routedBy, and None for routeToAll or when there is no source with that name.

The routing key of event for the named source: ctx.entityId for routeToSelf, the extracted key for routedBy, and None for routeToAll or when there is no source with that name.

Attributes

Source
Projection.scala
def routingKeyForDefault(event: Any, ctx: ProjectionContext): Option[String]

The routing key of event using the most recently added source, or ctx.entityId if there are no sources. The result follows the same rules as routingKey.

The routing key of event using the most recently added source, or ctx.entityId if there are no sources. The result follows the same rules as routingKey.

Attributes

Source
Projection.scala

The projection's ProjectionScope, derived from how it was built:

The projection's ProjectionScope, derived from how it was built:

Note that routeToAll alone does not make a projection global.

Attributes

Source
Projection.scala
def sourceNames: List[String]

The names of the sources this projection listens to.

The names of the sources this projection listens to.

Attributes

Source
Projection.scala
def sources: List[SourceBinding[_, A]]

The source bindings, same as bindings.

The source bindings, same as bindings.

Attributes

Source
Projection.scala
override def toString: String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

Definition Classes
Any
Source
Projection.scala
def validate(): List[String]

Checks the projection for problems that would stop it from doing anything, and returns a message for each. The list is empty if none are found.

Checks the projection for problems that would stop it from doing anything, and returns a message for each. The list is empty if none are found.

It reports a projection with no sources, one with no handlers, a source with an empty name, and a handler with a null event class.

Attributes

Source
Projection.scala

Concrete fields

val bindings: List[SourceBinding[_, A]]

Attributes

Source
Projection.scala
val entityPath: Option[EntityPath[A]]

Attributes

Source
Projection.scala
val isGlobal: Boolean

Attributes

Source
Projection.scala
val name: String

Attributes

Source
Projection.scala
val schema: Schema[A]

Attributes

Source
Projection.scala