Skip to main content

A powerful and flexible SQLAlchemy database type for nested mutation tracking

Project description

SQLAlchemy-Mutable aspires to be the most powerful and flexible SQLAlchemy database column type.

Its features include:

  1. Nested mutation tracking
  2. Mutation tracking for list and dict
  3. Support for storing database models in mutable columns
  4. Support for common literals and datetime objects
  5. Support for custom mutable classes
  6. 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


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.10.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_mutable-0.0.10-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy-mutable-0.0.10.tar.gz.

File metadata

  • Download URL: sqlalchemy-mutable-0.0.10.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.8

File hashes

Hashes for sqlalchemy-mutable-0.0.10.tar.gz
Algorithm Hash digest
SHA256 b628906a3cd7dc8c398a327d11cefb2c5a17196413eaec898b750fddad0fd5cf
MD5 f1392b7d69af28dcb73a269f1ea6326f
BLAKE2b-256 4d5abde24951fa0904efcfe38367579575a5da58d1aeba8bc69949126027fe5d

See more details on using hashes here.

File details

Details for the file sqlalchemy_mutable-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: sqlalchemy_mutable-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.8

File hashes

Hashes for sqlalchemy_mutable-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 654342061c6aef1333b8d0090d73e3ed578fddefdbf38e9d1291da7c73c2a60d
MD5 6272014cfc04bb0d320361329447b260
BLAKE2b-256 e591b1837b169eaa8fba0ff2b481dd50235cb27387541122d606c31f945ddcf6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page