apoc.trigger.add

Procedure Deprecated

apoc.trigger.add(name STRING, statement STRING, selector MAP<STRING, ANY>, config MAP<STRING, ANY>) - adds a trigger to the given Cypher statement. The selector for this procedure is {phase:'before/after/rollback/afterAsync'}.

This procedure is not intended to be used in a cluster environment, and may act unpredictably.

Please note that this procedure is deprecated.

Use the following ones instead, which allow for better support in a cluster:

deprecated procedure

new procedure

apoc.trigger.add(name STRING, statement STRING, selector MAP<STRING, ANY>, config MAP<STRING, ANY>)

apoc.trigger.install(databaseName STRING, name STRING, statement STRING, selector MAP<STRING, ANY>, config MAP<STRING, ANY>)

apoc.trigger.remove(name STRING)

apoc.trigger.drop(databaseName STRING, name STRING)

apoc.trigger.removeAll()

apoc.trigger.dropAll(databaseName STRING)

apoc.trigger.pause(name STRING)

apoc.trigger.stop(databaseName STRING, name STRING)

apoc.trigger.resume(name STRING)

apoc.trigger.start(databaseName STRING, name STRING)

where databaseName is the database where we want to execute the triggers.

Signature

apoc.trigger.add(name :: STRING, kernelTransaction :: STRING, selector :: MAP, config = {} :: MAP) :: (name :: STRING, query :: STRING, selector :: MAP, params :: MAP, installed :: BOOLEAN, paused :: BOOLEAN)

Input parameters

Name Type Default

name

STRING

null

kernelTransaction

STRING

null

selector

MAP

null

config

MAP

{}

Output parameters

Name Type

name

STRING

query

STRING

selector

MAP

params

MAP

installed

BOOLEAN

paused

BOOLEAN

Enable Triggers

By default triggers are disabled. We can enable them by setting the following property in apoc.conf:

apoc.conf
apoc.trigger.enabled=true
apoc.trigger.refresh=60000
Table 1. Description
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 examples in this section are based on the following graph:

CREATE (:Counter {count:0})
CREATE (f:Foo);

Let’s create a trigger that keeps a count of the number of nodes that have been deleted:

CALL apoc.trigger.add(
  'count-removals',
  'MATCH (c:Counter)
   SET c.count = c.count + size([f IN $deletedNodes WHERE id(f) > 0])',
  {}
);
Table 2. Results
name query selector params installed paused

"count-removals"

MATCH (c:Counter) SET c.count = c.count + size([f IN $deletedNodes WHERE id(f)  0])

{}

{}

TRUE

FALSE

We’ll now delete the Foo node:

MATCH (f:Foo)
DELETE f;
Results
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;
Table 3. Results
count

1