@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
96 lines (95 loc) • 3.67 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.Clause = void 0;
const CypherASTNode_1 = require("../CypherASTNode");
const Environment_1 = require("../Environment");
const compile_cypher_if_exists_1 = require("../utils/compile-cypher-if-exists");
const pad_block_1 = require("../utils/pad-block");
const to_cypher_params_1 = require("../utils/to-cypher-params");
const customInspectSymbol = Symbol.for("nodejs.util.inspect.custom");
/** Represents a clause AST node
* @group Clauses
*/
class Clause extends CypherASTNode_1.CypherASTNode {
/** Compiles a clause into Cypher and params */
build(config) {
const { prefix, extraParams = {}, labelOperator = ":", cypherVersion, unsafeEscapeOptions = {} } = config ?? {};
if (this.isRoot) {
const env = this.getEnv(prefix, {
labelOperator,
cypherVersion,
unsafeEscapeOptions,
});
const cypher = this.getCypher(env);
const cypherParams = (0, to_cypher_params_1.toCypherParams)(extraParams);
env.addExtraParams(cypherParams);
return {
cypher: this.prependCypherVersionClause(cypher, cypherVersion),
params: env.getParams(),
};
}
const root = this.getRoot();
if (root instanceof Clause) {
return root.build({ prefix, extraParams, labelOperator, cypherVersion, unsafeEscapeOptions });
}
throw new Error(`Cannot build root: ${root.constructor.name}`);
}
getEnv(prefix, config = {}) {
return new Environment_1.CypherEnvironment(prefix, config);
}
prependCypherVersionClause(cypher, version) {
let cypherVersionStr = "";
if (version) {
cypherVersionStr = `CYPHER ${version}\n`;
}
return `${cypherVersionStr}${cypher}`;
}
/** Custom string for browsers and templating
* @hidden
*/
toString() {
try {
const cypher = (0, pad_block_1.padBlock)(this.build().cypher);
return `<Clause ${this.constructor.name}> """\n${cypher}\n"""`;
}
catch (error) {
const errorName = error instanceof Error ? error.message : "";
return `<Clause ${this.constructor.name}> """\nError: ${errorName}\n"""`;
}
}
/** Custom log for console.log in Node
* @hidden
*/
[customInspectSymbol]() {
return this.toString();
}
addNextClause(clause) {
if (this.nextClause) {
throw new Error(`Cannot add <Clause ${clause.constructor.name}> to <Clause ${this.constructor.name}> because ${this.constructor.name} it is not the last clause.`);
}
this.nextClause = clause;
this.addChildren(this.nextClause);
}
compileNextClause(env) {
return (0, compile_cypher_if_exists_1.compileCypherIfExists)(this.nextClause, env, { prefix: "\n" });
}
}
exports.Clause = Clause;