Back to top
Documentation SurrealQL Statements DEFINE statement EVENT

DEFINE EVENT statement

Events can be triggered after any change or modification to the data in a record. Each trigger is able to see the $before and $after value of the record, enabling advanced custom logic with each trigger.

Requirements

Statement syntax

DEFINE EVENT @name ON [ TABLE ] @table WHEN @expression THEN @expression

Example usage

Below is an example showing how to create an event which upon updating a user's email address will create an entry recording the change on an event table.

-- Create a new event whenever a user changes their email address
DEFINE EVENT email ON TABLE user WHEN $before.email != $after.email THEN (
	CREATE event SET user = $this, time = time::now(), value = $after.email, action = 'email_changed'
);