When dealing with a lot of records it is essential to create a way in which clients can specify how many they wish to obtain exactly. Returning records in chunks as opposed to dumping it all at once saves a lot of bandwidth and processing power.

A technique that splits the records into chunks is called pagination. A client sends two parameters: page and pageSize . Records are split into page number of pages - each page containing pageSize number of records.

Note that I am going to be explaining how to implement this using the Sequelize library specifically.

Paginating records with Sequelize comes down to including specific properties in a query object that we then pass to the model’s .findAll() method. The object has two properties - offset and limit - which indicate respectively how many records to skip and how many to take.

And so to calculate the offset we have to multiply page by pageSize . limit is equal to pageSize . Here’s an example of how this works:

If we wanted to get say page number 7 and on each page have 25 records the calculations would look like this:

offset = page(7) * pageSize(25) = 175

limit = pageSize(25)

So on page 7 there would now be records from 175 to 200.

Now that we understand how this works, let’s move to the implementation.