Skip to content

Instantly share code, notes, and snippets.

@juliusdejon
Created April 6, 2020 07:11
Show Gist options
  • Save juliusdejon/7297e275aa3b7a015ed358a5b2ac6289 to your computer and use it in GitHub Desktop.
Save juliusdejon/7297e275aa3b7a015ed358a5b2ac6289 to your computer and use it in GitHub Desktop.
/**
* Pagination settings.
*
* @param {Object} pagination: {page, limit}
* @returns {Object}
*/
export function paginate(pagination) {
const { page = 1, limit = 10 } = pagination;
const offset = (page - 1) * limit;
return {
page,
offset,
limit,
};
}
@juliusdejon
Copy link
Author

juliusdejon commented Apr 6, 2020

Users.js

   const { page, offset, limit } = paginate(pagination);

  const options = {
      offset: offset,
      limit: limit,
     }  
  const response = await UserModel.findAndCountAll(options);

    return {
      results: response.rows,
      pagination: {
        limit,
        page,
        total: response.count
      }
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment