2

I am building a REST API which connects to a NEO4J instance. I am using the koa-neo4j library as the basis (https://github.com/assister-ai/koa-neo4j-starter-kit). I am a beginner at all these technologies but thanks to some help from this forum I have the basic functionality working. For example the below code allows me to create a new node with the label "metric" and set the name and dateAdded propertis.

URL:

/metric?metricName=Test&dateAdded=2/21/2017

index.js

app.defineAPI({
    method: 'POST',
    route: '/api/v1/imm/metric',
    cypherQueryFile: './src/api/v1/imm/metric/createMetric.cyp'
});

createMetric.cyp"

CREATE (n:metric { 
    name: $metricName, 
    dateAdded: $dateAdded
}) 
return ID(n) as id

However, I am struggling to know how I can approach more complicated examples. How can I handle situations when I don't know how many properties will be added when creating a new node beforehand or when I want to create multiple nodes in a single post statement. Ideally I would like to be able to pass something like JSON as part of the POST which would contain all of the nodes, labels and properties that I want to create. Is something like this possible? I tried using the below Cypher query and passing a JSON string in the POST body but it didn't work.

UNWIND $props AS properties
CREATE (n:metric)
SET n = properties
RETURN n

Would I be better off switching tothe Neo4j Rest API instead of the BOLT protocol and the KOA-NEO4J framework. From my research I thought it was better to use BOLT but I want to have a Rest API as the middle layer between my front and back end so I am willing to change over if this will be easier in the longer term.

Thanks for the help!

n4nite
  • 459
  • 4
  • 19

2 Answers2

3

Your Cypher syntax is bad in a couple of ways.

  1. UNWIND only accepts a collection as its argument, not a string.
  2. SET n = properties is only legal if properties is a map, not a string.

This query should work for creating a single node (assuming that $props is a map containing all the properties you want to store with the newly created node):

CREATE (n:metric $props)
RETURN n

If you want to create multiple nodes, then this query (essentially the same as yours) should work (but only if $prop_collection is a collection of maps):

UNWIND $prop_collection AS props
CREATE (n:metric)
SET n = props
RETURN n
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Thanks for your input. I tried your code and passed the following "props=[{metricName:"Test Name", dateCreated:"02/21/2017"}]" but I get the error "Parameter provided for node creation is not a Map". Clearly I am not structuring the "map" correctly - any pointers on this? – n4nite Feb 21 '17 at 21:10
  • 1
    @n4nite Your props parameter must be a map, and the value you want to use in your query should be an entry in that map whose value is a collection of maps. If we're using cybersam's example, to use the `$prop_collection` parameter, your props would have to be: `props={prop_collection: [{metricName:"Test Name", dateCreated:"02/21/2017"}]}` – InverseFalcon Feb 21 '17 at 21:22
  • Is there somewhere that I can read up on maps in Neo4j as I can't get this to work and can't find much when I search Google? I am using the examples and passing the "props" string as part of my POST statement to the API but I still get an error that "Parameter provided for node creation is not a Map". – n4nite Feb 21 '17 at 21:38
  • Thanks - it took me a bit of time to get this working but your answer is spot on. – n4nite Feb 27 '17 at 21:04
1

I too have faced difficulties when trying to pass complex types as arguments to neo4j, this has to do with type conversions between js and cypher over bolt and there is not much one could do except for filing an issue in the official neo4j JavaScript driver repo. koa-neo4j uses the official driver under the hood.

One way to go about such scenarios in koa-neo4j is using JavaScript to manipulate the arguments before sending to Cypher:

https://github.com/assister-ai/koa-neo4j#preprocess-lifecycle

Also possible to further manipulate the results of a Cypher query using postProcess lifecycle hook:

https://github.com/assister-ai/koa-neo4j#postprocess-lifecycle

Keyvan
  • 813
  • 9
  • 19
  • Hi Keyvan. Thanks for your comments and the great Koa-Neo4j framework. I am new to both Node, Koa and Neo4j so the framework has been a fantastic way to get up and running. Once I get more competent in my coding skills I hope to contribute to the framework! – n4nite Mar 04 '17 at 10:10
  • Cheers @n4nite, I'd be more than happy to help with any framework related issues that might arise, just [file a GitHub issue](https://github.com/assister-ai/koa-neo4j/issues) if you encountered one. I'm delighted to see that you're doing cool things with `koa-neo4j`. – Keyvan Mar 04 '17 at 12:27