Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ag-grid-mongo-query-builder

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ag-grid-mongo-query-builder - npm Package Compare versions

Comparing version 0.2.6 to 0.2.7

6

index.js

@@ -1,2 +0,2 @@

const {buildQuery, buildCountQuery, getPivotColumns} = require('./queryBuilder.js');
const {buildQuery, buildCountQuery, getPivotColumns, generateCSVData} = require('./queryBuilder.js');

@@ -13,2 +13,6 @@ module.exports.buildQuery = function(params){

return getPivotColumns(params, data);
}
module.exports.generateCSVData = async function(req, res, dbModel) {
return await generateCSVData(req, res, dbModel);
}

2

package.json
{
"name": "ag-grid-mongo-query-builder",
"version": "0.2.6",
"version": "0.2.7",
"description": "Utility to generate Mongo DB aggregation pipeline queries starting from AgGrid server side params",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -789,7 +789,120 @@ //const mongoose = require('mongoose');

const generateCSVData = async(req, res, dbModel) => {
const newcounPayload = {...req.body};
delete(newcounPayload.startRow);
delete(newcounPayload.endRow);
const countQuery = buildCountQuery(newcounPayload);
let totalCount = await getTotalCount(dbModel, countQuery, res);
let recordsPerPage = process.env.CSV_RECORDS_PER_PAGE ? process.env.CSV_RECORDS_PER_PAGE : 500;
let requestPerBatch = process.env.CSV_REQUEST_PER_BATCH ? process.env.CSV_REQUEST_PER_BATCH : 10;
let finalExportResultArray = [];
let startRow = 0;
let lastRow = 0;
let keys = req.body.displayFields ? req.body.displayFields : null;
if(totalCount && keys) {
const numPages = Math.ceil(totalCount/recordsPerPage);
const numBatches = Math.ceil(numPages/requestPerBatch);
console.log("Total no of pages" , numPages);
console.log("Total no of batch" , numBatches);
await getCSVDataByBatches(req, res, keys, numBatches, totalCount, requestPerBatch, recordsPerPage, startRow, lastRow, finalExportResultArray, 1, dbModel);
return finalExportResultArray;
} else{
console.log('Total count not found:');
res.status(400).send(" Total count not found !");
}
}
const getCSVDataByBatches = async(req, res, keys, numBatches, totalCount, requestPerBatch, recordsPerPage, startRow, lastRow, finalExportResultArray, currBatch, dbModel ) => {
if(currBatch > numBatches) return finalExportResultArray;
console.log(' Processing batch ',currBatch);
let reqCurrBatch = requestPerBatch;
const currentBatchLimit = (currBatch * requestPerBatch * recordsPerPage);
if(currentBatchLimit > totalCount) {
const batchLimit = ((currBatch - 1) * requestPerBatch * recordsPerPage);
const reqDiff = totalCount - batchLimit;
reqCurrBatch = Math.ceil(reqDiff/recordsPerPage);
}
const reArray = Array.from({length: reqCurrBatch}, (x, i) => i + 1);
const requests = reArray.map(itra => {
if(itra === 1 && currBatch === 1) {
startRow = startRow;
} else {
startRow = startRow + recordsPerPage;
}
lastRow = startRow + recordsPerPage;
return getCSVDataByPages(req, startRow, lastRow, res, dbModel)
});
try {
const response = await Promise.all(requests);
if(response && response.length > 0) {
response.map(eachRes => {
eachRes.map(function(obj) {
let tempDataObj = {};
for (let i in keys) {
if (obj[keys[i].fieldName] && Array.isArray(obj[keys[i].fieldName])) {
tempDataObj[keys[i].fieldName] = obj[keys[i].fieldName].toString();
} else if (obj[keys[i].fieldName] == "" || obj[keys[i].fieldName] == null || obj[keys[i].fieldName] == undefined) {
tempDataObj[keys[i].fieldName] = "";
} else {
tempDataObj[keys[i].fieldName] = obj[keys[i].fieldName];
}
if(keys[i].dataType === 'agDateColumnFilter') {
const dateVal = obj[keys[i].fieldName] ? new Date(obj[keys[i].fieldName]) : null;
if(dateVal) {
tempDataObj[keys[i].fieldName] = moment(dateVal).format("DD/MM/YYYY");
}
}
}
finalExportResultArray.push(tempDataObj);
});
});
}
currBatch = currBatch + 1;
await getCSVDataByBatches(req, res, keys, numBatches, totalCount, requestPerBatch, recordsPerPage, startRow, lastRow, finalExportResultArray, currBatch, dbModel);
} catch(err) {
console.log(`error: `, err);
}
}
const getCSVDataByPages = async(req, startRow, endRow, res, dbModel)=>{
req.body.startRow = startRow;
req.body.endRow = endRow;
const aggregationPipeline = buildQuery(req.body);
let query = dbModel.aggregate(aggregationPipeline.aggregationPipeline);
return await query.exec().then((results) => {
return results;
})
.catch((error) => {
console.log('got the error in csv export query execution:', error);
res.status(400).send("Error: Csv export query execution failed!");
})
}
const getTotalCount = async(dbModel, countQuery, res)=>{
const query = dbModel.aggregate(countQuery);
return await query.exec()
.then((results) => {
console.log('CSV Export: count query successful execution ..');
if(results && results[0] && results[0]['totalRows']) {
let lastRowIndex = results[0]['totalRows'];
return lastRowIndex;
} else {
return null
}
})
.catch((error) => {
console.log('CSV Export: got the error in count query execution:', error);
res.status(400).send("CSV Export: Error getting total count!");
})
}
module.exports.buildQuery=buildQuery;
module.exports.buildCountQuery=buildCountQuery;
module.exports.getPivotColumns=getPivotColumns;
module.exports.generateCSVData=generateCSVData;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc