Skip to main content

Nested mutable SQLAlchemy types. Includes JSON (RFC 7159).

Project description

sqlalchemy-mutables

A minimal package for nested mutable datatypes in SQLAlchemy. Main feature is a JSON field that is RFC-7159 compliant.

Developed for SQLAlchemy v1.3.24. Your mileage may very with other versions.

Installation

$ pip install sqlalchemy-mutables

Usage

from sqlalchemy_mutables import NestedMutableListColumn

class MyTable(Base):
    mutable_list = Column(NestedMutableListColumn)
from sqlalchemy_mutables import NestedMutableDictColumn

class MyTable(Base):
    mutable_dict = Column(NestedMutableDictColumn)

JSON

To be compliant with RFC-7159, 'primitives' need to be supported as top level values. These are values of type str|int|float|bool or None. Unfortunately, these types break SQLAlchemy Mutable functionality (specifically bool as this cannot be subclassed). To allow for these while maintaining mutable functionality for list and dict, a wrapper class has to be used via a hybrid_property.

from sqlalchemy_mutables import NestedMutableJSONColumn, NestedMutableJSONProperty

class MyTable(Base):
    _mutable_json = Column("mutable_json", NestedMutableJSONColumn)
    mutable_json = NestedMutableJSONProperty("_mutable_json")

It is possible to pass a custom functionality to the getter and/or setter:

def json_getter(value):
    if isinstance(value, (int, float)):
        value = value * 2
    return value

def json_setter(value):
    if isinstance(value, (int, float)):
        value = value / 2
    return value

class MyTable(Base):
    ...
    mutable_json = NestedMutableJSONProperty(
        "_mutable_json",
        fget=json_getter,
        fset=json_setter,
    )

Passed functions are executed in the existing getter and setter in the form of value = fc(value).

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_mutables-0.3.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_mutables-0.3.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_mutables-0.3.1.tar.gz.

File metadata

  • Download URL: sqlalchemy_mutables-0.3.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for sqlalchemy_mutables-0.3.1.tar.gz
Algorithm Hash digest
SHA256 e8e9fa1e551d9c06fc69d9d0d79dc540ee69af1a9da0aa1d4d9e74c6a9a49ebb
MD5 e74ff94aca22a3552be78d42a39a4c58
BLAKE2b-256 813b5fd23c789dea706179b02612c398501e6e65c31c55782d3bf0f4cd633cea

See more details on using hashes here.

File details

Details for the file sqlalchemy_mutables-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_mutables-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f3d0a6adb3df85d1863e858abf0d720a42bfdc3f3b67a5944e866534544d5c8e
MD5 a2b092576fc594c375c4c1ab105b7434
BLAKE2b-256 093d3c03b626762102c9e1b8e333c6860a7701376c746104d603150c89e48865

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