1
votes

I am trying to understand how to organize my node api code using neo4j as a db. I am using neo4j-javascript-driver.

I am trying to create constraints on some user fields. But I am not sure where to put the code. Because these create constraint statements should run only once. I thought of manually running statements on neo4j UI where you can run statements, but what if I go production, where I might not want to have to run statements again.

My code :

session
        .run("CREATE CONSTRAINT ON (user:USER) ASSERT user.email IS UNIQUE")
        .then((result) => {
            console.log("neo4j constraints successful");
            neoSession.close();
        })
        .catch((error) => {
            console.log("constraint.js error : ",error);
        });
1

1 Answers

3
votes

You could consider running these constraint queries as part of the deployment process. How are you deploying Neo4j? If you are doing it in some automated way (such as Ansible or Docker) you could have a Cypher script file that contains all these Cypher constraint queries and run this script after Neo4j has been successfully started using neo4j-shell.

Alternatively you could simply run these statements every time your application is started. If the constraints already exist there isn't really any harm from running the statements again.