参数
动态查询的一个常见挑战是跟踪参数。为了解决这个问题,您可以使用 Param 类来创建 Cypher® Builder 参数。
参数的行为类似于变量,但它们包含一个已定义的值,当调用 .build() 方法时,这些值会自动作为参数返回。例如
const movie = new Cypher.Node({ labels: ["Movie"] });
const titleProp = movie.property(movie);
const query = new Cypher.Match(movie).where(Cypher.eq(titleProp, new Cypher.Param("The Matrix")));
const { cypher, params } = query.build();
返回以下 Cypher
MATCH (this0:Movie)
WHERE this0[this0] = $param0
以及以下参数对象
{
param0: "The Matrix"
}
参数也可以像变量一样重复使用
const movie = new Cypher.Node({ labels: ["Movie"] });
const titleProp = movie.property(movie);
const titleParam = new Cypher.Param("The Matrix");
const query = new Cypher.Match(movie).where(Cypher.eq(titleProp, titleParam)).return(titleParam);
const {cypher, params} = query.build();
Cypher
MATCH (this0:Movie)
WHERE this0[this0] = $param0
RETURN this0, $param0
参数
{
param0: "The Matrix"
}
在这种情况下,Cypher Builder 为参数提供了一个名称,并正确地将其链接到 Param 对象。
请注意,如果使用了两个 Param 实例,则无论值是否相同,都应返回两个独立的参数。例如
const movie = new Cypher.Node({ labels: ["Movie"] });
const titleProp = movie.property(movie);
const titleParam1 = new Cypher.Param("The Matrix");
const titleParam2 = new Cypher.Param("The Matrix")
const query = new Cypher.Match(movie).where(Cypher.eq(titleProp, titleParam1)).return(titleParam2);
const {cypher, params} = query.build();
Cypher
MATCH (this0:Movie)
WHERE this0[this0] = $param0
RETURN this0, $param0
参数
{
param0: "The Matrix"
}