Opinionated utilities for working with SQLAlchemy
Project description
sqla-utils
Opinionated utilities for working with SQLAlchemy
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
Release history Release notifications | RSS feed
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.3.tar.gz
(10.9 kB
view details)
Built Distribution
File details
Details for the file sqla-utils-0.2.3.tar.gz
.
File metadata
- Download URL: sqla-utils-0.2.3.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c703bc11d3ec040a041ca62d340b37936a4a91d6b879d231fddcfc83d05295c |
|
MD5 | 4cacb45fb544aa8b779f3f30b7df1e10 |
|
BLAKE2b-256 | a3462acaadd8ea32b1c31ef6ae9686f770fe3a45b2552dd72ae31289a9e955f0 |
File details
Details for the file sqla_utils-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: sqla_utils-0.2.3-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b69c6a7ec1505ffec490118906a25988722fa465f28d4cad0cf545938697bdb |
|
MD5 | 24f73431edb43325d0b113f276849fcc |
|
BLAKE2b-256 | d6db9abc45f6d4d9c8ad79cf78368e8cc03a28cb687ff0a58fd8c7e12487dc40 |