Fluent Cypher
This package allows you to build any cypher query you like and get both the query string and the parameters as an object to be used with the official neo4j driver.
If you want to be able to connect seamlessy to your Neo4j instance have a look at fluent-neo4j otherwise you can always use this package with your own driver/connector.
What is Cypher
This guide explains the basic concepts of Cypher, Neo4j’s query language.
Following the official documentation it is always better to avoid literals so everything is treated as a parameter.
Table of Contents
Usage
const CypherQuery = ;//or var query = ; query
constuctor([config])
Option | Type | Description |
---|---|---|
onCreateSetTimestamp |
Boolean |
timestamps will be added for you like node.createdAt = timestamp() |
onUpdateSetTimestamp |
Boolean |
timestamps will be added for you like node.updatedAt = timestamp() |
userId |
String |
Property will be set like node.createdBy = {userId} and node.updatedBy = {userId} |
defaultNodeProps |
Object |
default props for every node |
forcetNodeProps |
Object |
force props for every node |
defaultRelProps |
Object |
default props for every relationship |
forcetRelProps |
Object |
force props for every relationship |
Building the query
.create(Pattern[, Pattern])
See Pattern for accepted arguments
query //CREATE (node), ()->[rel]->()query //CREATE (node1{prop:false}), (node2{val:12})query // 'CREATE (parent)-[:has]->(child)'
.match(Pattern[, Pattern])
See Pattern for accepted arguments
query // MATCH (node)query // MATCH (node), ()->[rel]->()query //MATCH (node1{prop:false}), (node2{val:12})query // 'MATCH (parent)-[:has]->(child)'
.optionalMatch(Pattern[, Pattern])
See Pattern for accepted arguments
query // MATCH OPTIONAL (node:Stuff)
.where(WhereItem[, WhereItem])
query// WHERE user.fullName =~ (?i).*tom.*
.merge(Pattern[, Pattern])
See Pattern for accepted arguments
query // MERGE (node)query // MERGE (node), ()->[rel:`type`]->()
.set(PropItem[, PropItem])
query // SET friend.rating = 5query // SET friend:lol:lel, friend.wow = rating
.onCreateSet(PropItem[, PropItem])
query // ON CREATE SET friend.rating = 5
.onMatchSet(PropItem[, PropItem])
query // ON MATCH SET friend.rating = 5
.remove(PropItem[, PropItem])
query// REMOVE p:one:may:april, p.t, p.lel, p.lol
.delete(DeleteItem[, DeleteItem])
query /*MATCH (lonely)WHERE NOT (lonely)-[:has]->(:Friend)DELETE friend*/
.detachDelete(DeleteItem[, DeleteItem])
query /*MATCH (me)-[:knows]->(friend)DETACH DELETE friend*/
.return(ReturnItem[, ReturnItem])
query // RETURN *query // RETURN nodequery // RETURN node.propquery // RETURN node.p as that
.returnDistinct(ReturnItem[, ReturnItem])
query // RETURN DISTINCT *
.limit(Integer)
query // LIMIT 1
.skip(Integer)
query // LIMIT 1
.orderBy(Integer)
query // ORDER BY node.key ASC
.unwind(UnwindItem)
query //UNWIND [1,2,3] as numberquery //UNWIND [1,2,3] as number (parameterized)query //UNWIND collection as listquery //UNWIND $param as entry
.with(AliasedItem[, AliasedItem])
query //WITH this as that, node AS something
.union()
query//UNION
.unionAll()
query//UNION ALL
.loadCsv(url, options)
q//LOAD CSV FROM "https://neo4j.com/docs/cypher-refcard/3.2/csv/artists.csv" AS row
.call(string)
q//CALL dbms.procedures()
.yield(string)
q//YIELD name, signature
Argument Types
Pattern
Cypher
As String see Node
As Object see Path
As Array see Cypher
String only
This is not manipulated at all and gets inserted in the context as is
'node' //(node) if in node context'rel:type' //...-[rel:type]->... if in rel context'CASE WHEN 1=1 THEN this ELSE that END as what' //CASE WHEN 1=1 THEN this ELSE that END as what
Node
As String
see Cypher
As Object
Key | Required | Type | Description |
---|---|---|---|
$ |
no | String | Variable name for node (must be valid variable name) |
label |
no | String | Label for node |
labels |
no | Array | Label for node |
...rest |
no | String | Arrray |
$: 'node' label: 'Cat' labels: 'Animal' 'Living' this: 'that' something: 'li' 'la' //(node:Cat:Animal:Living)
Rel
As String
Interpreted as Cypher
As Object
Props
Key | Required | Type | Description |
---|---|---|---|
$ |
no | String |
Variable name for rel (must be valid variable name) |
type |
yes (in merge) | String |
Type of rel |
depth |
no | Integer|String |
Eiter * or 4 or 1..2 |
direction |
no | String |
Eiter left or right (default) or both |
...rest |
no | String|Arrray |
Actual properties of the rel |
Example
$: 'rel' type: 'Follows' depth: '..5' direction: 'both' something: 'amigo' //...)-(rel:Follows*..5{something:['amigo']})-(...
Path
As Array
If the number of elements is even, the first Object is used for Path options
Props
Key | Required | Type | Description |
---|---|---|---|
$ |
no | String | Variable name for path (must be valid variable name) |
shotestPath |
no | Bool | Whether to use the shortestPath or not |
Example
$: 'myPath' shotestPath: true $: 'start' {} 'final' // myPath = shortestPath((start)-[]->(final))
.log()
As query.queryString
is a parametrised string you may want to print a string that you can copy and paste in the browser console.
query // => MATCH (node) // => MATCH (node) MATCH ()-[rel]->()
Test
Tests are written in ava, run the following command
npm t