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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
sqla_utils-0.1.4-py3-none-any.whl
(11.4 kB
view hashes)
Close
Hashes for sqla_utils-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e49ad9d1c9ffdc1e707ca6bbc3f27abf632314b6d3db1266c38102f3a58f762 |
|
MD5 | 363fa8cc7ac7aef03c6137694e8fedaf |
|
BLAKE2b-256 | 0dbf8734ebc0916399db26f7b9a50699df2ae947c6792e08013479873160070d |