expressjs / express-paginate

Paginate middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use this module with mysql DB/Module

javabala opened this issue · comments

Hi,

I am new to the express and node js. I don't know how to integrate this module with mysql module.

Can any one please guide me?

Use knex package (or another package that lets you write SQL queries) from NPM and connect it to your MySQL server.

Then write a query using knex (or your alternative) to paginate some data from your DB.

var start = (req.query.page - 1) * req.query.limit;
var end = (req.query.page * req.query.limit) - 1;
var query = 'SELECT * FROM some_table_name LIMIT ' + start + ',' + end;

(there are more efficient ways to write this SQL query, but this is just a simple example)

You just hook in the express-paginate middleware, then write normal SQL queries, and for your limit and offset numbers use the provided req.query.limit and req.query.page values (see the Readme).

Does this help @javabala?

commented

@niftylettuce

Are you sure you wrote correct example?
end is actually a limit and it is normally a fixed value. Like only 10, 15, 20, so on records. So

LIMIT 20, 10

actually means: I need just 10 records starting from 20th.

And in your example end is not a fixed value. It is increased with every page.
If your goal is to return only 10 records every time (that is limit is 10), then by your example you get this:

(page 1): LIMIT 0, 9 // returns 9 records. starting from 0th
(page 2): LIMIT 10, 19 // returns 19 records, starting from 10th
(page 3): LIMIT 20, 29 // returns 29 records, starting from 20th

Every time your limit value is increased. Hence it is not a limit in its initial sense.

@wzup can you rewrite this and add example to README?