Skip to main content

Opinionated utilities for working with SQLAlchemy

Project description


Opinionated utilities for working with SQLAlchemy

MIT License PyPI - Python Version GitHub pypi GitHub Workflow Status


Transaction Wrapper



DBObjectBase is a base class for mapped classes.


from datetime import datetime
from sqlalchemy import Column, DateTime, Integer, String
from sqla_utils import DBObjectBase, Transaction

class DBAppointment(DBObjectBase):
    __tablename__ = "appointments"

    id = Column(Integer, primary_key=True)
    date = Column(DateTime, nullable=False)
    description = Column(String(1000), nullable=False, default="")

Appointment items can then be queried like this:

from sqla_utils import begin_transaction

with begin_transaction() as t:
    app123 = DBAppointment.fetch_by_id(t, 123)
    great_apps = DBAppointment.fetch_all(t,"%great%"))

It is recommended to add custom query, creation, and update methods:

class DBAppointment(DBObjectBase):

    def create(cls, t: Transaction, date: datetime, description: str) -> DBAppointment:
        o = cls() = date
        o.description = description
        return o

    def fetch_all_after(cls, t: Transaction, date: datetime) -> List[DBAppointment]:
        return cls.fetch_all(t, cls.start >= dates.start)

    def update_description(self, t: Transaction, new_description: str) -> None:
        self.description = new_description

Database Builder


pytest Utilities

The sqla_utils.test module contains a few utilities for working with pytest and SQLAlchemy.


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

sqla_utils-0.6.0.tar.gz (12.4 kB view hashes)

Uploaded Source

Built Distribution

sqla_utils-0.6.0-py3-none-any.whl (14.4 kB view hashes)

Uploaded Python 3

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