@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
157 lines (156 loc) • 5.42 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.CypherAggregationFunction = void 0;
exports.count = count;
exports.min = min;
exports.max = max;
exports.avg = avg;
exports.sum = sum;
exports.collect = collect;
exports.percentileCont = percentileCont;
exports.percentileDisc = percentileDisc;
exports.stDev = stDev;
exports.stDevP = stDevP;
const normalize_variable_1 = require("../../utils/normalize-variable");
const CypherFunctions_1 = require("./CypherFunctions");
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-count | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
function count(expr) {
return new CypherAggregationFunction("count", [expr]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-min | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
function min(expr) {
return new CypherAggregationFunction("min", [expr]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-max | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
function max(expr) {
return new CypherAggregationFunction("max", [expr]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-avg | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
function avg(expr) {
return new CypherAggregationFunction("avg", [expr]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-sum | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
function sum(expr) {
return new CypherAggregationFunction("sum", [expr]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-collect | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
function collect(expr) {
return new CypherAggregationFunction("collect", [expr]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-percentilecont | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
function percentileCont(expr, percentile) {
const normalizedPercentile = (0, normalize_variable_1.normalizeExpr)(percentile);
return new CypherAggregationFunction("percentileCont", [expr, normalizedPercentile]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-percentiledisc | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
function percentileDisc(expr, percentile) {
const normalizedPercentile = (0, normalize_variable_1.normalizeExpr)(percentile);
return new CypherAggregationFunction("percentileDisc", [expr, normalizedPercentile]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-stdev | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
function stDev(expr) {
return new CypherAggregationFunction("stDev", [expr]);
}
/**
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#functions-stdevp | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
function stDevP(expr) {
return new CypherAggregationFunction("stDevP", [expr]);
}
/** Represents a Cypher Aggregation function
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating | Cypher Documentation}
* @group Functions
* @category Aggregations
*/
class CypherAggregationFunction extends CypherFunctions_1.CypherFunction {
/**
* @internal
*/
constructor(name, params) {
super(name);
this.hasDistinct = false;
this.hasStar = false;
for (const param of params) {
if (param === "*") {
this.hasStar = true;
}
else {
this.addParam(param);
}
}
}
/**
* Adds DISTINCT to remove duplicates on the aggregation functions
* @see {@link https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#_counting_with_and_without_duplicates | Cypher Documentation}
*/
distinct() {
if (this.hasStar) {
throw new Error("count(*) is not supported with DISTINCT");
}
this.hasDistinct = true;
return this;
}
/** @internal */
getCypher(env) {
const argsStr = this.hasStar ? "*" : this.serializeParams(env);
const distinctStr = this.hasDistinct ? "DISTINCT " : "";
return `${this.name}(${distinctStr}${argsStr})`;
}
}
exports.CypherAggregationFunction = CypherAggregationFunction;