The problem with this is that I'd have to .page(request.query.page, request.query.limit) and request.totalCount = result.totalevery GET requests where I'd want pagination. I don't like going this way because there are lot of code replication.
Second try
That's where I'd like your advices.
What I did to overcome this is creating a hapijs plugin:
reply[request.server.app.paginateMethod](query) translates to reply.appPaginate(query) because server.app.paginateMethod was set previously to options.paginateMethod, and options.paginateMethod is set into the Glue Manifest:
Hi,
I'm using hapipal with lots of their packages. My ORM is schwifty/objection.js. I'm new to the hapi/hapipal stack.
First try
The first thing I noticed is that hapi-pagination doesn't format the results itself because it doesn't know what ORM you're using (https://github.com/fknop/hapi-pagination/issues/11 https://github.com/fknop/hapi-pagination/issues/12).
Objection makes this pretty easy with its
page(page, pageSize)
method.So my first draw was this:
The problem with this is that I'd have to
.page(request.query.page, request.query.limit)
andrequest.totalCount = result.total
every GET requests where I'd want pagination. I don't like going this way because there are lot of code replication.Second try
That's where I'd like your advices.
What I did to overcome this is creating a hapijs plugin:
It's a decorator. It automatically finishes the DB request with
.page
and append thetotalCount
required forhapi-pagination
to therequest
.The decorator is called from the route like this:
reply[request.server.app.paginateMethod](query)
translates toreply.appPaginate(query)
becauseserver.app.paginateMethod
was set previously tooptions.paginateMethod
, andoptions.paginateMethod
is set into the Glue Manifest:Is this second try a good way to achieve my pagination? Maybe there are better ways to do this, I'd like some advices.