Conditional expressions (CASE)
This page describes how to create CASE expressions in Cypher® with Cypher Builder.
Simple CASE
The simple CASE form compares a single expressions against multiple values. It can be constructed with the Case class by passing the expression to be compared the constructor:
const person = new Cypher.Node();
new Cypher.Case(person.property("eyes"))
.when(new Cypher.Literal("blue")).then(new Cypher.Literal(1))
.when(new Cypher.Literal("brown"), new Cypher.Literal("hazel")).then(new Cypher.Literal(2))
.else(new Cypher.Literal(3))
The resulting Cypher®, note that END is added automatically:
CASE n.eyes
WHEN 'blue' THEN 1
WHEN 'brown', 'hazel' THEN 2
ELSE 3
END
Generic CASE
The generic CASE expression with conditional statements can be constructed with the Case class by ignoring the constructor parameter:
const person = new Cypher.Node();
new Cypher.Case()
.when(Cypher.eq(person.property("eyes"), new Cypher.Literal("blue")))
.then(new Cypher.Literal(1))
.when(Cypher.lt(person.property("age"), new Cypher.Literal(40)))
.then(new Cypher.Literal(2))
.else(new Cypher.Literal(3))
The resulting Cypher®, note that END is added automatically:
CASE
WHEN n.eyes = 'blue' THEN 1
WHEN n.age < 40 THEN 2
ELSE 3
END