eventSource.map()
Learn: Track changes with Event Feeds and Event Streams |
---|
Apply a function to each element of an event source's tracked Set.
Description
map()
applies an anonymous, read-only function to each element of an existing
event source's
tracked Set.
map()
returns a new event source. The new event source emits events that
contain transformed elements in the event’s
data
property.
map()
does not change the calling event source.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
functionBody |
Yes |
Anonymous, read-only function that operates on an existing event source's tracked Set elements. Writes are not permitted in the function. |
Return value
Type | Description |
---|---|
Hash for the new event source hash. The event source emits events for the original tracked Set. The emitted events contain transformations from the provided function. |
Examples
Basic example
Customer
collection documents have the following
structure:
{
id: "111",
coll: Customer,
ts: Time("2099-06-25T12:14:29.440Z"),
cart: Order("412653216549831168"),
orders: "hdW...",
name: 'Alice Appleseed',
email: 'alice.appleseed@example.com',
address: {
street: '87856 Mendota Court',
city: 'Washington',
state: 'DC',
postalCode: '20220',
country: 'US'
}
}
The following query uses map()
to transform the document structure in events:
Customer.all()
.eventSource()
.map(
customer => {
name: customer.name,
// Transformation. Combines `address.city` and `address.state`
// into a single `city` string.
city: "#{customer.address.city}, #{customer.address.state}"
}
)
// Hash for the new event source
"g9WD1YPG..."
When consumed as an Event Feed or
Event Stream, the event source emits events with the transformed value in the
data
property:
{
"type": "update",
// The `data` prop contains transformed
// `city` values.
"data": {
"name": "Alice Appleseed",
"city": "Washington, DC"
},
"txn_ts": 1730318669480000,
"cursor": "gsG...",
"stats": {
"read_ops": 2,
"storage_bytes_read": 738,
"compute_ops": 1,
"processing_time_ms": 9,
"rate_limits_hit": []
}
}
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!