Best pagination ordering and filtering practices.

476 views
Skip to first unread message

trikst...@gmail.com

unread,
Feb 17, 2017, 2:17:26 AM2/17/17
to nameko-dev
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.

Julio Trigo

unread,
Feb 17, 2017, 11:26:30 AM2/17/17
to nameko-dev, trikst...@gmail.com
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: https://pypi.python.org/pypi/sqlalchemy-filters

I hope this helps!
Julio
Reply all
Reply to author
Forward
0 new messages