@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
143 lines (142 loc) • 4.79 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*/
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_expr_1 = require("../../utils/normalize-expr");
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_expr_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_expr_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 {
hasDistinct = false;
hasStar = false;
/**
* @internal
*/
constructor(name, params) {
super(name);
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;