node.jsneo4jkoaneo4j-bolt

Passing sets of properties and nodes as a POST statement wit KOA-NEO4J or BOLT


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!


Solution

  • 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