A powerful and flexible sqlalchemy database type for nested mutation tracking
Project description
SQLAlchemy-Mutable
SQLAlchemy-Mutable aspires to be the most powerful and flexible SQLAlchemy database column type.
Its features include:
- Nested mutation tracking
- Mutation tracking for
list
,dict
, andtuple
- Support for storing database models in mutable columns
- Support for common literals and
datetime
objects - Support for custom mutable classes
- Support for converting existing classes to mutable classes
Installation
$ pip install sqlalchemy-mutable
Quickstart
Setup:
from sqlalchemy_mutable import Mutable, MutableType, MutableModelBase, Query
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
# create a session (standard)
engine = create_engine('sqlite:///:memory:')
session_factory = sessionmaker(bind=engine)
Session = scoped_session(session_factory)
session = Session()
Base = declarative_base()
# subclass `MutableModelBase` when creating database models
# which may be stored in a `Mutable` object
class MyModel(MutableModelBase, Base):
__tablename__ = 'mymodel'
id = Column(Integer, primary_key=True)
greeting = Column(String)
# initialize a database column with `MutableType`
mutable = Column(MutableType)
# add a `query` class attribute initialized with a scoped_session
# not necessary for use with Flask-SQLAlchemy
query = Query(Session)
def __init__(self):
# set mutable column to `Mutable` object
self.mutable = Mutable()
# create the database (standard)
Base.metadata.create_all(engine)
Examples:
model = MyModel()
session.add(model)
session.commit()
# nested mutable objects
model.mutable.nested_mutable = Mutable()
session.commit()
model.mutable.nested_mutable.greet = 'hello world'
session.commit()
print(model.mutable.nested_mutable.greet)
# nested mutable list and dict
model.mutable = {}
session.commit()
model.mutable['greet'] = ['hello world']
session.commit()
print(model.mutable)
# storing database models
model.mutable = model
session.commit()
print(model.mutable)
# common literals
model.mutable = 'hello world'
session.commit()
print(model.mutable)
Out:
hello world
{'greet': ['hello world']}
<__main__.MyModel object at 0x7fe54a2d7b00>
hello world
Citation
@software{bowen2020sqlalchemy-mutable,
author = {Dillon Bowen},
title = {SQLAlchemy-Mutable},
url = {https://dsbowen.github.io/sqlalchemy-mutable/},
date = {2020-10-16},
}
License
Users must cite this package in any publications which use it.
It is licensed with the MIT License.
Acknowledgments
Original inspiration drawn from SQLAlchemy-JSON.
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
sqlalchemy-mutable-0.0.13.tar.gz
(12.2 kB
view hashes)
Built Distribution
Close
Hashes for sqlalchemy-mutable-0.0.13.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 299578e605092ca3578c34a0354a9f5fefd9713964b59e1debcc01a80f55d24b |
|
MD5 | d9b0104db504fd86c676232efaa36581 |
|
BLAKE2b-256 | 13f180fe8d63ce25c7e7a69bba801872762bedf01dc3a94b9b235bc552bf3fae |
Close
Hashes for sqlalchemy_mutable-0.0.13-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e51513bf4392be51a03d7e9e86350b8eeef382b67f76781da0e61c8c900f9e6a |
|
MD5 | 40b5ff3e888caa6c8696d48ad7e199b3 |
|
BLAKE2b-256 | 3d972a170316e5039d712444e8cc5a4b64cbe0d1ec28edab5748a6a0425902e4 |