@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
55 lines (54 loc) • 1.58 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Projection = void 0;
const CypherASTNode_1 = require("../../CypherASTNode");
class Projection extends CypherASTNode_1.CypherASTNode {
columns = [];
isStar = false;
constructor(columns) {
super();
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;