@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
47 lines (46 loc) • 1.47 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*/
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 {
name;
params = [];
constructor(name, params = [], namespace) {
super();
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;