James Newton
James Newton

Reputation: 7104

Can Neo4j automatically set an updatedAt field each time a node or relationship is changed?

In the application that I am writing, I manually set a createdAt and an updatedAt field on each node and relationship. This means that I can check on a regular basis if anything new has been added by another user of the application.

However, any elements edited in the same database by a different application (such as the Neo4j browser) might not set these fields, so my app will remain unaware of the changes.

Is it possible to configure Neo4j so that any modification from any source sets the updatedAt field to the current server time?

I have read about the beforeCommit transaction event, and this seems a good lead to follow, but it looks as if I'll need to write some Java code somewhere. Can you give an example of how to go about this?

Upvotes: 1

Views: 239

Answers (1)

FylmTM
FylmTM

Reputation: 2007

You should probable take look at Improved Transaction Event API by GraphAware. It gives you better API for handling events.
Using this (or going with default functionality) you can create TransactionEventHandler (i.e. TimestampUpdater) with does job for you.


To add desired functionality to Neo4j server you should create unmanaged extension. This will allow you to enrich Neo4j server with custom functionality.

You should be OK with creating basic unmanaged extension following instruction from official Neo4j documentation.

Also, there is unmanaged extension example developed by Neo4j contributor - neo4j-tx-participation. It is very simple and can give a good overview on how this can be done.

And here can be found answer on how to add something to Neo4j on unmanaged extension initialization (database startup). It gives overview how PluginLifecycle can be used.


When you have your unmanaged extension and TimestampUpdater event handler, you can combine them.

Register handler in your PluginLifecycle:

public class ExtensionPluginLifecycle implements PluginLifecycle {

    @Override
    public Collection<Injectable<?>> start(GraphDatabaseService graphDatabaseService,
                                           Configuration config) {
        graphDatabaseService.registerTransactionEventHandler(new TimestampUpdater());
    }

    @Override
    public void stop() {
    }
}

Good luck!

Upvotes: 3

Related Questions