cannblw / knex-paginator

Simple paginator for Knex. It adds the .paginate() function to Knex's query builder.
15 stars 10 forks source link

Use latest .extend() feature of Knex #14

Open fellars opened 4 years ago

fellars commented 4 years ago

After running into same issue of it not working on latest, I found that latest version of knex has introduced an extend() function to support things like pagination.

I tried getting this function ported over to that but without success. It needs to return the QueryBuilder instead of an object. Anyone more competent than me willing to try?

This seems like the likely transition for this library since its no longer supported.

See details here: http://knexjs.org/#Builder-extending

felixmosh commented 4 years ago

What didn't worked for you? If you call .paginate as the last chain call it should work.

fellars commented 4 years ago

Im still seeing issue #6 show up even after updating to 2.0.0 version.

I still have to remove the node_modules folder inside knex-paginator for it to work, even with the change to the knex folder

So, hoping to just migrate this function over to the new extend functionality available in knex i referenced above.

fellars commented 4 years ago

Hey @felixmosh - i just realized you are the one who implemented the .extend() function knex :) nice work!

So, you'd be best to answer this - how to port this paginate function over to use that extend() feature.

From what I can tell, the issue is that this requires a 2nd sql query to get the totalCount (if isLengthAware is true), and it also manipulates the output, so that it returns the object instead of the querybuilder. That just means it has to be the last call in chain, which is fine.

The issue I guess, really, is the typescript is expecting it to return the QueryBuilder, so I would need to modify my type to allow for an object to be returned. Thats probably where I am stuck. I'm getting typescript syntax errors due to the return value of the function.

felixmosh commented 4 years ago

extend method only prevents monkey patching knex's query builder prototype. This paginate plugin is written in a hacky way, and the maintainer is not responsive at all.

I have a better approach in my project, I can publish it as npm package, just need to get approval of the method that it is using. https://github.com/tgriesser/knex/issues/3408

fellars commented 4 years ago

that would be great @felixmosh - thx

felixmosh commented 4 years ago

@fellars, https://github.com/felixmosh/knex-paginate just published it 🎊