Event Store logo

Menu

Documentation

Show Table of Contents

User Defined Projections

User defined projections are written in javascript (ECMASCRIPT 6).

Example projection:

options({ //option
	resultStreamName: "my_demo_projection_result",
	$includeLinks: false,
	reorderEvents: false,
	processingLag: 0
})

fromStream('account-1') //selector
.when({ //filter
	$init:function(state, evnt){
		return {
			count: 0
		}
	},
	myEventType: function(state, evnt){
		s.count += 1;
	}
})
.transformBy(function(state, evnt){ //transformation
	state.count = 10;
})
.outputState() //transformation

Projections API

Options

Name Description Notes
resultStreamName Overrides the default resulting stream name for the outputState() transformation, which is $projections-{projection-name}-result.
$includeLinks Configures the projection to include/exclude link to events. Default: false
processingLag When reorderEvents is turned on, this value is used to compare the total milliseconds between the first and last events in the buffer and if the value is equal or greater, the events in the buffer will be processed. The buffer is an ordered list of events. Default: 500ms

Only valid for fromStreams() selector

reorderEvents Process events by storing a buffer of events ordered by their prepare position Default: false

Only valid for fromStreams() selector

Selectors

Selector Description Notes
fromAll() Selects events from the $all stream. Provides
  • partitionBy
  • when
  • foreachStream
  • outputState
fromCategory({category}) Selects events from the `$ce-{category}` stream. Provides
  • partitionBy
  • when
  • foreachStream
  • outputState
fromStream({streamId}) Selects events from the {streamId} stream. Provides
  • partitionBy
  • when
  • outputState
fromStreams([]streams) Selects events from the streams supplied. Provides
  • partitionBy
  • when
  • outputState
fromStreamsMatching(function filter) Selects events from the $all stream that returns true for the given filter. Provides
  • partitionBy
  • when
  • foreachStream
  • outputState

Filters/Transormations

Filter/Partition Description Notes
when(handlers) Allows only the given events of a particular to pass through. Provides
  • $defines_state_transform
  • transformBy
  • filterBy
  • outputTo
  • outputState
foreachStream() Partitions the state for each of the streams provided. Provides
  • when
outputState() If the projection maintains state, setting this option will produce a stream called $projections-{projection-name}-result with the state as the event body. Provides
  • transformBy
  • filterBy
  • outputTo
partitionBy(function(event)) Partitions a projection by the partition returned from the handler. Provides
  • when
transformBy(function(state, event)) Provides the ability to transform the state of a projection by the provided handler. Provides
  • transformBy
  • filterBy
  • outputState
  • outputTo

Handlers

Each handler is provided with the current state of the projection as well as the event that triggered the handler. The event provided through the handler contains the following properties.

  • isJson:true/false
  • data:{}
  • body:{}
  • bodyRaw: string
  • sequenceNumber: integer
  • metadataRaw: {}
  • linkMetadataRaw: string
  • partition: string
Handler Description Notes
{event-type} When using fromAll() and 2 or more event type handlers are specified and the $by_event_type projection is enabled and running, the projection will start as a fromStreams('$et-event-type-foo', `$et-event-type-bar`) until the projection has caught up and then will move over to reading from the transaction log (i.e. from $all).
$init Provide the initialization for a projection. Commonly used to setup the initial state for a projection.
$initShared Provide the initialization for a projection where the projection is possibly partitioned.
$any Event type pattern match that will match any event type. Commonly used when the user is interested in any event type from the selector.
$deleted Will be called upon the deletion of a stream. Can only be used with `foreachStream`

Functions

Name Description Notes
emit(streamId, eventName, eventBody, metadata) Writes an event to the designated stream
linkTo(streamId, event, metadata) Writes a link to event to the designated stream

Event Store docs are hosted on GitHub. The repository is public and it’s open to issues and pull requests. Contributions, corrections and feedback are all welcome.