Skip to main content

Opinionated utilities for working with SQLAlchemy

Project description

sqla-utils

Opinionated utilities for working with SQLAlchemy

MIT License PyPI - Python Version GitHub pypi GitHub Workflow Status

Contents

Transaction Wrapper

FIXME

DBObjectBase

DBObjectBase is a base class for mapped classes.

Example:

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, DBAppointment.description.like("%great%"))

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

class DBAppointment(DBObjectBase):
    ...

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

    @classmethod
    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
        t.changed(self)

Database Builder

FIXME

pytest Utilities

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

FIXME

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.3.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

sqla_utils-0.3.1-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file sqla-utils-0.3.1.tar.gz.

File metadata

  • Download URL: sqla-utils-0.3.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.2 Linux/5.10.0-15-amd64

File hashes

Hashes for sqla-utils-0.3.1.tar.gz
Algorithm Hash digest
SHA256 f65e7b2c13cef3036c53d15626af640b2a92625a8a40933e96ac8621e5828224
MD5 f54cf8d9c581fc8215c91501ff5fe8de
BLAKE2b-256 77fe2ed2977371a8869268fa1003dafcefa08a23fa87f10c7308577cb2017fb6

See more details on using hashes here.

File details

Details for the file sqla_utils-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: sqla_utils-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.2 Linux/5.10.0-15-amd64

File hashes

Hashes for sqla_utils-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 196418e65a714d76cc263d42fb66a1dabe09a51a3604ce0391c74d232b46010d
MD5 7f1aa3b6717dc06557a15b49132997d5
BLAKE2b-256 520dd1f96ba030c64d7a637209ada3f8cdfcc2172d844517113182b798d3cb1d

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