UNPKG

@neo4j/cypher-builder

Version:

A programmatic API for building Cypher queries for Neo4j

61 lines (60 loc) 1.75 kB
"use strict"; /* * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] */ 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 { callback; 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 { env; /** @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;