@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
92 lines (91 loc) • 3.56 kB
JavaScript
;
/*
* 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.runFirstColumnMany = runFirstColumnMany;
exports.runFirstColumnSingle = runFirstColumnSingle;
const CypherFunctions_1 = require("../../../expressions/functions/CypherFunctions");
const MapExpr_1 = require("../../../expressions/map/MapExpr");
/**
* @group Functions
* @deprecated apoc methods will no longer be supported in Cypher Builder version 3
* @see [Apoc Documentation](https://neo4j.com/docs/apoc/current/overview/apoc.cypher/apoc.cypher.runFirstColumnMany/)
*/
function runFirstColumnMany(clause, params = []) {
return new RunFirstColumnFunction(clause, params, true);
}
/**
* @group Functions
* @deprecated apoc methods will no longer be supported in Cypher Builder version 3
* @see [Apoc Documentation](https://neo4j.com/docs/apoc/current/overview/apoc.cypher/apoc.cypher.runFirstColumnSingle/)
*/
function runFirstColumnSingle(clause, params = []) {
return new RunFirstColumnFunction(clause, params, false);
}
class RunFirstColumnFunction extends CypherFunctions_1.CypherFunction {
constructor(clause, variables, many) {
super(`apoc.cypher.runFirstColumn${many ? "Many" : "Single"}`); // Note: this argument is never used
this.innerClause = clause;
this.variables = this.parseVariablesInput(variables);
this.many = many;
}
/** @internal */
getCypher(env) {
let clauseStr;
let paramsStr;
if (typeof this.innerClause === "string") {
clauseStr = this.innerClause;
}
else {
clauseStr = this.innerClause.getRoot().getCypher(env);
}
if (Array.isArray(this.variables)) {
paramsStr = this.convertArrayToParams(env, this.variables);
}
else {
paramsStr = this.variables.getCypher(env);
}
if (this.many) {
return `apoc.cypher.runFirstColumnMany("${this.escapeQuery(clauseStr)}", ${paramsStr})`;
}
return `apoc.cypher.runFirstColumnSingle("${this.escapeQuery(clauseStr)}", ${paramsStr})`;
}
escapeQuery(query) {
return query.replaceAll(/(["\\])/g, "\\$1");
}
parseVariablesInput(variables) {
if (Array.isArray(variables) || variables instanceof MapExpr_1.MapExpr)
return variables;
return new MapExpr_1.MapExpr(variables);
}
convertArrayToParams(env, variables) {
const params = variables.reduce((acc, variable) => {
const globalScopeName = variable.getCypher(env);
const key = env.getReferenceId(variable);
acc[key] = globalScopeName;
return acc;
}, {});
const paramsStr = Object.entries(params)
.map(([key, value]) => {
return `${key}: ${value}`;
})
.join(", ");
return `{ ${paramsStr} }`;
}
}