0

I've installed a trigger in my Neo4j database (version 4.4.36) to automatically add the creation date to every newly created node. However, when I submit a form from my Next.js app, the app freezes, and the creation_date property is not being added to the newly created node. Here's the trigger I've installed:

CALL apoc.trigger.install(
  'db_bale', 'add_creation_date',
  'UNWIND $createdNodes AS n SET n.creation_date = $commitTime',
  {phase:'after'});

and here is the relevant part of my register action in nextJS app

"use server";
import * as z from "zod";
import { RegisterSchema } from "@/schemas";
import driver from "@/app/utils/noe4j";
import { DBLabels, UserPrivileges } from "@/globals/types";


export const register = async (values: z.infer<typeof RegisterSchema>) => {
  // ...
  const session = driver.session({ database: "qalam" });

  const query = `
    MERGE (counter: ${DBLabels.ID_COUNTER} {label: '${DBLabels.USER}'})
    ON CREATE SET counter.last_id = 0
    WITH counter
    CREATE (user: ${DBLabels.USER} {
        user_name: $user_name,
        first_name: $first_name,
        last_name: $last_name,
        email: $email,
        password: $password,
        privileges: $privileges
    })
    WITH user, counter
    CALL apoc.atomic.add(counter, 'last_id', 1) YIELD newValue
    SET user.user_id = newValue
    RETURN user
  `;

  const results = await session.executeWrite(async (tx) => {
    return tx.run(query, { ...new_user });
  });

  // ...
};

I've also checked the /etc/neo4j/apoc.conf file, and it contains the following configuration which I copied from the documentation of apoc triggers:

apoc.trigger.enabled=true
apoc.trigger.refresh=60000

also in `neo4j.conf', I added these two lines

dbms.security.procedures.allowlist=apoc.*
dbms.security.procedures.unrestricted=apoc.*

When I execute the MATCH (n) RETURN n query in the Neo4j browser, I can see that a new user is created, but the creation_date property is missing. Can you please help me understand why the trigger is not working as expected and the Next.js app is freezing?

I tried to change the default refresh time to 1000, but the same problem happens.

Also when running a create command from the neo4j browser CREATE (user: User {user_name: 'test'}) return user;

everything seems okay, and the creation date is added the new node as expected

1 Answer 1

0

Try changing your trigger to use {phase:'afterAsync'} instead of {phase:'after'}.

Here is the description of the afterAsync phase from the trigger documentation:

The trigger will be activated after the commit and inside a new transaction and thread that will not impact the original one. Heavy operations should be processed in this phase without blocking the original transaction. Please note that the 'after' and 'before' phases can sometimes block transactions, so generally the afterAsync phase is preferred.

Your Cypher query is calling apoc.atomic.add after creating a user node, and an atomic operation on the node might be causing problems with the trigger. Running the trigger asynchronously will hopefully avoid that.

Also, do you really need to have consecutive last_id values? And do you really need a trigger (you could just set creation_date in your CREATE clause using a timestamp() or datetime() value)? Both of those things add complexity and use more resources.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer, actually I do not necessarily need a trigger, I'm learning neo4j, so I just picked a use case that I can apply to my project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.