UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

121 lines (120 loc) 3.76 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.labelExpr = exports.LabelExpr = void 0; const compile_cypher_if_exists_1 = require("../../utils/compile-cypher-if-exists"); const escape_1 = require("../../utils/escape"); /** * Label Expression to be used as part of a Pattern * @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/expressions/#label-expressions | Cypher Documentation} * @group Patterns */ class LabelExpr { constructor(operator) { this.operator = operator; } compileLabel(expr, env) { if (typeof expr === "string") { return (0, escape_1.escapeLabel)(expr); } else if (expr instanceof LabelExpr) { return (0, compile_cypher_if_exists_1.compileCypherIfExists)(expr, env); } else { return `$(${(0, compile_cypher_if_exists_1.compileCypherIfExists)(expr, env)})`; } } } exports.LabelExpr = LabelExpr; class BinaryLabelExpr extends LabelExpr { constructor(operator, labels) { super(operator); this.labels = labels; } /** * @internal */ getCypher(env) { const labelStrs = this.labels.map((l) => this.compileLabel(l, env)); if (labelStrs.length === 0) return ""; return `(${labelStrs.join(this.operator)})`; } } class NotLabelExpr extends LabelExpr { constructor(label) { super("!"); this.label = label; } /** * @internal */ getCypher(env) { const labelStrs = this.compileLabel(this.label, env); return `${this.operator}${labelStrs}`; } } class WildcardLabelExpr extends LabelExpr { constructor() { super("%"); } /** * @internal */ getCypher() { return this.operator; } } /** Generates an `&` operator between labels or types * @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/expressions/#label-expressions | Cypher Documentation} * @group Expressions * @category Operators */ function labelAnd(...labels) { return new BinaryLabelExpr("&", labels); } /** Generates an `|` operator between labels or types * @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/expressions/#label-expressions | Cypher Documentation} * @group Expressions * @category Operators */ function labelOr(...labels) { return new BinaryLabelExpr("|", labels); } /** Generates an `!` operator for a label or type * @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/expressions/#label-expressions | Cypher Documentation} * @group Expressions * @category Operators */ function labelNot(label) { return new NotLabelExpr(label); } /** Generates an wildcard (`%`) operator to substitute a label or type * @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/expressions/#label-expressions | Cypher Documentation} * @group Expressions * @category Operators */ const wildcard = new WildcardLabelExpr(); exports.labelExpr = { and: labelAnd, or: labelOr, not: labelNot, wildcard, };