@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
73 lines (72 loc) • 2.32 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.RawCypherContext = exports.Raw = void 0;
const is_cypher_compilable_1 = require("../utils/is-cypher-compilable");
const to_cypher_params_1 = require("../utils/to-cypher-params");
const Clause_1 = require("./Clause");
/** Allows for a raw string to be used as a clause
* @group Utils
*/
class Raw extends Clause_1.Clause {
constructor(callback) {
super();
if (typeof callback === "string") {
this.callback = this.stringToCallback(callback);
}
else
this.callback = callback;
}
getCypher(env) {
const cbResult = this.callback(new RawCypherContext(env));
if (!cbResult)
return "";
let query;
let params = {};
if (typeof cbResult === "string")
query = cbResult;
else {
[query, params] = cbResult;
}
const cypherParams = (0, to_cypher_params_1.toCypherParams)(params);
env.addExtraParams(cypherParams);
return query;
}
stringToCallback(str) {
return () => str;
}
}
exports.Raw = Raw;
/**
* @group Utils
*/
class RawCypherContext {
/** @internal */
constructor(env) {
this.env = env;
}
/** Compiles a Cypher element in the current context */
compile(element) {
if (!(0, is_cypher_compilable_1.isCypherCompilable)(element))
throw new Error("Can't build. Passing a non Cypher Builder element to context.compile in RawCypher");
return element.getCypher(this.env);
}
}
exports.RawCypherContext = RawCypherContext;