@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
81 lines (80 loc) • 2.99 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 {
constructor(name, params = [], namespace) {
super();
this._optional = false;
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 {
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;