data= {
"id": 1,
"name": "samuel",
"Manager": [
{
"id": "",
"name": "manager1",
"approvers": [325,134],
}
]
}
In relation to the above data structure, we executed an add function using a corresponding Cypher query in Neo4j.
query = "CREATE CONSTRAINT ON (users:user) ASSERT users.name IS UNIQUE";
return cypher(
{
"query": query
}
).then(function () {
query = "CREATE (user:user{ name:"samuel "})
"RETURN user";
action = "create";
return cypher(
{
"query": query,
});
}).then(function (data) {
userId = data[0].user._id;
return Promise.each(data.manager, function (entry) {
query = "MATCH (user: user) WHERE id(user) = " + userId + " " +
" OPTIONAL MATCH (managerApprovers:user) WHERE id(managerApprovers) IN [325,134] " +
"CREATE (manager: managernode {name: "manager1"})<-[:HAS_MANAGER]-(user) " +
"FOREACH (a IN CASE WHEN managerApprovers IS NOT NULL THEN [managerApprovers] ELSE [] END | " +
"CREATE (managerApprovers)<-[:HAS_MANAGE_APPROVER]-(managernode)) RETURN user,managernode";
return cypher(
{
"query": query,
}).then(function (data) {
{
res.action = action;
res.result = data;
return res;
});
}
If we need to update the user's name along with updating the manager details and the relationship HAS_MANAGE_APPROVER, HAS_MANAGER, this is how it is accomplished in Neo4j.