存储过程
CALL db.labels()
然而,在 Cypher Builder 中,过程可以直接调用,CALL 子句会自动生成
const dbLabels = Cypher.db.labels();
const { cypher, params } = dbLabels.build();
Cypher Builder 具有多个内置过程,例如可以作为 JavaScript 函数调用的 db.* 过程。
您可以使用 callProcedure 方法在其他子句后添加过程
const withQuery = new Cypher.With("*").callProcedure(Cypher.db.labels()).yield("label");
const { cypher, params } = withQuery.build();
自定义过程
大多数过程依赖于 Neo4j 的设置,因此需要使用 Procedure 类进行定义
const myProcedure = new Cypher.Procedure("MyProcedure")
const { cypher } = myProcedure.build();
CALL MyProcedure()
参数
内置过程直接接收 Cypher 过程参数
const myProcedure = Cypher.db.nameFromElementId("element-id")
对于自定义过程,参数需要以数组形式传入 Procedure 类
const myProcedure = new Cypher.Procedure("MyProcedure", [new Cypher.Literal("param1")])
请注意,参数必须是 Cypher 表达式,例如 Cypher.Param、Cypher.Variable 或 Cypher.Literal
复用自定义过程
自定义过程可以包装在函数中,以便像内置过程一样重复使用
function myProcedure(myParam){
return new Cypher.Procedure("my-procedure", [myParam])
}
现在可以使用此函数轻松且安全地生成对 my-procedure 的调用
const procedureClause = myProcedure("my param")
const { cypher } = procedureClause.build();
CALL my-procedure("my param")
为 YIELD 定义类型
在 TypeScript 中,可以为自定义过程的 .yield 方法定义类型
function myProcedure(myParam: string) {
return new Cypher.Procedure<"column1" | "column2">("my-procedure", [myParam])
}
这样,在生成的过程里,TypeScript 在 .yield 方法中只会接受字符串 "column1" 和 "column2"
myProcedure("my param").yield("column1"); // OK
myProcedure("my param").yield("column1", "column2"); // OK
myProcedure("my param").yield("another"); // Type Error
|
如果想创建不允许 |