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

Uploaded Source

Built Distribution

sqla_utils-0.6.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqla_utils-0.6.1.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/6.8.0-31-generic

File hashes

Hashes for sqla_utils-0.6.1.tar.gz
Algorithm Hash digest
SHA256 5b214451141560169e5ce1da45263255708615dce03bdaa18bb29a3529df4c2c
MD5 e5f42d3c3c6fcbc10c54aee4c6b1e424
BLAKE2b-256 fe8e9f660c038454cb667689c896d731cd24e6e5c106f405ff7cf4663759efb8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sqla_utils-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cb818cfb1e73201900734cd04982fc2ae3117097731c015a3791557e0dc2df19
MD5 8cea3785f714ca3b90a591ccabd18ca4
BLAKE2b-256 75bbb52d3a5e4c7a12f149b6b309ca57d62982b607e587ff6ffb5d508952775c

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