@upsertOn
A projection definition requires one or multiple @upsertOn
type directives.
Example
In the following case the createUser
event will upsert data to the dataset identified by the value of its id
payload field.
The updateUser
event will upsert data to the dataset identified by its userId
payload field.
type User
@upsertOn(
on: {
topic: "userManagement"
eventTypes: ["createUser"]
}
identifyBy: { payload: ["id"] }
)
@upsertOn(
on: {
topic: "userManagement"
eventTypes: ["updateUser"]
}
identifyBy: { payload: ["userId"] }
) {
// ...
}
Arguments
You can use the following arguments on the @upsertOn
directive directly:
Argument | Description |
---|---|
on | restrict updates of the projection |
identifyBy | specify how the identifier of the data is generated |
filterBy | upsert entries in the projection by a filter |
Argument on
If eventTypes
and topic
are both specified for the on
argument, an event has to match both to be considered for the projection.
Argument | Description |
---|---|
eventTypes | restrict updates to the projection to a list of event types |
topic | restrict updates to the projection to a topic string |
Argument identifyBy
Specify how the identifier of the data is generated. The generated identifier separates all parts by a -
character.
Argument | Description |
---|---|
attributes | specify a list of attributes that should be part of the identifier |
payload | specify a list of event payload fields that should be part of the identifier |
Applying this to an example, the @upsertOn
directive will generate the id streamName-uuid
for an event of the stream called streamName
and with a payload {"id": "uuid"}
:
@upsertOn(
on: {
topic: "userManagement"
eventTypes: ["createUser"]
}
identifyBy: { attributes: ["stream"], payload: ["id"] }
)
Available Event Attributes
The events to trigger an upsert operation can be filtered using the following attributes:
Attribute | Description |
---|---|
id | the identifier of the event |
tenantId | the identifier of the tenant |
topic | the topic to which the event was published to |
type | name of the event type |
stream | name of the used stream |
correlationId | correlation identifier |
causationId | causation identifier |
reason | the reason why the event was published |
Argument filterBy
Alternative to the identifyBy
argument, you can use the filterBy
argument to specify a filter that determines which data should be upserted in the projection.
type User
@upsertOn(
on: {
topic: "userManagement"
eventTypes: ["createUser"]
}
filterBy: { fields: [{
projectionField: "name"
operation: "equals"
type: "String"
eventField: "userName"
}] }
){
// ...
}
This example shows how to use the filterBy
argument to upsert data in the projection based on a filter.
In this case, the createUser
event will upsert data in the projection if the name
field of the projection matches the userName
field of the event.
Types and operations
Type | Allowed operations |
---|---|
ID | equals , notEquals , in , notIn |
[ID] | inArray , notInArray , lengthEq , lengthNotEq , lengthGt , lengthGtOrEq , lengthLt , lengthLtOrEq , empty , notEmpty |
String | equals , notEquals , equalsCaseInsensitive , notEqualsCaseInsensitive , in , notIn , contains , notContains , containsCaseInsensitive , notContainsCaseInsensitive |
[String] | inArray , notInArray , lengthEq , lengthNotEq , lengthGt , lengthGtOrEq , lengthLt , lengthLtOrEq , empty , notEmpty |
File | equals , notEquals , in , notIn |
DateTime | equals , notEquals , in , notIn , before ,beforeOrAt ,after ,atOrAfter |
[DateTime] | inArray , notInArray , lengthEq , lengthNotEq , lengthGt , lengthGtOrEq , lengthLt , lengthLtOrEq , empty , notEmpty |
Int | equals , notEquals , lessThan , lessThanOrEqual , greaterThan , greaterThanOrEqual |
[Int] | inArray , notInArray , lengthEq , lengthNotEq , lengthGt , lengthGtOrEq , lengthLt , lengthLtOrEq , empty , notEmpty |
Float | equals , notEquals , lessThan , lessThanOrEqual , greaterThan , greaterThanOrEqual |
[Float] | inArray , notInArray , lengthEq , lengthNotEq , lengthGt , lengthGtOrEq , lengthLt , lengthLtOrEq , empty , notEmpty |
Boolean | equals , notEquals |
The in
and notIn
filters require an array as value.
The inArray
and notInArray
filters allow arrays or single values.