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.
This commit is contained in:
@@ -11,6 +11,10 @@ function getPagination( query ){
|
|||||||
}
|
}
|
||||||
if( query.elements ){
|
if( query.elements ){
|
||||||
limit.elements = parseInt( query.elements ) || 10;
|
limit.elements = parseInt( query.elements ) || 10;
|
||||||
|
/** Safe pagination limit */
|
||||||
|
if( limit.elements > 1000 ){
|
||||||
|
limit.elements = 1000;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return limit;
|
return limit;
|
||||||
}
|
}
|
||||||
@@ -18,7 +22,7 @@ function getPagination( query ){
|
|||||||
async function queryPage( page , elements , model ){
|
async function queryPage( page , elements , model ){
|
||||||
const skip = elements * page;
|
const skip = elements * page;
|
||||||
const total = await model.count();
|
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 {
|
return {
|
||||||
total : total,
|
total : total,
|
||||||
limit : elements,
|
limit : elements,
|
||||||
|
|||||||
Reference in New Issue
Block a user