Skip to main content

Python package to add pagination to a model and return bas64 encoded pagination token

Project description

SQLPage

Python library to easily add pagination on top of DB queries. Supports SQLAlchemy and SQLModel ORMs. This returns a base64 encoded string that can be used to paginate through the results.

Usage

Install the latest version from PyPi

pip install sqlpage

Now configure your ORM and then simply call the paginate function to get the paginated results.

result: PageData = paginate(session, query, page_size=page_size)

Configuring with ORMs

Before using the paginate function, you need to configure the ORM. As SQLAlchemy and SQLModel uses different ORMs, you need to configure them separately. This is a one-time setup and you would have already taken this while configuring the ORM when creating the DB models.

SQLAlchemy

Create your table model

Base = declarative_base()

class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)
    username = Column(String)
    email = Column(String)

Then create a DB session and call

engine = create_engine(f"sqlite:///{DATABASE_NAME}", echo=False)
Base.metadata.create_all(engine)

SessionSqlAlchemy = sessionmaker(bind=engine)
session = SessionSqlAlchemy()

query = session.query(TestTable)

result: PageData = paginate(session, query, page_size=100)

SQLModel

Create your table model

class User(SQLModel, table=True):
    __tablename__ = 'user'

    id: int = Field(primary_key=True)
    username: str = Field(default=None)
    email: str = Field(default=None)

Then create a DB session and call

engine = create_engine(f"sqlite:///{DATABASE_NAME}", echo=False)
Base.metadata.create_all(engine)

with Session(engine) as session:

    query = session.query(TestTable)

    result: PageData = paginate(session, query, page_size=100)

Contributing

Feel free to open an issues under the Issues tab. Contributions are welcome and appreciated.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlpage-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

sqlpage-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlpage-0.1.0.tar.gz.

File metadata

  • Download URL: sqlpage-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.7

File hashes

Hashes for sqlpage-0.1.0.tar.gz
Algorithm Hash digest
SHA256 21d980c50ed1272f3118fa94d45c29a8940b58fd5d94ba0a7debc433d9704adb
MD5 6fb2cc8e67993a2669180ff5e8c7c956
BLAKE2b-256 2c0a4ad98bd235439ecadd45c3047c0b1f071a75ece97d9de6d799d268518121

See more details on using hashes here.

File details

Details for the file sqlpage-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sqlpage-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.7

File hashes

Hashes for sqlpage-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a74324489088569152b43ba1d0361b7ab6a5eb2f00bf42010fea1e526bea1cd4
MD5 426e0001101379664434b91399676af6
BLAKE2b-256 45afdb5610d6adeac028624eec16546958518aaa9c14588672a034ec3ab9a264

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page