Merge

This page describes how to create a MERGE clause with the Cypher®.Merge class.

To add a MERGE clause, first create a valid pattern using the Pattern class and pass it to Merge constructor:

const movie = new Cypher.Node();
const actor = new Cypher.Node();
const pattern = new Cypher.Pattern(movie, { labels: ["Movie"] }).related({type: ["ACTED_IN"]}).to(actor);

const mergeQuery = new Cypher.Merge(pattern);
const { cypher, params } = matchQuery.build()

This generates the following MERGE clause:

MERGE (this:Movie)-[:ACTED_IN]->(this1)

Afterwards, you can add other clauses. For example:

const mergeQuery = new Cypher.Merge(pattern)
    .where(Cypher.eq(movie.property("name"), new Cypher.Param("my-movie")))
    .return(movie);
MERGE (this:Movie)-[:ACTED_IN]->(this1)
WHERE this.name = $param1
return this

ON CREATE and ON MATCH

Use the methods onCreateSet or onMatchSet to add ON CREATE SET or ON MATCH SET after a MERGE statement:

const node = new Cypher.Node();

const countProp = node.property("count");
const query = new Cypher.Merge(
    new Cypher.Pattern(node, {
        labels: ["MyLabel"],
    })
)
    .onCreateSet([countProp, new Cypher.Literal(1)])
    .onMatchSet([countProp, Cypher.plus(countProp, new Cypher.Literal(1))]);
MERGE (this0:MyLabel)
ON MATCH SET
    this0.count = (this0.count + 1)
ON CREATE SET
    this0.count = 1