ag-grid-mongo-query-builder
Advanced tools
Comparing version 0.2.6 to 0.2.7
@@ -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); | ||
} |
{ | ||
"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; | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36462
788
4