UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

69 lines (68 loc) 2.19 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.Projection = void 0; const CypherASTNode_1 = require("../../CypherASTNode"); class Projection extends CypherASTNode_1.CypherASTNode { constructor(columns) { super(); this.columns = []; this.isStar = false; this.addColumns(columns); } addColumns(columns) { const filteredColumns = columns.filter((v) => { if (v === "*") { this.isStar = true; return false; } return true; }); this.columns.push(...filteredColumns); } /** @internal */ getCypher(env) { let columnsStrs = this.columns.map((column) => { return this.serializeColumn(column, env); }); // Only a single star at the beginning is allowed if (this.isStar) { columnsStrs = ["*", ...columnsStrs]; } return columnsStrs.join(", "); } serializeColumn(column, env) { const hasAlias = Array.isArray(column); if (hasAlias) { const exprStr = column[0].getCypher(env); const alias = column[1]; let aliasStr; if (typeof alias === "string") { aliasStr = alias; } else { aliasStr = alias.getCypher(env); } return `${exprStr} AS ${aliasStr}`; } return column.getCypher(env); } } exports.Projection = Projection;