@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
137 lines (136 loc) • 4.62 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*/
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_expr_1 = require("../../utils/normalize-expr");
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((arg) => (0, normalize_expr_1.normalizeExpr)(arg));
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 {
accVariable;
defaultValue;
variable;
listExpr;
mapExpr;
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})`;
}
}