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.3.4.tar.gz
(12.1 kB
view details)
Built Distribution
File details
Details for the file sqla_utils-0.3.4.tar.gz
.
File metadata
- Download URL: sqla_utils-0.3.4.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.9.2 Linux/5.10.0-20-amd64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25ed42af9f37b62a5940def9788c7075d0fe4a1c5f66726ec1afb0051c171103 |
|
MD5 | 60c5371a0d3518da61f8f168fcbcd12b |
|
BLAKE2b-256 | d12c955bd1bf561688e37b785c169f912c5c208561e383b4b3ac72679fed79c6 |
File details
Details for the file sqla_utils-0.3.4-py3-none-any.whl
.
File metadata
- Download URL: sqla_utils-0.3.4-py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.9.2 Linux/5.10.0-20-amd64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39e23c88c5ec6def14f75f94da9fb58166097b2ec1147099c90a3ebd7b0e27f0 |
|
MD5 | 9dc42c91d68ac440d43b8001ff13d457 |
|
BLAKE2b-256 | 7d7bfd8c8a7ce34daf10daa8d6a233bd05acb69b5c2f672341545367af8f5d97 |