`perPage === 0` behavior
kugimiya opened this issue · comments
Hello! Can u add case for perPage === 0
? So, this mean, that page should contain all entries. Its very useful case. For example, Java's Spring pagination functionality works the same. Its the only way to get all entries in methods, that wrapped by .paginate({ perPage, currentPage });
Line 4 in fa6b11b
Thank you for requesting this feature 🙏🏼
It can be a cool feature to have, but I have 2 concerns:
- Security, Since
perPage
usually is user controlled input, it can allow an attacker to overload the DB by supplyingperPage=0
which will fetch all records from DB. perPage=0
is not an explicit behavior, maybe exposing an additional boolean var?
WDYT?
@felixmosh, oh, you said true. I didn't think it might overload the DB. :o
Exposing an additional boolean var should be ideal solution!
I've thought about it a bit, it is weird to add boolean which will cancel the main action of .paginate
.
Since knex
is a query builder, you can call paginate conditionally.
import { knex } from './db';
const query = knex('users'); // pay attention that there is no `await` here.
let data;
if (!req.query.fetchAll) {
data = await query.paginate({ perPage: 10, isFromStart: true });
} else {
const rawData = await query;
data = { data: rawData, pagination: {} };
}
Will this solve your requirements?
@felixmosh, yep! Thank you!