UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

107 lines (106 loc) 4.83 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. */ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NestedPattern = exports.Pattern = void 0; const Environment_1 = require("../Environment"); const WithWhere_1 = require("../clauses/mixins/sub-clauses/WithWhere"); const mixin_1 = require("../clauses/utils/mixin"); const Variable_1 = require("../references/Variable"); const compile_cypher_if_exists_1 = require("../utils/compile-cypher-if-exists"); const pad_block_1 = require("../utils/pad-block"); const PartialPattern_1 = require("./PartialPattern"); const PathAssign_1 = require("./PathAssign"); const PatternElement_1 = require("./PatternElement"); const labels_to_string_1 = require("./labels-to-string"); const QuantifiedPattern_1 = require("./quantified-patterns/QuantifiedPattern"); /** Represents a pattern of a single node or n-relationships to be used in clauses. * @see {@link https://neo4j.com/docs/cypher-manual/current/syntax/patterns/ | Cypher Documentation} * @group Patterns */ let Pattern = class Pattern extends PatternElement_1.PatternElement { constructor(nodeVariable, options = {}) { const firstArgumentIsVar = nodeVariable instanceof Variable_1.Variable; const variable = firstArgumentIsVar ? nodeVariable : undefined; super(variable); if (!firstArgumentIsVar) { options = nodeVariable ?? {}; } this.labels = options.labels; this.properties = options.properties; } related(ref, options) { if (ref instanceof Variable_1.Variable) { return new PartialPattern_1.PartialPattern(ref, options ?? {}, this); } else { return new PartialPattern_1.PartialPattern(undefined, options ?? ref ?? {}, this); } } /** Adds a quantifier to the pattern such as `{1,3}`, to be used as part of a {@link QuantifiedPath} */ quantifier(quantifier) { return new QuantifiedPattern_1.QuantifiedPattern(this, quantifier); } assignTo(variable) { return new PathAssign_1.PathAssign(this, variable); } /** * @internal */ getCypher(env) { const prevStr = this.previous?.getCypher(env) ?? ""; const nodeRefId = this.variable ? `${this.variable.getCypher(env)}` : ""; const propertiesStr = this.properties ? this.serializeParameters(this.properties, env) : ""; const nodeLabelStr = this.getLabelsStr(env); const whereStr = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.whereSubClause, env, { prefix: " " }); return `${prevStr}(${nodeRefId}${nodeLabelStr}${propertiesStr}${whereStr})`; } getLabelsStr(env) { if (this.labels) { return (0, labels_to_string_1.labelsToString)(this.labels, env); } return ""; } }; exports.Pattern = Pattern; exports.Pattern = Pattern = __decorate([ (0, mixin_1.mixin)(WithWhere_1.WithWhere) ], Pattern); // This exists to support a "previous" parameter without exposing this to the user class NestedPattern extends Pattern { constructor(nodeVariable, options = {}, previous) { // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument super(nodeVariable, options); this.previous = previous; } /** Overrides custom string to `Pattern` instead of `NestedPattern` * @hidden */ toString() { const cypher = (0, pad_block_1.padBlock)(this.getCypher(new Environment_1.CypherEnvironment())); return `<Pattern> """\n${cypher}\n"""`; } } exports.NestedPattern = NestedPattern;