ON Keyword
Syntax
ON trigger-type OF "table-name"
Parameters
trigger-type– The type of database trigger to listen for. Valid values are:INSERTUPDATEDELETE
"table-name"– The name of the database table to monitor.
Description
ON registers a database trigger for the current session. When the specified event occurs on the given table, the engine records the trigger in the system_automations table, linking it to the session. This enables scripts to react to data changes by executing associated actions (e.g., sending a notification, updating a variable).
The keyword performs the following steps:
- Validates the
trigger-typeand converts it to the internalTriggerKindenum. - Constructs a parameter name in the form
<table>_<trigger>.rhai(e.g.,orders_insert.rhai). - Inserts a row into
system_automationswith the trigger kind, target table, and parameter name. - Returns the number of rows affected (normally
1on success).
If the trigger type is invalid, the keyword raises a runtime error.
Example
ON INSERT OF "orders"
TALK "A new order was added. Processing..."
After execution, any new row inserted into the orders table will cause the session to be notified, allowing the script to handle the event.
Implementation Notes
- The keyword runs synchronously but performs the database insertion on a separate thread to avoid blocking.
- Errors during insertion are logged and returned as runtime errors.