@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
70 lines (69 loc) • 2.43 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CypherProcedure = exports.VoidCypherProcedure = void 0;
const Clause_1 = require("../clauses/Clause");
const CypherASTNode_1 = require("../CypherASTNode");
const compile_cypher_if_exists_1 = require("../utils/compile-cypher-if-exists");
const Yield_1 = require("./Yield");
/** Cypher Procedure that does not yield columns
* @see {@link https://neo4j.com/docs/cypher-manual/current/clauses/call/ | Cypher Documentation}
* @group Procedures
*/
class VoidCypherProcedure extends Clause_1.Clause {
name;
params;
_optional = false;
constructor(name, params = [], namespace) {
super();
this.name = namespace ? `${namespace}.${name}` : name;
this.params = params;
for (const param of params) {
if (param instanceof CypherASTNode_1.CypherASTNode) {
this.addChildren(param);
}
}
}
optional() {
this._optional = true;
return this;
}
/** @internal */
getCypher(env) {
const procedureCypher = this.getProcedureCypher(env);
const optionalStr = this.generateOptionalStr();
return `${optionalStr}CALL ${procedureCypher}`;
}
getProcedureCypher(env) {
const argsStr = this.params.map((expr) => expr.getCypher(env)).join(", ");
return `${this.name}(${argsStr})`;
}
generateOptionalStr() {
return this._optional ? "OPTIONAL " : "";
}
}
exports.VoidCypherProcedure = VoidCypherProcedure;
/** Cypher Procedure
* @see {@link https://neo4j.com/docs/cypher-manual/current/clauses/call/ | Cypher Documentation}
* @group Procedures
*/
class CypherProcedure extends VoidCypherProcedure {
yieldStatement;
yield(...columns) {
if (columns.length === 0)
throw new Error("Empty projection in CALL ... YIELD");
this.yieldStatement = new Yield_1.Yield(columns);
this.addChildren(this.yieldStatement);
return this.yieldStatement;
}
/** @internal */
getCypher(env) {
const callCypher = super.getCypher(env);
const yieldCypher = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.yieldStatement, env, { prefix: " " });
return `${callCypher}${yieldCypher}`;
}
}
exports.CypherProcedure = CypherProcedure;