@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
63 lines (62 loc) • 2.19 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.WithWhere = void 0;
const boolean_1 = require("../../../expressions/operations/boolean");
const comparison_1 = require("../../../expressions/operations/comparison");
const PropertyRef_1 = require("../../../references/PropertyRef");
const Variable_1 = require("../../../references/Variable");
const Where_1 = require("../../sub-clauses/Where");
const Mixin_1 = require("../Mixin");
class WithWhere extends Mixin_1.Mixin {
whereSubClause;
where(inputOrTarget, params) {
this.updateOrCreateWhereClause(inputOrTarget, params);
return this;
}
and(inputOrTarget, params) {
this.updateOrCreateWhereClause(inputOrTarget, params);
return this;
}
updateOrCreateWhereClause(inputOrTarget, params) {
const whereInput = this.createWhereInput(inputOrTarget, params);
if (!whereInput) {
return;
}
if (this.whereSubClause) {
this.whereSubClause.and(whereInput);
}
else {
const whereClause = new Where_1.Where(this, whereInput);
this.whereSubClause = whereClause;
}
}
createWhereInput(inputOrTarget, params) {
if (!inputOrTarget) {
return undefined;
}
if (inputOrTarget instanceof Variable_1.Variable || inputOrTarget instanceof PropertyRef_1.PropertyRef) {
return this.variableAndObjectToOperation(inputOrTarget, params ?? {});
}
return inputOrTarget;
}
/** Transforms a simple input into an operation sub tree */
variableAndObjectToOperation(target, params) {
let operation;
for (const [key, value] of Object.entries(params)) {
const property = target.property(key);
const eqOp = (0, comparison_1.eq)(property, value);
if (operation) {
operation = (0, boolean_1.and)(operation, eqOp);
}
else {
operation = eqOp;
}
}
return operation;
}
}
exports.WithWhere = WithWhere;