@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
60 lines (59 loc) • 2.06 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.CypherFunction = void 0;
const CypherASTNode_1 = require("../../CypherASTNode");
/** Represents a Cypher Function, all Cypher functions provided by the library extend from this class, and it can be used to use custom functions
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/ | Cypher Documentation}
* @group Functions
* @example
* ```ts
* const myFunction = new Cypher.Function("myFunction", [new Cypher.Literal("test"), new Cypher.Param("test2")]);
* ```
* _Cypher:_
* ```cypher
* myFunction("test", $param0)
* ```
*/
class CypherFunction extends CypherASTNode_1.CypherASTNode {
constructor(name, params = [], namespace) {
super();
this.params = [];
this.name = namespace ? `${namespace}.${name}` : name;
for (const param of params) {
this.addParam(param);
}
}
/** @internal */
getCypher(env) {
const argsStr = this.serializeParams(env);
return `${this.name}(${argsStr})`;
}
addParam(param) {
this.params.push(param);
if (param instanceof CypherASTNode_1.CypherASTNode) {
this.addChildren(param);
}
}
serializeParams(env) {
return this.params.map((expr) => expr.getCypher(env)).join(", ");
}
}
exports.CypherFunction = CypherFunction;