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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqla_utils-0.7.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file sqla_utils-0.7.0.tar.gz.

File metadata

  • Download URL: sqla_utils-0.7.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.3 Linux/6.8.0-71-generic

File hashes

Hashes for sqla_utils-0.7.0.tar.gz
Algorithm Hash digest
SHA256 3819442b8e22bfd2617bdd633cb714bb8b6d5940152828f4ce82daf4d8efb930
MD5 9cb00a4912d9f585dc2004febe5f77ab
BLAKE2b-256 f573d3f4c8ee0574d20a187a0630a3dcf7242552055fa6cae20e3fea0d2de31d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqla_utils-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.3 Linux/6.8.0-71-generic

File hashes

Hashes for sqla_utils-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 59007b8b424cb344a80afeef24afec67241f08232a102feae5463966939f2c94
MD5 25e9dd3ac7f74bd50779f1c6d318f3d5
BLAKE2b-256 d9e9cef82108504c623236548254249944387f000489cf63d3540f691c6f188a

See more details on using hashes here.

Supported by

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