参数

动态查询的一个常见挑战是跟踪参数。为了解决这个问题,您可以使用 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"
}

添加额外参数

可以向生成的参数对象中注入额外的参数

const { cypher, params } = query.build({
    extraParams: {
        myExtraParam1: "Extra Param",
    },
});

参数 myExtraParam 将被添加到对象 params

参数
{
    myExtraParam1: "Extra Param",
    param0: "The Matrix"
}
© . This site is unofficial and not affiliated with Neo4j, Inc.