Best pagination ordering and filtering practices.

Hello there! We're currently in a process of rewriting our monolithic
django web applications into a set of nameko-based microservices. We
heavily utilized django-rest-framework URL filters which allowed us to
build pretty complex filters to query our models, and now in order to avoid
rewriting tons and tons of front-end code we want to remain the same
interface. So the question is what is the best practice for passing
pagination, filtering and ordering parameters to nameko RPC calls? We are
now using SQLAlchemy ORM and a layer of Repository classes providing data
access layer, and still don't really get how to link together our "API
Gateway" and nameko RPC calls in terms of pagination, filtering, etc.

Hello! We use sqlalchemy-filters for that. It's a library that we built for
passing SQLAlchemy filtering, pagination and sorting to Nameko endpoints.
You can just call your RPC endpoint by doing something like this:

    filters = [{'field': 'name', 'op': '==', 'value': 'name_1'}]
    order_by = [
        {'field': 'name', 'direction': 'asc'},
        {'field': 'id', 'direction': 'desc'},
    ]
    page_number=1
    page_size=10

    rcp_service.my_endpoint(filters, order_by, page_number, page_size)

And then, use sqlalchemy-filters in my_endpoint to perform the filtering,
pagination and sorting.

    from sqlalchemy_filters import apply_filters, apply_pagination,
apply_sort

    # existing SQLAlchemy `query` object

    query = apply_filters(query, filters)
    query = apply_sort(query, order_by)
    query, pagination = apply_pagination(query, page_number=1, page_size=10)

The sqlalchemy-filters API accepts a SQLAlchemy query object as an argument
so that you can apply your filters to an already filtered query.

You can have a look at it here:

I hope this helps!
Julio

···

On Friday, 17 February 2017 07:17:26 UTC, trikst...@gmail.com wrote:

Hello there! We're currently in a process of rewriting our monolithic
django web applications into a set of nameko-based microservices. We
heavily utilized django-rest-framework URL filters which allowed us to
build pretty complex filters to query our models, and now in order to avoid
rewriting tons and tons of front-end code we want to remain the same
interface. So the question is what is the best practice for passing
pagination, filtering and ordering parameters to nameko RPC calls? We are
now using SQLAlchemy ORM and a layer of Repository classes providing data
access layer, and still don't really get how to link together our "API
Gateway" and nameko RPC calls in terms of pagination, filtering, etc.

1 Like