UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

87 lines (86 loc) 2.93 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.CompositeClause = void 0; const filter_truthy_1 = require("../../utils/filter-truthy"); const Clause_1 = require("../Clause"); const Union_1 = require("../Union"); /** The result of multiple clauses concatenated with `Cypher.utils.concat` * @group Utils */ class CompositeClause extends Clause_1.Clause { /** * @internal */ constructor(children, separator) { super(); this.separator = separator; this._children = []; this.concat(...children); } concat(...clauses) { const filteredChildren = this.filterClauses(clauses); this.addChildren(...filteredChildren); this._children = [...this._children, ...filteredChildren]; return this; } get empty() { return this._children.length === 0; } /** @deprecated Children from a composite clause should not be accessed as this will lead to unexpected behaviour */ get children() { return this._children; } /** @internal */ getCypher(env, importWithCypher) { // NOTE: importWithCypher used to pass down import WITH to UNION clauses const childrenStrs = this._children.map((c) => { if (importWithCypher && c instanceof Union_1.Union) { return c.getCypher(env, importWithCypher); } return c.getCypher(env); }); return childrenStrs.join(this.separator); } filterClauses(clauses) { const childrenRoots = (0, filter_truthy_1.filterTruthy)(clauses).map((c) => c.getRoot()); return this.filterEmptyComposite(childrenRoots).map((c) => { if (c instanceof CompositeClause) { return this.unwrapComposite(c); } return c; }); } filterEmptyComposite(children) { return children.filter((c) => { if (c instanceof CompositeClause && c.empty) return false; return true; }); } unwrapComposite(clause) { if (clause._children.length === 1) { return clause._children[0]; } else return clause; } } exports.CompositeClause = CompositeClause;