UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

146 lines (145 loc) 5.13 kB
"use strict"; /* * 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.keys = keys; exports.labels = labels; exports.range = range; exports.reverse = reverse; exports.tail = tail; exports.toBooleanList = toBooleanList; exports.toFloatList = toFloatList; exports.toIntegerList = toIntegerList; exports.toStringList = toStringList; exports.reduce = reduce; const filter_truthy_1 = require("../../utils/filter-truthy"); const normalize_variable_1 = require("../../utils/normalize-variable"); const CypherFunctions_1 = require("./CypherFunctions"); /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-keys | Cypher Documentation} * @group Functions * @category List */ function keys(expr) { return new CypherFunctions_1.CypherFunction("keys", [expr]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-labels | Cypher Documentation} * @group Functions * @category List */ function labels(nodeRef) { return new CypherFunctions_1.CypherFunction("labels", [nodeRef]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-range | Cypher Documentation} * @group Functions * @category List */ function range(start, end, step) { const params = (0, filter_truthy_1.filterTruthy)([start, end, step]).map(normalize_variable_1.normalizeExpr); return new CypherFunctions_1.CypherFunction("range", params); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-reverse-list | Cypher Documentation} * @group Functions * @category List */ function reverse(list) { return new CypherFunctions_1.CypherFunction("reverse", [list]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-tail | Cypher Documentation} * @group Functions * @category List */ function tail(list) { return new CypherFunctions_1.CypherFunction("tail", [list]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-tobooleanlist | Cypher Documentation} * @group Functions * @category List */ function toBooleanList(list) { return new CypherFunctions_1.CypherFunction("toBooleanList", [list]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-tofloatlist | Cypher Documentation} * @group Functions * @category List */ function toFloatList(list) { return new CypherFunctions_1.CypherFunction("toFloatList", [list]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-tointegerlist | Cypher Documentation} * @group Functions * @category List */ function toIntegerList(list) { return new CypherFunctions_1.CypherFunction("toIntegerList", [list]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-tostringlist | Cypher Documentation} * @group Functions * @category List */ function toStringList(list) { return new CypherFunctions_1.CypherFunction("toStringList", [list]); } /** Reduce a list by executing given expression. * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/list/#functions-reduce | Cypher Documentation} * @group Functions * @category List * @example * ```ts * Cypher.reduce(totalAge, new Cypher.Literal(0), n, Cypher.nodes(p), Cypher.plus(totalAge, n.property("age"))); * ``` * _Cypher:_ * ```cypher * reduce(totalAge = 0, n IN nodes(p) | totalAge + n.age) * ``` */ function reduce(accVariable, defaultValue, variable, listExpr, mapExpr) { return new ReducerFunction({ accVariable, defaultValue, variable, listExpr, mapExpr, }); } class ReducerFunction extends CypherFunctions_1.CypherFunction { constructor({ accVariable, defaultValue, variable, listExpr, mapExpr, }) { super("reduce"); this.accVariable = accVariable; this.defaultValue = defaultValue; this.variable = variable; this.listExpr = listExpr; this.mapExpr = mapExpr; } getCypher(env) { const accStr = `${this.accVariable.getCypher(env)} = ${this.defaultValue.getCypher(env)}`; const variableStr = this.variable.getCypher(env); const listExprStr = this.listExpr.getCypher(env); const mapExprStr = this.mapExpr.getCypher(env); return `${this.name}(${accStr}, ${variableStr} IN ${listExprStr} | ${mapExprStr})`; } }