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

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

Uploaded Source

Built Distribution

sqla_utils-0.2.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqla-utils-0.2.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for sqla-utils-0.2.1.tar.gz
Algorithm Hash digest
SHA256 5efd0474131fcab259d6a5c44414b74764ac364256ed8446145272c31d0891ad
MD5 42fcc1933163cccd387e8af59c42d9ce
BLAKE2b-256 42fdfbb526b531597f36482f8f05b7be855b02a80321b7c36004954bbf061fcb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqla_utils-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for sqla_utils-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d0d3d74a3adc8cf2b8bf521f065f11748df903d2c9f0f3b0cab1f6f369be86bd
MD5 4201949f5fe1349dc8c3d384a3144ab7
BLAKE2b-256 bf7eedd0caa5ea88f1b37aa599e98916646b2e60f037b9769f160e41805ef301

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