Securing unique nodes with neo4jclient

Is there a way to ensure uniqueness when creating a node with neo4jclient?

This transactions link shows how to do this using java and transactions, but I don't see transaction support in neo4jclient. I was able to do this using Cypher's explicit string query like this:

"start n=node:node_auto_index(name={id}) with count(*) as c where c=0 create x={name:{id}} return c" 

But this is obviously a hack. Is there a better way?

+4
source share
1 answer

Transaction support will ship with Neo4j 2.0 and later Neo4jClient. This issue is tracking work: https://bitbucket.org/Readify/neo4jclient/issue/91/support-cypher-transactions-integrated

It does not give you uniqueness, though ...

Neo4j does not have unique indexes - this is something to automatically provide this idea. (I expect that we will see this with Neo4j 2.0 labels in the future, but not yet.)

You need to either a) know that what you are creating is unique, or b) check first.

It seems you take route B.

Transactions allow you to verify and then create as part of a single transactional action, but still several calls through the wire.

The Cypher text you wrote out is actually preferable: you check and create in one statement. I am intrigued to understand why you think this is hacking.

You can execute this expression through Neo4jClient with something like:

 var id = 123; graphClient.Cypher .Start(new { n = Node.ByIndexLookup("node_auto_index", "name", id)}) .With("count(*) as c") .Where("c=0") .Create("x={0}", new MyType { name = id }) .Return<Node<MyType>>("c") 

Some of the With and Where statements would be nice if they were cleaner, but now they are functional.

There is also a Cypher CREATE UNIQUE clause that may also cover your scenario.

+2
source

Source: https://habr.com/ru/post/1480048/


All Articles