Opinonated 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.1.5.tar.gz
(10.3 kB
view details)
Built Distribution
File details
Details for the file sqla-utils-0.1.5.tar.gz
.
File metadata
- Download URL: sqla-utils-0.1.5.tar.gz
- Upload date:
- Size: 10.3 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb87c93c757f37a11e823e1f49a7f9e28620cb19ede67222961208d06322c438 |
|
MD5 | e4eb796a2590d1346bef4f51c1036106 |
|
BLAKE2b-256 | 1967a84cc4be16a46ba884b2f649f6b08910a0bb31e23f7782e5c1b04fca3cf0 |
File details
Details for the file sqla_utils-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: sqla_utils-0.1.5-py3-none-any.whl
- Upload date:
- Size: 11.6 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ffbfbf129eebaf602b16ffc1dbd440db56e0f86bb2c88ac5ac1c5f84563970e |
|
MD5 | 29384b2c605a3cee0ea4541cb7a6d157 |
|
BLAKE2b-256 | 0b93c4058651d6c57dffb78c96a1fc35bce3106d3172d71ec9298159fd36a10c |