apoc.trigger.install
Syntax |
|
||
Description |
Eventually adds a trigger for a given database which is invoked when a successful transaction occurs. |
||
Input arguments |
Name |
Type |
Description |
|
|
The name of the database to add the trigger to. |
|
|
|
The name of the trigger to add. |
|
|
|
The query to run when triggered. |
|
|
|
|
|
|
|
The parameters for the given Cypher statement. The default is: |
|
Return arguments |
Name |
Type |
Description |
|
|
The name of the trigger. |
|
|
|
The query belonging to the trigger. |
|
|
|
|
|
|
|
The parameters for the given Cypher statement. |
|
|
|
Whether or not the trigger was installed. |
|
|
|
Whether or not the trigger was paused. |
Enable Triggers
By default triggers are disabled.
We can enable them by setting the following property in apoc.conf
:
apoc.trigger.enabled=true
apoc.trigger.refresh=60000
Option Key | Value | Description |
---|---|---|
apoc.trigger.enabled |
true/false, default false |
Enable/Disable the feature |
apoc.trigger.refresh |
number, default 60000 |
Interval in ms after which a replication check is triggered across all cluster nodes |
Usage Examples
The There are several ways of doing this:
Moreover, the |
Installing, updating, or removing a trigger is an eventually consistent operation.
Therefore, they are not immediately added/updated/removed,
but have a refresh rate handled by the APOC configuration |
The examples in this section are based on the following graph, created in 'neo4j' db:
CREATE (:Counter {count:0})
CREATE (f:Foo);
To create a trigger that keeps a count of the number of nodes that have been deleted, run the following query:
CALL apoc.trigger.install(
'neo4j',
'count-removals',
'MATCH (c:Counter)
SET c.count = c.count + size([f IN $deletedNodes WHERE id(f) > 0])',
{}
);
name | query | selector | params | installed | paused |
---|---|---|---|---|---|
"count-removals" |
{} |
{} |
TRUE |
FALSE |
After a time defined by the configuration apoc.trigger.refresh
,
the Foo
node will be deleted:
MATCH (f:Foo)
DELETE f;
0 rows available after 20 ms, consumed after another 0 ms
Deleted 1 nodes
And finally, let’s check that the count
property on our Counter
node has been incremented:
MATCH (c:Counter)
RETURN c.count as count;
count |
---|
1 |