A package for SQLAlchemy models DML mixins.
Project description
model_dml
A package for SQLAlchemy models DML mixins.
Features
- Three methods for DML operations:
insert
,update
,delete
. - Add those methods to models through mixin classes.
- Support returning selected columns for all methods.
Installation
pip install model_dml
Usage
Custom Base
To use model_dml
, you need to create a custom base class for your models with a session_maker
method.
What is a sessionmaker:
class Base(sqlalchemy.orm.DeclarativeBase):
@classmethod
def session_maker(cls) -> sqlalchemy.orm.sessionmaker:
return <Namespace>.Session
This method exist to allow DML methods to access the sessionmaker
without creating a new reference
that would also need to be monkey patched. By returning the sessionmaker
from the namespace, only
<Namespace>.Session
needs to be monkey patched.
Compose your model with what you need
class User(base, model_dml.Insert, model_dml.Update, model_dml.Delete):
__tablename__ = "users"
id: sqlalchemy.orm.Mapped[int] = sqlalchemy.orm.mapped_column(primary_key=True)
name: sqlalchemy.orm.Mapped[str] = sqlalchemy.orm.mapped_column(sqlalchemy.String(30))
You can use model_dml.DML
which is a helper class that composes all the mixins together.
class User(base, model_dml.DML):
__tablename__ = "users"
id: sqlalchemy.orm.Mapped[int] = sqlalchemy.orm.mapped_column(primary_key=True)
name: sqlalchemy.orm.Mapped[str] = sqlalchemy.orm.mapped_column(sqlalchemy.String(30))
Use the DML methods
User.insert(dict(name="John"))
User.insert({'name': "Jane"})
user = User.insert({'name': "Jack"}, returning=[User.id, User.name])
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
model_dml-0.0.2.tar.gz
(13.0 kB
view hashes)
Built Distribution
Close
Hashes for model_dml-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfd944319d84f002a1a11a3e0d27b2ea0fa1c8d98a4fec8e7f4ef79676ff5085 |
|
MD5 | 0d88eabb0374eaaeba76362daf741b20 |
|
BLAKE2b-256 | c9dbd674e45c53d1cf3ed8ce9b4fd4c294005f77d4671d04db34bca8a494dd5a |