UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

77 lines (76 loc) 3.58 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.Foreach = void 0; const compile_cypher_if_exists_1 = require("../utils/compile-cypher-if-exists"); const pad_block_1 = require("../utils/pad-block"); const Clause_1 = require("./Clause"); const WithCreate_1 = require("./mixins/clauses/WithCreate"); const WithMerge_1 = require("./mixins/clauses/WithMerge"); const WithReturn_1 = require("./mixins/clauses/WithReturn"); const WithWith_1 = require("./mixins/clauses/WithWith"); const WithDelete_1 = require("./mixins/sub-clauses/WithDelete"); const WithSetRemove_1 = require("./mixins/sub-clauses/WithSetRemove"); const mixin_1 = require("./utils/mixin"); /** * @see {@link https://neo4j.com/docs/cypher-manual/current/clauses/foreach/ | Cypher Documentation} * @group Clauses */ let Foreach = class Foreach extends Clause_1.Clause { constructor(variable, listExpr, mapClause) { super(); this.variable = variable; this.listExpr = listExpr; this.mapClause = mapClause; } in(listExpr) { this.listExpr = listExpr; return this; } do(mapClause) { this.mapClause = mapClause; return this; } /** @internal */ getCypher(env) { if (!this.listExpr) throw new Error("FOREACH needs a source list after IN using .in()"); if (!this.mapClause) throw new Error("FOREACH needs an updating command using .do()"); const variableStr = this.variable.getCypher(env); const listExpr = this.listExpr.getCypher(env); const mapClauseStr = this.mapClause.getCypher(env); const nextClause = this.compileNextClause(env); const foreachStr = [`FOREACH (${variableStr} IN ${listExpr} |`, (0, pad_block_1.padBlock)(mapClauseStr), `)`].join("\n"); const setCypher = this.compileSetCypher(env); const deleteCypher = (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.deleteClause, env, { prefix: "\n" }); return `${foreachStr}${setCypher}${deleteCypher}${nextClause}`; } }; exports.Foreach = Foreach; exports.Foreach = Foreach = __decorate([ (0, mixin_1.mixin)(WithWith_1.WithWith, WithReturn_1.WithReturn, WithSetRemove_1.WithSetRemove, WithDelete_1.WithDelete, WithCreate_1.WithCreate, WithMerge_1.WithMerge) ], Foreach);