ag-grid-mongo-query-builder
Advanced tools
Comparing version 0.3.4 to 0.3.5
{ | ||
"name": "ag-grid-mongo-query-builder", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "Utility to generate Mongo DB aggregation pipeline queries starting from AgGrid server side params", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -60,3 +60,3 @@ //const mongoose = require('mongoose'); | ||
const groupQuery = !pivotActive && isGrouping ? buildGroupingQuery(groupKeys, rowGroupCols, valueCols, pivotCols,pivotMode, column) : []; | ||
const pivotQuery = pivotActive ? buildPivotQuery(groupKeys, rowGroupCols, valueCols, pivotCols) : {query: [], pivotColumns: []}; | ||
const pivotQuery = pivotActive ? buildPivotQuery(groupKeys, rowGroupCols, valueCols, pivotCols, pivotMode) : {query: [], pivotColumns: []}; | ||
//const aggregationQuery = isAggregation ? buildAggregationQuery(valueCols) : []; | ||
@@ -137,3 +137,3 @@ const sortQuery = isSorting ? buildSortQuery(sortModel) : null; | ||
const groupQuery = !pivotActive && isGrouping ? buildGroupingQuery(groupKeys, rowGroupCols, valueCols, pivotCols,pivotMode,column) : []; | ||
const pivotQuery = pivotActive ? buildPivotQuery(groupKeys, rowGroupCols, valueCols, pivotCols) : {query: [], pivotColumns: []}; | ||
const pivotQuery = pivotActive ? buildPivotQuery(groupKeys, rowGroupCols, valueCols, pivotCols, pivotMode) : {query: [], pivotColumns: []}; | ||
//const aggregationQuery = isAggregation ? buildAggregationQuery(valueCols) : []; | ||
@@ -453,2 +453,3 @@ const countQuery = {$count: "totalRows"}; | ||
console.log('Pivot cols:',pivotCols); | ||
let sameGroupAndAggCol = false; | ||
@@ -493,3 +494,3 @@ const _matchQuery = groupKeys.length ? groupKeys.map((key, index) => { | ||
console.log('group query1:',JSON.stringify(constantGroupQuery)); | ||
const aggregationArray = buildAggregationQuery(valueCols); | ||
const aggregationArray = buildAggregationQuery(valueCols, pivotMode); | ||
@@ -507,2 +508,5 @@ console.log('group query2:',JSON.stringify(aggregationArray)); | ||
groupProjection['childCount'] = 1; | ||
if(valueCols.find(col => col.id && groupsCol.id && col.id === groupsCol.id)) { | ||
sameGroupAndAggCol = true; | ||
} | ||
}); | ||
@@ -515,7 +519,23 @@ let projectionObj = {"$project" : groupProjection}; | ||
///add unique count fields | ||
if(aggregationArray && aggregationArray.length > 0) { | ||
if(!pivotMode && aggregationArray && aggregationArray.length > 0) { | ||
aggregationArray.map(aggre => { | ||
const aggKey = Object.keys(aggre); | ||
if(aggKey && aggre[aggKey] && aggre[aggKey].hasOwnProperty('$addToSet')){ | ||
addFieldObj[aggKey] = { $size: `$${aggKey}`} | ||
if(!groupKeys || groupKeys.length <= 0) { | ||
if(!sameGroupAndAggCol) { | ||
addFieldObj[aggKey] = {"$size": `$${aggKey}`}; | ||
addFieldObj['childCount'] = {"$size": `$${aggKey}`}; | ||
} else { | ||
if(rowGroupCols && rowGroupCols.length > 1) { | ||
addFieldObj[aggKey] = {"$size": `$${aggKey}`}; | ||
} | ||
} | ||
} else { | ||
if(!sameGroupAndAggCol) { | ||
addFieldObj[aggKey] = {"$size": `$${aggKey}`}; | ||
addFieldObj['childCount'] = {"$size": `$${aggKey}`}; | ||
} else { | ||
addFieldObj[aggKey] = "$_id"; | ||
} | ||
} | ||
} | ||
@@ -543,3 +563,3 @@ }) | ||
function buildPivotQuery(groupKeys , rowGroupCols, valueCols, pivotCols) { | ||
function buildPivotQuery(groupKeys , rowGroupCols, valueCols, pivotCols, pivotMode) { | ||
@@ -549,3 +569,3 @@ const excessIndex = rowGroupCols.length - groupKeys.length > 0 ? groupKeys.length : 0; | ||
const _finalMatchQuery= _matchQuery.length > 1 ? [{"$match": {"$and" : _matchQuery}}] : [{"$match": _matchQuery[0]}]; | ||
const aggregationArray = buildAggregationQuery(valueCols); | ||
const aggregationArray = buildAggregationQuery(valueCols, pivotMode); | ||
@@ -811,3 +831,3 @@ let tempGroup = null; | ||
function buildAggregationQuery(valueCols){ | ||
function buildAggregationQuery(valueCols, pivotMode){ | ||
if(!valueCols || valueCols== null || Object.keys(valueCols).length === 0) // if valuecols null or undefined.. just return empty array | ||
@@ -818,3 +838,3 @@ return []; | ||
if(Object.keys(aggFunction).includes('aggFunc') && aggFunction['aggFunc']){ | ||
return { [aggFunction.id]: aggregateQuery(aggFunction)}; | ||
return { [aggFunction.id]: aggregateQuery(aggFunction, pivotMode)}; | ||
} | ||
@@ -826,3 +846,3 @@ }); | ||
function aggregateQuery(aggFunction){ | ||
function aggregateQuery(aggFunction, pivotMode){ | ||
@@ -871,3 +891,3 @@ console.log('inside building aggregateQuery: ',aggFunction); | ||
case 'count' : return { $addToSet: "$"+aggFunction.id+""}; // counts each doc | ||
case 'count' : return pivotMode ? {$sum : 1} : { $addToSet: "$"+aggFunction.id+""}; // counts each doc | ||
} | ||
@@ -874,0 +894,0 @@ } |
43407
913