GET Methods

GET methods

The GET method is used to retrieve data on a server. Clients can use the GET method to access all of the resources of a given type, or they can use it to access a specific resource. ~ Postman.


Pagination function makes GET methods highly versatile, allowing the same method to query and paginate output with a single configuration line. This function utilizes query parameters such as page number, entry limit, and a configurable sorting system, making it adaptable for all endpoints.

Pagination available in: /books, /featured, /title, /author, /year, /categories and /price.

Additionally, the function returns a dictionary with pagination details, including the current page, next and previous pages, last page, and total item count for the query. This ensures efficient navigation and data management.

pagination.js
def paginate(query, page, limit, sort_field=None, sort_order=1):
    total_queryBooks = books_collection.count_documents(query) 
    total_pages = (total_queryBooks + limit - 1) // limit  
    
    results = books_collection.find(query).skip((page - 1) * limit).limit(limit)
 
    if sort_field:
        results = results.sort(sort_field, sort_order)
 
    books = list(results)
    
    pagination_info = {
        "pageCurrent": page,
        "pagePrevious": page - 1 if page > 1 else None,
        "pageNext": page + 1 if page * limit < total_queryBooks else None,
        "pageLast": total_pages,
        "items": total_queryBooks
    }
    
    return books, pagination_info

How to use Pagination Parametrization

Simple Way (default)

You can use the default values of pagination and don't worry about it!

It will use the default values of the specific endpoint.

localhost:3001/api/v1/books

Advanced Way (personalized)

You can use personalized parameters within the values allowed. For example, you can sort_values and order them and limit the pages!

Limit, pages and sort_by can only receive an integer value. And sort_type can only receive a string value.

To add (more) parameters use ? followed by the name of the parameter available, then = and finally the key/value you want to use within the type allowed.

localhost:3001/api/v1/books?limit=20?sort_type=pageCount

All GET endpoints available