felixmosh / knex-paginate

An extension of Knex's query builder with `paginate` method that will help with your pagination tasks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`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 });

function paginate({ perPage = 10, currentPage = 1, isFromStart = false, isLengthAware = false }) {

Thank you for requesting this feature 🙏🏼

It can be a cool feature to have, but I have 2 concerns:

  1. Security, Since perPage usually is user controlled input, it can allow an attacker to overload the DB by supplying perPage=0 which will fetch all records from DB.
  2. 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!