SQLPage is a Python library designed for seamless integration of pagination in your database queries. Compatible with SQLAlchemy and SQLModel ORMs, it simplifies the process of retrieving paginated data, offering a straightforward API to enhance your data handling capabilities without adding unnecessary complexity.
SQLPage is a powerful Python library designed to streamline pagination in database queries. This library is compatible with popular Object-Relational Mappers (ORMs) like SQLAlchemy and SQLModel, making it a versatile choice for developers looking to implement efficient pagination in their applications.
Key Features
- Simple Integration: Easily add pagination capabilities to your existing database queries without extensive modifications.
- Supports Multiple ORMs: Whether you're using SQLAlchemy or SQLModel, SQLPage caters to your needs and ensures consistent performance.
- Base64 Encoded Tokens: Generate base64 encoded strings that serve as pagination tokens, allowing you to navigate through query results effortlessly.
Usage Example
To get started, simply install the library via PyPi:
pip install sqlpage
After installation, configure your ORM and use the paginate
function to retrieve paginated results:
result: PageData = paginate(session, query, page_size=100)
The paginate
function returns a PageData
object with the following structure:
{
"next_page_token": "base64 encoded string",
"items": [
...
]
}
ORM Configuration
Before utilizing the paginate
function, ensure your ORM is properly configured. SQLAlchemy and SQLModel require distinct setup processes, which involve defining your database models and creating a session:
Example for SQLAlchemy:
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
username = Column(String)
email = Column(String)
Followed by session creation:
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)
Example for SQLModel:
class User(SQLModel, table=True):
__tablename__ = 'user'
id: int = Field(primary_key=True)
username: str = Field(default=None)
email: str = Field(default=None)
And then run:
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)
Get Involved
Contributions are welcome! You can help enhance SQLPage by opening issues or submitting pull requests in the Issues
tab.