From 857a8bf18b48678d522e420630b646602e0a45d4 Mon Sep 17 00:00:00 2001 From: "Josepablo C." Date: Thu, 5 Oct 2023 23:23:28 -0600 Subject: [PATCH] fix(Pagination): Improve performance of pagination - Add limit parameter to find method. - Set absolute pagination elements limit to 1000. - Adding pagination validation, do not accept negative values. If negative values are detected, default parameters are used. --- lib/Misc.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Misc.js b/lib/Misc.js index 3a488cb..1868336 100644 --- a/lib/Misc.js +++ b/lib/Misc.js @@ -11,6 +11,10 @@ function getPagination( query ){ } if( query.elements ){ limit.elements = parseInt( query.elements ) || 10; + /** Safe pagination limit */ + if( limit.elements > 1000 ){ + limit.elements = 1000; + } } return limit; } @@ -18,7 +22,7 @@ function getPagination( query ){ async function queryPage( page , elements , model ){ const skip = elements * page; const total = await model.count(); - const list = await model.find( {} , null, { skip : skip } ); + const list = await model.find( null , null, { skip : skip , limit : elements } ); return { total : total, limit : elements,