Giter Site home page Giter Site logo

glef1x / sqla-pagination Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 126 KB

Opinionated and powerful pagination tool for sqlalchemy

Python 100.00%
asyncio keyset keyset-pagination keyset-paging keysets pagination pagination-component pagination-components pagination-generator pagination-library

sqla-pagination's Introduction

sql-alchemy pagination for humans

Features

  • Support both async and sync sqlalchemy approaches without corrupting and duplicating API
  • Include variety of different pagination strategies such as keyset(infinite scrolling), limit-offset and others
  • Support PEP 484 (typehints) and consequentially static type checking using mypy, pyright or other tool
  • Transparent page abstraction

Getting started

from sqlalchemy import select, create_engine
from sqlalchemy.orm import sessionmaker

from sqlapagination import LimitOffsetPaginator

engine = create_engine("connection url")
pool = sessionmaker(engine)

paginator = LimitOffsetPaginator(select(Book).order_by(Book.id, Book.title))

with pool.begin() as session:
   stmt = paginator.get_modified_sql_statement()
   result = session.execute(stmt).all()
   page = paginator.parse_result(result)

   with paginator.bookmarked(page.next):
       stmt = paginator.get_modified_sql_statement()
       result = session.execute(stmt).all()
       new_page = paginator.parse_result(result)

What do bookmarks look like?

Bookmark is a plain dict, but for different pagination strategies a dict's payload differ from each other

Keyset:

{
    "keyset_pairs": {
        "id": (1,)
    },
    "direction": "forward",
}

Limit-offset:

{
    "offset": 10000,
}

Limitations:

  • Golden Rule: Always ensure your keysets are unique per row. If you violate this condition you risk skipped rows and other nasty problems. The simplest way to do this is to always include your primary key column(s) at the end of your ordering columns.
  • Any rows containing null values in their keysets will be omitted from the results, so your ordering columns should be NOT NULL. (This is a consequence of the fact that comparisons against NULL are always false in SQL.) This may change in the future if we work out an alternative implementation; but for now we recommend using coalesce as a workaround if you need to sort by nullable columns:

sqla-pagination's People

Contributors

glef1x avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.