A trigger is a mechanism that automatically executes predefined operations in response to specific events on nodes or edges. Triggers can be set to run before or after events such as insertion, update, or deletion of data. They allow you to enforce business rules, maintain data integrity, or perform auxiliary operations without manual intervention, ensuring that logic tied to DML operations is executed consistently and automatically.
To show triggers in the current graph:
GQLSHOW TRIGGER
To show node triggers in the current graph:
GQLSHOW NODE TRIGGER
To show edge triggers in the current graph:
GQLSHOW EDGE TRIGGER
Each trigger provides the following essential metadata:
Field | Description |
|---|---|
name | The name of the trigger. |
schema | The name of node or edge schema to which the trigger applies. |
description | The comment given to the trigger. |
timing | When the trigger takes effect — either before or after the event. |
event | The type of event that activates the trigger, including insert, update, and delete. |
call | The operation or logic executed by the trigger. |
To create a trigger AutoUpperCase for Student nodes that converts the name property to uppercase before insertion:
GQLCREATE TRIGGER "AutoUpperCase" ON NODE "Student" COMMENT "Converts name to uppercases before insertion" BEFORE INSERT CALL " $entity CALL { LET entity.name = upper(entity.name) } "
This trigger automatically converts the name property of a Student node to uppercase before insertion:
GQLINSERT (n:Student {name: "John Doe"}) RETURN n.name
Result:
| n.name |
|---|
| JOHN DOE |
To create a trigger AutoStaus for ENROLLED_IN edges, automatically setting the status property to "active" before insertion:
GQLCREATE TRIGGER "AutoStaus" ON EDGE "ENROLLED_IN" COMMENT "Sets status to true" BEFORE INSERT CALL " $entity CALL { LET entity.status = 'active' } "
This trigger automatically sets the status property of an ENROLLED_IN edge to "active" before insertion:
GQLMATCH (s:Student {name: "JOHN DOE"}), (c:Course {name: "Science"}) INSERT (s)-[e:ENROLLED_IN]->(c) RETURN e.status
Result:
| e.status |
|---|
| active |
To drop the node trigger AutoUpperCase:
GQLDROP NODE TRIGGER AutoUpperCase
To drop the edge trigger AutoStaus:
GQLDROP EDGE TRIGGER AutoStaus
Note: Dropping a node or edge schema will also remove any triggers associated with that schema.