jamesfer / cypher-query-builder

An flexible and intuitive query builder for Neo4j and Cypher.
http://jamesfer.me/cypher-query-builder/index.html
MIT License
105 stars 22 forks source link

Assign MATCH query to variable #114

Open kingo999 opened 4 years ago

kingo999 commented 4 years ago

Is it possible to assign a query to a variable ? For instance MATCH p=(n:Node)-[r:REL]->(x:Node) RETURN p

I would like to be able to assign to and access 'p' in the following example

db.match([ node('u', 'User', {id: '333333'}), relation('out', ['MEMBER_OF_GROUP','HAS_REPORT_ROLE'],'*'), node('r', 'Role', {allowed_delete: true}) ]).return('count(u) > 0 as authorised') .run() .then(function (results) { l.info(Is Authorised: + JSON.stringify(results[0])); });

jamesfer commented 4 years ago

Unfortunately it's not possible to do that right now unless you fallback to writing the first match clause with .raw():

db.raw`p=(U:User {id: ${id}})-[:MEMBER_OF_GROUP|HAS_REPORT_ROLE *]->(r:Role {allowed_delete: ${true}})`
  .return(....)
  ...

It is currently on the roadmap (#91) but I don't have a ETA

kingo999 commented 4 years ago

Thanks for the quick feedback. Does that also mean apoc functions like shortest path cannot be used outside of .raw ?

jamesfer commented 4 years ago

Yeah there is no special syntax for those.

databu commented 3 years ago

I've created a PR for this which simply adds another Pattern class, PathNamePattern, to allow this.

databu commented 3 years ago

By the way neo4j allows naming paths not only in MATCH, but also in CREATE and MERGE. Though the cypher reference mentions them only for MATCH.