@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
112 lines (111 loc) • 4.11 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.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 {
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 {
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})`;
}
}