Projections/Schema
Expressions
Learn how to use expressions in Freym to evaluate conditions and calculate values.
We use a simple expression engine called expr.
You can use some built in variables in your expressions. We extended the functionality by some useful functions.
Variables
metadata
: Event metadata. Possible fields:metadata.id
: The id of the event (string)metadata.tenantId
: The tenant id of the event (string)metadata.stream
: The stream name of the event (string)metadata.type
: The type name of the event (string)metadata.correlationId
: The tenant correlation id of the event (string)metadata.causationId
: The causation id of the event (string)metadata.reason
: The reason string of the event (string)metadata.topic
: The topic of the event (string)metadata.raisedAt
: The time when this event was raised, represented as unix timestamp (milliseconds) (int64)
payload
- all fields that the event payload contains
- if your payload field is an object (not a reference to an other projection), you can access all object fields, too:
payload.user.name
would access the name field of the user object in the user field of the event payload
projection
- all fields that the event projection contains
- the
projection
variable contains the state of the projection before the event is applied to the projection and will therefore be empty for the first event that generates a new projection data entry - if your projection field is an object (not a reference to an other projection), you can access all object fields, too:
payload.user.name
would access the name field of the user object in the user field of the projection
now
: current unix timestamp (int value)second
: number of milliseconds in a secondminute
: number of milliseconds in a minutehour
: number of milliseconds in an hourday
: number of milliseconds in a dayweek
: number of milliseconds in a weekmonth
: number of milliseconds in a 30 day monthyear
: number of milliseconds in a 365 day year
Please note that the metadata
and payload
variables are not available in every use case. To
find out more, check the documentation of the directive that you use in conjunction with your
expression.
Functions
append(array, value)
: appends thevalue
to thearray
intSum(arrayOfInts)
: calculates the sum of all elements in thearrayOfInts
(requires actualint
values in the array, you can make use ofintSum(map(arrayOfNumbers, {int(#)}))
to convert all values to integers)
Conditions
You can use ternaries to evaluate results based on conditions:
user.isOnline ? user.name : "offline";
This expression would return the username in case the user is online and return "offline"
for all users that are not online.
Language Definition
Please have a look at the language definition of the expression engine that we use to evaluate expressions.