UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

102 lines (101 loc) 3.58 kB
"use strict"; /* * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] */ Object.defineProperty(exports, "__esModule", { value: true }); exports.PredicateFunction = void 0; exports.all = all; exports.any = any; exports.exists = exists; exports.isEmpty = isEmpty; exports.none = none; exports.single = single; const Where_1 = require("../../clauses/sub-clauses/Where"); const compile_cypher_if_exists_1 = require("../../utils/compile-cypher-if-exists"); const CypherFunctions_1 = require("./CypherFunctions"); /** Represents a predicate function that can be used in a WHERE statement * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/predicate/ | Cypher Documentation} * @group Functions * @category Predicate */ class PredicateFunction extends CypherFunctions_1.CypherFunction { } exports.PredicateFunction = PredicateFunction; /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/predicate/#functions-all | Cypher Documentation} * @group Functions * @category Predicate */ function all(variable, listExpr, whereFilter) { return new ListPredicateFunction("all", variable, listExpr, whereFilter); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/predicate/#functions-any | Cypher Documentation} * @group Functions * @category Predicate */ function any(variable, listExpr, whereFilter) { return new ListPredicateFunction("any", variable, listExpr, whereFilter); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/predicate/#functions-exists | Cypher Documentation} * @group Functions * @category Predicate */ function exists(pattern) { return new ExistsFunction(pattern); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/predicate/#functions-isempty | Cypher Documentation} * @group Functions * @category Predicate */ function isEmpty(list) { return new PredicateFunction("isEmpty", [list]); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/predicate/#functions-none | Cypher Documentation} * @group Functions * @category Predicate */ function none(variable, listExpr, whereFilter) { return new ListPredicateFunction("none", variable, listExpr, whereFilter); } /** * @see {@link https://neo4j.com/docs/cypher-manual/current/functions/predicate/#functions-single | Cypher Documentation} * @group Functions * @category Predicate */ function single(variable, listExpr, whereFilter) { return new ListPredicateFunction("single", variable, listExpr, whereFilter); } class ExistsFunction extends PredicateFunction { pattern; constructor(pattern) { super("exists"); this.pattern = pattern; } getCypher(env) { const patternStr = this.pattern.getCypher(env); return `exists(${patternStr})`; } } /** Predicate function that uses a list comprehension "var IN list WHERE .." */ class ListPredicateFunction extends PredicateFunction { variable; listExpr; whereSubClause; constructor(name, variable, listExpr, whereFilter) { super(name); this.variable = variable; this.listExpr = listExpr; this.whereSubClause = new Where_1.Where(this, whereFilter); } getCypher(env) { const whereStr = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.whereSubClause, env, { prefix: " " }); const listExprStr = this.listExpr.getCypher(env); const varCypher = this.variable.getCypher(env); return `${this.name}(${varCypher} IN ${listExprStr}${whereStr})`; } }