Search code examples
javascriptneo4jcypherneo4j-driver

Neo4j - passing string parameters through JavaScript driver doesn't work


I am during the process of rewriting queries with already injected values to passing query and params object.

Queries which use ints work fine (I need to use neo4j's int translation):

...
ID(node)=$nodeId
...
LIMIT $fetchLimit
import { int } from 'neo4j-driver'
...
params = {
  nodeId: int(nodeId)
  fetchLimit: int(fetchLimit)
}

In some queries I am searching for strings and I am not using "normal" string search

WHERE node.property=$value       <-- "normal" search
WHERE node.property=~"$value.*"  <-- my search

Passing parameters doesn't work in this case.

I have a feeling that when I pass value: 'foo', neo4j resolves it as below:

WHERE node.property=~"$value.*"
...
value: 'foo'

*(injects parameters)*

WHERE node.property=~"'foo'.*"   <-- these inner quotation marks shouldn't be there

How to pass strings to this query and expect proper results?

(note that I am sure that query is written properly because when I directly inject parameters to query using JS's ${value}


Solution

  • You can merge two strings with +. The regular expression after =~ is just a string.

    So, working Cypher would be:

    WHERE node.property =~ $value + ".*"
    

    Alternatively, you could assemble the expression string in JavaScript:

    search = 'foo'
    params = {
      value: `${search}.*`
    }
    
    WHERE node.property =~ $value