I have been trying to setup trigger to monitor any change in a node or a relation in my neo4j DB, also that updated node or relation data should be send as request body to an external POST api.
So far i have been able to make an POST api call on any change in db, also API endpoints are being called, but i want to put the trigger condition on a specific type of node or relation. also i am not able to get what data was updated and send it as request body. The neo4j's apoc documentation are not very clear to me.
here's structure of a very basic trigger which calls my POST api endpoint whenever any data change(insert, update, delete) happens in my db, and sends "testkey=testvalue"
CALL apoc.trigger.install(
'neo4j',
'dbchangetrigger',
"CALL apoc.load.jsonParams('http://192.168.1.122:9500/api/trigger/triggerFromDb',{method: 'POST'},'testkey=testvalue')",
{phase: 'after'}
);
From the documentation:
The transaction data from Neo4j is turned into appropriate data structures to be consumed as parameters to a statement, i.e. $createdNodes
.
A table of available trigger parameters is included in the following:
You will need to use these data structures to get the nodes affected and test its label in a “where” clause so the query proceeds for specific labels.
Here is an example for creating nodes.
CALL apoc.trigger.install(
'neo4j',
'POST_Created_Nodes',
'
UNWIND $createdNodes AS n
WITH n
WHERE n:LabelToPost
//place code here to post nodes
//with label LabelToPost
',
{}
)
There are other data structures you need to process similarly to handle changes due to deletions and updates.
You could process each category in separate triggers, you could merge them in one.
You could also manipulate the $createdNodes list into a list of a projection of the node data and post them in one requests, where the payload is a list. It depends on the capabilities of your API.